标签:
TCP服务器代码:
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_STREAM, 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 if (listen(iSockFd, 100) < 0) 56 { 57 cerr << "fail to listen on " << szIp << ":" << usPort << ", err: " << strerror(errno) << endl; 58 } 59 cout << "listen on socket fd " << iSockFd << endl; 60 61 uint8_t acRecvBuf[1024 * 1024]; 62 while (true) 63 { 64 sockaddr_in oClientAddr; 65 socklen_t iAddrLen = sizeof(oClientAddr); 66 int iAcceptFd = accept(iSockFd, (sockaddr *)&oClientAddr, &iAddrLen); 67 if (iAcceptFd < 0) 68 { 69 cerr << "fail to accpet, err: " << strerror(errno) << endl; 70 continue; 71 } 72 cout << "recv connection from " << inet_ntoa(oClientAddr.sin_addr) << ":" << ntohs(oClientAddr.sin_port) << endl; 73 74 while (true) 75 { 76 ssize_t iRecvLen = recv(iAcceptFd, acRecvBuf, sizeof(acRecvBuf), 0); 77 if (iRecvLen < 0) 78 { 79 cerr << "fail to recv, close connection, err: " << strerror(errno) << endl; 80 close(iAcceptFd); 81 break; 82 } 83 if (iRecvLen == 0) 84 { 85 cout << "connection closed by client" << endl; 86 close(iAcceptFd); 87 break; 88 } 89 cout << "recv data len: " << iRecvLen << endl; 90 91 ssize_t iSendLen = send(iAcceptFd, acRecvBuf, iRecvLen, 0); 92 if (iSendLen < 0) 93 { 94 cerr << "fail to send, err: " << strerror(errno) << endl; 95 close(iAcceptFd); 96 break; 97 } 98 cout << "echo to client, len: " << iSendLen << endl; 99 } 100 101 } 102 }
TCP客户端代码:
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_STREAM, 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 if (connect(iSockFd, (sockaddr *)&oServerAddr, sizeof(oServerAddr)) < 0) 51 { 52 cerr << "fail to connect to " << szIp << ":" << usPort << ", err: " << strerror(errno) << endl; 53 return -1; 54 } 55 cout << "connect to " << szIp << ":" << usPort << endl; 56 57 ssize_t iSendLen = send(iSockFd, szMsg, uiMsgLen, 0); 58 if (iSendLen < 0) 59 { 60 cerr << "fail to send, err: " << strerror(errno) << endl; 61 return -1; 62 } 63 cout << "send len: " << iSendLen << ", msg: " << szMsg << endl; 64 65 char szRecvBuf[1024 * 1024]; 66 char *pRecvBuf = szRecvBuf; 67 size_t uiBufLen = sizeof(szRecvBuf); 68 size_t uiRecvTotal = 0; 69 while (uiRecvTotal < iSendLen) 70 { 71 ssize_t iRecvLen = recv(iSockFd, pRecvBuf, uiBufLen, 0); 72 if (iRecvLen < 0) 73 { 74 cerr << "fail to recv, close connection, err: " << strerror(errno) << endl; 75 close(iSockFd); 76 return -1; 77 } 78 if (iRecvLen == 0) 79 { 80 cout << "connection closed by server" << endl; 81 close(iSockFd); 82 break; 83 } 84 pRecvBuf += iRecvLen; 85 uiBufLen -= iRecvLen; 86 uiRecvTotal += iRecvLen; 87 } 88 szRecvBuf[uiRecvTotal] = ‘\0‘; 89 cout << "recv len: " << uiRecvTotal << ", msg: " << szRecvBuf << endl; 90 close(iSockFd); 91 92 return 0; 93 }
标签:
原文地址:http://www.cnblogs.com/glacierh/p/4771661.html