基于UDP的windows Socket编程

服务器端源代码(UDPServ.cpp):

 1 #pragma comment(lib, "Ws2_32.lib")
 2 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 
 8 
 9 int _tmain(int argc, _TCHAR* argv[])
10 {
11     WORD wVersionRequested;
12     WSADATA wsaData;
13     int err;
14 
15     wVersionRequested = MAKEWORD(2, 2);
16 
17     err = WSAStartup(wVersionRequested, &wsaData);
18     if (err != 0) {
19         /* Tell the user that we could not find a usable */
20         /* Winsock DLL.                                  */
21         printf("WSAStartup failed with error: %d\n", err);
22         return 1;
23     }
24 
25     if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
26         /* Tell the user that we could not find a usable */
27         /* WinSock DLL.                                  */
28         printf("Could not find a usable version of Winsock.dll\n");
29         WSACleanup();
30         return 1;
31     }
32     else
33         printf("The Winsock 2.2 dll was found okay\n");
34 
35     SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM, 0);
36 
37     SOCKADDR_IN  addrServ;
38     addrServ.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
39     addrServ.sin_family = AF_INET;
40     addrServ.sin_port = htons(4999);
41 
42     bind(sockSrv,(SOCKADDR*)&addrServ,sizeof(SOCKADDR));
43 
44     SOCKADDR_IN  addrClient;
45     int length = sizeof(SOCKADDR);
46     char recvBuf[100];
47 
48     recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&length);
49 
50     printf("%s",recvBuf);
51 
52     closesocket(sockSrv);
53 
54     WSACleanup();
55 
56     system("PAUSE");
57     return 0;
58 }

客户端源代码(UDPClient.cpp):

 1 #pragma comment(lib, "Ws2_32.lib")
 2 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     WORD wVersionRequested;
11     WSADATA wsaData;
12     int err;
13 
14     wVersionRequested = MAKEWORD(2, 2);
15 
16     err = WSAStartup(wVersionRequested, &wsaData);
17     if (err != 0) {
18         /* Tell the user that we could not find a usable */
19         /* Winsock DLL.                                  */
20         printf("WSAStartup failed with error: %d\n", err);
21         return 1;
22     }
23 
24     if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
25         /* Tell the user that we could not find a usable */
26         /* WinSock DLL.                                  */
27         printf("Could not find a usable version of Winsock.dll\n");
28         WSACleanup();
29         return 1;
30     }
31     else
32         printf("The Winsock 2.2 dll was found okay\n");
33 
34 
35     SOCKET sockClient = socket(AF_INET,SOCK_DGRAM, 0);
36 
37     SOCKADDR_IN  addrServ;
38     addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
39     addrServ.sin_family = AF_INET;
40     addrServ.sin_port = htons(4999);
41 
42     sendto(sockClient,"Hello\n",strlen("Hello\n")+1,0,(SOCKADDR*)&addrServ,sizeof(SOCKADDR));
43 
44     closesocket(sockClient);
45 
46     system("PAUSE");
47     WSACleanup();
48         
49     return 0;
50 }

转载于:https://www.cnblogs.com/china-victory/archive/2012/11/06/2757634.html

你可能感兴趣的:(基于UDP的windows Socket编程)