这次使用UDP,同样上代码说明: 在需要使用的地方包含socket库,这里使用winsocket2的库,微软封装处理。 #include <winsock2.h> #pragma comment(lib, "WS2_32.lib") 服务器端代码: int StartUDPSrv() { #pragma region 初始化套接字库 WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(1, 1), &wsaData); //版本1.1 if (NO_ERROR != iResult) { cout<<"Error at WSAStartup()/n"; return -1; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return -1; }//如果不是1.1的则退出 #pragma endregion 初始化套接字库 #pragma region 创建套接字 // Create a SOCKET for listening for incoming connection requests SOCKET socketSrv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (INVALID_SOCKET == socketSrv) { cout<<"Error at socket():/n"<< WSAGetLastError(); WSACleanup(); return -1; } #pragma endregion 创建套接字 #pragma region 绑定套接字 int err; SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSrv.sin_port = htons(8086); err = bind(socketSrv, (SOCKADDR*) &addrSrv, sizeof(addrSrv)); if (SOCKET_ERROR == err) { cout<<"Error at Bind!/n"<< WSAGetLastError(); closesocket(socketSrv); WSACleanup(); return -1; } #pragma endregion 绑定套接字 #pragma region 等待接收 SOCKADDR_IN addrClient; int len = sizeof(sockaddr); char recvBuf[100]; while (1) { memset(recvBuf, '*', sizeof(char)*100); //置空接受数组 err = recvfrom(socketSrv, recvBuf, strlen(recvBuf), 0, (SOCKADDR*) &addrClient, &len); int len2 = strlen(recvBuf); if (SOCKET_ERROR == err) { cout<<"Error at RecvFrom Srv!/n"<< WSAGetLastError(); /*closesocket(socketSrv); WSACleanup(); return -1;*/ } cout<<recvBuf<<endl; /************************************************************************/ /* 发送数据 */ /************************************************************************/ char sendBuf[100]; sprintf_s(sendBuf, 100, "Welcome %s to This UDP Service!", inet_ntoa(addrClient.sin_addr)); err = sendto(socketSrv, sendBuf, strlen(sendBuf)+1, 0, (SOCKADDR*) &addrClient, sizeof(SOCKADDR)); if (SOCKET_ERROR == err) { cout<<"Error at SrvSendto!/n"<< WSAGetLastError(); closesocket(socketSrv); WSACleanup(); return -1; } } #pragma endregion 等待接收 closesocket(socketSrv); WSACleanup(); return 1; } 客户端代码: int CallUDPClient() { #pragma region 初始化套接字库 WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(1, 1), &wsaData); //版本1.1 if (NO_ERROR != iResult) { cout<<"Error at WSAStartup()/n"; return -1; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return -1; }//如果不是1.1的则退出 #pragma endregion 初始化套接字库 #pragma region 创建套接字 // Create a SOCKET for listening for incoming connection requests SOCKET socketClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (INVALID_SOCKET == socketClient) { cout<<"Error at socket():/n"<< WSAGetLastError(); WSACleanup(); return -1; } #pragma endregion 创建套接字 #pragma region 发送数据 SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addrSrv.sin_port = htons(8086); int len = sizeof(SOCKADDR); //char sendBuf[100] = "Hello! This is UDP Client!"; char sendBuf[100]; gets_s(sendBuf); int err = sendto(socketClient, sendBuf, strlen(sendBuf)+1, 0, (SOCKADDR*) &addrSrv, len); if (SOCKET_ERROR == err) { cout<<"Error at Sendto Client:/n"<< WSAGetLastError(); closesocket(socketClient); WSACleanup(); return -1; } #pragma endregion 发送数据 #pragma region 接受数据 char recvBuf[100]; err = recvfrom(socketClient, recvBuf, strlen(recvBuf), 0, (SOCKADDR*)&addrSrv, &len); if (SOCKET_ERROR == err) { cout<<"Error at RecvFrom Client:/n"<< WSAGetLastError(); closesocket(socketClient); WSACleanup(); return -1; } cout<<recvBuf<<endl; #pragma endregion 接受数据 closesocket(socketClient); WSACleanup(); return 1; } 测试环境VS2008+SP3