{"id":90850,"date":"2026-08-06T07:33:02","date_gmt":"2026-08-05T23:33:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/90850.html"},"modified":"2026-08-06T07:33:02","modified_gmt":"2026-08-05T23:33:02","slug":"day-22-23-tcp-%e8%81%8a%e5%a4%a9%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%9a%e4%bb%8e%e9%9b%b6%e6%89%8b%e5%86%99%e7%bd%91%e7%bb%9c%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/90850.html","title":{"rendered":"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1"},"content":{"rendered":"<p>\u7f51\u7edc\u7f16\u7a0b\u6ca1\u90a3\u4e48\u795e\u79d8&#xff0c;\u5c31\u50cf\u6253\u7535\u8bdd\u2014\u2014\u4e70\u624b\u673a\u3001\u63d2\u5361\u3001\u62e8\u53f7\u3001\u901a\u8bdd\u3001\u6302\u65ad&#xff0c;\u4e94\u4e2a\u52a8\u4f5c\u800c\u5df2\u3002\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u6362\u6210 C \u8bed\u8a00\u7684\u51e0\u4e2a\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<hr \/>\n<h3 id=\"\u5f00\u7bc7\u5148\u5efa\u7acb\u4e00\u4e2a\u76f4\u89c9\">\u5f00\u7bc7&#xff1a;\u5148\u5efa\u7acb\u4e00\u4e2a\u76f4\u89c9<\/h3>\n<p>\u5728\u5b66\u4e60\u5177\u4f53\u4ee3\u7801\u4e4b\u524d&#xff0c;\u5148\u628a\u81ea\u5df1\u60f3\u8c61\u6210\u4e00\u5bb6\u5916\u5356\u5e97\u8001\u677f&#xff0c;\u4f60\u5c31\u5bb9\u6613\u7406\u89e3 TCP \u901a\u4fe1\u5230\u5e95\u5728\u5e72\u561b\u4e86&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u5916\u5356\u5e97\u573a\u666f<\/td>\n<td style=\"background-color:#dddddd\">\u5bf9\u5e94\u7684\u7f51\u7edc\u7f16\u7a0b\u6982\u5ff5<\/td>\n<td style=\"background-color:#dddddd\">\u5927\u767d\u8bdd<\/td>\n<\/tr>\n<tr>\n<td>\u5b89\u88c5\u4e00\u90e8\u5916\u5356\u4e13\u7528\u7535\u8bdd<\/td>\n<td>socket()<\/td>\n<td>\u521b\u5efa\u901a\u4fe1\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td>\u5370\u5728\u540d\u7247\u4e0a\u7684\u7535\u8bdd\u53f7\u7801<\/td>\n<td>bind()\u00a0\u7ed1\u5b9a IP &#043; \u7aef\u53e3<\/td>\n<td>\u5bf9\u5916\u516c\u5e03\u4f60\u7684\u8054\u7cfb\u65b9\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>\u5750\u5728\u5e97\u91cc\u7b49\u7535\u8bdd\u54cd<\/td>\n<td>listen()<\/td>\n<td>\u8fdb\u5165&#034;\u7b49\u5f85\u63a5\u5355&#034;\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u4eba\u6253\u7535\u8bdd\u8fdb\u6765&#xff0c;\u4f60\u62ff\u8d77\u8bdd\u7b52<\/td>\n<td>accept()<\/td>\n<td>\u63a5\u53d7\u8fde\u63a5&#xff0c;\u5efa\u7acb\u901a\u8bdd<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u4eba\u62e8\u53f7\u6253\u7ed9\u5e97\u91cc<\/td>\n<td>connect()<\/td>\n<td>\u5ba2\u6237\u7aef\u4e3b\u52a8\u53d1\u8d77\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u53cc\u65b9\u5bf9\u8bdd&#xff1a;\u8bf4 \/ \u542c<\/td>\n<td>write()\u00a0\u53d1\u9001 \/\u00a0read()\u00a0\u63a5\u6536<\/td>\n<td>\u6570\u636e\u6536\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u6302\u65ad\u7535\u8bdd<\/td>\n<td>close()<\/td>\n<td>\u5173\u95ed\u8fde\u63a5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e97\u8001\u677f&#xff08;\u670d\u52a1\u5668&#xff09;&#xff1a;\u88ab\u52a8\u7b49\u7535\u8bdd&#xff0c;\u6c38\u8fdc\u5f00\u7740\u5e97\u95e8\u3002<br \/>\n\u5ba2\u4eba&#xff08;\u5ba2\u6237\u7aef&#xff09;&#xff1a;\u4e3b\u52a8\u6253\u7535\u8bdd&#xff0c;\u5403\u5b8c\u5c31\u6302\u3002<\/p>\n<p>\u641e\u61c2\u4e86&#034;\u8c01\u4e3b\u52a8\u3001\u8c01\u88ab\u52a8&#034;\u8fd9\u4e2a\u6838\u5fc3\u95ee\u9898&#xff0c;\u540e\u9762\u7684\u4ee3\u7801\u90fd\u662f\u56f4\u7ed5\u8fd9 7 \u4e2a\u52a8\u4f5c\u5c55\u5f00\u7684\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e00\u90e8\u5206\u4e24\u4e2a\u5fc5\u987b\u63d0\u524d\u641e\u61c2\u7684\u53cd\u4eba\u7c7b\u7ec6\u8282\">\u7b2c\u4e00\u90e8\u5206&#xff1a;\u4e24\u4e2a\u5fc5\u987b\u63d0\u524d\u641e\u61c2\u7684&#034;\u53cd\u4eba\u7c7b&#034;\u7ec6\u8282<\/h3>\n<p>\u5728\u5199\u4ee3\u7801\u4e4b\u524d&#xff0c;\u6709\u4e24\u4e2a\u6982\u5ff5\u5982\u679c\u4e0d\u8bb2\u6e05\u695a&#xff0c;\u4f60\u4f1a\u5728\u00a0bind()\u00a0\u90a3\u91cc\u5361\u534a\u5929\u3002<\/p>\n<h4 id=\"\u7b2c1\u6b65\u8ba4\u8bc6-struct-sockaddr_in-\u5b83\u5c31\u4e00\u5f20\u5feb\u9012\u5355\">\u7b2c1\u6b65&#xff1a;\u8ba4\u8bc6\u00a0struct sockaddr_in\u00a0\u2014\u2014 \u5b83\u5c31\u4e00\u5f20&#034;\u5feb\u9012\u5355&#034;<\/h4>\n<p>\u4f60\u8981\u5bc4\u5feb\u9012&#xff0c;\u5fc5\u987b\u586b\u4e00\u5f20\u5355\u5b50&#xff1a;\u6536\u4ef6\u5730\u5740\u3001\u6536\u4ef6\u4eba\u3001\u7535\u8bdd\u3002\u7f51\u7edc\u901a\u4fe1\u4e5f\u4e00\u6837\u2014\u2014\u4f60\u5f97\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#034;\u6211\u8981\u901a\u8fc7\u54ea\u4e2a IP\u3001\u54ea\u4e2a\u7aef\u53e3\u901a\u4fe1&#034;\u3002\u8fd9\u4e2a&#034;\u5feb\u9012\u5355&#034;\u5c31\u662f\u00a0sockaddr_in\u00a0\u7ed3\u6784\u4f53\u3002<\/p>\n<p>#include &lt;netinet\/in.h&gt;<\/p>\n<p>struct sockaddr_in {<br \/>\n    sa_family_t    sin_family;   \/\/ 1. \u5730\u5740\u7c7b\u578b&#xff1a;\u586b AF_INET&#xff0c;\u8868\u793a IPv4<br \/>\n    in_port_t      sin_port;     \/\/ 2. \u7aef\u53e3\u53f7&#xff1a;\u5fc5\u987b\u7528 htons() \u8f6c\u6362&#xff08;\u89c1\u4e0b\u6587&#xff09;<br \/>\n    struct in_addr sin_addr;     \/\/ 3. IP \u5730\u5740&#xff1a;\u91cc\u9762\u53ea\u6709\u4e00\u4e2a\u5b57\u6bb5 s_addr<br \/>\n    char           sin_zero[8];  \/\/ 4. \u586b\u5145\u5360\u4f4d&#xff0c;\u5168\u586b 0 \u5c31\u884c<br \/>\n};<\/p>\n<p>\u4e00\u5f20\u8868\u770b\u61c2\u6bcf\u4e2a\u5b57\u6bb5\u600e\u4e48\u586b&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u5b57\u6bb5<\/td>\n<td style=\"background-color:#dddddd\">\u8be5\u586b\u4ec0\u4e48<\/td>\n<td style=\"background-color:#dddddd\">\u4e3a\u4ec0\u4e48\u8fd9\u6837\u586b<\/td>\n<\/tr>\n<tr>\n<td>sin_family<\/td>\n<td>AF_INET<\/td>\n<td>\u8868\u793a\u7528 IPv4 \u5730\u5740&#xff08;\u5982 192.168.1.1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>sin_port<\/td>\n<td>htons(8080)<\/td>\n<td>\u7aef\u53e3\u53f7 8080&#xff0c;\u4f46\u5fc5\u987b\u8f6c\u6210&#034;\u7f51\u7edc\u5b57\u8282\u5e8f&#034;<\/td>\n<\/tr>\n<tr>\n<td>sin_addr.s_addr<\/td>\n<td>INADDR_ANY\u00a0\u6216\u00a0inet_addr(&#034;127.0.0.1&#034;)<\/td>\n<td>INADDR_ANY\u00a0&#061; \u76d1\u542c\u672c\u673a\u6240\u6709 IP&#xff1b;127.0.0.1\u00a0&#061; \u53ea\u5728\u672c\u673a\u6d4b\u8bd5<\/td>\n<\/tr>\n<tr>\n<td>sin_zero<\/td>\n<td>\u4e0d\u7528\u7ba1&#xff0c;memset\u00a0\u7f6e 0 \u5373\u53ef<\/td>\n<td>\u5386\u53f2\u9057\u7559\u7684\u586b\u5145\u4f4d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"\u7b2c2\u6b65\u5b57\u8282\u5e8f-\u5927\u7aef\u5c0f\u7aef\u5230\u5e95\u662f\u4ec0\u4e48\u9b3c\">\u7b2c2\u6b65&#xff1a;\u5b57\u8282\u5e8f \u2014\u2014 &#034;\u5927\u7aef\u5c0f\u7aef&#034;\u5230\u5e95\u662f\u4ec0\u4e48\u9b3c<\/h4>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u6709\u7684\u56fd\u5bb6\u5199\u65e5\u671f\u662f&#034;\u5e74-\u6708-\u65e5&#034;&#xff0c;\u6709\u7684\u662f&#034;\u6708-\u65e5-\u5e74&#034;\u3002\u540c\u4e00\u4e2a\u65e5\u671f&#xff0c;\u5199\u6cd5\u4e0d\u540c\u5c31\u4f1a\u4e71\u5957\u3002\u8ba1\u7b97\u673a\u5185\u90e8\u5b58\u6570\u5b57\u4e5f\u4e00\u6837\u2014\u2014\u4e0d\u540c CPU \u67b6\u6784\u5b58\u591a\u5b57\u8282\u6570\u5b57\u7684\u987a\u5e8f\u4e0d\u540c&#xff0c;\u5206\u4e3a&#034;\u5927\u7aef&#034;\u548c&#034;\u5c0f\u7aef&#034;\u4e24\u79cd\u6d3e\u7cfb\u3002<\/p>\n<p>\u7f51\u7edc\u901a\u4fe1\u4e3a\u4e86\u4fdd\u8bc1\u5168\u4e16\u754c\u673a\u5668\u90fd\u80fd\u4e92\u76f8\u542c\u61c2&#xff0c;\u5f3a\u5236\u89c4\u5b9a&#xff1a;\u7f51\u7edc\u4f20\u8f93\u7edf\u4e00\u7528\u5927\u7aef\u5e8f&#xff08;Network Byte Order&#xff09;\u3002\u6240\u4ee5\u4f60\u7684\u7a0b\u5e8f\u5728\u628a\u7aef\u53e3\u53f7\u3001IP \u5730\u5740\u4f20\u7ed9\u7f51\u7edc\u51fd\u6570\u4e4b\u524d&#xff0c;\u5fc5\u987b\u8f6c\u6362\u6210\u5927\u7aef\u3002<\/p>\n<p>\u8bb0\u5fc6\u53e3\u8bc0&#xff1a;\u51e1\u662f\u586b\u8fdb\u00a0sockaddr_in\u00a0\u7aef\u53e3\u548c IP \u5b57\u6bb5\u7684\u503c&#xff0c;\u7edf\u7edf\u7528\u00a0htons()\u00a0\/\u00a0htonl()\u00a0\u5305\u4e00\u5c42\u3002<\/p>\n<table>\n<tbody>\n<tr>\u51fd\u6570\u8bb0\u5fc6\u65b9\u6cd5\u7528\u9014\u793a\u4f8b<\/tr>\n<tr>\n<td>htons()<\/td>\n<td>Host\u00a0to\u00a0Network\u00a0Short<\/td>\n<td>16 \u4f4d\u7aef\u53e3\u53f7\u8f6c\u6362<\/td>\n<td>htons(8080)<\/td>\n<\/tr>\n<tr>\n<td>htonl()<\/td>\n<td>Host\u00a0to\u00a0Network\u00a0Long<\/td>\n<td>32 \u4f4d IP \u8f6c\u6362<\/td>\n<td>htonl(INADDR_ANY)<\/td>\n<\/tr>\n<tr>\n<td>ntohs()<\/td>\n<td>Network\u00a0to\u00a0Host\u00a0Short<\/td>\n<td>\u53cd\u5411\u8f6c\u6362&#xff08;\u8bfb\u5bf9\u65b9\u7aef\u53e3\u65f6\u7528&#xff09;<\/td>\n<td>ntohs(client_addr.sin_port)<\/td>\n<\/tr>\n<tr>\n<td>ntohl()<\/td>\n<td>Network\u00a0to\u00a0Host\u00a0Long<\/td>\n<td>\u53cd\u5411\u8f6c\u6362&#xff08;\u8bfb\u5bf9\u65b9 IP \u65f6\u7528&#xff09;<\/td>\n<td>\u7565<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u65b0\u624b\u6700\u5bb9\u6613\u8e29\u7684\u5751&#xff1a;\u76f4\u63a5\u628a\u00a08080\u00a0\u8d4b\u503c\u7ed9\u00a0sin_port&#xff0c;\u4e0d\u8c03\u7528\u00a0htons()\u3002\u7f16\u8bd1\u901a\u8fc7\u3001\u8fd0\u884c\u4e0d\u62a5\u9519&#xff0c;\u4f46\u6b7b\u6d3b\u8fde\u4e0d\u4e0a\u2014\u2014\u56e0\u4e3a\u4f60\u7684 8080 \u88ab\u5bf9\u65b9\u673a\u5668\u5f53\u6210\u4e00\u4e2a\u5929\u6587\u6570\u5b57\u7684\u7aef\u53e3\u53f7\u4e86\u3002<\/p>\n<h4 id=\"\u7b2c3\u6b65sockaddr-\u548c-sockaddr_in-\u7684\u5957\u5a03\u5173\u7cfb\">\u7b2c3\u6b65&#xff1a;sockaddr\u00a0\u548c\u00a0sockaddr_in\u00a0\u7684&#034;\u5957\u5a03&#034;\u5173\u7cfb<\/h4>\n<p>bind()\u3001connect()\u3001accept()\u00a0\u8fd9\u4e9b\u51fd\u6570\u8981\u6c42\u7684\u53c2\u6570\u7c7b\u578b\u662f\u00a0struct sockaddr *&#xff08;\u901a\u7528\u5730\u5740\u7ed3\u6784&#xff09;\u3002\u4f46\u6211\u4eec\u5b9e\u9645\u586b\u7684\u662f\u00a0struct sockaddr_in&#xff08;IPv4 \u4e13\u7528&#xff09;\u3002\u600e\u4e48\u529e&#xff1f;\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5373\u53ef&#xff1a;<\/p>\n<p>\/\/ \u6211\u4eec\u5b9e\u9645\u7528\u7684\u662f sockaddr_in<br \/>\nstruct sockaddr_in server_addr;<br \/>\n\/\/ \u4f20\u7ed9 bind \u65f6\u5f3a\u884c\u8f6c\u6210 sockaddr *<br \/>\nbind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<\/p>\n<p>\u4f60\u4f1a\u5728\u51e0\u4e4e\u6240\u6709\u7f51\u7edc\u4ee3\u7801\u91cc\u770b\u5230\u8fd9\u79cd\u00a0(struct sockaddr *)\u00a0\u7684\u5199\u6cd5\u3002\u8bb0\u4f4f\u53e3\u8bc0&#xff1a;\u5199\u7684\u65f6\u5019\u7528\u00a0_in&#xff0c;\u4f20\u7684\u65f6\u5019\u5f3a\u8f6c\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e8c\u90e8\u5206\u670d\u52a1\u5668\u7aef-\u624b\u628a\u624b\u5199\u51fa-echo-\u670d\u52a1\u5668\">\u7b2c\u4e8c\u90e8\u5206&#xff1a;\u670d\u52a1\u5668\u7aef \u2014\u2014 \u624b\u628a\u624b\u5199\u51fa Echo \u670d\u52a1\u5668<\/h3>\n<p>\u76ee\u6807&#xff1a;\u5199\u4e00\u4e2a\u670d\u52a1\u5668\u7a0b\u5e8f&#xff0c;\u5ba2\u6237\u7aef\u8fde\u4e0a\u6765\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31\u539f\u6837\u56de\u4ec0\u4e48\u3002<\/p>\n<h4 id=\"\u7b2c1\u6b65\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u4ee3\u7801\">\u7b2c1\u6b65&#xff1a;\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u4ee3\u7801<\/h4>\n<p>\u5728\u7ec8\u7aef\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u6e90\u6587\u4ef6&#xff1a;<\/p>\n<p># \u8fdb\u5165\u4f60\u7684\u5b66\u4e60\u76ee\u5f55&#xff08;\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539&#xff09;<br \/>\ncd ~\/linux-learning<br \/>\n# \u521b\u5efa\u6e90\u6587\u4ef6<br \/>\nnano tcp_server.c<\/p>\n<p>\u5c06\u4ee5\u4e0b\u5b8c\u6574\u4ee3\u7801\u7c98\u8d34\u8fdb\u53bb&#xff0c;\u7136\u540e\u6309\u00a0Ctrl&#043;O\u00a0\u4fdd\u5b58&#xff0c;Ctrl&#043;X\u00a0\u9000\u51fa&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;       \/\/ printf, perror<br \/>\n#include &lt;stdlib.h&gt;      \/\/ exit<br \/>\n#include &lt;string.h&gt;      \/\/ memset, strlen<br \/>\n#include &lt;unistd.h&gt;      \/\/ read, write, close<br \/>\n#include &lt;sys\/socket.h&gt;  \/\/ socket, bind, listen, accept<br \/>\n#include &lt;netinet\/in.h&gt;  \/\/ sockaddr_in, INADDR_ANY, htons<br \/>\n#include &lt;arpa\/inet.h&gt;   \/\/ inet_ntoa<\/p>\n<p>#define PORT 8080             \/\/ \u670d\u52a1\u5668\u7aef\u53e3\u53f7&#xff0c;\u53ef\u4ee5\u6539\u6210\u522b\u7684<br \/>\n#define BUFFER_SIZE 1024      \/\/ \u6536\u53d1\u7f13\u51b2\u533a\u5927\u5c0f<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_fd; \/\/ server_fd&#061;\u76d1\u542c\u5957\u63a5\u5b57, client_fd&#061;\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u7684\u5957\u63a5\u5b57<br \/>\n    struct sockaddr_in server_addr, client_addr; \/\/ \u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5730\u5740\u4fe1\u606f<br \/>\n    socklen_t client_len &#061; sizeof(client_addr);  \/\/ accept \u9700\u8981\u7684\u957f\u5ea6\u53c2\u6570<br \/>\n    char buffer[BUFFER_SIZE]; \/\/ \u6570\u636e\u7f13\u51b2\u533a<br \/>\n    int n;                    \/\/ read \u7684\u8fd4\u56de\u503c&#xff08;\u8bfb\u5230\u591a\u5c11\u5b57\u8282&#xff09;<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c1\u6b65&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#xff08;\u76f8\u5f53\u4e8e\u4e70\u4e00\u90e8\u7535\u8bdd&#xff09; &#061;&#061;&#061;<br \/>\n    \/\/ AF_INET&#061;IPv4, SOCK_STREAM&#061;TCP\u534f\u8bae, 0&#061;\u81ea\u52a8\u9009\u62e9<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (server_fd &lt; 0) {      \/\/ \u8fd4\u56de -1 \u8868\u793a\u521b\u5efa\u5931\u8d25<br \/>\n        perror(&#034;socket&#034;);     \/\/ \u6253\u5370\u7cfb\u7edf\u9519\u8bef\u4fe1\u606f<br \/>\n        exit(1);              \/\/ \u76f4\u63a5\u9000\u51fa<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c2\u6b65&#xff1a;\u586b\u5199\u670d\u52a1\u5668\u5730\u5740\u4fe1\u606f&#xff08;\u76f8\u5f53\u4e8e\u5370\u540d\u7247&#xff09; &#061;&#061;&#061;<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr)); \/\/ \u5148\u628a\u6574\u5757\u5185\u5b58\u6e05\u96f6<br \/>\n    server_addr.sin_family &#061; AF_INET;             \/\/ IPv4<br \/>\n    server_addr.sin_port &#061; htons(PORT);           \/\/ \u7aef\u53e3\u53f7&#xff0c;\u5fc5\u987b htons \u8f6c\u6362<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;     \/\/ \u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c3\u6b65&#xff1a;\u7ed1\u5b9a\u5730\u5740&#xff08;\u76f8\u5f53\u4e8e\u516c\u5e03\u7535\u8bdd\u53f7\u7801&#xff09; &#061;&#061;&#061;<br \/>\n    if (bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) {<br \/>\n        perror(&#034;bind&#034;);       \/\/ \u6700\u5e38\u89c1\u5931\u8d25\u539f\u56e0&#xff1a;\u7aef\u53e3\u88ab\u5360\u7528<br \/>\n        close(server_fd);     \/\/ \u5148\u5173\u6389\u5957\u63a5\u5b57\u518d\u9000\u51fa<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c4\u6b65&#xff1a;\u5f00\u59cb\u76d1\u542c&#xff08;\u76f8\u5f53\u4e8e\u5750\u5728\u5e97\u91cc\u7b49\u7535\u8bdd\u54cd&#xff09; &#061;&#061;&#061;<br \/>\n    \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570 5 \u8868\u793a\u6700\u591a\u5141\u8bb8 5 \u4e2a\u8fde\u63a5\u5728\u6392\u961f\u7b49\u5f85<br \/>\n    if (listen(server_fd, 5) &lt; 0) {<br \/>\n        perror(&#034;listen&#034;);<br \/>\n        close(server_fd);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    printf(&#034;\u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u6b63\u5728\u76d1\u542c\u7aef\u53e3 %d &#8230;\\\\n&#034;, PORT);<br \/>\n    printf(&#034;\u63d0\u793a&#xff1a;\u6253\u5f00\u53e6\u4e00\u4e2a\u7ec8\u7aef\u8fd0\u884c .\/tcp_client \u6765\u8fde\u63a5\\\\n&#034;);<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c5\u6b65&#xff1a;\u5faa\u73af\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5 &#061;&#061;&#061;<br \/>\n    while (1) {<br \/>\n        \/\/ accept \u4f1a\u963b\u585e\u5728\u8fd9\u91cc&#xff0c;\u76f4\u5230\u6709\u5ba2\u6237\u7aef\u8fde\u4e0a\u6765<br \/>\n        client_fd &#061; accept(server_fd, (struct sockaddr *)&amp;client_addr, &amp;client_len);<br \/>\n        if (client_fd &lt; 0) {<br \/>\n            perror(&#034;accept&#034;);<br \/>\n            continue;         \/\/ \u5931\u8d25\u4e86\u5c31\u7ee7\u7eed\u7b49\u4e0b\u4e00\u4e2a<br \/>\n        }<\/p>\n<p>        \/\/ \u6253\u5370\u5ba2\u6237\u7aef\u7684 IP \u548c\u7aef\u53e3<br \/>\n        printf(&#034;\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u8fdb\u6765\u4e86: %s:%d\\\\n&#034;,<br \/>\n               inet_ntoa(client_addr.sin_addr),    \/\/ \u628a\u4e8c\u8fdb\u5236 IP \u8f6c\u6210\u53ef\u8bfb\u5b57\u7b26\u4e32<br \/>\n               ntohs(client_addr.sin_port));       \/\/ \u628a\u7f51\u7edc\u5b57\u8282\u5e8f\u7aef\u53e3\u8f6c\u56de\u6765<\/p>\n<p>        \/\/ &#061;&#061;&#061; \u7b2c6\u6b65&#xff1a;\u6536\u53d1\u6570\u636e&#xff08;Echo \u56de\u663e&#xff09; &#061;&#061;&#061;<br \/>\n        \/\/ \u5faa\u73af\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e<br \/>\n        while ((n &#061; read(client_fd, buffer, sizeof(buffer) &#8211; 1)) &gt; 0) {<br \/>\n            buffer[n] &#061; &#039;\\\\0&#039;;   \/\/ \u624b\u52a8\u52a0\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26&#xff0c;\u9632\u6b62 printf \u8d8a\u754c<br \/>\n            printf(&#034;\u6536\u5230: %s&#034;, buffer);<br \/>\n            \/\/ \u539f\u6837\u5199\u56de\u7ed9\u5ba2\u6237\u7aef<br \/>\n            write(client_fd, buffer, n);<br \/>\n        }<\/p>\n<p>        \/\/ \u5faa\u73af\u7ed3\u675f&#xff1a;\u8981\u4e48 n&#061;&#061;0&#xff08;\u5ba2\u6237\u7aef\u4e3b\u52a8\u65ad\u5f00&#xff09;&#xff0c;\u8981\u4e48 n&lt;0&#xff08;\u51fa\u9519&#xff09;<br \/>\n        if (n &lt; 0) {<br \/>\n            perror(&#034;read&#034;);<br \/>\n        } else {<br \/>\n            printf(&#034;\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\\\\n&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ &#061;&#061;&#061; \u7b2c7\u6b65&#xff1a;\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 &#061;&#061;&#061;<br \/>\n        close(client_fd);<br \/>\n    }<\/p>\n<p>    \/\/ \u7406\u8bba\u4e0a\u4e0d\u4f1a\u6267\u884c\u5230\u8fd9\u91cc&#xff08;\u4e0a\u9762\u662f\u6b7b\u5faa\u73af&#xff09;&#xff0c;\u4f46\u5199\u4e0a\u662f\u597d\u4e60\u60ef<br \/>\n    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4 id=\"\u7b2c2\u6b65\u9010\u884c\u7406\u89e3\u6bcf\u4e2a\u51fd\u6570\u5728\u5e72\u4ec0\u4e48\">\u7b2c2\u6b65&#xff1a;\u9010\u884c\u7406\u89e3\u6bcf\u4e2a\u51fd\u6570\u5728\u5e72\u4ec0\u4e48<\/h4>\n<p>\u4e0b\u9762\u8fd9\u5f20\u8868\u662f\u6574\u4e2a\u670d\u52a1\u5668\u7aef\u7684&#034;\u9aa8\u67b6&#034;\u3002\u628a\u5b83\u6253\u5370\u51fa\u6765\u8d34\u5728\u663e\u793a\u5668\u65c1\u8fb9&#xff0c;\u5199\u4ee3\u7801\u65f6\u5bf9\u7167\u7740\u770b&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd;width:92px\">\u64cd\u4f5c\u6b65\u9aa4<\/td>\n<td style=\"background-color:#dddddd;width:175px\">\u8c03\u7528\u7684\u51fd\u6570<\/td>\n<td style=\"background-color:#dddddd\">\u5b83\u5728\u5e72\u4ec0\u4e48&#xff08;\u5927\u767d\u8bdd&#xff09;<\/td>\n<td style=\"background-color:#dddddd\">\u5173\u952e\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c1\u6b65<\/td>\n<td style=\"width:175px\">socket(AF_INET, SOCK_STREAM, 0)<\/td>\n<td>\u4e70\u4e00\u90e8&#034;\u7535\u8bdd&#034;<\/td>\n<td>SOCK_STREAM\u00a0&#061; TCP&#xff0c;SOCK_DGRAM\u00a0&#061; UDP<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c2\u6b65<\/td>\n<td style=\"width:175px\">\u586b\u5145\u00a0sockaddr_in<\/td>\n<td>\u586b\u5feb\u9012\u5355&#xff1a;IP &#043; \u7aef\u53e3<\/td>\n<td>htons(PORT)\u00a0\u5fc5\u987b\u52a0<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c3\u6b65<\/td>\n<td style=\"width:175px\">bind()<\/td>\n<td>\u628a\u7535\u8bdd\u53f7\u7801\u516c\u5e03\u51fa\u53bb<\/td>\n<td>\u7aef\u53e3\u88ab\u5360\u7528\u4f1a\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c4\u6b65<\/td>\n<td style=\"width:175px\">listen(server_fd, 5)<\/td>\n<td>\u5750\u4e0b\u6765\u7b49\u7535\u8bdd<\/td>\n<td>5\u00a0&#061; \u6700\u591a\u540c\u65f6\u6392\u961f 5 \u4e2a\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c5\u6b65<\/td>\n<td style=\"width:175px\">accept()<\/td>\n<td>\u63a5\u8d77\u7535\u8bdd&#xff0c;\u62ff\u5230\u4e00\u4e2a&#034;\u901a\u8bdd\u7ebf\u8def&#034;<\/td>\n<td>\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 fd&#xff0c;\u4e13\u95e8\u8ddf\u8fd9\u4e2a\u5ba2\u6237\u7aef\u804a<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c6\u6b65<\/td>\n<td style=\"width:175px\">read()\u00a0\/\u00a0write()<\/td>\n<td>\u901a\u8bdd&#xff1a;\u542c\u5bf9\u65b9\u8bf4\u8bdd \/ \u56de\u8bdd<\/td>\n<td>\u548c\u6587\u4ef6\u8bfb\u5199\u4e00\u6a21\u4e00\u6837<\/td>\n<\/tr>\n<tr>\n<td style=\"width:92px\">\u7b2c7\u6b65<\/td>\n<td style=\"width:175px\">close(client_fd)<\/td>\n<td>\u6302\u65ad\u8fd9\u901a\u7535\u8bdd<\/td>\n<td>\u5148\u5173\u5ba2\u6237\u7aef fd&#xff0c;\u6700\u540e\u5173\u670d\u52a1\u5668 fd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u65b0\u624b\u6700\u5bb9\u6613\u641e\u6df7\u7684\u5730\u65b9&#xff1a;server_fd\u00a0\u548c\u00a0client_fd\u00a0\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u4e1c\u897f\u3002server_fd\u00a0\u53ea\u8d1f\u8d23&#034;\u63a5\u7535\u8bdd&#034;&#xff08;\u76d1\u542c&#xff09;&#xff0c;\u4ece\u4e0d\u4f20\u8f93\u6570\u636e\u3002client_fd\u00a0\u662f\u00a0accept\u00a0\u8fd4\u56de\u7684&#xff0c;\u4e13\u95e8\u7528\u6765\u548c\u67d0\u4e00\u4e2a\u5ba2\u6237\u7aef\u6536\u53d1\u6570\u636e\u3002\u628a\u5b83\u4eec\u60f3\u8c61\u6210&#034;\u603b\u673a&#034;\u548c&#034;\u5206\u673a&#034;\u5c31\u4e0d\u4f1a\u641e\u6df7\u4e86\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e09\u90e8\u5206\u5ba2\u6237\u7aef-\u624b\u628a\u624b\u5199\u51fa-echo-\u5ba2\u6237\u7aef\">\u7b2c\u4e09\u90e8\u5206&#xff1a;\u5ba2\u6237\u7aef \u2014\u2014 \u624b\u628a\u624b\u5199\u51fa Echo \u5ba2\u6237\u7aef<\/h3>\n<h4 id=\"\u7b2c1\u6b65\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u4ee3\u7801\">\u7b2c1\u6b65&#xff1a;\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u4ee3\u7801<\/h4>\n<p>\u521b\u5efa\u5ba2\u6237\u7aef\u6e90\u6587\u4ef6&#xff1a;<\/p>\n<p># \u521b\u5efa\u5ba2\u6237\u7aef\u6e90\u6587\u4ef6<br \/>\nnano tcp_client.c<\/p>\n<p>\u5c06\u4ee5\u4e0b\u4ee3\u7801\u7c98\u8d34\u8fdb\u53bb&#xff0c;\u4fdd\u5b58\u9000\u51fa&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;       \/\/ printf, fgets, perror<br \/>\n#include &lt;stdlib.h&gt;      \/\/ exit<br \/>\n#include &lt;string.h&gt;      \/\/ memset, strlen, strncmp<br \/>\n#include &lt;unistd.h&gt;      \/\/ read, write, close<br \/>\n#include &lt;sys\/socket.h&gt;  \/\/ socket, connect<br \/>\n#include &lt;netinet\/in.h&gt;  \/\/ sockaddr_in, htons<br \/>\n#include &lt;arpa\/inet.h&gt;   \/\/ inet_pton<\/p>\n<p>#define PORT 8080             \/\/ \u8fde\u63a5\u54ea\u4e2a\u7aef\u53e3&#xff08;\u5fc5\u987b\u548c\u670d\u52a1\u5668\u4e00\u81f4&#xff09;<br \/>\n#define BUFFER_SIZE 1024      \/\/ \u6536\u53d1\u7f13\u51b2\u533a\u5927\u5c0f<\/p>\n<p>int main() {<br \/>\n    int sock;                 \/\/ \u5ba2\u6237\u7aef\u53ea\u6709\u4e00\u4e2a\u5957\u63a5\u5b57\u5c31\u591f\u4e86<br \/>\n    struct sockaddr_in server_addr; \/\/ \u670d\u52a1\u5668\u7684\u5730\u5740\u4fe1\u606f<br \/>\n    char send_buf[BUFFER_SIZE];     \/\/ \u53d1\u9001\u7f13\u51b2\u533a<br \/>\n    char recv_buf[BUFFER_SIZE];     \/\/ \u63a5\u6536\u7f13\u51b2\u533a<br \/>\n    int n;<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c1\u6b65&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57 &#061;&#061;&#061;<br \/>\n    sock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sock &lt; 0) {<br \/>\n        perror(&#034;socket&#034;);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c2\u6b65&#xff1a;\u586b\u5199\u76ee\u6807\u670d\u52a1\u5668\u7684\u5730\u5740 &#061;&#061;&#061;<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_port &#061; htons(PORT);           \/\/ \u7aef\u53e3\u53f7&#xff0c;\u5fc5\u987b htons<\/p>\n<p>    \/\/ inet_pton&#xff1a;\u628a &#034;127.0.0.1&#034; \u8fd9\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684 IP \u8f6c\u6210\u4e8c\u8fdb\u5236\u683c\u5f0f<br \/>\n    \/\/ 127.0.0.1 \u662f&#034;\u672c\u673a\u73af\u56de\u5730\u5740&#034;&#xff0c;\u610f\u601d\u5c31\u662f\u8fde\u81ea\u5df1\u8fd9\u53f0\u7535\u8111<br \/>\n    if (inet_pton(AF_INET, &#034;127.0.0.1&#034;, &amp;server_addr.sin_addr) &lt;&#061; 0) {<br \/>\n        perror(&#034;inet_pton&#034;);  \/\/ \u5931\u8d25\u901a\u5e38\u56e0\u4e3a IP \u5730\u5740\u683c\u5f0f\u5199\u9519\u4e86<br \/>\n        close(sock);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c3\u6b65&#xff1a;\u8fde\u63a5\u670d\u52a1\u5668&#xff08;\u76f8\u5f53\u4e8e\u62e8\u53f7&#xff09; &#061;&#061;&#061;<br \/>\n    \/\/ connect \u4f1a\u89e6\u53d1 TCP \u4e09\u6b21\u63e1\u624b&#xff0c;\u963b\u585e\u76f4\u5230\u8fde\u63a5\u6210\u529f\u6216\u8d85\u65f6<br \/>\n    if (connect(sock, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) {<br \/>\n        perror(&#034;connect&#034;);    \/\/ \u6700\u5e38\u89c1\u5931\u8d25\u539f\u56e0&#xff1a;\u670d\u52a1\u5668\u6ca1\u542f\u52a8<br \/>\n        close(sock);<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    printf(&#034;\u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668 127.0.0.1:%d\\\\n&#034;, PORT);<br \/>\n    printf(&#034;\u73b0\u5728\u53ef\u4ee5\u8f93\u5165\u6587\u5b57\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u4e86\u3002\u8f93\u5165 quit \u9000\u51fa\u3002\\\\n&#034;);<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c4\u6b65&#xff1a;\u5faa\u73af\u8f93\u5165\u5e76\u6536\u53d1\u6570\u636e &#061;&#061;&#061;<br \/>\n    while (1) {<br \/>\n        printf(&#034;&gt; &#034;);                 \/\/ \u63d0\u793a\u7b26<br \/>\n        fflush(stdout);               \/\/ \u5f3a\u5236\u5237\u65b0&#xff0c;\u786e\u4fdd &gt; \u7acb\u5373\u663e\u793a<\/p>\n<p>        \/\/ \u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u884c\u8f93\u5165<br \/>\n        fgets(send_buf, sizeof(send_buf), stdin);<br \/>\n        \/\/ \u8f93\u5165 &#034;quit&#034; \u5c31\u9000\u51fa<br \/>\n        if (strncmp(send_buf, &#034;quit&#034;, 4) &#061;&#061; 0) {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u53d1\u9001\u7ed9\u670d\u52a1\u5668<br \/>\n        write(sock, send_buf, strlen(send_buf));<\/p>\n<p>        \/\/ \u63a5\u6536\u670d\u52a1\u5668\u7684\u56de\u663e<br \/>\n        n &#061; read(sock, recv_buf, sizeof(recv_buf) &#8211; 1);<br \/>\n        if (n &lt;&#061; 0) {<br \/>\n            if (n &#061;&#061; 0)<br \/>\n                printf(&#034;\u670d\u52a1\u5668\u5173\u95ed\u4e86\u8fde\u63a5\\\\n&#034;);<br \/>\n            else<br \/>\n                perror(&#034;read&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        recv_buf[n] &#061; &#039;\\\\0&#039;;           \/\/ \u624b\u52a8\u52a0\u7ed3\u675f\u7b26<br \/>\n        printf(&#034;\u56de\u663e: %s&#034;, recv_buf);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u7b2c5\u6b65&#xff1a;\u5173\u95ed\u5957\u63a5\u5b57 &#061;&#061;&#061;<br \/>\n    close(sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4 id=\"\u7b2c2\u6b65\u5ba2\u6237\u7aef\u6d41\u7a0b\u901f\u67e5\u8868\">\u7b2c2\u6b65&#xff1a;\u5ba2\u6237\u7aef\u6d41\u7a0b\u901f\u67e5\u8868<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u64cd\u4f5c\u6b65\u9aa4<\/td>\n<td style=\"background-color:#dddddd\">\u8c03\u7528\u7684\u51fd\u6570<\/td>\n<td style=\"background-color:#dddddd\">\u5b83\u5728\u5e72\u4ec0\u4e48<\/td>\n<td style=\"background-color:#dddddd\">\u548c\u670d\u52a1\u5668\u6709\u4ec0\u4e48\u533a\u522b<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c1\u6b65<\/td>\n<td>socket()<\/td>\n<td>\u521b\u5efa\u5957\u63a5\u5b57<\/td>\n<td>\u548c\u670d\u52a1\u5668\u4e00\u6837<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c2\u6b65<\/td>\n<td>\u586b\u00a0sockaddr_in<\/td>\n<td>\u6307\u5b9a\u76ee\u6807\u670d\u52a1\u5668\u5730\u5740<\/td>\n<td>\u670d\u52a1\u5668\u586b\u81ea\u5df1\u7684 IP&#xff0c;\u5ba2\u6237\u7aef\u586b\u5bf9\u65b9\u7684 IP<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c3\u6b65<\/td>\n<td>connect()<\/td>\n<td>\u4e3b\u52a8\u53d1\u8d77\u8fde\u63a5<\/td>\n<td>\u5ba2\u6237\u7aef\u72ec\u6709&#xff0c;\u670d\u52a1\u5668\u4e0d\u9700\u8981\u8fd9\u6b65<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c4\u6b65<\/td>\n<td>write()\u00a0\/\u00a0read()<\/td>\n<td>\u6536\u53d1\u6570\u636e<\/td>\n<td>\u548c\u670d\u52a1\u5668\u4e00\u6837<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c5\u6b65<\/td>\n<td>close()<\/td>\n<td>\u5173\u95ed\u8fde\u63a5<\/td>\n<td>\u548c\u670d\u52a1\u5668\u4e00\u6837<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u5173\u952e\u533a\u522b&#xff1a;\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u00a0bind()\u00a0\u548c\u00a0listen()\u00a0\u548c\u00a0accept()\u3002\u5ba2\u6237\u7aef\u53ea\u9700\u00a0socket()\u00a0\u2192\u00a0connect()\u00a0\u2192 \u6536\u53d1 \u2192\u00a0close()\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u7ed9\u5ba2\u6237\u7aef\u5206\u914d\u4e00\u4e2a\u4e34\u65f6\u7aef\u53e3\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u56db\u90e8\u5206\u7f16\u8bd1\u8fd0\u884c-\u4eb2\u773c\u770b\u5230\u4e24\u53f0\u865a\u62df\u7535\u8bdd\u901a\u8bdd\">\u7b2c\u56db\u90e8\u5206&#xff1a;\u7f16\u8bd1\u8fd0\u884c \u2014\u2014 \u4eb2\u773c\u770b\u5230\u4e24\u53f0&#034;\u865a\u62df\u7535\u8bdd&#034;\u901a\u8bdd<\/h3>\n<h4 id=\"\u7b2c1\u6b65\u7f16\u8bd1\u4e24\u4e2a\u7a0b\u5e8f\">\u7b2c1\u6b65&#xff1a;\u7f16\u8bd1\u4e24\u4e2a\u7a0b\u5e8f<\/h4>\n<p># \u7f16\u8bd1\u670d\u52a1\u5668&#xff08;-o \u540e\u9762\u662f\u8f93\u51fa\u7684\u53ef\u6267\u884c\u6587\u4ef6\u540d&#xff09;<br \/>\ngcc -o tcp_server tcp_server.c<\/p>\n<p># \u7f16\u8bd1\u5ba2\u6237\u7aef<br \/>\ngcc -o tcp_client tcp_client.c<\/p>\n<p>\u26a0\ufe0f \u5982\u679c\u63d0\u793a\u00a0gcc: command not found&#xff0c;\u8bf4\u660e\u6ca1\u88c5\u7f16\u8bd1\u5668\u3002\u6267\u884c&#xff1a;sudo apt install gcc&#xff08;Ubuntu\/Debian&#xff09;\u6216\u00a0sudo yum install gcc&#xff08;CentOS&#xff09;\u3002<\/p>\n<h4 id=\"\u7b2c2\u6b65\u6253\u5f00\u4e24\u4e2a\u7ec8\u7aef\u7a97\u53e3\">\u7b2c2\u6b65&#xff1a;\u6253\u5f00\u4e24\u4e2a\u7ec8\u7aef\u7a97\u53e3<\/h4>\n<p>\u7ec8\u7aef1&#xff08;\u670d\u52a1\u5668\u7aef&#xff09;&#xff1a;<\/p>\n<p>.\/tcp_server<br \/>\n# \u4f60\u4f1a\u770b\u5230&#xff1a;<br \/>\n# \u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u6b63\u5728\u76d1\u542c\u7aef\u53e3 8080 &#8230;<br \/>\n# \u63d0\u793a&#xff1a;\u6253\u5f00\u53e6\u4e00\u4e2a\u7ec8\u7aef\u8fd0\u884c .\/tcp_client \u6765\u8fde\u63a5<\/p>\n<p>\u7ec8\u7aef2&#xff08;\u5ba2\u6237\u7aef&#xff09;&#xff1a;<\/p>\n<p>.\/tcp_client<br \/>\n# \u4f60\u4f1a\u770b\u5230&#xff1a;<br \/>\n# \u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668 127.0.0.1:8080<br \/>\n# \u73b0\u5728\u53ef\u4ee5\u8f93\u5165\u6587\u5b57\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u4e86\u3002\u8f93\u5165 quit \u9000\u51fa\u3002<br \/>\n# &gt;<\/p>\n<h4 id=\"\u7b2c3\u6b65\u5f00\u59cb\u5bf9\u8bdd\u6d4b\u8bd5\">\u7b2c3\u6b65&#xff1a;\u5f00\u59cb\u5bf9\u8bdd\u6d4b\u8bd5<\/h4>\n<p>\u5728\u5ba2\u6237\u7aef\u7ec8\u7aef\u8f93\u5165&#xff1a;<\/p>\n<p>&gt; hello world<br \/>\n# \u56de\u663e: hello world<br \/>\n&gt; \u4f60\u597d&#xff0c;\u7f51\u7edc\u7f16\u7a0b<br \/>\n# \u56de\u663e: \u4f60\u597d&#xff0c;\u7f51\u7edc\u7f16\u7a0b<br \/>\n&gt; quit<br \/>\n# &#xff08;\u7a0b\u5e8f\u9000\u51fa&#xff09;<\/p>\n<p>\u540c\u65f6\u89c2\u5bdf\u670d\u52a1\u5668\u7ec8\u7aef&#xff0c;\u4f1a\u663e\u793a&#xff1a;<\/p>\n<p>\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u8fdb\u6765\u4e86: 127.0.0.1:xxxxx<br \/>\n\u6536\u5230: hello world<br \/>\n\u6536\u5230: \u4f60\u597d&#xff0c;\u7f51\u7edc\u7f16\u7a0b<br \/>\n\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5<\/p>\n<p>\u26a0\ufe0f\u00a0\u5e38\u89c1\u95ee\u9898&#xff1a;\u5982\u679c\u5ba2\u6237\u7aef\u663e\u793a\u00a0connect: Connection refused&#xff0c;99% \u662f\u56e0\u4e3a\u670d\u52a1\u5668\u6ca1\u542f\u52a8\u3002\u5148\u786e\u8ba4\u7ec8\u7aef1\u91cc\u00a0.\/tcp_server\u00a0\u8fd8\u5728\u8fd0\u884c\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e94\u90e8\u52068-\u4e2a\u6838\u5fc3\u51fd\u6570\u901f\u8bb0\u5361\">\u7b2c\u4e94\u90e8\u5206&#xff1a;8 \u4e2a\u6838\u5fc3\u51fd\u6570\u901f\u8bb0\u5361<\/h3>\n<p>\u628a\u8fd9 8 \u4e2a\u51fd\u6570\u6b7b\u8bb0\u786c\u80cc\u4e0b\u6765&#xff0c;TCP \u7f16\u7a0b\u5c31\u638c\u63e1\u4e86 80%&#xff1a;<\/p>\n<p>\/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57 \u2014 \u4e70\u7535\u8bdd<br \/>\nint socket(int domain, int type, int protocol);<br \/>\n\/\/ domain: AF_INET(IPv4) \u6216 AF_INET6(IPv6)<br \/>\n\/\/ type: SOCK_STREAM(TCP) \u6216 SOCK_DGRAM(UDP)<br \/>\n\/\/ protocol: \u901a\u5e38\u586b 0&#xff0c;\u8ba9\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9<\/p>\n<p>\/\/ 2. \u7ed1\u5b9a\u5730\u5740 \u2014 \u516c\u5e03\u7535\u8bdd\u53f7\u7801<br \/>\nint bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<br \/>\n\/\/ \u670d\u52a1\u5668\u5fc5\u8c03&#xff0c;\u5ba2\u6237\u7aef\u901a\u5e38\u4e0d\u7528&#xff08;connect \u65f6\u7cfb\u7edf\u81ea\u52a8\u5206\u914d&#xff09;<\/p>\n<p>\/\/ 3. \u5f00\u59cb\u76d1\u542c \u2014 \u5750\u4e0b\u7b49\u7535\u8bdd<br \/>\nint listen(int sockfd, int backlog);<br \/>\n\/\/ backlog: \u7b49\u5f85\u961f\u5217\u6700\u5927\u957f\u5ea6&#xff0c;\u6d4b\u8bd5\u65f6\u586b 5 \u5c31\u884c<\/p>\n<p>\/\/ 4. \u63a5\u53d7\u8fde\u63a5 \u2014 \u63a5\u8d77\u7535\u8bdd<br \/>\nint accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<br \/>\n\/\/ \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 fd&#xff0c;\u4e13\u95e8\u548c\u8fd9\u4e2a\u5ba2\u6237\u7aef\u901a\u4fe1<br \/>\n\/\/ \u539f sockfd \u7ee7\u7eed\u76d1\u542c\u65b0\u8fde\u63a5<\/p>\n<p>\/\/ 5. \u53d1\u8d77\u8fde\u63a5 \u2014 \u62e8\u53f7<br \/>\nint connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<br \/>\n\/\/ \u5ba2\u6237\u7aef\u72ec\u6709\u3002\u6210\u529f\u8fd4\u56de 0&#xff0c;\u5931\u8d25\u8fd4\u56de -1<\/p>\n<p>\/\/ 6. \u8bfb\u53d6\u6570\u636e \u2014 \u542c\u5bf9\u65b9\u8bf4\u8bdd<br \/>\nssize_t read(int fd, void *buf, size_t count);<br \/>\n\/\/ \u8fd4\u56de 0 \u8868\u793a\u5bf9\u65b9\u5173\u95ed\u4e86\u8fde\u63a5&#xff08;\u6302\u7535\u8bdd&#xff09;<br \/>\n\/\/ \u8fd4\u56de -1 \u8868\u793a\u51fa\u9519<\/p>\n<p>\/\/ 7. \u53d1\u9001\u6570\u636e \u2014 \u5bf9\u5bf9\u65b9\u8bf4\u8bdd<br \/>\nssize_t write(int fd, const void *buf, size_t count);<br \/>\n\/\/ \u6ce8\u610f&#xff1a;TCP \u662f\u6d41\u5f0f\u534f\u8bae&#xff0c;\u4e00\u6b21 write \u4e0d\u4e00\u5b9a\u5bf9\u5e94\u4e00\u6b21 read<\/p>\n<p>\/\/ 8. \u5173\u95ed\u8fde\u63a5 \u2014 \u6302\u7535\u8bdd<br \/>\nint close(int fd);<br \/>\n\/\/ \u89e6\u53d1 TCP \u56db\u6b21\u6325\u624b<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e03\u90e8\u5206\u5e38\u89c1\u95ee\u9898\u901f\u67e5\u8868\">\u7b2c\u4e03\u90e8\u5206&#xff1a;\u5e38\u89c1\u95ee\u9898\u901f\u67e5\u8868<\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u95ee\u9898\u73b0\u8c61<\/td>\n<td style=\"background-color:#dddddd\">\u53ef\u80fd\u539f\u56e0<\/td>\n<td style=\"background-color:#dddddd\">\u89e3\u51b3\u529e\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>bind: Address already in use<\/td>\n<td>\u4e0a\u4e00\u6b21\u8fd0\u884c\u7684\u670d\u52a1\u5668\u8fd8\u6ca1\u5b8c\u5168\u91ca\u653e\u7aef\u53e3&#xff08;TCP \u6709 TIME_WAIT \u72b6\u6001&#xff0c;\u901a\u5e38\u7b49 1-2 \u5206\u949f&#xff09;<\/td>\n<td>\u2460\u7b49\u51e0\u5206\u949f\u518d\u8bd5&#xff1b;\u2461\u6216\u7528\u00a0setsockopt\u00a0\u8bbe\u7f6e\u00a0SO_REUSEADDR&#xff1b;\u2462\u6362\u4e2a\u7aef\u53e3\u53f7<\/td>\n<\/tr>\n<tr>\n<td>connect: Connection refused<\/td>\n<td>\u670d\u52a1\u5668\u6ca1\u542f\u52a8&#xff0c;\u6216\u7aef\u53e3\u53f7\u4e0d\u4e00\u81f4<\/td>\n<td>\u2460\u786e\u8ba4\u00a0.\/tcp_server\u00a0\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u6b63\u5728\u8fd0\u884c&#xff1b;\u2461\u68c0\u67e5\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u00a0PORT\u00a0\u5b8f\u662f\u5426\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td>connect: Network is unreachable<\/td>\n<td>IP \u5730\u5740\u5199\u9519\u4e86&#xff0c;\u6216\u7f51\u7edc\u4e0d\u901a<\/td>\n<td>\u2460\u5148\u7528\u00a0ping 127.0.0.1\u00a0\u6d4b\u8bd5\u672c\u673a\u73af\u56de\u662f\u5426\u6b63\u5e38&#xff1b;\u2461\u68c0\u67e5 IP \u662f\u5426\u6709\u62fc\u5199\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u6237\u7aef\u8f93\u5165\u4e2d\u6587&#xff0c;\u670d\u52a1\u5668\u663e\u793a\u4e71\u7801<\/td>\n<td>\u4e24\u7aef\u5b57\u7b26\u7f16\u7801\u4e0d\u4e00\u81f4<\/td>\n<td>\u786e\u4fdd\u4e24\u7aef\u7ec8\u7aef\u90fd\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002read\/write\u00a0\u4e0d\u4fee\u6539\u5b57\u8282\u5185\u5bb9&#xff0c;\u4e71\u7801\u901a\u5e38\u662f\u663e\u793a\u95ee\u9898<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u6237\u7aef\u53d1\u4e86 100 \u5b57\u8282&#xff0c;\u670d\u52a1\u5668\u00a0read\u00a0\u53ea\u8bfb\u5230 50 \u5b57\u8282<\/td>\n<td>TCP \u662f\u6d41\u5f0f\u534f\u8bae&#xff0c;\u6ca1\u6709\u6d88\u606f\u8fb9\u754c\u3002\u4e00\u6b21\u00a0write\u00a0\u7684\u6570\u636e\u53ef\u80fd\u88ab\u62c6\u6210\u591a\u6b21\u00a0read<\/td>\n<td>\u5e94\u7528\u5c42\u9700\u8981\u81ea\u5df1\u5236\u5b9a\u534f\u8bae&#xff1a;\u2460\u56fa\u5b9a\u957f\u5ea6&#xff1b;\u2461\u957f\u5ea6\u524d\u7f00&#xff1b;\u2462\u7279\u6b8a\u5206\u9694\u7b26&#xff08;\u5982\u6362\u884c\u00a0\\\\n&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>accept\u00a0\u8fd4\u56de -1&#xff0c;errno\u00a0\u663e\u793a\u00a0EINTR<\/td>\n<td>\u7cfb\u7edf\u8c03\u7528\u88ab\u4fe1\u53f7\u4e2d\u65ad<\/td>\n<td>\u901a\u5e38\u52a0\u00a0if (errno &#061;&#061; EINTR) continue;\u00a0\u5373\u53ef<\/td>\n<\/tr>\n<tr>\n<td>\u7f16\u8bd1\u65f6\u62a5\u00a0undefined reference to &#8230;<\/td>\n<td>\u6ca1\u6709\u94fe\u63a5\u5bf9\u5e94\u7684\u5e93<\/td>\n<td>Socket \u51fd\u6570\u5728\u6807\u51c6 C \u5e93\u4e2d&#xff0c;\u4e0d\u9700\u8981\u989d\u5916\u00a0-l\u3002\u4f46\u5982\u679c\u662f\u591a\u7ebf\u7a0b\u7248\u672c\u9700\u8981\u52a0\u00a0-lpthread<\/td>\n<\/tr>\n<tr>\n<td>perror: socket: Too many open files<\/td>\n<td>\u6587\u4ef6\u63cf\u8ff0\u7b26\u7528\u5b8c\u4e86&#xff08;\u6253\u5f00\u4e86\u592a\u591a\u8fde\u63a5\u6ca1\u5173&#xff09;<\/td>\n<td>\u2460\u68c0\u67e5\u662f\u5426\u6bcf\u4e2a\u00a0client_fd\u00a0\u90fd\u6b63\u786e\u00a0close&#xff1b;\u2461\u7528\u00a0ulimit -n\u00a0\u67e5\u770b\u5e76\u63d0\u9ad8\u9650\u5236<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"\u4f5c\u4e1aday-22-23-\u5de9\u56fa\u7ec3\u4e60\">\u4f5c\u4e1a&#xff08;Day 22-23 \u5de9\u56fa\u7ec3\u4e60&#xff09;<\/h3>\n<li>\u4fee\u6539\u670d\u52a1\u5668&#xff0c;\u8ba9\u5b83\u5728\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u00a0bye\u00a0\u65f6\u4e3b\u52a8\u5173\u95ed\u8fde\u63a5&#xff0c;\u5e76\u5728\u5173\u95ed\u524d\u53d1\u9001\u00a0Goodbye!\u00a0\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u7528\u00a0fork()\u00a0\u6539\u9020\u670d\u52a1\u5668&#xff0c;\u4f7f\u5176\u80fd\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef&#xff08;\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff09;&#xff0c;\u53c2\u7167\u7b2c\u516d\u90e8\u5206\u7684\u9aa8\u67b6\u4ee3\u7801\u3002<\/li>\n<li>\u5728\u5ba2\u6237\u7aef\u589e\u52a0\u8d85\u65f6\u673a\u5236&#xff1a;\u5982\u679c 5 \u79d2\u5185\u6ca1\u6709\u6536\u5230\u670d\u52a1\u5668\u56de\u663e&#xff0c;\u81ea\u52a8\u9000\u51fa&#xff08;\u63d0\u793a&#xff1a;\u7528\u00a0setsockopt\u00a0\u8bbe\u7f6e\u00a0SO_RCVTIMEO&#xff09;\u3002<\/li>\n<li>\u6311\u6218\u9898&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u804a\u5929\u5ba4\u2014\u2014\u670d\u52a1\u5668\u6536\u5230\u4efb\u4f55\u5ba2\u6237\u7aef\u7684\u6d88\u606f\u540e&#xff0c;\u8f6c\u53d1\u7ed9\u6240\u6709\u5df2\u8fde\u63a5\u7684\u5ba2\u6237\u7aef&#xff08;\u9700\u8981\u7ef4\u62a4\u5ba2\u6237\u7aef fd \u5217\u8868&#xff0c;\u53ef\u7528\u00a0select\u00a0\u6216\u591a\u7ebf\u7a0b&#xff09;\u3002<\/li>\n<hr \/>\n<h3 id=\"\u77e5\u8bc6\u5173\u7cfb\u56fe\">\u77e5\u8bc6\u5173\u7cfb\u56fe<\/h3>\n<p>socket() \u2500\u2500\u2500\u2500 \u521b\u5efa\u901a\u4fe1\u7aef\u70b9<br \/>\n   \u2502<br \/>\n   \u251c\u2500\u2500 \u670d\u52a1\u5668\u8def\u7ebf&#xff1a;bind() \u2192 listen() \u2192 accept() \u2192 read()\/write() \u2192 close()<br \/>\n   \u2502<br \/>\n   \u2514\u2500\u2500 \u5ba2\u6237\u7aef\u8def\u7ebf&#xff1a;connect() \u2192 write()\/read() \u2192 close()<\/p>\n<p>\u6838\u5fc3\u6982\u5ff5&#xff1a;<br \/>\n   sockaddr_in  \u2500\u2500\u2500\u2500 \u5730\u5740&#034;\u5feb\u9012\u5355&#034;<br \/>\n   htons\/htonl  \u2500\u2500\u2500\u2500 \u5b57\u8282\u5e8f\u8f6c\u6362&#xff08;\u5927\u7aef\u7edf\u4e00&#xff09;<br \/>\n   TCP \u4e09\u6b21\u63e1\u624b  \u2500\u2500\u2500\u2500 connect() \u8fde\u63a5\u6210\u529f\u65f6\u5df2\u5b8c\u6210<br \/>\n   TCP \u56db\u6b21\u6325\u624b  \u2500\u2500\u2500\u2500 close() \u89e6\u53d1<\/p>\n<hr \/>\n<p>\u7f51\u7edc\u7f16\u7a0b\u65e0\u975e\u5c31\u662f&#034;\u8fde\u7ebf\u3001\u8bf4\u8bdd\u3001\u6302\u7535\u8bdd&#034;\u7684\u5faa\u73af\u3002\u4eca\u5929\u4f60\u4eb2\u624b\u5199\u51fa\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684 TCP \u901a\u4fe1\u7a0b\u5e8f\u2014\u2014\u8fd9\u4e00\u6b65\u8de8\u8d8a\u4e86&#034;\u5355\u673a\u7f16\u7a0b&#034;\u5230&#034;\u7f51\u7edc\u7f16\u7a0b&#034;\u7684\u5206\u6c34\u5cad\u3002\u591a\u6572\u51e0\u904d\u4ee3\u7801&#xff0c;\u8ba9\u8fd9\u4e9b\u51fd\u6570\u5f62\u6210\u808c\u8089\u8bb0\u5fc6&#xff0c;\u4f60\u4f1a\u53d1\u73b0\u5b83\u771f\u7684\u6ca1\u90a3\u4e48\u795e\u79d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7edc\u7f16\u7a0b\u6ca1\u90a3\u4e48\u795e\u79d8&#xff0c;\u5c31\u50cf\u6253\u7535\u8bdd\u2014\u2014\u4e70\u624b\u673a\u3001\u63d2\u5361\u3001\u62e8\u53f7\u3001\u901a\u8bdd\u3001\u6302\u65ad&#xff0c;\u4e94\u4e2a\u52a8\u4f5c\u800c\u5df2\u3002\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u6362\u6210 C \u8bed\u8a00\u7684\u51e0\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u5f00\u7bc7&#xff1a;\u5148\u5efa\u7acb\u4e00\u4e2a\u76f4\u89c9\u5728\u5b66\u4e60\u5177\u4f53\u4ee3\u7801\u4e4b\u524d&#xff0c;\u5148\u628a\u81ea\u5df1\u60f3\u8c61\u6210\u4e00\u5bb6\u5916\u5356\u5e97\u8001\u677f&#xff0c;\u4f60\u5c31\u5bb9\u6613\u7406\u89e3 TCP \u901a\u4fe1\u5230\u5e95\u5728\u5e72\u561b\u4e86&#xff1a;\u5916\u5356\u5e97\u573a\u666f\u5bf9\u5e94\u7684\u7f51\u7edc\u7f16\u7a0b\u6982\u5ff5\u5927\u767d\u8bdd\u5b89\u88c5\u4e00\u90e8\u5916\u5356\u4e13\u7528\u7535\u8bddsocket()\u521b\u5efa\u901a\u4fe1\u5de5\u5177\u5370\u5728\u540d\u7247\u4e0a\u7684\u7535\u8bdd\u53f7\u7801bind()\u00a0\u7ed1\u5b9a IP  \u7aef\u53e3\u5bf9\u5916\u516c\u5e03\u4f60\u7684\u8054\u7cfb\u65b9\u5f0f\u5750\u5728\u5e97\u91cc\u7b49\u7535<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[174,205,43],"topic":[],"class_list":["post-90850","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-tcp-ip","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/90850.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u7f51\u7edc\u7f16\u7a0b\u6ca1\u90a3\u4e48\u795e\u79d8&#xff0c;\u5c31\u50cf\u6253\u7535\u8bdd\u2014\u2014\u4e70\u624b\u673a\u3001\u63d2\u5361\u3001\u62e8\u53f7\u3001\u901a\u8bdd\u3001\u6302\u65ad&#xff0c;\u4e94\u4e2a\u52a8\u4f5c\u800c\u5df2\u3002\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u6362\u6210 C \u8bed\u8a00\u7684\u51e0\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u5f00\u7bc7&#xff1a;\u5148\u5efa\u7acb\u4e00\u4e2a\u76f4\u89c9\u5728\u5b66\u4e60\u5177\u4f53\u4ee3\u7801\u4e4b\u524d&#xff0c;\u5148\u628a\u81ea\u5df1\u60f3\u8c61\u6210\u4e00\u5bb6\u5916\u5356\u5e97\u8001\u677f&#xff0c;\u4f60\u5c31\u5bb9\u6613\u7406\u89e3 TCP \u901a\u4fe1\u5230\u5e95\u5728\u5e72\u561b\u4e86&#xff1a;\u5916\u5356\u5e97\u573a\u666f\u5bf9\u5e94\u7684\u7f51\u7edc\u7f16\u7a0b\u6982\u5ff5\u5927\u767d\u8bdd\u5b89\u88c5\u4e00\u90e8\u5916\u5356\u4e13\u7528\u7535\u8bddsocket()\u521b\u5efa\u901a\u4fe1\u5de5\u5177\u5370\u5728\u540d\u7247\u4e0a\u7684\u7535\u8bdd\u53f7\u7801bind()\u00a0\u7ed1\u5b9a IP \u7aef\u53e3\u5bf9\u5916\u516c\u5e03\u4f60\u7684\u8054\u7cfb\u65b9\u5f0f\u5750\u5728\u5e97\u91cc\u7b49\u7535\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/90850.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-05T23:33:02+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/90850.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/90850.html\",\"name\":\"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-05T23:33:02+00:00\",\"dateModified\":\"2026-08-05T23:33:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/90850.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/90850.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/90850.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/90850.html","og_locale":"zh_CN","og_type":"article","og_title":"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u7f51\u7edc\u7f16\u7a0b\u6ca1\u90a3\u4e48\u795e\u79d8&#xff0c;\u5c31\u50cf\u6253\u7535\u8bdd\u2014\u2014\u4e70\u624b\u673a\u3001\u63d2\u5361\u3001\u62e8\u53f7\u3001\u901a\u8bdd\u3001\u6302\u65ad&#xff0c;\u4e94\u4e2a\u52a8\u4f5c\u800c\u5df2\u3002\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u6362\u6210 C \u8bed\u8a00\u7684\u51e0\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u5f00\u7bc7&#xff1a;\u5148\u5efa\u7acb\u4e00\u4e2a\u76f4\u89c9\u5728\u5b66\u4e60\u5177\u4f53\u4ee3\u7801\u4e4b\u524d&#xff0c;\u5148\u628a\u81ea\u5df1\u60f3\u8c61\u6210\u4e00\u5bb6\u5916\u5356\u5e97\u8001\u677f&#xff0c;\u4f60\u5c31\u5bb9\u6613\u7406\u89e3 TCP \u901a\u4fe1\u5230\u5e95\u5728\u5e72\u561b\u4e86&#xff1a;\u5916\u5356\u5e97\u573a\u666f\u5bf9\u5e94\u7684\u7f51\u7edc\u7f16\u7a0b\u6982\u5ff5\u5927\u767d\u8bdd\u5b89\u88c5\u4e00\u90e8\u5916\u5356\u4e13\u7528\u7535\u8bddsocket()\u521b\u5efa\u901a\u4fe1\u5de5\u5177\u5370\u5728\u540d\u7247\u4e0a\u7684\u7535\u8bdd\u53f7\u7801bind()\u00a0\u7ed1\u5b9a IP \u7aef\u53e3\u5bf9\u5916\u516c\u5e03\u4f60\u7684\u8054\u7cfb\u65b9\u5f0f\u5750\u5728\u5e97\u91cc\u7b49\u7535","og_url":"https:\/\/www.wsisp.com\/helps\/90850.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-05T23:33:02+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/90850.html","url":"https:\/\/www.wsisp.com\/helps\/90850.html","name":"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-05T23:33:02+00:00","dateModified":"2026-08-05T23:33:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/90850.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/90850.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/90850.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Day 22-23 TCP \u804a\u5929\u670d\u52a1\u5668\uff1a\u4ece\u96f6\u624b\u5199\u7f51\u7edc\u901a\u4fe1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/90850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=90850"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/90850\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=90850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=90850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=90850"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=90850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}