在windows环境下,使用netstat命令查看网络状态,具体命令如下所示:
netstat -ano | findstr listenport
在windows环境下,创建udp程序接收端,具体代码如下:
// Command line options: // udp_server [-i:IP] [-p:port] // -i:IP Local IP address to listen on // -p:int Local port #include <string> #include <winsock2.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "ws2_32.lib") const int DEFAULT_BUFFER_LENGTH = 4096; void Usage() { printf("usage: udp_server [-i:IP] [-p:port]\n"); printf(" -i:IP Local IP address to listen on\n"); printf(" -p:int Local port\n"); ExitProcess(1); } struct AddrInfo { std::string ip; int port; }; AddrInfo GetInputAddrInfo(int argc, char **argv) { if(argc < 3) { Usage(); } AddrInfo addrInfo; for(int i = 1; i < argc; i++) { if ((argv[i][0] == '-')) { switch (tolower(argv[i][1])) { case 'p': //port if (strlen(argv[i]) > 3) { addrInfo.port = atoi(&argv[i][3]); } break; case 'i': //ip if (strlen(argv[i]) > 3) { addrInfo.ip = std::string(&argv[i][3]); } break; default: Usage(); break; } } } return addrInfo; } int main(int argc, char **argv) { AddrInfo addrInfo = GetInputAddrInfo(argc, argv); WSADATA wsd; if (WSAStartup(MAKEWORD(1,1), &wsd) != 0) { printf("WSAStartup failed!\n"); return 1; } SOCKET s; if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { printf("socket() failed; %d\n", WSAGetLastError()); return 1; } SOCKADDR_IN local; local.sin_family = AF_INET; local.sin_port = htons((short)addrInfo.port); local.sin_addr.s_addr = inet_addr(addrInfo.ip.c_str()); if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR) { printf("bind() failed: %d\n", WSAGetLastError()); return 1; } char* recvBuf = (char*)malloc(DEFAULT_BUFFER_LENGTH); if (recvBuf == NULL) { printf("GlobalAlloc() failed: %d\n", GetLastError()); return 1; } SOCKADDR_IN sender; while(true) { int sendSize = sizeof(SOCKADDR_IN); int ret = recvfrom(s, recvBuf, DEFAULT_BUFFER_LENGTH, 0, (SOCKADDR *)&sender, &sendSize); if (ret == SOCKET_ERROR) { printf("recvfrom() failed; %d\n", WSAGetLastError()); break; } else if (ret == 0) { break; } else { recvBuf[ret] = '\0'; printf("[%s] send me [%s]\n", inet_ntoa(sender.sin_addr), recvBuf); } } closesocket(s); free(recvBuf); WSACleanup(); return 0; }
运行上述程序,使用命令netstat -ano | findstr 8888,获得如下输出结果:
按照相同步骤,创建udp程序发送端,具体代码如下:
// Command line options: // udp_client [-r:IP] [-p:port] // -r:IP Recipient's IP address // -p:int Remote port // -n:x Number of times to send message #include <string> #include <winsock2.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "ws2_32.lib") const char SEND_MESSAGE[] = "Hello world!"; void Usage() { printf("usage: udp_client [-r:IP] [-p:port] [-n:x]\n"); printf(" -r:IP Recipient's IP address\n"); printf(" -p:int Remote port\n"); printf(" -n:x Number of times to send message"); ExitProcess(1); } struct InputInfo { std::string ip; int port; int sendTimes; }; InputInfo GetInputInfo(int argc, char **argv) { if(argc < 4) { Usage(); } InputInfo inputInfo; for(int i = 1; i < argc; i++) { if ((argv[i][0] == '-')) { switch (tolower(argv[i][1])) { case 'p': //port if (strlen(argv[i]) > 3) { inputInfo.port = atoi(&argv[i][3]); } break; case 'r': //ip if (strlen(argv[i]) > 3) { inputInfo.ip = std::string(&argv[i][3]); } break; case 'n': //Number of times to send message if (strlen(argv[i]) > 3) { inputInfo.sendTimes = atol(&argv[i][3]); } break; default: Usage(); break; } } } return inputInfo; } int main(int argc, char **argv) { InputInfo inputInfo = GetInputInfo(argc, argv); WSADATA wsd; if (WSAStartup(MAKEWORD(1,1), &wsd) != 0) { printf("WSAStartup failed!\n"); return 1; } SOCKET s; if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { printf("socket() failed; %d\n", WSAGetLastError()); return 1; } SOCKADDR_IN recipient; recipient.sin_family = AF_INET; recipient.sin_port = htons((short)inputInfo.port); recipient.sin_addr.s_addr = inet_addr(inputInfo.ip.c_str()); for(int i = 0; i < inputInfo.sendTimes; ++i) { int ret = sendto(s, SEND_MESSAGE, strlen(SEND_MESSAGE), 0, (SOCKADDR*)&recipient, sizeof(recipient)); if(ret == SOCKET_ERROR) { printf("sendto() failed, %d\n", WSAGetLastError()); break; } else if(ret == 0) { break; } else { printf("send [%s] to [%s]\n", std::string(SEND_MESSAGE, ret).c_str(), inet_ntoa(recipient.sin_addr)); } } closesocket(s); WSACleanup(); return 0; }