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 != ‘\0‘) 30 { 31 cerr << "fail to parse port: " << argv[2] << endl; 32 return -1; 33 } 34 35 int iSockFd = socket(AF_INET, SOCK_DGRAM, 0); 36 if (iSockFd < 0) 37 { 38 cerr << "fail to create socket, err: " << strerror(errno) << endl; 39 return -1; 40 } 41 cout << "create socket fd " << iSockFd << endl; 42 43 sockaddr_in oAddr; 44 memset(&oAddr, 0, sizeof(oAddr)); 45 oAddr.sin_family = AF_INET; 46 oAddr.sin_addr.s_addr = iIp; 47 oAddr.sin_port = htons(usPort); 48 if (bind(iSockFd, (sockaddr *)&oAddr, sizeof(oAddr)) < 0) 49 { 50 cerr << "fail to bind addr " << szIp << ":" << usPort << ", err: " << strerror(errno) << endl; 51 return -1; 52 } 53 cout << "bind addr " << szIp << ":" >> usPort << endl; 54 55 uint8_t acRecvBuf[64 * 1024]; 56 while (true) 57 { 58 sockaddr_in oClientAddr; 59 socklen_t iAddrLen = sizeof(oClientAddr); 60 ssize_t iRecvLen = recvfrom(iSockFd, acRecvBuf, sizeof(acRecvBuf), 0, (sockaddr *)&oClientAddr, &iAddrLen); 61 if (iRecvLen < 0) 62 { 63 cerr << "fail to recv, err: " << strerror(errno) << endl; 64 continue; 65 } 66 cout << "recv data from " << inet_ntoa(oClientAddr.sin_addr) << ":" << ntohs(oClientAddr.sin_port) << ", len: " << iRecvLen << endl; 67 68 ssize_t iSendLen = sendto(iSockFd, acRecvBuf, iRecvLen, 0, (sockaddr *)&oClientAddr, iAddrLen); 69 if (iSendLen < 0) 70 { 71 cerr << "fail to send, err: " << strerror(errno) << endl; 72 continue; 73 } 74 cout << "echo to client, len: " << iSendLen << endl; 75 } 76 77 return 0; 78 }
UDP客户端代码:
1 #include <errno.h> 2 #include <string.h> 3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <arpa/inet.h> 7 #include <stdlib.h> 8 #include <iostream> 9 10 using namespace std; 11 12 int main(int argc, char *argv[]) 13 { 14 if (argc != 4) 15 { 16 cout << "usage: " << argv[0] << " ip port message" << 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 != ‘\0‘) 30 { 31 cerr << "fail to parse port: " << argv[2] << endl; 32 return -1; 33 } 34 char *szMsg = argv[3]; 35 size_t uiMsgLen = strlen(szMsg); 36 37 int iSockFd = socket(AF_INET, SOCK_DGRAM, 0); 38 if (iSockFd < 0) 39 { 40 cerr << "fail to create socket, err: " << strerror(errno) << endl; 41 return -1; 42 } 43 cout << "create socket fd " << iSockFd << endl; 44 45 sockaddr_in oServerAddr; 46 memset(&oServerAddr, 0, sizeof(oServerAddr)); 47 oServerAddr.sin_family = AF_INET; 48 oServerAddr.sin_addr.s_addr = iIp; 49 oServerAddr.sin_port = htons(usPort); 50 ssize_t iSendLen = sendto(iSockFd, szMsg, uiMsgLen, 0, (sockaddr *)&oServerAddr, sizeof(oServerAddr)); 51 if (iSendLen < 0) 52 { 53 cerr << "fail to send, err: " << strerror(errno) << endl; 54 return -1; 55 } 56 cout << "send to " << szIp << ":" << usPort << ", len: " << uiMsgLen << ", msg: " << szMsg << endl; 57 58 char szRecvBuf[64 * 1024]; 59 sockaddr_in oRecvAddr; 60 socklen_t iAddrLen = sizeof(oRecvAddr); 61 ssize_t iRecvLen = recvfrom(iSockFd, szRecvBuf, sizeof(szRecvBuf), 0, (sockaddr *)&oRecvAddr, &iAddrLen); 62 if (iRecvLen < 0) 63 { 64 cerr << "fail to recv, err: " << strerror(errno) << endl; 65 return -1; 66 } 67 szRecvBuf[iRecvLen] = ‘\0‘; 68 cout << "recv from " << inet_ntoa(oRecvAddr.sin_addr) << ":" << ntohs(oRecvAddr.sin_port) << ", len: " << iRecvLen << ", msg: " << szRecvBuf << endl; 69 70 return 0; 71 }
时间: 2024-10-18 13:37:57