UDP服务器代码:
1 #include <errno.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <netinet/in.h> 7 #include <arpa/inet.h> 8 #include <iostream> 9 10 using namespace std; 11 12 int main(int argc, char *argv[]) 13 { 14 if (argc != 3) 15 { 16 cout << "usage: " << argv[0] << " ip port" << endl; 17 return -1; 18 } 19 20 char *szIp = argv[1]; 21 in_addr_t iIp = inet_addr(szIp); 22 if (iIp == INADDR_NONE) 23 { 24 cerr << "fail to parse ip: " << szIp << endl; 25 return -1; 26 } 27 char *pEnd = NULL; 28 uint16_t usPort = strtoul(argv[2], &pEnd, 10); 29 if (*pEnd != '