UDP Socket编程 C/C++实现 (Windows Platform SDK)

来源:互联网 发布:win7网络共享无法访问 编辑:程序博客网 时间:2024/04/30 13:58
挺无聊一小程序,俩SB一人说一句,据说聊天程序最好用UDP写。 Server:
[csharp] view plaincopy
  1. #pragma comment (lib,"ws2_32.lib")  
  2.  
  3. #include <Winsock2.h>  
  4.  
  5. #include <stdio.h>  
  6.   
  7.   
  8.   
  9. void main()  
  10.   
  11. {  
  12.   
  13.     //版本协商  
  14.   
  15.     WORD wVersionRequested;  
  16.   
  17.     WSADATA wsaData;  
  18.   
  19.     int err;  
  20.   
  21.       
  22.   
  23.     wVersionRequested = MAKEWORD( 1, 1 );  
  24.   
  25.       
  26.   
  27.     err = WSAStartup( wVersionRequested, &wsaData );  
  28.   
  29.     if ( err != 0 ) {  
  30.   
  31.         /* Tell the user that we could not find a usable */  
  32.   
  33.         /* WinSock DLL.                                  */  
  34.   
  35.         return;  
  36.   
  37.     }  
  38.   
  39.       
  40.   
  41.     /* Confirm that the WinSock DLL supports 2.2.*/  
  42.   
  43.     /* Note that if the DLL supports versions greater    */  
  44.   
  45.     /* than 2.2 in addition to 2.2, it will still return */  
  46.   
  47.     /* 2.2 in wVersion since that is the version we      */  
  48.   
  49.     /* requested.                                        */  
  50.   
  51.       
  52.   
  53.     if ( LOBYTE( wsaData.wVersion ) != 1 ||  
  54.   
  55.         HIBYTE( wsaData.wVersion ) != 1) {  
  56.   
  57.         /* Tell the user that we could not find a usable */  
  58.   
  59.         /* WinSock DLL.                                  */  
  60.   
  61.         WSACleanup( );  
  62.   
  63.         return;   
  64.   
  65.     }  
  66.   
  67.       
  68.   
  69.     /* The WinSock DLL is acceptable. Proceed. */  
  70.   
  71.     //创建数据报套接字  
  72.   
  73.     SOCKET svr = socket(AF_INET,SOCK_DGRAM,0);  
  74.   
  75.     //创建本地地址信息  
  76.   
  77.     SOCKADDR_IN addr;  
  78.   
  79.     addr.sin_family = AF_INET;  
  80.   
  81.     addr.sin_port = htons(6000);  
  82.   
  83.     addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  84.   
  85.     int len = sizeof(sockaddr);  
  86.   
  87.     bind(svr,(sockaddr*)&addr,len);  
  88.   
  89.     //创建客户端地址对象  
  90.   
  91.     SOCKADDR_IN addrClient;  
  92.   
  93.     char recvBuf[128];  
  94.   
  95.     char sendBuf[128];  
  96.   
  97.     char tempBuf[256];  
  98.   
  99.       
  100.   
  101.     while(true)  
  102.   
  103.     {  
  104.   
  105.         //接收数据  
  106.   
  107.         recvfrom(svr,recvBuf,128,0,(sockaddr*)&addrClient,&len);  
  108.   
  109.         char* ipClient = inet_ntoa(addrClient.sin_addr);  
  110.   
  111.         sprintf(tempBuf,"%s said: %s/n",ipClient,recvBuf);  
  112.   
  113.         printf("%s",tempBuf);  
  114.   
  115.         gets(sendBuf);  
  116.   
  117.         //发送数据  
  118.   
  119.         sendto(svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrClient,len);  
  120.   
  121.     }  
  122.   
  123.     closesocket(svr);  
  124.   
  125.     WSACleanup();  
  126.   
  127. }  
Client:
[csharp] view plaincopy
  1. #pragma comment (lib,"ws2_32.lib")  
  2.  
  3. #include <Winsock2.h>  
  4.  
  5. #include <stdio.h>  
  6.   
  7.   
  8.   
  9. void main()  
  10.   
  11. {  
  12.   
  13.     //版本协商  
  14.   
  15.     WORD wVersionRequested;  
  16.   
  17.     WSADATA wsaData;  
  18.   
  19.     int err;  
  20.   
  21.       
  22.   
  23.     wVersionRequested = MAKEWORD( 1, 1 );  
  24.   
  25.       
  26.   
  27.     err = WSAStartup( wVersionRequested, &wsaData );  
  28.   
  29.     if ( err != 0 ) {  
  30.   
  31.         /* Tell the user that we could not find a usable */  
  32.   
  33.         /* WinSock DLL.                                  */  
  34.   
  35.         return;  
  36.   
  37.     }  
  38.   
  39.       
  40.   
  41.     /* Confirm that the WinSock DLL supports 2.2.*/  
  42.   
  43.     /* Note that if the DLL supports versions greater    */  
  44.   
  45.     /* than 2.2 in addition to 2.2, it will still return */  
  46.   
  47.     /* 2.2 in wVersion since that is the version we      */  
  48.   
  49.     /* requested.                                        */  
  50.   
  51.       
  52.   
  53.     if ( LOBYTE( wsaData.wVersion ) != 1 ||  
  54.   
  55.         HIBYTE( wsaData.wVersion ) != 1 ) {  
  56.   
  57.         /* Tell the user that we could not find a usable */  
  58.   
  59.         /* WinSock DLL.                                  */  
  60.   
  61.         WSACleanup( );  
  62.   
  63.         return;   
  64.   
  65.     }  
  66.   
  67.       
  68.   
  69.     /* The WinSock DLL is acceptable. Proceed. */  
  70.   
  71.     //创建服务器套接字  
  72.   
  73.     SOCKET Svr = socket(AF_INET,SOCK_DGRAM,0);  
  74.   
  75.     //创建地址  
  76.   
  77.     SOCKADDR_IN addrSvr;  
  78.   
  79.     addrSvr.sin_family = AF_INET;  
  80.   
  81.     addrSvr.sin_port = htons(6000);  
  82.   
  83.     addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");  
  84.   
  85.     char recvBuf[128];  
  86.   
  87.     char sendBuf[128];  
  88.   
  89.     int len = sizeof(sockaddr);  
  90.   
  91.   
  92.   
  93.     while(true)  
  94.   
  95.     {  
  96.   
  97.         gets(sendBuf);  
  98.   
  99.         //发送数据  
  100.   
  101.         sendto(Svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrSvr,len);  
  102.   
  103.         //接收数据  
  104.   
  105.         recvfrom(Svr,recvBuf,128,0,(sockaddr*)&addrSvr,&len);  
  106.   
  107.         char* ipSvr = inet_ntoa(addrSvr.sin_addr);  
  108.   
  109.         printf("%s said: %s/n",ipSvr,recvBuf);  
  110.   
  111.     }  
  112.   
  113.     closesocket(Svr);  
  114.   
  115.     WSACleanup();  
  116.   
  117. }  
0 0