UDP Socket編程 C/C++實現

  1. //services
  2. #pragma comment (lib,"ws2_32.lib")  
  3.  
  4. #include <Winsock2.h>  
  5.   
  6. #include <stdio.h>  
  7.   
  8.   
  9.   
  10. void main()  
  11.   
  12. {  
  13.   
  14.     //版本協商  
  15.   
  16.     WORD wVersionRequested;  
  17.   
  18.     WSADATA wsaData;  
  19.   
  20.     int err;  
  21.   
  22.       
  23.   
  24.     wVersionRequested = MAKEWORD( 1, 1 );  
  25.   
  26.       
  27.   
  28.     err = WSAStartup( wVersionRequested, &wsaData );  
  29.   
  30.     if ( err != 0 ) {  
  31.   
  32.         /* Tell the user that we could not find a usable */  
  33.   
  34.         /* WinSock DLL.                                  */  
  35.   
  36.         return;  
  37.   
  38.     }  
  39.   
  40.       
  41.   
  42.     /* Confirm that the WinSock DLL supports 2.2.*/  
  43.   
  44.     /* Note that if the DLL supports versions greater    */  
  45.   
  46.     /* than 2.2 in addition to 2.2, it will still return */  
  47.   
  48.     /* 2.2 in wVersion since that is the version we      */  
  49.   
  50.     /* requested.                                        */  
  51.   
  52.       
  53.   
  54.     if ( LOBYTE( wsaData.wVersion ) != 1 ||  
  55.   
  56.         HIBYTE( wsaData.wVersion ) != 1) {  
  57.   
  58.         /* Tell the user that we could not find a usable */  
  59.   
  60.         /* WinSock DLL.                                  */  
  61.   
  62.         WSACleanup( );  
  63.   
  64.         return;   
  65.   
  66.     }  
  67.   
  68.       
  69.   
  70.     /* The WinSock DLL is acceptable. Proceed. */  
  71.   
  72.     //創建數據報套接字  
  73.   
  74.     SOCKET svr = socket(AF_INET,SOCK_DGRAM,0);  
  75.   
  76.     //創建本地地址信息  
  77.   
  78.     SOCKADDR_IN addr;  
  79.   
  80.     addr.sin_family = AF_INET;  
  81.   
  82.     addr.sin_port = htons(6000);  
  83.   
  84.     addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  85.   
  86.     int len = sizeof(sockaddr);  
  87.   
  88.     bind(svr,(sockaddr*)&addr,len);  
  89.   
  90.     //創建客戶端地址對象  
  91.   
  92.     SOCKADDR_IN addrClient;  
  93.   
  94.     char recvBuf[128];  
  95.   
  96.     char sendBuf[128];  
  97.   
  98.     char tempBuf[256];  
  99.   
  100.       
  101.   
  102.     while(true)  
  103.   
  104.     {  
  105.   
  106.         //接收數據  
  107.   
  108.         recvfrom(svr,recvBuf,128,0,(sockaddr*)&addrClient,&len);  
  109.   
  110.         char* ipClient = inet_ntoa(addrClient.sin_addr);  
  111.   
  112.         sprintf(tempBuf,"%s said: %s/n",ipClient,recvBuf);  
  113.   
  114.         printf("%s",tempBuf);  
  115.   
  116.         gets(sendBuf);  
  117.   
  118.         //發送數據  
  119.   
  120.         sendto(svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrClient,len);  
  121.   
  122.     }  
  123.   
  124.     closesocket(svr);  
  125.   
  126.     WSACleanup();  
  127.   


  128. //client

  129. #pragma comment (lib,"ws2_32.lib")  
  130.  
  131. #include <Winsock2.h>  
  132.   
  133. #include <stdio.h>  
  134.   
  135.   
  136.   
  137. void main()  
  138.   
  139. {  
  140.   
  141.     //版本協商  
  142.   
  143.     WORD wVersionRequested;  
  144.   
  145.     WSADATA wsaData;  
  146.   
  147.     int err;  
  148.   
  149.       
  150.   
  151.     wVersionRequested = MAKEWORD( 1, 1 );  
  152.   
  153.       
  154.   
  155.     err = WSAStartup( wVersionRequested, &wsaData );  
  156.   
  157.     if ( err != 0 ) {  
  158.   
  159.         /* Tell the user that we could not find a usable */  
  160.   
  161.         /* WinSock DLL.                                  */  
  162.   
  163.         return;  
  164.   
  165.     }  
  166.   
  167.       
  168.   
  169.     /* Confirm that the WinSock DLL supports 2.2.*/  
  170.   
  171.     /* Note that if the DLL supports versions greater    */  
  172.   
  173.     /* than 2.2 in addition to 2.2, it will still return */  
  174.   
  175.     /* 2.2 in wVersion since that is the version we      */  
  176.   
  177.     /* requested.                                        */  
  178.   
  179.       
  180.   
  181.     if ( LOBYTE( wsaData.wVersion ) != 1 ||  
  182.   
  183.         HIBYTE( wsaData.wVersion ) != 1 ) {  
  184.   
  185.         /* Tell the user that we could not find a usable */  
  186.   
  187.         /* WinSock DLL.                                  */  
  188.   
  189.         WSACleanup( );  
  190.   
  191.         return;   
  192.   
  193.     }  
  194.   
  195.       
  196.   
  197.     /* The WinSock DLL is acceptable. Proceed. */  
  198.   
  199.     //創建服務器套接字  
  200.   
  201.     SOCKET Svr = socket(AF_INET,SOCK_DGRAM,0);  
  202.   
  203.     //創建地址  
  204.   
  205.     SOCKADDR_IN addrSvr;  
  206.   
  207.     addrSvr.sin_family = AF_INET;  
  208.   
  209.     addrSvr.sin_port = htons(6000);  
  210.   
  211.     addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");  
  212.   
  213.     char recvBuf[128];  
  214.   
  215.     char sendBuf[128];  
  216.   
  217.     int len = sizeof(sockaddr);  
  218.   
  219.   
  220.   
  221.     while(true)  
  222.   
  223.     {  
  224.   
  225.         gets(sendBuf);  
  226.   
  227.         //發送數據  
  228.   
  229.         sendto(Svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrSvr,len);  
  230.   
  231.         //接收數據  
  232.   
  233.         recvfrom(Svr,recvBuf,128,0,(sockaddr*)&addrSvr,&len);  
  234.   
  235.         char* ipSvr = inet_ntoa(addrSvr.sin_addr);  
  236.   
  237.         printf("%s said: %s/n",ipSvr,recvBuf);  
  238.   
  239.     }  
  240.   
  241.     closesocket(Svr);  
  242.   
  243.     WSACleanup();  
  244.   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章