51Testing软件测试论坛

标题: Socket之UDP通信例子 [打印本页]

作者: 悠悠小仙仙    时间: 2017-11-3 15:10
标题: Socket之UDP通信例子

UDP通信方式:

1,客户端不需要进行连接,而是直接访问服务器通过sendto来发送数据,recvfrom接受数据

2,服务端,不需要监听、接受等待客户端请求,当收到客户端socket通过sendto来回应客户端请求。

Client端:

  1. #include <winsock2.h>  
  2. #include <iostream>  
  3.   
  4. #pragma comment(lib, "WS2_32.lib")  
  5. const unsigned int PORT = 11024;  
  6. const int kBufferSize   = 1024;  
  7.   
  8. int main() {  
  9.   
  10.     WSADATA wsData;  
  11.     if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) {  
  12.         std::cout << "start up failed\n" << std::endl;  
  13.         return 0;  
  14.     }  
  15.     // connect the socket;  
  16.   
  17.     SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  
  18.     SOCKADDR_IN addIn;  
  19.     addIn.sin_family = AF_INET;  
  20.     addIn.sin_port = htons(PORT);  
  21.     addIn.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");  
  22.       
  23.     char cSendBuffer[kBufferSize] = "Client sends a Message to the Server, please confirm?";  
  24.     int bRecvLen = sizeof(addIn);  
  25.     int bSendRet = sendto(s, cSendBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, sizeof(addIn));  
  26.     while(bSendRet != SOCKET_ERROR) {  
  27.         char cRecvBuffer[kBufferSize] = "#";  
  28.         int bRecv = recvfrom(s, cRecvBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, &bRecvLen);  
  29.         if(bRecv != SOCKET_ERROR) {  
  30.             std::cout << "Client recvie the infromation from the server is: " << cRecvBuffer << std::endl;  
  31.             ZeroMemory(cSendBuffer, kBufferSize);  
  32.             std::cout << "please input the information the sends to the server: ";  
  33.             std::cin >> cSendBuffer;  
  34.             bSendRet = sendto(s, cSendBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, sizeof(addIn));  
  35.         } else{  
  36.             break;  
  37.         }  
  38.     }  
  39.   
  40.     if(WSACleanup() != 0) {  
  41.         std::cout << "clean up failed\n";  
  42.         return -1;  
  43.     }  
  44.     closesocket(s);  
  45.     return 0;  
  46. }  
复制代码

Server端:

  1. #include <iostream>  
  2. #include <winsock2.h>  
  3.   
  4. #pragma comment(lib, "WS2_32.lib")  
  5.   
  6. const unsigned int PORT = 11024;  
  7. const int kBufferSize   = 1024;  
  8.   
  9. int main() {  
  10.     WSADATA wsData;  
  11.     if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) {  
  12.         std::cout << "start failed" << std::endl;  
  13.         return -1;  
  14.     }  
  15.     //  
  16.     SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  
  17.     SOCKADDR_IN addIn;  
  18.       
  19.     addIn.sin_family = AF_INET;  
  20.     addIn.sin_port = htons(PORT);  
  21.     addIn.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");  
  22.     int bBindRet = bind(s, (LPSOCKADDR) &addIn, sizeof(addIn));  
  23.     if(bBindRet != 0) {  
  24.         std::cout << "bind failed" << std::endl;  
  25.         return -1;  
  26.     }  
  27.   
  28.     char cRecvBuffer[kBufferSize];  
  29.     int ilen = sizeof(addIn);                 
  30.     int iRecv = recvfrom(s, cRecvBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, &ilen);  
  31.     while(iRecv != SOCKET_ERROR) {  
  32.         std::cout << "recvie information: " << std::endl;  
  33.         char cSendBuffer[kBufferSize];  
  34.         std::cout << "Input the information that the client sends; " << std::endl;  
  35.         std::cin >> cSendBuffer;  
  36.         sendto(s, cSendBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, sizeof(addIn));  
  37.     }  
  38.   
  39.     if(WSACleanup() != 0) {  
  40.         std::cout << "Clean up failed" << std::endl;  
  41.         return -1;  
  42.     }  
  43.   
  44.     return 0;  
  45. }  
复制代码







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2