{"id":290429,"date":"2024-03-20T05:30:00","date_gmt":"2024-03-19T21:30:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/rhcjjytxydwl.html"},"modified":"2024-03-20T05:30:00","modified_gmt":"2024-03-19T21:30:00","slug":"rhcjjytxydwl","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/rhcjjytxydwl.html","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u57fa\u4e8eTCP\u534f\u8bae\u7684\u7f51\u7edc\u7a0b\u5e8f–\u534f\u8bae\u901a\u8baf\u6d41\u7a0b"},"content":{"rendered":"

\u5728TCP\/IP\u5408\u540c\u4e2d\uff0c\u201dIP\u5730\u5740+TCP\u6216UDP\u7aef\u6807\u8bed\u201d\u53ef\u4ee5\u60df\u4e00\u6807\u793a\u7f51\u8def\u901a\u4fe1\u4e2d\u7684\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u201dIP\u5730\u5740+\u7aef\u6807\u8bed\u201d\u5c31\u79f0\u4e3asocket\u3002\u672c\u6587\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684TCP\u5408\u540c\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u600e\u6837\u521b\u5efa\u57fa\u4e8eTCP\u5408\u540c\u7684\u7f51\u8def\u7a0b\u5e8f\u3002<\/p>\n

TCP\u5408\u540c\u901a\u4fe1\u6d41\u7a0b<\/p>\n

\u53f3\u56fe\u63cf\u8ff0\u4e86TCP\u5408\u540c\u7684\u901a\u4fe1\u6d41\u7a0b(\u6b64\u56fe\u6765\u81ea\u4e92\u8054\u7f51)\uff1a<\/p>\n

linux socket_linux socket_linux socket<\/p>\n

\u53f3\u56fe\u5219\u63cf\u8ff0TCP\u6784\u5efa\u8054\u63a5\u7684\u8fc7\u7a0b(\u6b64\u56fe\u6765\u81ea\u4e92\u8054\u7f51)\uff1a<\/p>\n

linux socket_linux socket_linux socket<\/p>\n

\u670d\u52a1\u5668\u8c03\u7528socket()\u3001bind()\u3001listen()\u51fd\u6570\u5b8c\u6210\u521d\u59cb\u5316\u540e\uff0c\u8c03\u7528accept()\u963b\u585e\u7b49\u5f85\uff0c\u5904\u4e8e\u7a83\u542c\u7aef\u53e3\u7684\u72b6\u6001\uff0c\u987e\u5ba2\u7aef\u8c03\u7528socket()\u521d\u59cb\u5316\u540e\uff0c\u8c03\u7528connect()\u53d1\u51faSYN\u6bb5\u5e76\u963b\u585e\u7b49\u5f85\u670d\u52a1\u5668\u5e94\u7b54\uff0c\u670d\u52a1\u5668\u5e94\u7b54\u4e00\u4e2aSYN-ACK\u6bb5\uff0c\u987e\u5ba2\u7aef\u6536\u5230\u540e\u4ececonnect()\u8fd4\u56de\uff0c\u540c\u65f6\u5e94\u7b54\u4e00\u4e2aACK\u6bb5\uff0c\u670d\u52a1\u5668\u6536\u5230\u540e\u4eceaccept()\u8fd4\u56de\u3002<\/p>\n

TCP\u8054\u63a5\u5b8c\u5584\u540e\u6570\u636e\u4f20\u8f93\u7684\u8fc7\u7a0b\uff1a<\/p>\n

\u6784\u5efa\u8054\u63a5\u540e\uff0cTCP\u5408\u540c\u63d0\u4f9b\u5168\u53cc\u5de5\u7684\u901a\u8baf\u670d\u52a1\uff0c\u5e76\u4e14\u901a\u5e38\u7684\u987e\u5ba2\u7aef\/\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u6d41\u7a0b\u662f\u7531\u987e\u5ba2\u7aef\u4e3b\u52a8\u53d1\u8d77\u6073\u6c42\uff0c\u670d\u52a1\u5668\u88ab\u52a8\u5904\u7406\u6073\u6c42\uff0c\u4e00\u95ee\u4e00\u7b54\u7684\u5f62\u5f0f\u3002\u4e3a\u6b64\uff0c\u670d\u52a1\u5668\u4eceaccept()\u8fd4\u56de\u540e\u7acb\u5373\u8c03\u7528read()\uff0c\u8bfbsocket\u5982\u540c\u8bfb\u7ba1\u7ebf\u4e00\u6837\uff0c\u5047\u5982\u6ca1\u6709\u6570\u636e\u62b5\u8fbe\u5c31\u963b\u585e\u7b49\u5f85\uff0c\u8fd9\u65f6\u987e\u5ba2\u7aef\u8c03\u7528write()\u53d1\u9001\u6073\u6c42\u7ed9\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u6536\u5230\u540e\u4eceread()\u8fd4\u56de\uff0c\u5bf9\u987e\u5ba2\u7aef\u7684\u6073\u6c42\u8fdb\u884c\u5904\u7406\uff0c\u5728\u6b64\u671f\u95f4\u987e\u5ba2\u7aef\u8c03\u7528read()\u963b\u585e\u7b49\u5f85\u670d\u52a1\u5668\u7684\u5e94\u7b54\uff0c\u670d\u52a1\u5668\u8c03\u7528write()\u5c06\u5904\u7406\u7ed3\u679c\u53d1\u56de\u7ed9\u987e\u5ba2\u7aef\uff0c\u518d\u5ea6\u8c03\u7528read()\u963b\u585e\u7b49\u5f85\u4e0b\u4e00\u6761\u6073\u6c42\uff0c\u987e\u5ba2\u7aef\u6536\u5230\u540e\u4eceread()\u8fd4\u56de\uff0c\u53d1\u9001\u4e0b\u4e00\u6761\u6073\u6c42\uff0c\u8fd9\u4e48\u5faa\u73af\u4e0b\u53bb\u3002<\/p>\n

\u53f3\u56fe\u63cf\u8ff0\u4e86\u5173\u6389TCP\u8054\u63a5\u7684\u8fc7\u7a0b\uff1a<\/p>\n

linux socket_linux socket_linux socket<\/p>\n

\u82e5\u679c\u987e\u5ba2\u7aef\u6ca1\u6709\u66f4\u591a\u7684\u6073\u6c42\u4e86\uff0c\u5c31\u8c03\u7528close()\u5173\u6389\u8054\u63a5\uff0c\u5982\u540c\u5199\u7aef\u5173\u6389\u7684\u7ba1\u7ebf\u4e00\u6837\uff0c\u670d\u52a1\u5668\u7684read()\u8fd4\u56de0\uff0c\u8fd9\u6837\u670d\u52a1\u5668\u5c31\u6653\u5f97\u987e\u5ba2\u7aef\u5173\u6389\u4e86\u8054\u63a5\uff0c\u4e5f\u8c03\u7528close()\u5173\u6389\u8054\u63a5\u3002\u6ce8\u610f\uff0c\u4efb\u4f55\u4e00\u65b9\u8c03\u7528close()\u540e\uff0c\u8054\u63a5\u7684\u4e24\u4e2a\u4f20\u8f93\u65b9\u5411\u90fd\u5173\u6389\uff0c\u4e0d\u80fd\u518d\u53d1\u9001\u6570\u636e\u4e86\u3002\u5047\u5982\u4e00\u65b9\u8c03\u7528shutdown()\u5219\u8054\u63a5\u5904\u4e8e\u534a\u5173\u6389\u72b6\u6001\uff0c\u4ecd\u53ef\u63a5\u6536\u5bf9\u65b9\u53d1\u6765\u7684\u6570\u636e\u3002<\/p>\n

linux socket_linux socket_linux socket<\/p>\n

\u5728\u5b66\u4e60socket\u7f16\u7a0b\u65f6\u8981\u6ce8\u610f\u5e94\u7528\u7a0b\u5e8f\u548cTCP\u5408\u540c\u5c42\u662f\u600e\u4e48\u4ea4\u4e92\u7684\uff1a<\/p>\n

\u5e94\u7528\u7a0b\u5e8f\u8c03\u7528\u67d0\u4e2asocket\u51fd\u6570\u65f6TCP\u5408\u540c\u5c42\u5b8c\u6210\u54ea\u4e9b\u52a8\u4f5c\uff0c\u4f8b\u5982\u8c03\u7528connect()\u4f1a\u53d1\u51faSYN\u6bb5\u5e94\u7528\u7a0b\u5e8f\u600e\u6837\u6653\u5f97TCP\u5408\u540c\u5c42\u7684\u72b6\u6001\u53d8\u5316\uff0c\u4f8b\u5982\u4ece\u67d0\u4e2a\u963b\u585e\u7684socket\u51fd\u6570\u8fd4\u56de\u5c31\u8868\u660eTCP\u5408\u540c\u6536\u5230\u4e86\u4e2a\u522b\u6bb5\uff0c\u518d\u4f8b\u5982read()\u8fd4\u56de0\u5c31\u8868\u660e\u6536\u5230\u4e86FIN\u6bb5<\/p>\n

\u4e0b\u8fb9\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684TCP\u7f51\u8def\u7a0b\u5e8f\u6765\u7406\u89e3\u76f8\u5173\u6982\u5ff5\u3002\u7a0b\u5e8f\u5206\u4e3a\u670d\u52a1\u5668\u7aef\u548c\u987e\u5ba2\u7aef\u4e24\u90e8\u4efd\uff0c\u5b83\u4eec\u4e4b\u95f4\u901a\u8fc7socket\u8fdb\u884c\u901a\u8baf\u3002<\/p>\n

\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f<\/p>\n

\u4e0b\u8fb9\u662f\u4e00\u4e2a\u6781\u5176\u7b80\u5355\u7684\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f\uff0c\u5b83\u4ece\u987e\u5ba2\u7aef\u8bfb\u5b57\u7b26\uff0c\u4e4b\u540e\u5c06\u6bcf\u4f4d\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u5e76\u56de\u732e\u7ed9\u987e\u5ba2\u7aef\uff1a<\/p>\n

\n

#include \n#include \n#include \n#include \n#include \n#define MAXLINE 80\n#define SERV_PORT 8000\nint main(void)\n{\n  struct sockaddr_in servaddr, cliaddr;\n  socklen_t cliaddr_len;\n  int listenfd, connfd;\n  char buf[MAXLINE];\n  char str[INET_ADDRSTRLEN];\n  int i, n;\n  \/\/ socket() \u6253\u5f00\u4e00\u4e2a\u7f51\u7edc\u901a\u8baf\u7aef\u53e3\uff0c\u5982\u679c\u6210\u529f\u7684\u8bdd\uff0c\n  \/\/ \u5c31\u50cf open() \u4e00\u6837\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\n  \/\/ \u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u50cf\u8bfb\u5199\u6587\u4ef6\u4e00\u6837\u7528 read\/write \u5728\u7f51\u7edc\u4e0a\u6536\u53d1\u6570\u636e\u3002\n  listenfd = socket(AF_INET, SOCK_STREAM, 0);\n  bzero(&servaddr, sizeof(servaddr));\n  servaddr.sin_family = AF_INET;\n  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);\n  servaddr.sin_port = htons(SERV_PORT);\n  \n  \/\/ bind() \u7684\u4f5c\u7528\u662f\u5c06\u53c2\u6570 listenfd \u548c servaddr \u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\n  \/\/ \u4f7f listenfd \u8fd9\u4e2a\u7528\u4e8e\u7f51\u7edc\u901a\u8baf\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u76d1\u542c servaddr \u6240\u63cf\u8ff0\u7684\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\n  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));\n  \/\/ listen() \u58f0\u660e listenfd \u5904\u4e8e\u76d1\u542c\u72b6\u6001\uff0c\n  \/\/ \u5e76\u4e14\u6700\u591a\u5141\u8bb8\u6709 20 \u4e2a\u5ba2\u6237\u7aef\u5904\u4e8e\u8fde\u63a5\u5f85\u72b6\u6001\uff0c\u5982\u679c\u63a5\u6536\u5230\u66f4\u591a\u7684\u8fde\u63a5\u8bf7\u6c42\u5c31\u5ffd\u7565\u3002\n  listen(listenfd, 20);\n  printf(\"Accepting connections ...n\");\n  while (1)\n  {\n    cliaddr_len = sizeof(cliaddr);\n    \/\/ \u5178\u578b\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u53ef\u4ee5\u540c\u65f6\u670d\u52a1\u4e8e\u591a\u4e2a\u5ba2\u6237\u7aef\uff0c\n    \/\/ \u5f53\u6709\u5ba2\u6237\u7aef\u53d1\u8d77\u8fde\u63a5\u65f6\uff0c\u670d\u52a1\u5668\u8c03\u7528\u7684 accept() \u8fd4\u56de\u5e76\u63a5\u53d7\u8fd9\u4e2a\u8fde\u63a5\uff0c\n    \/\/ \u5982\u679c\u6709\u5927\u91cf\u7684\u5ba2\u6237\u7aef\u53d1\u8d77\u8fde\u63a5\u800c\u670d\u52a1\u5668\u6765\u4e0d\u53ca\u5904\u7406\uff0c\u5c1a\u672a accept \u7684\u5ba2\u6237\u7aef\u5c31\u5904\u4e8e\u8fde\u63a5\u7b49\u5f85\u72b6\u6001\u3002\n    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);\n   \n    n = read(connfd, buf, MAXLINE);\n    printf(\"received from %s at PORT %dn\",\n        inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),\n        ntohs(cliaddr.sin_port));\n  \n    for (i = 0; i < n; i++)\n    {\n      buf[i] = toupper(buf[i]);\n    }\n      \n    write(connfd, buf, n);\n    close(connfd);\n  }\n}<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u628a\u91cc\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u6587\u4ef6server.c\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u8fb9\u7684\u547d\u4ee4<\/a>\u7f16\u8bd1\uff1a<\/p>\n

\n

$ gcc server.c -o server<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u4e4b\u540e\u8fd0\u884c\u7f16\u8bd1\u4e0b\u6765\u7684server\u7a0b\u5e8f\uff1a<\/p>\n

\n

$ .\/server<\/code><\/pre>\n<\/p>\n

linux socket_linux socket_linux socket<\/p>\n

\u590d\u5236<\/p>\n

\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7ss\u547d\u4ee4\u6765\u67e5\u770b\u4e3b\u673a\u4e0a\u7684\u7aef\u53e3\u7a83\u542c\u60c5\u51b5\uff1a<\/p>\n

\u5982\u4e0a\u56fe\u6240\u793a\uff0cserver\u7a0b\u5e8f\u65e9\u5df2\u5f00\u59cb\u7a83\u542c\u4e3b\u673a\u76848000\u7aef\u53e3\u4e86\u3002<\/p>\n

\u4e0b\u8fb9\u8ba9\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u6bb5\u7a0b\u5e8f\u4e2d\u7528\u5230\u7684socket\u76f8\u5173\u7684API\u3002<\/p>\n

\n

int socket(int family, int type, int protocol);<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

socket()\u6253\u5f00\u4e00\u4e2a\u7f51\u8def\u901a\u4fe1\u7aef\u53e3\uff0c\u5047\u5982\u6210\u529f\u7684\u8bdd\uff0c\u5982\u540copen()\u4e00\u6837\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u50cf\u8bfb\u5199\u6587\u4ef6\u4e00\u6837\u7528read\/write\u5728\u7f51\u8def\u4e0a\u6536\u53d1\u6570\u636e\u3002\u5bf9\u4e8eIPv4\uff0cfamily\u53c2\u6570\u6307\u5b9a\u4e3aAF_INET\u3002\u5bf9\u4e8eTCP\u5408\u540c\uff0ctype\u53c2\u6570\u6307\u5b9a\u4e3aSOCK_STREAM\uff0c\u8868\u793a\u9762\u5411\u6d41\u7684\u4f20\u8f93\u5408\u540c\u3002\u82e5\u679c\u662fUDP\u5408\u540c\uff0c\u5219type\u53c2\u6570\u6307\u5b9a\u4e3aSOCK_DGRAM\uff0c\u8868\u793a\u9762\u5411\u6570\u636e\u62a5\u7684\u4f20\u8f93\u5408\u540c\u3002protocol\u6307\u5b9a\u4e3a0\u5373\u53ef\u3002<\/p>\n

\n

int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u670d\u52a1\u5668\u987b\u8981\u8c03\u7528bind\u51fd\u6570\u7ed1\u5b9a\u4e00\u4e2a\u56fa\u5b9a\u7684\u7f51\u8def\u5730\u5740\u548c\u7aef\u6807\u8bed\u3002bind()\u7684\u4f5c\u7528\u662f\u5c06\u53c2\u6570sockfd\u548cmyaddr\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u4f7fsockfd\u8fd9\u4e2a\u7528\u4e8e\u7f51\u8def\u901a\u4fe1\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7a83\u542cmyaddr\u6240\u63cf\u8ff0\u7684\u5730\u5740\u548c\u7aef\u6807\u8bed\u3002structsockaddr*\u662f\u4e00\u4e2a\u901a\u7528\u8868\u9488\u7c7b\u578b\uff0cmyaddr\u53c2\u6570\u5b9e\u9645\u4e0a\u53ef\u4ee5\u63a5\u53d7\u591a\u79cd\u5408\u540c\u7684sockaddr\u7ed3\u6784\u4f53\u67e5\u770blinux\u662f\u4ec0\u4e48\u7cfb\u7edf\uff0c\u800c\u5b83\u4eec\u7684\u5bbd\u5ea6\u5404\u4e0d\u76f8\u540c\uff0c\u6240\u4ee5\u987b\u8981\u7b2c\u4e09\u4e2a\u53c2\u6570addrlen\u6307\u5b9a\u7ed3\u6784\u4f53\u7684\u539a\u5ea6\u3002<\/p>\n

\u7a0b\u5e8f\u4e2d\u5bf9myaddr\u53c2\u6570\u7684\u521d\u59cb\u5316\u4e3a\uff1a<\/p>\n

\n

bzero(&servaddr, sizeof(servaddr));\nservaddr.sin_family = AF_INET;\nservaddr.sin_addr.s_addr = htonl(INADDR_ANY);\nservaddr.sin_port = htons(SERV_PORT);<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u9996\u5148\u5c06\u6574\u4e2a\u7ed3\u6784\u4f53\u6e05\u96f6\uff0c\u4e4b\u540e\u8bbe\u7f6e\u5730\u5740\u7c7b\u578b\u4e3aAF_INET\uff0c\u7f51\u8def\u5730\u5740\u4e3aINADDR_ANY\uff0c\u8fd9\u4e2a\u5b8f\u8868\u793a\u672c\u5730\u7684\u4efb\u610fIP\u5730\u5740\uff0c\u7531\u4e8e\u670d\u52a1\u5668\u53ef\u80fd\u6709\u591a\u4e2a\u7f51\u5361\uff0c\u6bcf\u4f4d\u7f51\u5361\u4e5f\u53ef\u80fd\u7ed1\u5b9a\u591a\u4e2aIP\u5730\u5740\uff0c\u8fd9\u6837\u8bbe\u7f6e\u53ef\u4ee5\u5728\u6240\u6709\u7684IP\u5730\u5740\u4e0a\u7a83\u542c\uff0c\u76f4\u81f3\u4e0e\u67d0\u4e2a\u987e\u5ba2\u7aef\u6784\u5efa\u4e86\u8054\u63a5\u65f6\u624d\u786e\u5b9a\u51fa\u6765\u7a76\u7adf\u7528\u90a3\u4e2aIP\u5730\u5740\uff0c\u7aef\u6807\u8bed\u4e3aSERV_PORT\uff0c\u6211\u4eec\u5b9a\u4e49\u4e3a8000\u3002<\/p>\n

\n

int listen(int sockfd, int backlog);<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

listen()\u7533\u660esockfd\u5904\u4e8e\u7a83\u542c\u72b6\u6001linux\u5b9a\u65f6\u5668\uff0c\u800c\u4e14\u6700\u591a\u5bb9\u8bb8\u6709backlog\u4e2a\u987e\u5ba2\u7aef\u5904\u4e8e\u8fde\u63a5\u5f85\u72b6\u6001\uff0c\u5047\u5982\u63a5\u6536\u5230\u66f4\u591a\u7684\u8054\u63a5\u6073\u6c42\u5c31\u5ffd\u89c6\u3002<\/p>\n

\n

int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u4e09\u65b9\u63e1\u624b\u5b8c\u6210\u540e\uff0c\u670d\u52a1\u5668\u8c03\u7528accept()\u63a5\u53d7\u8054\u63a5\uff0c\u5047\u5982\u670d\u52a1\u5668\u8c03\u7528accept()\u65f6\u8fd8\u6ca1\u6709\u987e\u5ba2\u7aef\u7684\u8054\u63a5\u6073\u6c42\uff0c\u5c31\u963b\u585e\u7b49\u5f85\u76f4\u81f3\u6709\u987e\u5ba2\u7aef\u8054\u63a5\u4e0a\u6765\u3002cliaddr\u662f\u4e00\u4e2a\u4f20\u51fa\u53c2\u6570\uff0caccept()\u8fd4\u56de\u65f6\u4f20\u51fa\u987e\u5ba2\u7aef\u7684\u5730\u5740\u548c\u7aef\u6807\u8bed\u3002addrlen\u53c2\u6570\u662f\u4e00\u4e2a\u4f20\u5165\u4f20\u51fa\u53c2\u6570(value-resultargument)\uff0c\u4f20\u5165\u7684\u662f\u8c03\u7528\u8005\u63d0\u4f9b\u7684\u7f13\u51b2\u533acliaddr\u7684\u539a\u5ea6\u4ee5\u9632\u6b62\u7f13\u51b2\u533a\u6ea2\u51fa\u95ee\u9898\uff0c\u4f20\u51fa\u7684\u662f\u987e\u5ba2\u7aef\u5730\u5740\u7ed3\u6784\u4f53\u7684\u5b9e\u9645\u5bbd\u5ea6(\u6709\u53ef\u80fd\u6ca1\u6709\u5360\u6ee1\u8c03\u7528\u8005\u63d0\u4f9b\u7684\u7f13\u51b2\u533a)\u3002\u5047\u5982\u7ed9cliaddr\u53c2\u6570\u4f20NULLlinux socket<\/strong>\uff0c\u8868\u793a\u4e0d\u5173\u5fc3\u987e\u5ba2\u7aef\u7684\u5730\u5740\u3002<\/p>\n

\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u4e3b\u8981\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n

\n

while (1)\n{\n  cliaddr_len = sizeof(cliaddr);\n  connfd = accept(listenfd,\n      (struct sockaddr *)&cliaddr, &cliaddr_len);\n  n = read(connfd, buf, MAXLINE);\n  ......\n  close(connfd);\n}<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u6574\u4e2a\u662f\u4e00\u4e2awhile\u6b7b\u5faa\u73aflinux socket<\/strong>\uff0c\u6bcf\u6b21\u5faa\u73af\u5904\u7406\u4e00\u4e2a\u987e\u5ba2\u7aef\u8054\u63a5\u3002\u56e0\u4e3acliaddr_len\u662f\u4f20\u5165\u4f20\u51fa\u53c2\u6570\uff0c\u6bcf\u6b21\u8c03\u7528accept()\u4e4b\u524d\u5e94\u5f53\u91cd\u65b0\u8d4b\u5e74\u7387\u3002accept()\u7684\u53c2\u6570listenfd\u662f\u4ee5\u524d\u7684\u7a83\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u800caccept()\u7684\u8fd4\u56de\u503c\u662f\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26connfd\uff0c\u7136\u540e\u4e0e\u987e\u5ba2\u7aef\u4e4b\u95f4\u5c31\u901a\u8fc7\u8fd9\u4e2aconnfd\u901a\u4fe1\uff0c\u6700\u540e\u5173\u6389connfd\u65ad\u6389\u8054\u63a5\uff0c\u800c\u4e0d\u5173\u6389listenfd\uff0c\u518d\u5ea6\u56de\u5230\u5faa\u73af\u5f00\u5934listenfd\u4e00\u76f4\u7528\u4f5caccept\u7684\u53c2\u6570\u3002<\/p>\n

\u987e\u5ba2\u7aef\u7a0b\u5e8f<\/p>\n

\u4e0b\u8fb9\u662f\u987e\u5ba2\u7aef\u7a0b\u5e8f\uff0c\u5b83\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u5f97\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d1\u7ed9\u670d\u52a1\u5668\uff0c\u4e4b\u540e\u63a5\u6536\u670d\u52a1\u5668\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u5e76\u590d\u5370\uff1a<\/p>\n

\n

#include \n#include \n#include \n#include \n#include \n#include \n#define MAXLINE 80\n#define SERV_PORT 8000\nint main(int argc, char *argv[])\n{\n  struct sockaddr_in servaddr;\n  char buf[MAXLINE];\n  int sockfd, n;\n  char *str;\n  \n  if (argc != 2)\n  {\n    fputs(\"usage: .\/client messagen\", stderr);\n    exit(1);\n  }\n  str = argv[1];\n  \n  sockfd = socket(AF_INET, SOCK_STREAM, 0);\n  bzero(&servaddr, sizeof(servaddr));\n  servaddr.sin_family = AF_INET;\n  inet_pton(AF_INET, \"127.0.0.1\", &servaddr.sin_addr);\n  servaddr.sin_port = htons(SERV_PORT);\n  \n  \/\/ \u7531\u4e8e\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u56fa\u5b9a\u7684\u7aef\u53e3\u53f7\uff0c\u56e0\u6b64\u4e0d\u5fc5\u8c03\u7528 bind()\uff0c\u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u53f7\u7531\u5185\u6838\u81ea\u52a8\u5206\u914d\u3002\n  \/\/ \u6ce8\u610f\uff0c\u5ba2\u6237\u7aef\u4e0d\u662f\u4e0d\u5141\u8bb8\u8c03\u7528 bind()\uff0c\u53ea\u662f\u6ca1\u6709\u5fc5\u8981\u8c03\u7528 bind() \u56fa\u5b9a\u4e00\u4e2a\u7aef\u53e3\u53f7\uff0c\n  \/\/ \u670d\u52a1\u5668\u4e5f\u4e0d\u662f\u5fc5\u987b\u8c03\u7528 bind()\uff0c\u4f46\u5982\u679c\u670d\u52a1\u5668\u4e0d\u8c03\u7528 bind()\uff0c\u5185\u6838\u4f1a\u81ea\u52a8\u7ed9\u670d\u52a1\u5668\u5206\u914d\u76d1\u542c\u7aef\u53e3\uff0c\n  \/\/ \u6bcf\u6b21\u542f\u52a8\u670d\u52a1\u5668\u65f6\u7aef\u53e3\u53f7\u90fd\u4e0d\u4e00\u6837\uff0c\u5ba2\u6237\u7aef\u8981\u8fde\u63a5\u670d\u52a1\u5668\u5c31\u4f1a\u9047\u5230\u9ebb\u70e6\u3002\n  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));\n  write(sockfd, str, strlen(str));\n  n = read(sockfd, buf, MAXLINE);\n  printf(\"Response from server:n\");\n  write(STDOUT_FILENO, buf, n);\n  printf(\"n\");\n  close(sockfd);\n  return 0;\n}<\/a><\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u628a\u91cc\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u6587\u4ef6client.c\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u8fb9\u7684\u547d\u4ee4\u7f16\u8bd1\uff1a<\/p>\n

\n

$ gcc client.c -o client<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u4e4b\u540e\u8fd0\u884c\u7f16\u8bd1\u4e0b\u6765\u7684client\u7a0b\u5e8f\uff1a<\/p>\n

\n

$ .\/client hello<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u6b64\u65f6\u670d\u52a1\u5668\u7aef\u4f1a\u6536\u5230\u6073\u6c42\u5e76\u8fd4\u56de\u8f6c\u6362\u4e3a\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\uff1a<\/p>\n

\u800c\u987e\u5ba2\u7aef\u5728\u53d1\u9001\u6073\u6c42\u540e\u4f1a\u6536\u5230\u8f6c\u6362\u8fc7\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n

\u5728\u987e\u5ba2\u7aef\u7684\u4ee3\u7801\u4e2d\u6709\u4e24\u70b9\u987b\u8981\u6ce8\u610f\uff1a<\/p>\n

1.\u56e0\u4e3a\u987e\u5ba2\u7aef\u4e0d\u987b\u8981\u56fa\u5b9a\u7684\u7aef\u6807\u8bed\uff0c\u56e0\u800c\u6bcb\u987b\u8c03\u7528bind()\uff0c\u987e\u5ba2\u7aef\u7684\u7aef\u6807\u8bed\u7531\u5185\u6838\u624b\u52a8\u5206\u914d\u30022.\u987e\u5ba2\u7aef\u987b\u8981\u8c03\u7528connect()\u8054\u63a5\u670d\u52a1\u5668\uff0cconnect\u548cbind\u7684\u53c2\u6570\u65b9\u5f0f\u4e00\u81f4\uff0c\u533a\u522b\u5728\u4e8ebind\u7684\u53c2\u6570\u662f\u81ea\u5df1\u7684\u5730\u5740\uff0c\u800cconnect\u7684\u53c2\u6570\u662f\u5bf9\u65b9\u7684\u5730\u5740\u3002<\/p>\n

\u81f3\u6b64\u6211\u4eec\u65e9\u5df2\u4f7f\u7528socket\u6280\u672f\u5b8c\u6210\u4e86\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u987e\u5ba2\u7aef\u670d\u52a1\u5668\u7a0b\u5e8f\uff0c\u5c3d\u7ba1\u79bb\u5b9e\u9645\u5e94\u7528\u8fd8\u5341\u5206\u9065\u8fdc\uff0c\u4f46\u5c31\u5b66\u4e60\u800c\u8a00\u65e9\u5df2\u8db3\u591f\u4e86\u3002<\/p>\n

\u63d0\u9ad8\u670d\u52a1\u5668\u7aef\u7684\u54cd\u5e94\u80fd\u529b<\/p>\n

\u5176\u5b9e\u6211\u4eec\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u53ef\u4ee5\u54cd\u5e94\u987e\u5ba2\u7aef\u7684\u6073\u6c42\uff0c\u5e76\u4e14\u8fd9\u6837\u7684\u6548\u7387\u592a\u4f4e\u4e86\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u670d\u52a1\u5668\u7a0b\u5e8f\u987b\u8981\u5c31\u80fd\u540c\u65f6\u5904\u7406\u591a\u4e2a\u987e\u5ba2\u7aef\u7684\u6073\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7fork\u7cfb\u7edf<\/a>\u8c03\u7528\u521b\u5efa\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u6bcf\u4f4d\u6073\u6c42\uff0c\u4e0b\u8fb9\u662f\u5927\u4f53\u7684\u5b9e\u73b0\u601d\u8def\uff1a<\/p>\n

\n

listenfd = socket(...);\nbind(listenfd, ...);\nlisten(listenfd, ...);\nwhile (1)\n{\n  connfd = accept(listenfd, ...);\n  n = fork();\n  if (n == -1)\n  {\n    perror(\"call to fork\");\n    exit(1);\n  }\n  else if (n == 0)\n  {\n    \/\/ \u5728\u5b50\u8fdb\u7a0b\u4e2d\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\n    close(listenfd);\n    while (1)\n    {\n      read(connfd, ...);\n      ...\n      write(connfd, ...);\n    }\n    close(connfd);\n    exit(0);\n  }\n  else\n  {\n    close(connfd);\n  }  \n}<\/code><\/pre>\n<\/p>\n

\u590d\u5236<\/p>\n

\u6b64\u65f6\u7236\u8fdb\u7a0b\u7684\u4efb\u52a1\u5c31\u662f\u4e0d\u65ad\u7684\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u800c\u7531\u5b50\u8fdb\u7a0b\u53bb\u54cd\u5e94\u987e\u5ba2\u7aef\u7684\u5177\u4f53\u6073\u6c42\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u9ad8\u670d\u52a1\u5668\u7aef\u7684\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u5efa\u57fa\u4e8eTCP\u5408\u540c\u7684\u7f51\u8def\u7a0b\u5e8f\u4ecb\u7ecd\u4e86linuxsocket\u7f16\u7a0b\u4e2d\u7684\u57fa\u672c\u6982\u5ff5\u3002\u901a\u8fc7\u5b83\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230socket\u7a0b\u5e8f\u5de5\u4f5c\u7684\u57fa\u672c\u539f\u7406\uff0c\u4ee5\u53ca\u4e00\u4e9b\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u7684\u601d\u8def\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u534f\u8bae\u7684\u901a\u8baf\u6d41\u7a0b(\u6b64\u56fe\u6765\u81ea\u4e92\u8054\u7f51)\uff1a\u4e0b\u9762\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f\uff0c\u5b83\u4ece\u5ba2\u6237\u7aef\u8bfb\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u5e76\u56de\u9001\u7ed9\u5ba2\u6237\u7aef\uff1a\u51fd\u6570\u7ed1\u5b9a\u4e00\u4e2a\u56fa\u5b9a\u7684\u7f51\u7edc\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002bind()\uff0c\u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u53f7\u7531\u5185\u6838\u81ea\u52a8\u5206\u914d\u3002\u7cfb\u7edf\u8c03\u7528\u521b\u5efa\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u6bcf\u4e2a\u8bf7\u6c42\uff0c\u4e0b\u9762\u662f\u5927\u4f53\u7684\u5b9e\u73b0\u601d\u8def\uff1a\u7f16\u7a0b\u4e2d\u7684\u57fa\u672c\u6982\u5ff5\u3002<\/p>\n","protected":false},"author":1,"featured_media":290430,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=290429"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/290430"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=290429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=290429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=290429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}