UDP(面向无连接)的socket编程

服务器端(接收端)程序:
1、创建套接字(socket)。
2、将套接字绑定到一个本地地址和端口上(bind)。
3、等待接收数据(recvfrom)。
4、关闭套接字。

客户端(发送端)程序:
1、创建套接字(socket)。
2、向服务器发送数据(sendto)。
3、关闭套接字。

1)recvfrom
The recvfrom function receives a datagram and stores the source address.

int recvfrom(
SOCKET s,                   
char FAR* buf,              
int len,                    
int flags,                  
struct sockaddr FAR *from,  
int FAR *fromlen            
);

Parameters
s
[in] Descriptor identifying a bound socket.
buf
[out] Buffer for the incoming data.
len
[in] Length of buf.
flags
[in] Indicator specifying the way in which the call is made.
from
[out] Optional pointer to a buffer that will hold the source address upon return.
fromlen
[in, out] Optional pointer to the size of the from buffer.

2)sendto
The sendto function sends data to a specific destination.

int sendto(
SOCKET s,                        
const char FAR *buf,            
int len,                         
int flags,                       
const struct sockaddr FAR *to,  
int tolen                        
);

Parameters
s
[in] Descriptor identifying a (possibly connected) socket.
buf
[in] Buffer containing the data to be transmitted.
len
[in] Length of the data in buf.
flags
[in] Indicator specifying the way in which the call is made.
to
[in] Optional pointer to the address of the target socket.
tolen
[in] Size of the address in to.

eg:
头文件:Winsock2.h、stdio.h,连接ws2_32.lib。
服务器端:
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );//ver1.1

err = WSAStartup( wVersionRequested, &wsaData );//加载winsocket库
if ( err != 0 ) {                               //版本协商
    return;
}


if ( LOBYTE( wsaData.wVersion ) != 1 ||       //low ver
    HIBYTE( wsaData.wVersion ) != 1 )         //high ver
{       
    WSACleanup( );//释放winsocket库
    return; 
}
SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,0);//创建数据报socket
SOCKADDR_IN addrSrv;//定义地址结构体变量
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;//地址族
addrSrv.sin_port=htons(10002);//端口

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//绑定
SOCKADDR_IN addrClient;//定义地址结构变量
int len=sizeof(SOCKADDR);
char recvBuf[100];

recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
printf("%s\n",recvBuf);
closesocket(sockSrv);//关闭套接字
WSACleanup();//释放winsocket库

}

客户端:
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );//ver1.1

err = WSAStartup( wVersionRequested, &wsaData );//加载winsocket库
if ( err != 0 ) {                               //版本协商
    return;
}


if ( LOBYTE( wsaData.wVersion ) != 1 ||       //low ver
    HIBYTE( wsaData.wVersion ) != 1 )         //high ver
{       
    WSACleanup( );//释放winsocket库
    return; 
}
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);//创建数据报socket
SOCKADDR_IN addrSrv;//定义地址结构体变量
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(10002);

sendto(sockClient,"Hello",strlen("Hello")+1,0,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
closesocket(sockClient);//关闭套接字
WSACleanup();//释放winsocket库

}

你可能感兴趣的:(VC++)