{"id":63652,"date":"2026-01-22T07:41:16","date_gmt":"2026-01-21T23:41:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/63652.html"},"modified":"2026-01-22T07:41:16","modified_gmt":"2026-01-21T23:41:16","slug":"%e3%80%90linux%e3%80%91tcp%e5%8d%8f%e8%ae%ae%e3%80%902%e3%80%91%ef%bc%9a-%e4%bb%8e-echo-%e5%88%b0%e8%bf%9c%e7%a8%8b%e5%91%bd%e4%bb%a4%e6%89%a7%e8%a1%8c%ef%bc%9alinux-tcp-%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/63652.html","title":{"rendered":"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5"},"content":{"rendered":"<p>\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u7684\u6838\u5fc3\u6280\u80fd&#xff0c;Linux \u4e0b\u7684 TCP \u670d\u52a1\u5668\u5f00\u53d1\u662f\u7ed5\u4e0d\u5f00\u7684\u77e5\u8bc6\u70b9\u3002\u672c\u6587\u5c06\u4ece\u57fa\u7840\u7684 socket \u7f16\u7a0b\u5165\u624b&#xff0c;\u4e00\u6b65\u6b65\u5b9e\u73b0 echo \u670d\u52a1\u5668&#xff0c;\u5e76\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u6c60\u4f18\u5316\u5e76\u53d1\u80fd\u529b&#xff0c;\u6700\u540e\u6269\u5c55\u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u573a\u666f\u5e76\u8865\u5145\u5b89\u5168\u9632\u62a4\u65b9\u6848&#xff0c;\u5168\u7a0b\u4ee5\u5b9e\u6218\u4ee3\u7801\u548c\u6838\u5fc3\u95ee\u9898\u4e3a\u6838\u5fc3\u5c55\u5f00\u3002<\/p>\n<h3>\u4e00\u3001\u57fa\u7840\u7bc7&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u80fd\u8dd1\u7684 echo \u670d\u52a1\u5668<\/h3>\n<p>echo \u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91\u5f88\u7b80\u5355 \u2014\u2014 \u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31\u8fd4\u56de\u4ec0\u4e48\u3002\u8fd9\u4e00\u6b65\u6211\u4eec\u5148\u641e\u5b9a\u57fa\u7840\u7684 socket \u7f16\u7a0b\u6d41\u7a0b&#xff0c;\u4ee5\u53ca\u5f00\u53d1\u4e2d\u6700\u5bb9\u6613\u8e29\u7684\u7ed1\u5b9a\u3001\u8fde\u63a5\u95ee\u9898\u3002<\/p>\n<h4>1.1 \u5148\u89e3\u51b3\u4e24\u4e2a\u6838\u5fc3\u95ee\u9898&#xff1a;\u7ed1\u5b9a\u4e0e\u8fde\u63a5<\/h4>\n<p>\u5728\u7f16\u5199\u670d\u52a1\u5668\u4ee3\u7801\u524d&#xff0c;\u5148\u7406\u6e05\u4e24\u4e2a\u9ad8\u9891\u95ee\u9898&#xff1a;bind\u7ed1\u5b9a\u548cconnect\u8fde\u63a5\u53c2\u6570\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234108-69716414e13e0.png\" alt=\"image\" \/><\/p>\n<p>\u670d\u52a1\u5668\u7aef\u8c03\u7528bind\u51fd\u6570\u65f6&#xff0c;\u6838\u5fc3\u662f\u7ed1\u5b9a IP \u548c\u7aef\u53e3&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u7ed1\u5b9a0.0.0.0&#xff0c;\u8868\u793a\u76d1\u542c\u670d\u52a1\u5668\u6240\u6709\u7f51\u5361\u7684\u8be5\u7aef\u53e3&#xff0c;\u8fd9\u662f\u670d\u52a1\u5668\u7684\u5e38\u89c4\u64cd\u4f5c&#xff1b;<\/li>\n<li>\u5982\u679c\u7ed1\u5b9a\u5177\u4f53 IP&#xff08;\u5982192.168.1.100&#xff09;&#xff0c;\u5219\u53ea\u76d1\u542c\u8be5\u7f51\u5361\u7684\u8bf7\u6c42&#xff0c;\u9002\u5408\u591a\u7f51\u5361\u7684\u5b9a\u5411\u76d1\u542c\u573a\u666f&#xff1b;<\/li>\n<li>\u7ed1\u5b9a\u5931\u8d25\u5e38\u89c1\u539f\u56e0&#xff1a;\u7aef\u53e3\u88ab\u5360\u7528\u3001\u6743\u9650\u4e0d\u8db3&#xff08;1024 \u4ee5\u4e0b\u7aef\u53e3\u9700\u8981 root&#xff09;\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234109-697164156f0f2.png\" alt=\"image\" \/><\/p>\n<p>\u5ba2\u6237\u7aef\u7684connect\u51fd\u6570\u6838\u5fc3\u662f\u6307\u5b9a\u8981\u8fde\u63a5\u7684\u670d\u52a1\u5668\u4fe1\u606f&#xff1a;<\/p>\n<ul>\n<li>\u53c2\u6570\u5fc5\u987b\u662f\u670d\u52a1\u5668\u7684struct sockaddr_in\u7ed3\u6784\u4f53&#xff0c;\u5305\u542b\u670d\u52a1\u5668 IP \u548c\u7aef\u53e3&#xff1b;<\/li>\n<li>\u5ba2\u6237\u7aef\u65e0\u9700\u624b\u52a8bind\u672c\u5730\u7aef\u53e3&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5206\u914d\u4e00\u4e2a\u672a\u4f7f\u7528\u7684\u7aef\u53e3&#xff0c;\u8fd9\u662f\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u6838\u5fc3\u533a\u522b\u4e4b\u4e00\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234109-69716415aa30d.png\" alt=\"image\" \/><\/p>\n<p>\u7ed9\u5927\u5bb6\u8d34\u4e00\u6bb5\u5ba2\u6237\u7aefconnect\u7684\u6838\u5fc3\u4ee3\u7801&#xff0c;\u4e00\u770b\u5c31\u61c2&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u670d\u52a1\u5668\u5730\u5740\u7ed3\u6784<br \/>\nstruct sockaddr_in server_addr;<br \/>\nmemset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\nserver_addr.sin_family &#061; AF_INET;<br \/>\nserver_addr.sin_port &#061; htons(8080); \/\/ \u670d\u52a1\u5668\u7aef\u53e3<br \/>\ninet_pton(AF_INET, &#034;127.0.0.1&#034;, &amp;server_addr.sin_addr); \/\/ \u670d\u52a1\u5668IP<\/p>\n<p>\/\/ \u8fde\u63a5\u670d\u52a1\u5668<br \/>\nint ret &#061; connect(client_sock, (struct sockaddr*)&amp;server_addr, sizeof(server_addr));<br \/>\nif (ret &#061;&#061; -1) {<br \/>\n    perror(&#034;connect failed&#034;);<br \/>\n    close(client_sock);<br \/>\n    return -1;<br \/>\n}<\/p>\n<h4>1.2 echo \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u5b8c\u6574\u5b9e\u73b0<\/h4>\n<p>\u7406\u6e05\u6838\u5fc3\u95ee\u9898\u540e&#xff0c;\u76f4\u63a5\u4e0a echo \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u6838\u5fc3\u4ee3\u7801&#xff0c;\u7ed3\u5408\u56fe\u7247\u91cc\u7684\u903b\u8f91\u62c6\u89e3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234110-69716416106b2.png\" alt=\"image\" \/><\/p>\n<p>echo \u5ba2\u6237\u7aef\u6838\u5fc3\u6d41\u7a0b&#xff1a;\u521b\u5efa socket \u2192 \u8fde\u63a5\u670d\u52a1\u5668 \u2192 \u53d1\u9001\u6570\u636e \u2192 \u63a5\u6536\u8fd4\u56de\u6570\u636e \u2192 \u5173\u95ed socket\u3002<\/p>\n<p>\u6838\u5fc3\u4ee3\u7801\u7247\u6bb5&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<\/p>\n<p>#define SERVER_IP &#034;127.0.0.1&#034;<br \/>\n#define SERVER_PORT 8080<br \/>\n#define BUF_SIZE 1024<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u5ba2\u6237\u7aefsocket<br \/>\n    int client_sock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (client_sock &#061;&#061; -1) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u521d\u59cb\u5316\u670d\u52a1\u5668\u5730\u5740<br \/>\n    struct sockaddr_in server_addr;<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(SERVER_PORT);<br \/>\n    inet_pton(AF_INET, SERVER_IP, &amp;server_addr.sin_addr);<\/p>\n<p>    \/\/ 3. \u8fde\u63a5\u670d\u52a1\u5668<br \/>\n    if (connect(client_sock, (struct sockaddr*)&amp;server_addr, sizeof(server_addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;connect failed&#034;);<br \/>\n        close(client_sock);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u53d1\u9001\u6570\u636e\u5e76\u63a5\u6536echo<br \/>\n    char buf[BUF_SIZE] &#061; &#034;Hello Echo Server!&#034;;<br \/>\n    write(client_sock, buf, strlen(buf)); \/\/ \u53d1\u9001\u6570\u636e<br \/>\n    memset(buf, 0, BUF_SIZE);<br \/>\n    read(client_sock, buf, BUF_SIZE); \/\/ \u63a5\u6536\u8fd4\u56de\u6570\u636e<br \/>\n    printf(&#034;Received from server: %s\\\\n&#034;, buf);<\/p>\n<p>    \/\/ 5. \u5173\u95edsocket<br \/>\n    close(client_sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234110-697164166e0dc.png\" alt=\"image\" \/><\/p>\n<p>echo \u670d\u52a1\u5668\u6838\u5fc3\u6d41\u7a0b&#xff1a;\u521b\u5efa socket \u2192 \u7ed1\u5b9a IP \u7aef\u53e3 \u2192 \u76d1\u542c \u2192 \u63a5\u53d7\u8fde\u63a5 \u2192 \u8bfb\u53d6\u6570\u636e \u2192 \u539f\u6837\u8fd4\u56de \u2192 \u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p>\u6838\u5fc3\u4ee3\u7801\u7247\u6bb5&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<\/p>\n<p>#define SERVER_PORT 8080<br \/>\n#define BUF_SIZE 1024<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u76d1\u542csocket<br \/>\n    int listen_sock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (listen_sock &#061;&#061; -1) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u521d\u59cb\u5316\u670d\u52a1\u5668\u5730\u5740&#xff08;\u7ed1\u5b9a0.0.0.0:8080&#xff09;<br \/>\n    struct sockaddr_in server_addr;<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(SERVER_PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u7b49\u4ef7\u4e8e0.0.0.0<\/p>\n<p>    \/\/ 3. \u7ed1\u5b9a\u5730\u5740<br \/>\n    if (bind(listen_sock, (struct sockaddr*)&amp;server_addr, sizeof(server_addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        close(listen_sock);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u76d1\u542c&#xff08;backlog\u8bbe\u4e3a5&#xff0c;\u5141\u8bb85\u4e2a\u534a\u8fde\u63a5&#xff09;<br \/>\n    if (listen(listen_sock, 5) &#061;&#061; -1) {<br \/>\n        perror(&#034;listen failed&#034;);<br \/>\n        close(listen_sock);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    printf(&#034;Echo server listening on 0.0.0.0:%d\\\\n&#034;, SERVER_PORT);<\/p>\n<p>    \/\/ 5. \u63a5\u53d7\u8fde\u63a5\u5e76\u5904\u7406echo<br \/>\n    struct sockaddr_in client_addr;<br \/>\n    socklen_t client_addr_len &#061; sizeof(client_addr);<br \/>\n    int conn_sock &#061; accept(listen_sock, (struct sockaddr*)&amp;client_addr, &amp;client_addr_len);<br \/>\n    if (conn_sock &#061;&#061; -1) {<br \/>\n        perror(&#034;accept failed&#034;);<br \/>\n        close(listen_sock);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    printf(&#034;Client connected: %s:%d\\\\n&#034;, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));<\/p>\n<p>    \/\/ 6. \u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e\u5e76\u539f\u6837\u8fd4\u56de<br \/>\n    char buf[BUF_SIZE];<br \/>\n    ssize_t n &#061; read(conn_sock, buf, BUF_SIZE);<br \/>\n    if (n &gt; 0) {<br \/>\n        write(conn_sock, buf, n); \/\/ \u539f\u6837\u8fd4\u56de<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u5173\u95ed\u8fde\u63a5<br \/>\n    close(conn_sock);<br \/>\n    close(listen_sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3>\u4e8c\u3001\u5e76\u53d1\u7bc7 1&#xff1a;\u591a\u8fdb\u7a0b\u7248\u672c echo \u670d\u52a1\u5668<\/h3>\n<p>\u4e0a\u9762\u7684\u57fa\u7840\u7248\u670d\u52a1\u5668\u6709\u4e2a\u81f4\u547d\u95ee\u9898 \u2014\u2014 \u53ea\u80fd\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5904\u7406\u5b8c\u5c31\u9000\u51fa\u4e86\u3002\u8981\u652f\u6301\u5e76\u53d1&#xff0c;\u9996\u5148\u60f3\u5230\u7528\u591a\u8fdb\u7a0b&#xff1a;\u7236\u8fdb\u7a0b\u8d1f\u8d23\u63a5\u53d7\u8fde\u63a5&#xff0c;\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u5904\u7406\u4e1a\u52a1\u3002<\/p>\n<h4>2.1 \u591a\u8fdb\u7a0b\u6838\u5fc3\u903b\u8f91\u4e0e\u4ee3\u7801<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234110-69716416a845c.png\" alt=\"image\" \/><\/p>\n<p>\u6838\u5fc3\u8bbe\u8ba1\u601d\u8def&#xff1a;<\/p>\n<li>\u7236\u8fdb\u7a0b\u4e00\u76f4\u5faa\u73af\u8c03\u7528accept&#xff0c;\u62ff\u5230\u65b0\u7684\u8fde\u63a5\u5957\u63a5\u5b57conn_sock&#xff1b;<\/li>\n<li>\u6bcf\u6b21\u62ff\u5230conn_sock\u540e&#xff0c;fork()\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542c\u5957\u63a5\u5b57listen_sock&#xff0c;\u8981\u4e3b\u52a8\u5173\u95ed&#xff08;\u907f\u514d\u6587\u4ef6\u63cf\u8ff0\u7b26\u6cc4\u6f0f&#xff09;&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u4e13\u6ce8\u5904\u7406conn_sock\u7684 echo \u4e1a\u52a1&#xff0c;\u5904\u7406\u5b8c\u5173\u95edconn_sock\u5e76\u9000\u51fa\u3002<\/li>\n<p>\u4f18\u5316\u540e\u7684\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u6838\u5fc3\u4ee3\u7801&#xff08;\u53ea\u6539 accept \u540e\u7684\u903b\u8f91&#xff09;&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\/\/ \u66ff\u6362\u57fa\u7840\u7248\u4e2daccept\u540e\u7684\u4ee3\u7801<br \/>\nwhile (1) { \/\/ \u7236\u8fdb\u7a0b\u5faa\u73af\u63a5\u53d7\u8fde\u63a5<br \/>\n    struct sockaddr_in client_addr;<br \/>\n    socklen_t client_addr_len &#061; sizeof(client_addr);<br \/>\n    int conn_sock &#061; accept(listen_sock, (struct sockaddr*)&amp;client_addr, &amp;client_addr_len);<br \/>\n    if (conn_sock &#061;&#061; -1) {<br \/>\n        perror(&#034;accept failed&#034;);<br \/>\n        continue;<br \/>\n    }<br \/>\n    printf(&#034;Client connected: %s:%d\\\\n&#034;, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));<\/p>\n<p>    \/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u4e1a\u52a1<br \/>\n    pid_t pid &#061; fork();<br \/>\n    if (pid &#061;&#061; -1) {<br \/>\n        perror(&#034;fork failed&#034;);<br \/>\n        close(conn_sock);<br \/>\n        continue;<br \/>\n    } else if (pid &#061;&#061; 0) { \/\/ \u5b50\u8fdb\u7a0b<br \/>\n        close(listen_sock); \/\/ \u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u5173\u95ed&#xff01;<br \/>\n        \/\/ \u5904\u7406echo\u4e1a\u52a1<br \/>\n        char buf[BUF_SIZE];<br \/>\n        ssize_t n &#061; read(conn_sock, buf, BUF_SIZE);<br \/>\n        if (n &gt; 0) {<br \/>\n            write(conn_sock, buf, n);<br \/>\n        }<br \/>\n        close(conn_sock); \/\/ \u5b50\u8fdb\u7a0b\u5904\u7406\u5b8c\u5173\u95ed\u8fde\u63a5<br \/>\n        exit(0); \/\/ \u5b50\u8fdb\u7a0b\u9000\u51fa<br \/>\n    } else { \/\/ \u7236\u8fdb\u7a0b<br \/>\n        close(conn_sock); \/\/ \u7236\u8fdb\u7a0b\u4e0d\u9700\u8981\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u5173\u95ed&#xff01;<br \/>\n    }<br \/>\n}<\/p>\n<h4>2.2 \u89e3\u51b3\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/h4>\n<p>\u591a\u8fdb\u7a0b\u6a21\u578b\u6709\u4e2a\u5751&#xff1a;\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u56de\u6536&#xff0c;\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u5360\u7528\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234111-6971641734806.png\" alt=\"image\" \/><\/p>\n<h5>\u65b9\u6848 1&#xff1a;\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff08;\u63a8\u8350&#xff09;<\/h5>\n<p>\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6\u4f1a\u7ed9\u7236\u8fdb\u7a0b\u53d1SIGCHLD\u4fe1\u53f7&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff0c;\u7528waitpid\u975e\u963b\u585e\u56de\u6536\u5b50\u8fdb\u7a0b&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;signal.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>\/\/ \u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff1a;\u56de\u6536\u50f5\u5c38\u8fdb\u7a0b<br \/>\nvoid handle_sigchld(int sig) {<br \/>\n    \/\/ WNOHANG&#xff1a;\u975e\u963b\u585e&#xff0c;\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u5c31\u56de\u6536&#xff0c;\u6ca1\u6709\u5c31\u8fd4\u56de<br \/>\n    while (waitpid(-1, NULL, WNOHANG) &gt; 0);<br \/>\n}<\/p>\n<p>\/\/ \u5728main\u51fd\u6570\u7684socket\u521b\u5efa\u540e&#xff0c;\u6ce8\u518c\u4fe1\u53f7<br \/>\nsignal(SIGCHLD, handle_sigchld);<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234111-6971641753b6b.png\" alt=\"image\" \/><\/p>\n<h5>\u65b9\u6848 2&#xff1a;\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b&#xff08;\u5b64\u513f\u8fdb\u7a0b&#xff09;<\/h5>\n<p>\u6838\u5fc3\u903b\u8f91&#xff1a;<\/p>\n<li>\u7236\u8fdb\u7a0b fork \u51fa\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u518d fork \u51fa\u5b59\u5b50\u8fdb\u7a0b&#xff0c;\u7136\u540e\u5b50\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa&#xff1b;<\/li>\n<li>\u5b59\u5b50\u8fdb\u7a0b\u53d8\u6210\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u7531\u7cfb\u7edf&#xff08;init\/systemd&#xff09;\u63a5\u7ba1&#xff0c;\u9000\u51fa\u540e\u81ea\u52a8\u56de\u6536&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u4e0d\u9700\u8981\u963b\u585e&#xff0c;\u4e5f\u4e0d\u7528\u5904\u7406\u4fe1\u53f7\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234111-697164177c4cc.png\" alt=\"image\" \/><\/p>\n<p>\u4ee3\u7801\u5b9e\u73b0&#xff08;\u4fee\u6539 fork \u90e8\u5206&#xff09;&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>pid_t pid &#061; fork();<br \/>\nif (pid &#061;&#061; -1) {<br \/>\n    perror(&#034;fork failed&#034;);<br \/>\n    close(conn_sock);<br \/>\n    continue;<br \/>\n} else if (pid &#061;&#061; 0) { \/\/ \u5b50\u8fdb\u7a0b<br \/>\n    pid_t grand_pid &#061; fork();<br \/>\n    if (grand_pid &#061;&#061; -1) {<br \/>\n        exit(1);<br \/>\n    } else if (grand_pid &#061;&#061; 0) { \/\/ \u5b59\u5b50\u8fdb\u7a0b<br \/>\n        close(listen_sock);<br \/>\n        \/\/ \u5904\u7406echo\u4e1a\u52a1<br \/>\n        char buf[BUF_SIZE];<br \/>\n        ssize_t n &#061; read(conn_sock, buf, BUF_SIZE);<br \/>\n        if (n &gt; 0) write(conn_sock, buf, n);<br \/>\n        close(conn_sock);<br \/>\n        exit(0);<br \/>\n    } else { \/\/ \u5b50\u8fdb\u7a0b<br \/>\n        exit(0); \/\/ \u5b50\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa&#xff0c;\u5b59\u5b50\u8fdb\u7a0b\u53d8\u6210\u5b64\u513f<br \/>\n    }<br \/>\n} else { \/\/ \u7236\u8fdb\u7a0b<br \/>\n    close(conn_sock);<br \/>\n    \/\/ \u7236\u8fdb\u7a0b\u4e0d\u9700\u8981\u5904\u7406\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u56e0\u4e3a\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa\u4e86<br \/>\n}<\/p>\n<h4>2.3 \u591a\u8fdb\u7a0b\u6a21\u578b\u7684\u5c0f\u7ec6\u8282&#xff1a;\u83b7\u53d6\u5ba2\u6237\u7aef IP<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234111-69716417d0dab.png\" alt=\"image\" \/><\/p>\n<p>\u901a\u8fc7accept\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570client_addr&#xff0c;\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5ba2\u6237\u7aef\u7684 IP \u548c\u7aef\u53e3&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\/\/ accept\u540e\u7684\u4ee3\u7801<br \/>\nchar client_ip[INET_ADDRSTRLEN];<br \/>\ninet_ntop(AF_INET, &amp;client_addr.sin_addr, client_ip, INET_ADDRSTRLEN);<br \/>\nuint16_t client_port &#061; ntohs(client_addr.sin_port);<br \/>\nprintf(&#034;Client connected: %s:%d\\\\n&#034;, client_ip, client_port);<\/p>\n<h4>2.4 \u591a\u8fdb\u7a0b\u6a21\u578b\u7684\u75db\u70b9<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234111-69716417f2ff2.png\" alt=\"image\" \/><\/p>\n<p>\u591a\u8fdb\u7a0b\u867d\u7136\u89e3\u51b3\u4e86\u5e76\u53d1\u95ee\u9898&#xff0c;\u4f46\u7f3a\u70b9\u5f88\u660e\u663e&#xff1a;<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\u5927&#xff1b;<\/li>\n<li>\u6bcf\u4e2a\u8fdb\u7a0b\u5360\u7528\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4&#xff0c;\u8fde\u63a5\u6570\u591a\u4e86\u4f1a\u5bfc\u81f4\u670d\u52a1\u5668\u5185\u5b58\u3001CPU \u538b\u529b\u5267\u589e&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u95f4\u901a\u4fe1\u590d\u6742&#xff08;\u5982\u679c\u9700\u8981\u5171\u4eab\u6570\u636e&#xff09;\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001\u5e76\u53d1\u7bc7 2&#xff1a;\u591a\u7ebf\u7a0b\u7248\u672c\u670d\u52a1\u5668<\/h3>\n<p>\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u8fdb\u7a0b&#xff0c;\u521b\u5efa \/ \u9500\u6bc1\u5f00\u9500\u8fdc\u5c0f\u4e8e\u8fdb\u7a0b&#xff0c;\u4e14\u7ebf\u7a0b\u95f4\u5171\u4eab\u8fdb\u7a0b\u8d44\u6e90&#xff08;\u5982\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u5185\u5b58&#xff09;&#xff0c;\u9002\u5408\u9ad8\u5e76\u53d1\u573a\u666f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234112-6971641832022.png\" alt=\"image\" \/><\/p>\n<h4>3.1 \u591a\u7ebf\u7a0b\u6838\u5fc3\u903b\u8f91\u4e0e\u4ee3\u7801<\/h4>\n<p>\u6838\u5fc3\u601d\u8def&#xff1a;<\/p>\n<li>\u7236\u8fdb\u7a0b&#xff08;\u4e3b\u7ebf\u7a0b&#xff09;\u5faa\u73afaccept\u83b7\u53d6conn_sock&#xff1b;<\/li>\n<li>\u6bcf\u6b21\u62ff\u5230conn_sock\u540e&#xff0c;\u521b\u5efa\u65b0\u7ebf\u7a0b\u5904\u7406\u4e1a\u52a1&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u5904\u7406\u5b8c\u4e1a\u52a1\u540e\u5173\u95edconn_sock\u5e76\u9000\u51fa\u3002<\/li>\n<p>\u6838\u5fc3\u4ee3\u7801&#xff08;\u66ff\u6362\u591a\u8fdb\u7a0b\u7684 fork \u903b\u8f91&#xff09;&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u5904\u7406\u51fd\u6570&#xff08;\u6ce8\u610f\u53c2\u6570\u5fc5\u987b\u662fvoid*\u7c7b\u578b&#xff09;<br \/>\nvoid* echo_handler(void* arg) {<br \/>\n    int conn_sock &#061; *(int*)arg;<br \/>\n    free(arg); \/\/ \u91ca\u653e\u4f20\u53c2\u7684\u5185\u5b58<\/p>\n<p>    \/\/ \u5206\u79bb\u7ebf\u7a0b&#xff1a;\u7ebf\u7a0b\u9000\u51fa\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90&#xff0c;\u4e0d\u9700\u8981pthread_join<br \/>\n    pthread_detach(pthread_self());<\/p>\n<p>    \/\/ \u5904\u7406echo\u4e1a\u52a1<br \/>\n    char buf[BUF_SIZE];<br \/>\n    ssize_t n &#061; read(conn_sock, buf, BUF_SIZE);<br \/>\n    if (n &gt; 0) {<br \/>\n        write(conn_sock, buf, n);<br \/>\n    }<\/p>\n<p>    close(conn_sock);<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\/\/ accept\u540e\u7684\u903b\u8f91<br \/>\nwhile (1) {<br \/>\n    struct sockaddr_in client_addr;<br \/>\n    socklen_t client_addr_len &#061; sizeof(client_addr);<br \/>\n    int conn_sock &#061; accept(listen_sock, (struct sockaddr*)&amp;client_addr, &amp;client_addr_len);<br \/>\n    if (conn_sock &#061;&#061; -1) {<br \/>\n        perror(&#034;accept failed&#034;);<br \/>\n        continue;<br \/>\n    }<\/p>\n<p>    \/\/ \u4e3a\u7ebf\u7a0b\u4f20\u53c2&#xff08;\u4e0d\u80fd\u76f4\u63a5\u4f20\u6808\u53d8\u91cf&#xff0c;\u8981\u7528\u5806&#xff09;<br \/>\n    int* p_conn_sock &#061; malloc(sizeof(int));<br \/>\n    *p_conn_sock &#061; conn_sock;<\/p>\n<p>    \/\/ \u521b\u5efa\u7ebf\u7a0b<br \/>\n    pthread_t tid;<br \/>\n    if (pthread_create(&amp;tid, NULL, echo_handler, p_conn_sock) !&#061; 0) {<br \/>\n        perror(&#034;pthread_create failed&#034;);<br \/>\n        free(p_conn_sock);<br \/>\n        close(conn_sock);<br \/>\n    }<br \/>\n}<\/p>\n<h4>3.2 \u7ebf\u7a0b\u4e0e\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e24\u4e2a\u5173\u952e\u95ee\u9898<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234112-697164185eec1.png\" alt=\"image\" \/><\/p>\n<h5>\u95ee\u9898 1&#xff1a;\u8fdb\u7a0b\u6253\u5f00\u7684 FD&#xff0c;\u7ebf\u7a0b\u80fd\u770b\u5230\u5417&#xff1f;&#xff08;\u7b54\u6848&#xff1a;1&#xff09;<\/h5>\n<p>\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;FD&#xff09;\u662f\u8fdb\u7a0b\u7ea7\u8d44\u6e90&#xff0c;\u6240\u6709\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u4e2a\u8fdb\u7a0b\u7684 FD \u8868\u3002\u6bd4\u5982\u4e3b\u7ebf\u7a0b\u6253\u5f00\u7684listen_sock&#xff0c;\u6240\u6709\u5b50\u7ebf\u7a0b\u90fd\u80fd\u770b\u5230&#xff1b;\u5b50\u7ebf\u7a0b\u521b\u5efa\u7684conn_sock&#xff0c;\u4e3b\u7ebf\u7a0b\u4e5f\u80fd\u64cd\u4f5c\u3002<\/p>\n<h5>\u95ee\u9898 2&#xff1a;\u7ebf\u7a0b\u6562\u4e0d\u6562\u5173\u95ed\u81ea\u5df1\u4e0d\u9700\u8981\u7684 FD&#xff1f;&#xff08;\u7b54\u6848&#xff1a;\u6562&#xff0c;\u4f46\u8981\u8c28\u614e&#xff09;<\/h5>\n<ul>\n<li>\u53ef\u4ee5\u5173\u95ed&#xff1a;\u6bd4\u5982\u5b50\u7ebf\u7a0b\u4e0d\u9700\u8981listen_sock&#xff0c;\u53ef\u4ee5\u5173\u95ed&#xff0c;\u4e0d\u5f71\u54cd\u4e3b\u7ebf\u7a0b\u7684listen_sock&#xff08;FD \u7684\u5f15\u7528\u8ba1\u6570\u51cf 1&#xff0c;\u53ea\u6709\u5f15\u7528\u8ba1\u6570\u4e3a 0 \u65f6\u624d\u771f\u6b63\u5173\u95ed&#xff09;&#xff1b;<\/li>\n<li>\u8c28\u614e\u70b9&#xff1a;\u4e0d\u8981\u5173\u95ed\u5176\u4ed6\u7ebf\u7a0b\u8fd8\u5728\u4f7f\u7528\u7684 FD&#xff01;\u6bd4\u5982\u4e3b\u7ebf\u7a0b\u8fd8\u5728\u5904\u7406conn_sock&#xff0c;\u5b50\u7ebf\u7a0b\u5982\u679c\u5173\u95ed\u8fd9\u4e2a FD&#xff0c;\u4f1a\u5bfc\u81f4\u4e3b\u7ebf\u7a0b\u7684 IO \u64cd\u4f5c\u5931\u8d25\u3002<\/li>\n<\/ul>\n<h4>3.3 \u77ed\u670d\u52a1 vs \u957f\u670d\u52a1<\/h4>\n<p>\u591a\u7ebf\u7a0b\u6a21\u578b\u9002\u914d\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u77ed\u670d\u52a1&#xff1a;\u5355\u6b21\u8bf7\u6c42 &#8211; \u54cd\u5e94\u5c31\u65ad\u5f00&#xff08;\u5982 echo \u670d\u52a1\u5668&#xff09;&#xff0c;\u7ebf\u7a0b\u521b\u5efa \/ \u9500\u6bc1\u7684\u5f00\u9500\u53ef\u4ee5\u63a5\u53d7&#xff1b;<\/li>\n<li>\u957f\u670d\u52a1&#xff1a;\u8fde\u63a5\u4fdd\u6301&#xff0c;\u6301\u7eed\u4ea4\u4e92&#xff08;\u5982\u62a2\u7968\u3001\u7ffb\u8bd1\u670d\u52a1&#xff09;&#xff0c;\u7ebf\u7a0b\u4f1a\u957f\u671f\u5b58\u5728&#xff0c;\u66f4\u80fd\u4f53\u73b0\u7ebf\u7a0b\u7684\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<h3>\u56db\u3001\u5e76\u53d1\u7bc7 3&#xff1a;\u7ebf\u7a0b\u6c60\u7248\u672c\u670d\u52a1\u5668<\/h3>\n<p>\u591a\u7ebf\u7a0b\u867d\u7136\u6bd4\u591a\u8fdb\u7a0b\u9ad8\u6548&#xff0c;\u4f46\u5982\u679c\u8fde\u63a5\u6570\u66b4\u589e&#xff08;\u6bd4\u5982\u4e0a\u4e07&#xff09;&#xff0c;\u9891\u7e41\u521b\u5efa \/ \u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u4f9d\u7136\u5f88\u5927\u3002\u7ebf\u7a0b\u6c60\u5c31\u662f\u63d0\u524d\u521b\u5efa\u4e00\u6279\u7ebf\u7a0b&#xff0c;\u653e\u5728\u6c60\u5b50\u91cc&#xff0c;\u6709\u8fde\u63a5\u5c31\u5206\u914d\u7ebf\u7a0b\u5904\u7406&#xff0c;\u5904\u7406\u5b8c\u7ebf\u7a0b\u56de\u5230\u6c60\u5b50&#xff0c;\u907f\u514d\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234112-69716418c4ac0.png\" alt=\"image\" \/><\/p>\n<h4>4.1 \u7ebf\u7a0b\u6c60\u6838\u5fc3\u8bbe\u8ba1<\/h4>\n<p>\u7ebf\u7a0b\u6c60\u7684\u6838\u5fc3\u662f \u201c\u4efb\u52a1\u961f\u5217 &#043; \u4e92\u65a5\u9501 &#043; \u6761\u4ef6\u53d8\u91cf\u201d&#xff1a;<\/p>\n<li>\u63d0\u524d\u521b\u5efa N \u4e2a\u7ebf\u7a0b&#xff0c;\u7ebf\u7a0b\u542f\u52a8\u540e\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf\u4e0a&#xff1b;<\/li>\n<li>\u4e3b\u7ebf\u7a0baccept\u62ff\u5230conn_sock\u540e&#xff0c;\u628aconn_sock\u5c01\u88c5\u6210\u4efb\u52a1&#xff0c;\u52a0\u5165\u4efb\u52a1\u961f\u5217&#xff1b;<\/li>\n<li>\u53d1\u9001\u6761\u4ef6\u53d8\u91cf\u4fe1\u53f7&#xff0c;\u5524\u9192\u4e00\u4e2a\u7a7a\u95f2\u7ebf\u7a0b\u5904\u7406\u4efb\u52a1&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u5904\u7406\u5b8c\u4efb\u52a1\u540e&#xff0c;\u56de\u5230\u963b\u585e\u72b6\u6001&#xff0c;\u7b49\u5f85\u4e0b\u4e00\u4e2a\u4efb\u52a1\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234112-69716418ef152.png\" alt=\"image\" \/><\/p>\n<h4>4.2 \u7ebf\u7a0b\u6c60\u6838\u5fc3\u4ee3\u7801&#xff08;\u7b80\u5316\u7248&#xff09;<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>#define THREAD_POOL_SIZE 4 \/\/ \u7ebf\u7a0b\u6c60\u5927\u5c0f<br \/>\n#define BUF_SIZE 1024<br \/>\n#define SERVER_PORT 8080<\/p>\n<p>\/\/ \u4efb\u52a1\u7ed3\u6784\u4f53&#xff1a;\u5b58\u653e\u8fde\u63a5\u5957\u63a5\u5b57<br \/>\ntypedef struct {<br \/>\n    int conn_sock;<br \/>\n} Task;<\/p>\n<p>\/\/ \u4efb\u52a1\u961f\u5217<br \/>\nTask task_queue[1024];<br \/>\nint queue_head &#061; 0;<br \/>\nint queue_tail &#061; 0;<\/p>\n<p>\/\/ \u540c\u6b65\u673a\u5236<br \/>\npthread_mutex_t queue_mutex &#061; PTHREAD_MUTEX_INITIALIZER;<br \/>\npthread_cond_t queue_cond &#061; PTHREAD_COND_INITIALIZER;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u5904\u7406\u51fd\u6570<br \/>\nvoid* thread_worker(void* arg) {<br \/>\n    while (1) {<br \/>\n        \/\/ \u52a0\u9501<br \/>\n        pthread_mutex_lock(&amp;queue_mutex);<\/p>\n<p>        \/\/ \u4efb\u52a1\u961f\u5217\u4e3a\u7a7a&#xff0c;\u963b\u585e\u7b49\u5f85<br \/>\n        while (queue_head &#061;&#061; queue_tail) {<br \/>\n            pthread_cond_wait(&amp;queue_cond, &amp;queue_mutex);<br \/>\n        }<\/p>\n<p>        \/\/ \u53d6\u51fa\u4efb\u52a1<br \/>\n        Task task &#061; task_queue[queue_head];<br \/>\n        queue_head &#061; (queue_head &#043; 1) % 1024;<\/p>\n<p>        \/\/ \u89e3\u9501<br \/>\n        pthread_mutex_unlock(&amp;queue_mutex);<\/p>\n<p>        \/\/ \u5904\u7406echo\u4e1a\u52a1<br \/>\n        char buf[BUF_SIZE];<br \/>\n        ssize_t n &#061; read(task.conn_sock, buf, BUF_SIZE);<br \/>\n        if (n &gt; 0) {<br \/>\n            write(task.conn_sock, buf, n);<br \/>\n        }<br \/>\n        close(task.conn_sock);<br \/>\n    }<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u7ebf\u7a0b\u6c60<br \/>\nvoid init_thread_pool() {<br \/>\n    pthread_t tid;<br \/>\n    for (int i &#061; 0; i &lt; THREAD_POOL_SIZE; i&#043;&#043;) {<br \/>\n        pthread_create(&amp;tid, NULL, thread_worker, NULL);<br \/>\n        pthread_detach(tid);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6dfb\u52a0\u4efb\u52a1\u5230\u961f\u5217<br \/>\nvoid add_task(int conn_sock) {<br \/>\n    pthread_mutex_lock(&amp;queue_mutex);<\/p>\n<p>    \/\/ \u653e\u5165\u4efb\u52a1\u961f\u5217<br \/>\n    task_queue[queue_tail].conn_sock &#061; conn_sock;<br \/>\n    queue_tail &#061; (queue_tail &#043; 1) % 1024;<\/p>\n<p>    \/\/ \u53d1\u9001\u4fe1\u53f7\u5524\u9192\u7ebf\u7a0b<br \/>\n    pthread_cond_signal(&amp;queue_cond);<\/p>\n<p>    pthread_mutex_unlock(&amp;queue_mutex);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521d\u59cb\u5316\u7ebf\u7a0b\u6c60<br \/>\n    init_thread_pool();<\/p>\n<p>    \/\/ \u521b\u5efa\u76d1\u542csocket&#xff08;\u548c\u57fa\u7840\u7248\u4e00\u6837&#xff09;<br \/>\n    int listen_sock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    struct sockaddr_in server_addr;<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(SERVER_PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    bind(listen_sock, (struct sockaddr*)&amp;server_addr, sizeof(server_addr));<br \/>\n    listen(listen_sock, 5);<\/p>\n<p>    \/\/ \u4e3b\u7ebf\u7a0b\u5faa\u73afaccept<br \/>\n    while (1) {<br \/>\n        struct sockaddr_in client_addr;<br \/>\n        socklen_t client_addr_len &#061; sizeof(client_addr);<br \/>\n        int conn_sock &#061; accept(listen_sock, (struct sockaddr*)&amp;client_addr, &amp;client_addr_len);<br \/>\n        if (conn_sock &#061;&#061; -1) continue;<\/p>\n<p>        \/\/ \u6dfb\u52a0\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60<br \/>\n        add_task(conn_sock);<br \/>\n    }<\/p>\n<p>    close(listen_sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234113-697164195bdcc.png\" alt=\"image\" \/><\/p>\n<p>\u7ebf\u7a0b\u6c60\u7684\u4f18\u52bf\u5728\u4e8e \u201c\u89e3\u8026\u201d&#xff1a;<\/p>\n<ul>\n<li>\u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23 \u201c\u63a5\u8fde\u63a5\u201d&#xff0c;\u4e0d\u5904\u7406\u4e1a\u52a1&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u6c60\u53ea\u8d1f\u8d23 \u201c\u5904\u7406\u4e1a\u52a1\u201d&#xff0c;\u4e0d\u7528\u5173\u5fc3\u8fde\u63a5\u600e\u4e48\u6765&#xff1b;<\/li>\n<li>\u4e0a\u5c42\u4e1a\u52a1\u903b\u8f91&#xff08;\u6bd4\u5982\u628a echo \u6539\u6210\u7ffb\u8bd1\u3001\u62a2\u7968&#xff09;\u53ea\u9700\u8981\u4fee\u6539thread_worker\u91cc\u7684\u5904\u7406\u903b\u8f91\u5373\u53ef&#xff0c;\u4e0d\u7528\u52a8\u5e95\u5c42\u7684\u8fde\u63a5\u903b\u8f91\u3002<\/li>\n<\/ul>\n<h3>\u4e94\u3001\u6269\u5c55\u7bc7&#xff1a;\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u670d\u52a1\u5668&#xff08;\u9644\u5b89\u5168\u9632\u62a4&#xff09;<\/h3>\n<p>\u628a echo \u7684\u903b\u8f91\u6539\u6210 \u201c\u5ba2\u6237\u7aef\u53d1\u547d\u4ee4&#xff0c;\u670d\u52a1\u5668\u6267\u884c\u5e76\u8fd4\u56de\u7ed3\u679c\u201d&#xff0c;\u662f\u5f88\u5e38\u89c1\u7684\u540e\u7aef\u9700\u6c42&#xff0c;\u4f46\u8fd9\u91cc\u6709\u5de8\u5927\u7684\u5b89\u5168\u98ce\u9669&#xff0c;\u5fc5\u987b\u505a\u9632\u62a4\u3002<\/p>\n<h4>5.1 \u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u6838\u5fc3\u903b\u8f91<\/h4>\n<p>\u6838\u5fc3\u601d\u8def&#xff1a;\u5ba2\u6237\u7aef\u53d1\u9001\u547d\u4ee4\u5b57\u7b26\u4e32&#xff08;\u5982ls\u3001pwd&#xff09;&#xff0c;\u670d\u52a1\u5668\u7528popen\u6267\u884c\u547d\u4ee4&#xff0c;\u8bfb\u53d6\u6267\u884c\u7ed3\u679c\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u6838\u5fc3\u4ee3\u7801&#xff08;\u66ff\u6362\u7ebf\u7a0b\u6c60\u7684 echo \u5904\u7406\u903b\u8f91&#xff09;&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\/\/ \u66ff\u6362thread_worker\u91cc\u7684echo\u903b\u8f91<br \/>\nchar buf[BUF_SIZE];<br \/>\nssize_t n &#061; read(task.conn_sock, buf, BUF_SIZE);<br \/>\nif (n &gt; 0) {<br \/>\n    buf[n-1] &#061; &#039;\\\\0&#039;; \/\/ \u53bb\u6389\u6362\u884c\u7b26<\/p>\n<p>    \/\/ \u6267\u884c\u547d\u4ee4\u5e76\u8bfb\u53d6\u7ed3\u679c<br \/>\n    FILE* fp &#061; popen(buf, &#034;r&#034;);<br \/>\n    if (fp &#061;&#061; NULL) {<br \/>\n        const char* err &#061; &#034;Command execute failed\\\\n&#034;;<br \/>\n        write(task.conn_sock, err, strlen(err));<br \/>\n        close(task.conn_sock);<br \/>\n        continue;<br \/>\n    }<\/p>\n<p>    \/\/ \u8bfb\u53d6\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u5e76\u8fd4\u56de<br \/>\n    char result[BUF_SIZE];<br \/>\n    while (fgets(result, BUF_SIZE, fp) !&#061; NULL) {<br \/>\n        write(task.conn_sock, result, strlen(result));<br \/>\n    }<br \/>\n    pclose(fp);<br \/>\n}<br \/>\nclose(task.conn_sock);<\/p>\n<h4>5.2 \u5b89\u5168\u9632\u62a4&#xff1a;\u767d\u540d\u5355\u673a\u5236<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234113-69716419d1ed1.png\" alt=\"image\" \/><\/p>\n<p>\u5982\u679c\u76f4\u63a5\u6267\u884c\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u547d\u4ee4&#xff0c;\u98ce\u9669\u6781\u5927 \u2014\u2014 \u6bd4\u5982\u5ba2\u6237\u7aef\u53d1rm -rf \/&#xff0c;\u670d\u52a1\u5668\u76f4\u63a5\u73a9\u5b8c\u3002\u6240\u4ee5\u5fc5\u987b\u505a\u767d\u540d\u5355&#xff1a;\u53ea\u5141\u8bb8\u6267\u884c\u6307\u5b9a\u7684\u5b89\u5168\u547d\u4ee4\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234114-6971641a7d4c8.png\" alt=\"image\" \/><\/p>\n<h4>5.3 \u767d\u540d\u5355\u6838\u5fc3\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\/\/ \u5b9a\u4e49\u767d\u540d\u5355\u547d\u4ee4<br \/>\nconst char* whitelist[] &#061; {&#034;ls&#034;, &#034;pwd&#034;, &#034;date&#034;, &#034;whoami&#034;, NULL};<\/p>\n<p>\/\/ \u68c0\u67e5\u547d\u4ee4\u662f\u5426\u5728\u767d\u540d\u5355\u4e2d<br \/>\nint is_in_whitelist(const char* cmd) {<br \/>\n    for (int i &#061; 0; whitelist[i] !&#061; NULL; i&#043;&#043;) {<br \/>\n        if (strcmp(cmd, whitelist[i]) &#061;&#061; 0) {<br \/>\n            return 1;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\/\/ \u5728\u6267\u884c\u547d\u4ee4\u524d\u52a0\u68c0\u67e5<br \/>\nif (n &gt; 0) {<br \/>\n    buf[n-1] &#061; &#039;\\\\0&#039;;<br \/>\n    \/\/ \u68c0\u67e5\u767d\u540d\u5355<br \/>\n    if (!is_in_whitelist(buf)) {<br \/>\n        const char* err &#061; &#034;Error: Command not allowed\\\\n&#034;;<br \/>\n        write(task.conn_sock, err, strlen(err));<br \/>\n        close(task.conn_sock);<br \/>\n        continue;<br \/>\n    }<br \/>\n    \/\/ \u6267\u884c\u767d\u540d\u5355\u5185\u7684\u547d\u4ee4<br \/>\n    FILE* fp &#061; popen(buf, &#034;r&#034;);<br \/>\n    \/\/ &#8230; \u540e\u7eed\u903b\u8f91\u4e0d\u53d8<br \/>\n}<\/p>\n<h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<p>\u672c\u6587\u4ece\u57fa\u7840\u7684 echo \u670d\u52a1\u5668\u51fa\u53d1&#xff0c;\u4e00\u6b65\u6b65\u4f18\u5316\u5e76\u53d1\u80fd\u529b&#xff08;\u591a\u8fdb\u7a0b\u2192\u591a\u7ebf\u7a0b\u2192\u7ebf\u7a0b\u6c60&#xff09;&#xff0c;\u6700\u540e\u6269\u5c55\u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u5e76\u8865\u5145\u5b89\u5168\u9632\u62a4&#xff0c;\u6838\u5fc3\u77e5\u8bc6\u70b9\u603b\u7ed3&#xff1a;<\/p>\n<li>\u57fa\u7840 socket \u7f16\u7a0b&#xff1a;\u670d\u52a1\u5668bind&#043;listen&#043;accept&#xff0c;\u5ba2\u6237\u7aefconnect&#xff0c;\u6ce8\u610f FD \u7684\u7ba1\u7406&#xff1b;<\/li>\n<li>\u5e76\u53d1\u4f18\u5316&#xff1a;\u591a\u8fdb\u7a0b\u89e3\u51b3\u57fa\u7840\u5e76\u53d1\u4f46\u5f00\u9500\u5927&#xff0c;\u591a\u7ebf\u7a0b\u8f7b\u91cf\u5316\u4f46\u9700\u6ce8\u610f FD \u5171\u4eab&#xff0c;\u7ebf\u7a0b\u6c60\u662f\u9ad8\u5e76\u53d1\u6700\u4f18\u89e3&#xff1b;<\/li>\n<li>\u5b89\u5168\u9632\u62a4&#xff1a;\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u5fc5\u987b\u52a0\u767d\u540d\u5355&#xff0c;\u9650\u5236\u53ef\u6267\u884c\u7684\u547d\u4ee4\u8303\u56f4&#xff0c;\u907f\u514d\u6076\u610f\u653b\u51fb\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234114-6971641aca94c.png\" alt=\"image\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234115-6971641b553f8.png\" alt=\"image\" \/><\/p>\n<p>\u6700\u540e\u8865\u5145\u4e24\u4e2a\u5b9e\u6218\u5c0f\u6280\u5de7&#xff1a;<\/p>\n<ul>\n<li>\u8c03\u8bd5\u65f6\u7528netstat -anp | grep \u7aef\u53e3\u67e5\u770b\u7aef\u53e3\u76d1\u542c\u72b6\u6001&#xff0c;\u786e\u8ba4\u670d\u52a1\u5668\u662f\u5426\u6b63\u5e38\u542f\u52a8&#xff1b;<\/li>\n<li>\u7528ps -ef | grep \u8fdb\u7a0b\u540d\u67e5\u770b\u8fdb\u7a0b \/ \u7ebf\u7a0b\u72b6\u6001&#xff0c;\u6392\u67e5\u50f5\u5c38\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u6cc4\u6f0f\u95ee\u9898\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u7684\u6838\u5fc3\u6280\u80fd&#xff0c;Linux \u4e0b\u7684 TCP \u670d\u52a1\u5668\u5f00\u53d1\u662f\u7ed5\u4e0d\u5f00\u7684\u77e5\u8bc6\u70b9\u3002\u672c\u6587\u5c06\u4ece\u57fa\u7840\u7684 socket \u7f16\u7a0b\u5165\u624b&#xff0c;\u4e00\u6b65\u6b65\u5b9e\u73b0 echo \u670d\u52a1\u5668&#xff0c;\u5e76\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u6c60\u4f18\u5316\u5e76\u53d1\u80fd\u529b&#xff0c;\u6700\u540e\u6269\u5c55\u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u573a\u666f\u5e76\u8865\u5145\u5b89\u5168\u9632\u62a4\u65b9\u6848&#xff0c;\u5168\u7a0b\u4ee5\u5b9e\u6218\u4ee3\u7801\u548c\u6838\u5fc3\u95ee\u9898\u4e3a\u6838\u5fc3\u5c55\u5f00\u3002<br \/>\n\u4e00\u3001\u57fa\u7840\u7bc7&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u80fd\u8dd1\u7684 echo \u670d\u52a1\u5668<br \/>\necho \u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91\u5f88\u7b80\u5355 \u2014\u2014 \u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31<\/p>\n","protected":false},"author":2,"featured_media":63632,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,205,43],"topic":[],"class_list":["post-63652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","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>\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \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\/63652.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u7684\u6838\u5fc3\u6280\u80fd&#xff0c;Linux \u4e0b\u7684 TCP \u670d\u52a1\u5668\u5f00\u53d1\u662f\u7ed5\u4e0d\u5f00\u7684\u77e5\u8bc6\u70b9\u3002\u672c\u6587\u5c06\u4ece\u57fa\u7840\u7684 socket \u7f16\u7a0b\u5165\u624b&#xff0c;\u4e00\u6b65\u6b65\u5b9e\u73b0 echo \u670d\u52a1\u5668&#xff0c;\u5e76\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u6c60\u4f18\u5316\u5e76\u53d1\u80fd\u529b&#xff0c;\u6700\u540e\u6269\u5c55\u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u573a\u666f\u5e76\u8865\u5145\u5b89\u5168\u9632\u62a4\u65b9\u6848&#xff0c;\u5168\u7a0b\u4ee5\u5b9e\u6218\u4ee3\u7801\u548c\u6838\u5fc3\u95ee\u9898\u4e3a\u6838\u5fc3\u5c55\u5f00\u3002 \u4e00\u3001\u57fa\u7840\u7bc7&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u80fd\u8dd1\u7684 echo \u670d\u52a1\u5668 echo \u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91\u5f88\u7b80\u5355 \u2014\u2014 \u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/63652.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-21T23:41:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234108-69716414e13e0.png\" \/>\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=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/63652.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/63652.html\",\"name\":\"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-21T23:41:16+00:00\",\"dateModified\":\"2026-01-21T23:41:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/63652.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/63652.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/63652.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5\"}]},{\"@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":"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \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\/63652.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u7684\u6838\u5fc3\u6280\u80fd&#xff0c;Linux \u4e0b\u7684 TCP \u670d\u52a1\u5668\u5f00\u53d1\u662f\u7ed5\u4e0d\u5f00\u7684\u77e5\u8bc6\u70b9\u3002\u672c\u6587\u5c06\u4ece\u57fa\u7840\u7684 socket \u7f16\u7a0b\u5165\u624b&#xff0c;\u4e00\u6b65\u6b65\u5b9e\u73b0 echo \u670d\u52a1\u5668&#xff0c;\u5e76\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u6c60\u4f18\u5316\u5e76\u53d1\u80fd\u529b&#xff0c;\u6700\u540e\u6269\u5c55\u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u573a\u666f\u5e76\u8865\u5145\u5b89\u5168\u9632\u62a4\u65b9\u6848&#xff0c;\u5168\u7a0b\u4ee5\u5b9e\u6218\u4ee3\u7801\u548c\u6838\u5fc3\u95ee\u9898\u4e3a\u6838\u5fc3\u5c55\u5f00\u3002 \u4e00\u3001\u57fa\u7840\u7bc7&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u80fd\u8dd1\u7684 echo \u670d\u52a1\u5668 echo \u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91\u5f88\u7b80\u5355 \u2014\u2014 \u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31","og_url":"https:\/\/www.wsisp.com\/helps\/63652.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-21T23:41:16+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260121234108-69716414e13e0.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/63652.html","url":"https:\/\/www.wsisp.com\/helps\/63652.html","name":"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-21T23:41:16+00:00","dateModified":"2026-01-21T23:41:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/63652.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/63652.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/63652.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011TCP\u534f\u8bae\u30102\u3011\uff1a \u4ece echo \u5230\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\uff1aLinux TCP \u670d\u52a1\u5668\u7684\u5e76\u53d1\u4e0e\u5b89\u5168\u5b9e\u8df5"}]},{"@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\/63652","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=63652"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/63652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/63632"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=63652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=63652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=63652"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=63652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}