UdpServer: #include <winsock2.h> #include <stdio.h> /* udp server */ void main() { WORD wVersionRequested = MAKEWORD(2,2); WSADATA wsaData; if (WSAStartup(wVersionRequested,&wsaData)!=0) { printf("WSAStartup failure/n"); WSACleanup(); return; } printf("server Startup success/n"); SOCKET sockSvr = socket(AF_INET,SOCK_DGRAM,0); if (sockSvr==INVALID_SOCKET) { printf("server create socket failure/n"); WSACleanup(); return; } printf("server create socket success/n"); SOCKADDR_IN sadressIn; sadressIn.sin_addr.S_un.S_addr = htonl(INADDR_ANY); sadressIn.sin_family = AF_INET; sadressIn.sin_port = htons(6000); if (bind(sockSvr,(SOCKADDR*)&sadressIn,sizeof(SOCKADDR))!=0) { printf("server bind failure/n"); WSACleanup(); return; } printf("server bind success/n"); //recevie message from client; char recvBuffer[128]; SOCKADDR_IN sadressInClient; int len = sizeof(SOCKADDR); recvfrom(sockSvr,recvBuffer,128,0,(SOCKADDR*)&sadressInClient,&len); printf("come from client's message: %s/n",recvBuffer); closesocket(sockSvr); WSACleanup(); } UdpClient: #include <stdio.h> #include <WINSOCK2.H> /* udp client */ void main() { WORD wVersionRequested = MAKEWORD(2,2); WSADATA wsaData; if (WSAStartup(wVersionRequested,&wsaData)!=0) { printf("client WSAStartup failure/n"); WSACleanup(); return; } printf("client Startup success/n"); SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0); if (sockClient==INVALID_SOCKET) { printf("client create socket failure/n"); WSACleanup(); return; } printf("client create socket success/n"); //send message to server char sendBuffer[] = "Hello,QQ"; SOCKADDR_IN saddressSvr; saddressSvr.sin_addr.S_un.S_addr = inet_addr("192.168.0.100"); saddressSvr.sin_family = AF_INET; saddressSvr.sin_port = htons(6000); sendto(sockClient,sendBuffer,strlen(sendBuffer)+1,0,(SOCKADDR*)&saddressSvr,sizeof(SOCKADDR)); printf("client send message 'Hello,QQ' to server/n"); closesocket(sockClient); WSACleanup(); printf("client has close/n"); }