{"id":15002,"date":"2025-04-18T21:42:14","date_gmt":"2025-04-18T13:42:14","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/15002.html"},"modified":"2025-04-18T21:42:14","modified_gmt":"2025-04-18T13:42:14","slug":"linux-tcp%e5%9b%9e%e6%98%be%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%9e%e7%8e%b0%ef%bc%88deepseek%e6%95%99%e6%88%91%e5%86%99%e4%bb%a3%e7%a0%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/15002.html","title":{"rendered":"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09"},"content":{"rendered":"<p>\u6211\u8ba9deepseek\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f&#xff0c;\u7ed3\u679c\u6211\u7adf\u7136\u6709\u597d\u591a\u770b\u4e0d\u660e\u767d\u7684\u6280\u5de7&#xff0c;\u6cea\u6d41\u6ee1\u9762\u4e86\u3002<\/p>\n<h2><\/h2>\n<h2>1 \u5199\u4e00\u4e2alinux\u7a0b\u5e8f&#xff0c;\u5b9e\u73b0\u4e00\u4e2atcp\u670d\u52a1\u5668&#xff0c;\u529f\u80fd\u662f\u5c06\u6536\u5230\u7684\u6570\u636e\u539f\u8def\u8fd4\u56de<\/h2>\n<p>\u5df2\u6df1\u5ea6\u601d\u8003&#xff08;\u7528\u65f6 95 \u79d2&#xff09;<\/p>\n<p>\u597d\u7684&#xff0c;\u6211\u73b0\u5728\u9700\u8981\u5199\u4e00\u4e2aLinux\u4e0b\u7684TCP\u670d\u52a1\u5668\u7a0b\u5e8f&#xff0c;\u529f\u80fd\u662f\u5c06\u6536\u5230\u7684\u6570\u636e\u539f\u8def\u8fd4\u56de\u3002\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u56de\u663e\u670d\u52a1\u5668&#xff0c;Echo Server\u3002\u6211\u4e4b\u524d\u6ca1\u5199\u8fc7\u8fd9\u6837\u7684\u7a0b\u5e8f&#xff0c;\u4f46\u6709\u4e00\u4e9bC\u8bed\u8a00\u548c\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6&#xff0c;\u6240\u4ee5\u5f97\u4ed4\u7ec6\u60f3\u60f3\u8be5\u600e\u4e48\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p>\u9996\u5148&#xff0c;\u6211\u5e94\u8be5\u56de\u5fc6\u4e00\u4e0bTCP\u670d\u52a1\u5668\u7684\u5de5\u4f5c\u6d41\u7a0b\u3002\u4e00\u822c\u6765\u8bf4&#xff0c;TCP\u670d\u52a1\u5668\u7684\u6b65\u9aa4\u5927\u6982\u662f\u8fd9\u6837\u7684&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u76d1\u542c\u8fde\u63a5&#xff0c;\u63a5\u53d7\u8fde\u63a5&#xff0c;\u7136\u540e\u5904\u7406\u6570\u636e\u3002\u5bf9\u5427&#xff1f;<\/p>\n<\/p>\n<p>\u90a3\u5177\u4f53\u5230\u6bcf\u4e2a\u6b65\u9aa4&#xff0c;\u5e94\u8be5\u600e\u4e48\u505a\u5462&#xff1f;\u9996\u5148&#xff0c;\u521b\u5efa\u5957\u63a5\u5b57\u3002\u4f7f\u7528socket()\u51fd\u6570&#xff0c;\u53c2\u6570\u5e94\u8be5\u662fAF_INET&#xff08;IPv4&#xff09;&#xff0c;SOCK_STREAM&#xff08;TCP&#xff09;&#xff0c;\u534f\u8bae\u53ef\u4ee5\u586b0&#xff0c;\u8ba9\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u3002\u7136\u540e\u9700\u8981\u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3\u5230\u5957\u63a5\u5b57&#xff0c;\u8fd9\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2asockaddr_in\u7ed3\u6784&#xff0c;\u5305\u62ec\u5730\u5740\u65cf\u3001\u7aef\u53e3\u53f7\u548cIP\u5730\u5740\u3002\u8fd9\u91cc\u670d\u52a1\u5668\u7684IP\u5730\u5740\u901a\u5e38\u7528INADDR_ANY&#xff0c;\u8868\u793a\u76d1\u542c\u6240\u6709\u53ef\u7528\u7684\u63a5\u53e3\u3002\u7ed1\u5b9a\u65f6\u7528bind()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p>\u63a5\u7740\u662f\u76d1\u542c&#xff0c;\u7528listen()\u51fd\u6570&#xff0c;\u8bbe\u7f6e\u7b49\u5f85\u8fde\u63a5\u961f\u5217\u7684\u6700\u5927\u957f\u5ea6\u3002\u7136\u540e\u8fdb\u5165\u5faa\u73af&#xff0c;\u7528accept()\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u3002accept()\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57&#xff0c;\u7528\u4e8e\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u3002\u8fd9\u65f6\u5019&#xff0c;\u670d\u52a1\u5668\u901a\u5e38\u4f1afork\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6216\u8005\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6765\u5904\u7406\u8fd9\u4e2a\u8fde\u63a5&#xff0c;\u6216\u8005\u4f7f\u7528\u975e\u963b\u585e\u7684\u65b9\u5f0f&#xff0c;\u4f46\u8fd9\u91cc\u53ef\u80fd\u7b80\u5355\u5904\u7406&#xff0c;\u9010\u4e2a\u5904\u7406\u8fde\u63a5&#xff1f;\u4e0d\u8fc7\u8fd9\u6837\u7684\u8bdd&#xff0c;\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002\u6216\u8005\u662f\u5426\u5e94\u8be5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u8005\u591a\u8fdb\u7a0b&#xff1f;\u6216\u8005\u53ef\u80fd\u4f8b\u5b50\u4e2d\u7528\u5355\u7ebf\u7a0b&#xff0c;\u9010\u4e2a\u5904\u7406&#xff0c;\u4f46\u8fd9\u6837\u53ef\u80fd\u4e0d\u591f\u5b9e\u7528\u3002\u4e0d\u8fc7\u5bf9\u4e8e\u7b80\u5355\u7684\u793a\u4f8b\u6765\u8bf4&#xff0c;\u53ef\u80fd\u53ef\u4ee5\u6682\u65f6\u4e0d\u8003\u8651\u5e76\u53d1&#xff0c;\u5148\u5904\u7406\u5355\u4e2a\u8fde\u63a5&#xff0c;\u7136\u540e\u518d\u4f18\u5316&#xff1f;<\/p>\n<\/p>\n<p>\u7136\u540e&#xff0c;\u5f53\u5ba2\u6237\u7aef\u8fde\u63a5\u540e&#xff0c;\u670d\u52a1\u5668\u9700\u8981\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e&#xff0c;\u7136\u540e\u5c06\u6570\u636e\u56de\u53d1\u56de\u53bb\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u662f\u5728\u4e00\u4e2a\u5faa\u73af\u91cc&#xff0c;recv()\u8bfb\u53d6\u6570\u636e&#xff0c;\u7136\u540esend()\u53d1\u9001\u56de\u53bb\u3002\u76f4\u5230\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5&#xff0c;\u8fd9\u65f6\u5019recv()\u8fd4\u56de0&#xff0c;\u6216\u8005\u51fa\u9519&#xff0c;\u7136\u540e\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p>\u90a3\u5177\u4f53\u5230\u4ee3\u7801\u7ed3\u6784&#xff0c;\u5927\u6982\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<\/p>\n<p>1. \u521b\u5efasocket&#xff1a;socket()<\/p>\n<p>2. \u8bbe\u7f6e\u5730\u5740\u7ed3\u6784&#xff1a;sockaddr_in&#xff0c;\u5305\u62ec\u7aef\u53e3\u53f7\u7b49\u3002<\/p>\n<p>3. bind()\u7ed1\u5b9a\u5730\u5740<\/p>\n<p>4. listen()\u5f00\u59cb\u76d1\u542c<\/p>\n<p>5. accept()\u63a5\u53d7\u8fde\u63a5&#xff0c;\u5f97\u5230\u65b0\u7684socket<\/p>\n<p>6. \u5faa\u73af&#xff1a;recv()\u63a5\u6536\u6570\u636e&#xff0c;send()\u53d1\u9001\u56de\u53bb<\/p>\n<p>7. \u5173\u95ed\u8fde\u63a5<\/p>\n<\/p>\n<p>\u4f46\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u662f&#xff0c;\u5982\u4f55\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef&#xff1f;\u6bd4\u5982&#xff0c;\u5f53\u4e00\u4e2a\u5ba2\u6237\u7aef\u5728\u5904\u7406\u7684\u65f6\u5019&#xff0c;\u5176\u4ed6\u5ba2\u6237\u7aef\u65e0\u6cd5\u8fde\u63a5\u3002\u6240\u4ee5\u53ef\u80fd\u5e94\u8be5\u6bcf\u63a5\u53d7\u5230\u4e00\u4e2a\u8fde\u63a5&#xff0c;\u5c31\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6216\u8005\u7ebf\u7a0b\u6765\u5904\u7406\u3002\u6bd4\u5982&#xff0c;\u4f7f\u7528fork()\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u5728\u5b50\u8fdb\u7a0b\u4e2d\u5904\u7406\u6570\u636e\u6536\u53d1&#xff0c;\u800c\u4e3b\u8fdb\u7a0b\u7ee7\u7eed\u7b49\u5f85\u5176\u4ed6\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p>\u8fd9\u6837&#xff0c;\u4e3b\u8fdb\u7a0b\u5728accept\u4e4b\u540efork&#xff0c;\u5b50\u8fdb\u7a0b\u5904\u7406\u5177\u4f53\u7684\u901a\u4fe1&#xff0c;\u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u7ee7\u7eed\u5faa\u73afaccept\u3002<\/p>\n<\/p>\n<p>\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f&#xff0c;\u5b50\u8fdb\u7a0b\u9700\u8981\u6b63\u786e\u5173\u95ed\u4e0d\u9700\u8981\u7684\u5957\u63a5\u5b57&#xff0c;\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u9732\u3002\u6b64\u5916&#xff0c;\u8fd8\u9700\u8981\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u95ee\u9898&#xff0c;\u901a\u8fc7signal(SIGCHLD, SIG_IGN)\u6765\u5ffd\u7565\u5b50\u8fdb\u7a0b\u7ed3\u675f\u4fe1\u53f7&#xff0c;\u6216\u8005\u4f7f\u7528waitpid()\u56de\u6536\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p>\u90a3\u73b0\u5728&#xff0c;\u6211\u9700\u8981\u628a\u8fd9\u4e9b\u6b65\u9aa4\u8f6c\u5316\u4e3aC\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p>\u9996\u5148&#xff0c;\u5934\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u5305\u62ec&#xff1a;<\/p>\n<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;string.h&gt;<\/p>\n<p>#include &lt;unistd.h&gt;<\/p>\n<p>#include &lt;sys\/types.h&gt;<\/p>\n<p>#include &lt;sys\/socket.h&gt;<\/p>\n<p>#include &lt;netinet\/in.h&gt;<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<\/p>\n<\/p>\n<p>\u7136\u540e&#xff0c;\u4e3b\u51fd\u6570\u7684\u7ed3\u6784&#xff1a;<\/p>\n<\/p>\n<p>int main() {<\/p>\n<p>int sockfd, newsockfd, portno;<\/p>\n<p>struct sockaddr_in serv_addr, cli_addr;<\/p>\n<p>socklen_t clilen;<\/p>\n<p>\/\/ \u521b\u5efasocket<\/p>\n<p>sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<p>if (sockfd &lt; 0) {<\/p>\n<p>perror(&#034;ERROR opening socket&#034;);<\/p>\n<p>exit(1);<\/p>\n<p>}<\/p>\n<p>\/\/ \u8bbe\u7f6eserv_addr\u7ed3\u6784<\/p>\n<p>bzero((char *) &amp;serv_addr, sizeof(serv_addr));<\/p>\n<p>portno &#061; 12345; \/\/ \u5047\u8bbe\u7aef\u53e3\u53f7\u662f12345<\/p>\n<p>serv_addr.sin_family &#061; AF_INET;<\/p>\n<p>serv_addr.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>serv_addr.sin_port &#061; htons(portno);<\/p>\n<p>\/\/ bind<\/p>\n<p>if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<\/p>\n<p>perror(&#034;ERROR on binding&#034;);<\/p>\n<p>exit(1);<\/p>\n<p>}<\/p>\n<p>\/\/ listen<\/p>\n<p>listen(sockfd, 5);<\/p>\n<p>clilen &#061; sizeof(cli_addr);<\/p>\n<p>while (1) {<\/p>\n<p>\/\/ accept<\/p>\n<p>newsockfd &#061; accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen);<\/p>\n<p>if (newsockfd &lt; 0) {<\/p>\n<p>perror(&#034;ERROR on accept&#034;);<\/p>\n<p>exit(1);<\/p>\n<p>}<\/p>\n<p>\/\/ fork\u5b50\u8fdb\u7a0b<\/p>\n<p>pid_t pid &#061; fork();<\/p>\n<p>if (pid &lt; 0) {<\/p>\n<p>perror(&#034;ERROR on fork&#034;);<\/p>\n<p>exit(1);<\/p>\n<p>}<\/p>\n<p>if (pid &#061;&#061; 0) { \/\/ \u5b50\u8fdb\u7a0b<\/p>\n<p>close(sockfd); \/\/ \u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542csocket<\/p>\n<p>\/\/ \u5904\u7406\u6570\u636e<\/p>\n<p>char buffer[256];<\/p>\n<p>int n;<\/p>\n<p>while (1) {<\/p>\n<p>n &#061; recv(newsockfd, buffer, 255, 0);<\/p>\n<p>if (n &lt; 0) {<\/p>\n<p>perror(&#034;ERROR reading from socket&#034;);<\/p>\n<p>break;<\/p>\n<p>} else if (n &#061;&#061; 0) {<\/p>\n<p>\/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>\/\/ \u53d1\u9001\u56de\u53bb<\/p>\n<p>n &#061; send(newsockfd, buffer, n, 0);<\/p>\n<p>if (n &lt; 0) {<\/p>\n<p>perror(&#034;ERROR writing to socket&#034;);<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>close(newsockfd);<\/p>\n<p>exit(0); \/\/ \u5b50\u8fdb\u7a0b\u7ed3\u675f<\/p>\n<p>} else { \/\/ \u7236\u8fdb\u7a0b<\/p>\n<p>close(newsockfd); \/\/ \u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>close(sockfd);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<\/p>\n<p>\u4e0d\u8fc7&#xff0c;\u8fd9\u91cc\u53ef\u80fd\u6709\u51e0\u4e2a\u95ee\u9898\u9700\u8981\u6ce8\u610f&#xff1a;<\/p>\n<\/p>\n<p>1. \u5b50\u8fdb\u7a0b\u7ed3\u675f\u540e\u4f1a\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u9700\u8981\u5904\u7406\u3002\u53ef\u4ee5\u901a\u8fc7signal(SIGCHLD, SIG_IGN)\u6765\u8ba9\u7cfb\u7edf\u81ea\u52a8\u56de\u6536&#xff0c;\u6216\u8005\u5728\u7236\u8fdb\u7a0b\u4e2d\u8c03\u7528waitpid\u3002<\/p>\n<\/p>\n<p>\u6240\u4ee5\u5e94\u8be5\u5728\u4e3b\u51fd\u6570\u5f00\u59cb\u65f6\u8bbe\u7f6e\u5ffd\u7565SIGCHLD\u4fe1\u53f7&#xff1a;<\/p>\n<\/p>\n<p>signal(SIGCHLD, SIG_IGN);<\/p>\n<\/p>\n<p>\u8fd9\u6837&#xff0c;\u5f53\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6&#xff0c;\u7236\u8fdb\u7a0b\u4e0d\u4f1a\u79ef\u7d2f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p>2. \u9519\u8bef\u5904\u7406\u9700\u8981\u66f4\u5b8c\u5584\u3002\u6bd4\u5982&#xff0c;accept\u53ef\u80fd\u88ab\u4fe1\u53f7\u4e2d\u65ad&#xff0c;\u6240\u4ee5\u9700\u8981\u8003\u8651EINTR\u7684\u60c5\u51b5&#xff1f;\u4e0d\u8fc7\u5bf9\u4e8e\u7b80\u5355\u793a\u4f8b\u53ef\u80fd\u53ef\u4ee5\u6682\u65f6\u5ffd\u7565\u3002<\/p>\n<\/p>\n<p>3. \u7aef\u53e3\u53f7\u53ef\u80fd\u88ab\u5360\u7528&#xff0c;\u6216\u8005\u6743\u9650\u95ee\u9898&#xff08;\u6bd4\u5982\u7ed1\u5b9a\u5230\u4f4e\u4e8e1024\u7684\u7aef\u53e3\u9700\u8981root\u6743\u9650&#xff09;&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u9009\u4e00\u4e2a\u8f83\u9ad8\u7684\u7aef\u53e3&#xff0c;\u6bd4\u598212345\u3002<\/p>\n<\/p>\n<p>4. recv\u548csend\u7684\u8fd4\u56de\u503c\u5904\u7406&#xff1a;recv\u8fd4\u56de\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u8282\u6570&#xff0c;\u53ef\u80fd\u6bd4\u8bf7\u6c42\u7684\u5c11&#xff0c;\u6240\u4ee5\u9700\u8981\u5faa\u73af\u8bfb\u53d6&#xff1f;\u4e0d\u8fc7\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;\u53ef\u80fd\u4e0d\u9700\u8981&#xff0c;\u56e0\u4e3a\u5982\u679c\u6570\u636e\u91cf\u5927&#xff0c;\u53ef\u80fd\u9700\u8981\u591a\u6b21\u8bfb\u53d6&#xff0c;\u4f46\u56de\u663e\u670d\u52a1\u5668\u4e00\u822c\u53ef\u80fd\u4e0d\u9700\u8981&#xff0c;\u6bcf\u6b21\u8bfb\u53d6\u5c3d\u53ef\u80fd\u591a\u7684\u6570\u636e&#xff0c;\u7136\u540e\u53d1\u9001\u56de\u53bb\u5373\u53ef\u3002\u4f46\u8fd9\u91cc\u53ef\u80fd\u53ea\u662f\u7b80\u5355\u5904\u7406&#xff0c;\u6bcf\u6b21\u6700\u591a\u8bfb\u53d6255\u5b57\u8282&#xff0c;\u7136\u540e\u56de\u53d1\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u5904\u7406\u66f4\u5927\u7684\u6570\u636e\u548c\u66f4\u590d\u6742\u7684\u60c5\u51b5&#xff0c;\u4f46\u4f5c\u4e3a\u793a\u4f8b&#xff0c;\u53ef\u80fd\u8db3\u591f\u3002<\/p>\n<\/p>\n<p>\u53e6\u5916&#xff0c;\u5173\u4e8e\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u95ee\u9898&#xff0c;serv_addr\u7684\u7aef\u53e3\u53f7\u9700\u8981\u7528htons\u8f6c\u6362&#xff0c;\u5730\u5740\u7528INADDR_ANY\u5df2\u7ecf\u662f\u7f51\u7edc\u5b57\u8282\u5e8f\u5417&#xff1f;\u662f\u7684&#xff0c;\u56e0\u4e3aINADDR_ANY\u901a\u5e38\u662f0&#xff0c;\u6240\u4ee5\u65e0\u6240\u8c13&#xff0c;\u4f46\u5176\u4ed6\u60c5\u51b5\u4e0b\u9700\u8981inet_addr\u6216inet_pton\u6765\u5904\u7406IP\u5730\u5740\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p>\u73b0\u5728&#xff0c;\u6d4b\u8bd5\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u5426\u53ef\u884c\u3002\u7f16\u8bd1\u4e4b\u540e\u8fd0\u884c&#xff0c;\u7528telnet\u6216\u8005nc\u547d\u4ee4\u8fde\u63a5&#xff0c;\u53d1\u9001\u6570\u636e&#xff0c;\u770b\u662f\u5426\u56de\u663e\u3002<\/p>\n<\/p>\n<p>\u53ef\u80fd\u7684\u9519\u8bef\u70b9&#xff1a;<\/p>\n<\/p>\n<p>&#8211; \u5728fork\u4e4b\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u548c\u7236\u8fdb\u7a0b\u90fd\u8981\u6b63\u786e\u5173\u95ed\u4e0d\u9700\u8981\u7684\u5957\u63a5\u5b57\u3002\u6bd4\u5982\u5b50\u8fdb\u7a0b\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p>&#8211; \u5728\u5faa\u73af\u8bfb\u53d6\u6570\u636e\u65f6&#xff0c;\u5f53recv\u8fd4\u56de0\u65f6&#xff0c;\u8868\u793a\u5ba2\u6237\u7aef\u5173\u95ed\u4e86\u8fde\u63a5&#xff0c;\u8fd9\u65f6\u5019\u5e94\u8be5\u9000\u51fa\u5faa\u73af&#xff0c;\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u5b50\u8fdb\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p>\u8fd9\u6837\u5e94\u8be5\u53ef\u4ee5\u5904\u7406\u6bcf\u4e2a\u8fde\u63a5\u3002\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u540e&#xff0c;\u670d\u52a1\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u56de\u663e&#xff0c;\u4e3b\u8fdb\u7a0b\u7ee7\u7eed\u7b49\u5f85\u5176\u4ed6\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p>\u6b64\u5916&#xff0c;\u9700\u8981\u786e\u4fdd\u7f16\u8bd1\u65f6\u94fe\u63a5\u76f8\u5e94\u7684\u5e93&#xff0c;\u6bd4\u5982\u5728Linux\u4e0b\u7528gcc\u7f16\u8bd1&#xff0c;\u4e0d\u9700\u8981\u7279\u522b\u7684\u5e93&#xff0c;\u4f46\u8981\u6ce8\u610f\u4ee3\u7801\u4e2d\u7684\u5934\u6587\u4ef6\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<p>\u7efc\u4e0a&#xff0c;\u8fd9\u6837\u7684\u4ee3\u7801\u5e94\u8be5\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684TCP\u56de\u663e\u670d\u52a1\u5668\u3002\u53ef\u80fd\u8fd8\u6709\u4e00\u4e9b\u7ec6\u8282\u9700\u8981\u8c03\u6574&#xff0c;\u6bd4\u5982\u5904\u7406recv\u548csend\u7684\u8fd4\u56de\u503c\u662f\u5426\u6b63\u786e&#xff0c;\u6bd4\u5982n\u53ef\u80fd\u5c0f\u4e8e\u9884\u671f\u7684\u5b57\u8282\u6570&#xff0c;\u662f\u5426\u9700\u8981\u5faa\u73af\u53d1\u9001&#xff1f;\u4f8b\u5982&#xff0c;send\u53ef\u80fd\u6ca1\u6709\u4e00\u6b21\u6027\u53d1\u9001\u6240\u6709\u6570\u636e&#xff0c;\u53ef\u80fd\u9700\u8981\u591a\u6b21\u53d1\u9001\u3002\u4f46\u662f\u5728\u56de\u663e\u7684\u60c5\u51b5\u4e0b&#xff0c;\u53ef\u80fd\u53ef\u4ee5\u5ffd\u7565&#xff0c;\u56e0\u4e3a\u5982\u679c\u7f51\u7edc\u6b63\u5e38\u7684\u8bdd&#xff0c;\u4e00\u822csend\u4f1a\u53d1\u9001\u5168\u90e8\u6570\u636e\u3002\u4f46\u4e3a\u4e86\u5065\u58ee\u6027&#xff0c;\u53ef\u80fd\u9700\u8981\u5faa\u73af\u53d1\u9001&#xff0c;\u76f4\u5230\u6240\u6709\u6570\u636e\u90fd\u53d1\u9001\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728\u53d1\u9001\u90e8\u5206&#xff1a;<\/p>\n<\/p>\n<p>int bytes_sent &#061; 0;<\/p>\n<p>while (bytes_sent &lt; n) {<\/p>\n<p>int result &#061; send(newsockfd, buffer &#043; bytes_sent, n &#8211; bytes_sent, 0);<\/p>\n<p>if (result &lt; 0) {<\/p>\n<p>perror(&#034;ERROR writing to socket&#034;);<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>bytes_sent &#043;&#061; result;<\/p>\n<p>}<\/p>\n<\/p>\n<p>\u540c\u6837&#xff0c;recv\u53ef\u80fd\u4e5f\u9700\u8981\u591a\u6b21\u8bfb\u53d6&#xff0c;\u76f4\u5230\u6536\u5230\u6240\u6709\u9884\u671f\u7684\u6570\u636e&#xff1f;\u4e0d\u8fc7\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;\u56e0\u4e3a\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u53ef\u80fd\u4e0d\u5b9a\u957f&#xff0c;\u6240\u4ee5\u6bcf\u6b21\u8bfb\u53d6\u5c3d\u53ef\u80fd\u591a\u7684\u6570\u636e&#xff0c;\u7136\u540e\u7acb\u5373\u56de\u53d1\u3002\u6240\u4ee5\u53ef\u80fd\u4e0d\u9700\u8981\u5faa\u73af\u8bfb\u53d6&#xff0c;\u800c\u662f\u4e00\u6b21\u8bfb\u53d6&#xff0c;\u4e00\u6b21\u53d1\u9001\u3002\u4f46\u8fd9\u6837\u7684\u8bdd&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u91cf\u5f88\u5927&#xff0c;\u8d85\u8fc7\u7f13\u51b2\u533a&#xff0c;\u53ef\u80fd\u9700\u8981\u591a\u6b21\u8bfb\u53d6\u3002\u4e0d\u8fc7\u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4&#xff0c;\u53ef\u80fd\u53ef\u4ee5\u63a5\u53d7\u3002<\/p>\n<\/p>\n<p>\u603b\u7ed3&#xff0c;\u4ee3\u7801\u7684\u5927\u81f4\u7ed3\u6784\u662f\u6b63\u786e\u7684&#xff0c;\u53ef\u80fd\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7ec6\u8282&#xff0c;\u6bd4\u5982\u5904\u7406send\u548crecv\u7684\u8fd4\u56de\u503c&#xff0c;\u786e\u4fdd\u6240\u6709\u6570\u636e\u90fd\u88ab\u6b63\u786e\u53d1\u9001\u548c\u63a5\u6536\u3002\u6b64\u5916&#xff0c;\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u56de\u6536\u95ee\u9898&#xff0c;\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002\u901a\u8fc7\u8bbe\u7f6e\u5ffd\u7565SIGCHLD\u4fe1\u53f7&#xff0c;\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u3002<\/p>\n<\/p>\n<p>\u73b0\u5728&#xff0c;\u628a\u8fd9\u4e9b\u601d\u8def\u8f6c\u5316\u4e3a\u6700\u7ec8\u7684\u4ee3\u7801\u3002<\/p>\n<h4>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0TCP\u56de\u663e\u670d\u52a1\u5668\u7684C\u8bed\u8a00\u7a0b\u5e8f&#xff0c;\u8be5\u670d\u52a1\u5668\u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u539f\u6837\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef&#xff1a;<\/h4>\n<p>c<\/p>\n<p>\u590d\u5236<\/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;signal.h&gt;<br \/>\n#include &lt;sys\/types.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 BUFFER_SIZE 1024<br \/>\n#define PORT 12345<\/p>\n<p>void handle_client(int sockfd) {<br \/>\n    char buffer[BUFFER_SIZE];<br \/>\n    ssize_t bytes_received;<\/p>\n<p>    while ((bytes_received &#061; recv(sockfd, buffer, BUFFER_SIZE, 0)) &gt; 0) {<br \/>\n        ssize_t bytes_sent &#061; 0;<br \/>\n        while (bytes_sent &lt; bytes_received) {<br \/>\n            ssize_t result &#061; send(sockfd, buffer &#043; bytes_sent, bytes_received &#8211; bytes_sent, 0);<br \/>\n            if (result &lt; 0) {<br \/>\n                perror(&#034;send failed&#034;);<br \/>\n                close(sockfd);<br \/>\n                return;<br \/>\n            }<br \/>\n            bytes_sent &#043;&#061; result;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    if (bytes_received &lt; 0) {<br \/>\n        perror(&#034;recv failed&#034;);<br \/>\n    }<br \/>\n    close(sockfd);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_fd;<br \/>\n    struct sockaddr_in server_addr, client_addr;<br \/>\n    socklen_t client_len &#061; sizeof(client_addr);<\/p>\n<p>    \/\/ \u5ffd\u7565SIGCHLD\u4fe1\u53f7\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b<br \/>\n    signal(SIGCHLD, SIG_IGN);<\/p>\n<p>    \/\/ \u521b\u5efaTCP\u5957\u63a5\u5b57<br \/>\n    if ((server_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {<br \/>\n        perror(&#034;socket creation failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    server_addr.sin_port &#061; htons(PORT);<\/p>\n<p>    \/\/ \u7ed1\u5b9a\u5957\u63a5\u5b57<br \/>\n    if (bind(server_fd, (struct sockaddr*)&amp;server_addr, sizeof(server_addr)) &lt; 0) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        close(server_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u5f00\u59cb\u76d1\u542c<br \/>\n    if (listen(server_fd, 5) &lt; 0) {<br \/>\n        perror(&#034;listen failed&#034;);<br \/>\n        close(server_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    printf(&#034;Server listening on port %d&#8230;\\\\n&#034;, PORT);<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n        if ((client_fd &#061; accept(server_fd, (struct sockaddr*)&amp;client_addr, &amp;client_len)) &lt; 0) {<br \/>\n            perror(&#034;accept failed&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u663e\u793a\u5ba2\u6237\u7aef\u4fe1\u606f<br \/>\n        printf(&#034;New connection from %s:%d\\\\n&#034;,<br \/>\n               inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));<\/p>\n<p>        \/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u5ba2\u6237\u7aef<br \/>\n        pid_t pid &#061; fork();<br \/>\n        if (pid &lt; 0) {<br \/>\n            perror(&#034;fork failed&#034;);<br \/>\n            close(client_fd);<br \/>\n        } else if (pid &#061;&#061; 0) {    \/\/ \u5b50\u8fdb\u7a0b<br \/>\n            close(server_fd);     \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n            handle_client(client_fd);<br \/>\n            exit(EXIT_SUCCESS);<br \/>\n        } else {                  \/\/ \u7236\u8fdb\u7a0b<br \/>\n            close(client_fd);     \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h4>\u7a0b\u5e8f\u8bf4\u660e&#xff1a;<\/h4>\n<li>\n<p>\u521b\u5efa\u5957\u63a5\u5b57&#xff1a;\u4f7f\u7528socket()\u521b\u5efaTCP\u5957\u63a5\u5b57<\/p>\n<\/li>\n<li>\n<p>\u7ed1\u5b9a\u5730\u5740&#xff1a;\u7ed1\u5b9a\u5230\u6240\u6709\u63a5\u53e3&#xff08;INADDR_ANY&#xff09;\u7684\u6307\u5b9a\u7aef\u53e3<\/p>\n<\/li>\n<li>\n<p>\u76d1\u542c\u8fde\u63a5&#xff1a;\u4f7f\u7528listen()\u8fdb\u5165\u76d1\u542c\u72b6\u6001<\/p>\n<\/li>\n<li>\n<p>\u63a5\u53d7\u8fde\u63a5&#xff1a;\u901a\u8fc7accept()\u5faa\u73af\u63a5\u53d7\u65b0\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u591a\u8fdb\u7a0b\u5904\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7236\u8fdb\u7a0b\u63a5\u53d7\u8fde\u63a5\u540e\u521b\u5efa\u5b50\u8fdb\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\u901a\u4fe1&#xff08;handle_client\u51fd\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u7236\u8fdb\u7a0b\u7ee7\u7eed\u76d1\u542c\u65b0\u8fde\u63a5<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6570\u636e\u5904\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528recv()\u63a5\u6536\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528send()\u786e\u4fdd\u5b8c\u6574\u53d1\u9001\u6240\u6709\u63a5\u6536\u5230\u7684\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u81ea\u52a8\u6e05\u7406\u8d44\u6e90<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h4>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/h4>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<p>gcc echo_server.c -o echo_server<br \/>\n.\/echo_server <\/p>\n<h4>\u6d4b\u8bd5\u65b9\u6cd5&#xff1a;<\/h4>\n<p>\u4f7f\u7528telnet\u6216netcat\u8fde\u63a5\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<p>nc 127.0.0.1 12345 <\/p>\n<p>\u8f93\u5165\u4efb\u610f\u5185\u5bb9&#xff0c;\u670d\u52a1\u5668\u4f1a\u5c06\u76f8\u540c\u5185\u5bb9\u56de\u663e\u3002<\/p>\n<h4>\u7279\u70b9&#xff1a;<\/h4>\n<ul>\n<li>\n<p>\u652f\u6301\u591a\u5ba2\u6237\u7aef\u5e76\u53d1\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>\u5b8c\u6574\u5904\u7406TCP\u6570\u636e\u6d41&#xff08;\u5904\u7406\u77ed\u5305\u548c\u7c98\u5305\u95ee\u9898&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u8be6\u7ec6\u7684\u9519\u8bef\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>\u663e\u793a\u5ba2\u6237\u7aef\u8fde\u63a5\u4fe1\u606f<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u4f7f\u7528\u4e86\u4f20\u7edf\u7684\u591a\u8fdb\u7a0b\u6a21\u578b&#xff0c;\u9002\u5408\u6f14\u793a\u57fa\u672c\u7684TCP\u670d\u52a1\u5668\u5de5\u4f5c\u539f\u7406\u3002\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u53ef\u80fd\u4f1a\u4f7f\u7528\u66f4\u9ad8\u6548\u7684I\/O\u590d\u7528\u65b9\u5f0f&#xff08;\u5982epoll&#xff09;\u6216\u7ebf\u7a0b\u6c60\u6765\u5904\u7406\u5e76\u53d1\u8fde\u63a5\u3002<\/p>\n<h2>2 signal(SIGCHLD, SIG_IGN);\u4e3a\u4ec0\u4e48\u80fd\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b<\/h2>\n<p>\u5df2\u6df1\u5ea6\u601d\u8003&#xff08;\u7528\u65f6 27 \u79d2&#xff09;<\/p>\n<p>\u597d\u7684&#xff0c;\u7528\u6237\u95ee\u7684\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528signal(SIGCHLD, SIG_IGN);\u53ef\u4ee5\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002\u6211\u9700\u8981\u5148\u56de\u987e\u4e00\u4e0b\u50f5\u5c38\u8fdb\u7a0b\u7684\u76f8\u5173\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<p>\u9996\u5148&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u662f\u4ec0\u4e48&#xff1f;\u5f53\u5b50\u8fdb\u7a0b\u7ed3\u675f\u8fd0\u884c\u65f6&#xff0c;\u7236\u8fdb\u7a0b\u9700\u8981\u8c03\u7528wait\u6216waitpid\u6765\u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff0c;\u5426\u5219\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4ecd\u7136\u4fdd\u7559\u5728\u7cfb\u7edf\u4e2d&#xff0c;\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u8fd9\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39&#xff0c;\u5982\u679c\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u5b58\u5728&#xff0c;\u53ef\u80fd\u4f1a\u5f71\u54cd\u7cfb\u7edf\u6027\u80fd\u3002<\/p>\n<\/p>\n<p>\u90a3\u7528\u6237\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d&#xff0c;\u7236\u8fdb\u7a0b\u5728\u5904\u7406\u5b8c\u5ba2\u6237\u7aef\u8fde\u63a5\u540e&#xff0c;\u901a\u8fc7fork\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u5904\u7406\u5b8c\u5ba2\u6237\u7aef\u8bf7\u6c42\u540e\u9000\u51fa&#xff0c;\u8fd9\u65f6\u5019\u7236\u8fdb\u7a0b\u5982\u679c\u6ca1\u6709\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u7ec8\u6b62\u72b6\u6001&#xff0c;\u5c31\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p>\u901a\u5e38\u5904\u7406SIGCHLD\u4fe1\u53f7\u7684\u65b9\u6cd5\u662f\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528wait\u6216waitpid\u6765\u56de\u6536\u5b50\u8fdb\u7a0b\u3002\u4f46\u7528\u6237\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u662fsignal(SIGCHLD, SIG_IGN);&#xff0c;\u4e5f\u5c31\u662f\u5ffd\u7565SIGCHLD\u4fe1\u53f7\u3002\u8fd9\u91cc\u9700\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u53ef\u4ee5\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p>\u6839\u636e\u4e4b\u524d\u5b66\u7684\u77e5\u8bc6&#xff0c;\u5f53\u7236\u8fdb\u7a0b\u5ffd\u7565SIGCHLD\u4fe1\u53f7\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff0c;\u4e0d\u4f1a\u8ba9\u5b50\u8fdb\u7a0b\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u4fe1\u53f7\u88ab\u8bbe\u7f6e\u4e3aSIG_IGN\u65f6&#xff0c;\u5185\u6838\u4f1a\u5904\u7406\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u76f4\u63a5\u91ca\u653e\u76f8\u5173\u8d44\u6e90&#xff0c;\u4e0d\u9700\u8981\u7236\u8fdb\u7a0b\u4e3b\u52a8\u8c03\u7528wait\u3002\u8fd9\u53ef\u80fd\u662fPOSIX\u6807\u51c6\u4e2d\u7684\u884c\u4e3a&#xff0c;\u4f46\u9700\u8981\u786e\u8ba4\u4e0d\u540cUNIX\u7cfb\u7edf\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p>\u4e0d\u8fc7&#xff0c;\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u4e0d\u662f\u6240\u6709\u7cfb\u7edf\u90fd\u652f\u6301\u8fd9\u79cd\u884c\u4e3a\u3002\u6bd4\u5982&#xff0c;\u5728System V\u7684\u4f20\u7edf\u4e2d&#xff0c;\u5ffd\u7565SIGCHLD\u5e76\u4e0d\u80fd\u963b\u6b62\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f&#xff0c;\u5fc5\u987b\u663e\u5f0f\u5904\u7406\u3002\u4f46\u6839\u636e\u7528\u6237\u63d0\u4f9b\u7684\u4ee3\u7801\u662f\u5728Linux\u73af\u5883\u4e0b\u8fd0\u884c\u7684&#xff0c;\u800cLinux\u9075\u5faa\u7684\u662fBSD\u7684\u884c\u4e3a&#xff0c;\u5f53\u5ffd\u7565SIGCHLD\u65f6&#xff0c;\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u540e\u4f1a\u88ab\u81ea\u52a8\u56de\u6536&#xff0c;\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u56e0\u6b64&#xff0c;\u8fd9\u6837\u7684\u8bbe\u7f6e\u786e\u5b9e\u6709\u6548\u3002<\/p>\n<\/p>\n<p>\u8fd9\u6837\u5904\u7406\u7684\u597d\u5904\u662f\u4ee3\u7801\u66f4\u7b80\u6d01&#xff0c;\u4e0d\u9700\u8981\u5728\u7236\u8fdb\u7a0b\u4e2d\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6216\u8005\u8c03\u7528wait\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u6765\u8bf4&#xff0c;\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4f46\u9700\u8981\u6307\u51fa&#xff0c;\u8fd9\u79cd\u505a\u6cd5\u53ef\u80fd\u5728\u67d0\u4e9bUNIX\u53d8\u79cd\u4e2d\u4e0d\u53ef\u884c&#xff0c;\u4f46\u5728Linux\u4e0b\u662f\u6709\u6548\u7684\u3002<\/p>\n<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b&#xff0c;\u7528\u6237\u7684\u95ee\u9898\u6838\u5fc3\u5728\u4e8e\u7406\u89e3\u5ffd\u7565SIGCHLD\u4fe1\u53f7\u5982\u4f55\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002\u9700\u8981\u89e3\u91caSIGCHLD\u7684\u4f5c\u7528&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u7684\u6210\u56e0&#xff0c;\u4ee5\u53ca\u5ffd\u7565\u8be5\u4fe1\u53f7\u540e\u7cfb\u7edf\u5982\u4f55\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3002\u6b64\u5916&#xff0c;\u8fd8\u8981\u63d0\u5230\u4e0d\u540c\u7cfb\u7edf\u7684\u5dee\u5f02&#xff0c;\u786e\u4fdd\u7528\u6237\u77e5\u9053\u8fd9\u79cd\u65b9\u6cd5\u7684\u9002\u7528\u73af\u5883\u3002<\/p>\n<\/p>\n<p>\u5728\u56de\u7b54\u65f6&#xff0c;\u5e94\u8be5\u5206\u6b65\u9aa4\u89e3\u91ca&#xff0c;\u9996\u5148\u8bf4\u660e\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u539f\u56e0&#xff0c;\u7136\u540e\u8bf4\u660eSIGCHLD\u4fe1\u53f7\u7684\u4f5c\u7528&#xff0c;\u63a5\u7740\u89e3\u91ca\u5ffd\u7565\u8be5\u4fe1\u53f7\u65f6\u7cfb\u7edf\u7684\u884c\u4e3a&#xff0c;\u6700\u540e\u63d0\u53ca\u517c\u5bb9\u6027\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u8fd9\u6837\u7528\u6237\u80fd\u6e05\u6670\u5730\u7406\u89e3\u6574\u4e2a\u8fc7\u7a0b&#xff0c;\u5e76\u77e5\u9053\u5728\u4f55\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u5408\u9002\u3002<\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e2d&#xff0c;signal(SIGCHLD, SIG_IGN);\u00a0\u8fd9\u4e00\u884c\u4ee3\u7801\u80fd\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u7684\u539f\u56e0\u4e0eUNIX\u7cfb\u7edf\u7684\u8fdb\u7a0b\u7ba1\u7406\u673a\u5236\u5bc6\u5207\u76f8\u5173\u3002\u4ee5\u4e0b\u662f\u5206\u6b65\u89e3\u91ca&#xff1a;<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u50f5\u5c38\u8fdb\u7a0b\u7684\u6210\u56e0<\/h4>\n<li>\n<p>\u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b \u5f53\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u65f6&#xff0c;\u5b83\u4e0d\u4f1a\u7acb\u5373\u4ece\u7cfb\u7edf\u4e2d\u6d88\u5931\u3002 \u7236\u8fdb\u7a0b\u5fc5\u987b\u8c03\u7528\u00a0wait()\u00a0\u6216\u00a0waitpid()\u00a0\u6765\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3002 \u5982\u679c\u7236\u8fdb\u7a0b\u6ca1\u6709\u505a\u8fd9\u4ef6\u4e8b&#xff0c;\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26&#xff08;PID&#xff09;\u4f1a\u6b8b\u7559\u5728\u7cfb\u7edf\u4e2d&#xff0c;\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff08;Zombie Process&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u50f5\u5c38\u8fdb\u7a0b\u7684\u5371\u5bb3 \u5360\u7528\u7cfb\u7edf\u8d44\u6e90&#xff08;PID\u6709\u9650&#xff09;&#xff0c;\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u53ef\u80fd\u5bfc\u81f4\u65e0\u6cd5\u521b\u5efa\u65b0\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<hr \/>\n<h4>\u4e8c\u3001SIGCHLD\u00a0\u4fe1\u53f7\u7684\u4f5c\u7528<\/h4>\n<li>\n<p>\u4fe1\u53f7\u89e6\u53d1\u6761\u4ef6 \u5f53\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u65f6&#xff0c;\u5185\u6838\u4f1a\u5411\u7236\u8fdb\u7a0b\u53d1\u9001\u00a0SIGCHLD\u00a0\u4fe1\u53f7&#xff0c;\u901a\u77e5\u7236\u8fdb\u7a0b&#034;\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u4e86&#034;\u3002<\/p>\n<\/li>\n<li>\n<p>\u9ed8\u8ba4\u884c\u4e3a \u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;SIGCHLD\u00a0\u4fe1\u53f7\u7684\u9ed8\u8ba4\u5904\u7406\u65b9\u5f0f\u662f\u5ffd\u7565&#xff08;SIG_DFL&#xff09;&#xff0c;\u4f46\u4e0d\u4f1a\u81ea\u52a8\u56de\u6536\u50f5\u5c38\u8fdb\u7a0b\u3002 \u6b64\u65f6\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u4e3b\u52a8\u8c03\u7528\u00a0wait()&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u4ecd\u7136\u4f1a\u5b58\u5728\u3002<\/p>\n<\/li>\n<hr \/>\n<h4>\u4e09\u3001SIG_IGN\u00a0\u7684\u7279\u6b8a\u884c\u4e3a<\/h4>\n<li>\n<p>\u663e\u5f0f\u5ffd\u7565\u4fe1\u53f7 \u901a\u8fc7\u00a0signal(SIGCHLD, SIG_IGN);\u00a0\u663e\u5f0f\u544a\u77e5\u7cfb\u7edf&#xff1a;&#034;\u7236\u8fdb\u7a0b\u5b8c\u5168\u4e0d\u60f3\u5173\u5fc3\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#034;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5185\u6838\u7684\u4f18\u5316\u5904\u7406 \u5728Linux&#xff08;\u4ee5\u53ca\u5927\u591a\u6570\u73b0\u4ee3UNIX\u7cfb\u7edf&#xff09;\u4e2d&#xff0c;\u5f53\u8bbe\u7f6e\u4e86\u00a0SIGCHLD\u00a0\u7684\u5904\u7406\u65b9\u5f0f\u4e3a\u00a0SIG_IGN\u00a0\u65f6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5185\u6838\u4f1a\u7acb\u5373\u81ea\u52a8\u56de\u6536\u6240\u6709\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u518d\u4fdd\u7559\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u4e0d\u4f1a\u8fdb\u5165\u50f5\u5c38\u72b6\u6001<\/p>\n<\/li>\n<li>\n<p>\u7236\u8fdb\u7a0b\u4e0d\u9700\u8981&#xff08;\u4e5f\u65e0\u6cd5&#xff09;\u8c03\u7528\u00a0wait()\u00a0\u51fd\u6570<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u56db\u3001\u4e0e\u4f20\u7edf\u884c\u4e3a\u7684\u533a\u522b<\/h4>\n<li>\n<p>\u5386\u53f2\u5dee\u5f02<\/p>\n<ul>\n<li>\n<p>\u5728System V UNIX\u7cfb\u7edf\u4e2d&#xff0c;\u5373\u4f7f\u8bbe\u7f6e\u4e86\u00a0SIG_IGN&#xff0c;\u4ecd\u7136\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u4f46\u5728BSD\u6d3e\u751f\u7684\u7cfb\u7edf&#xff08;\u5305\u62ecLinux&#xff09;\u4e2d&#xff0c;SIG_IGN\u00a0\u4f1a\u89e6\u53d1\u81ea\u52a8\u56de\u6536\u884c\u4e3a<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>POSIX\u6807\u51c6 POSIX.1-2001\u660e\u786e\u89c4\u5b9a\u4e86\u8fd9\u79cd\u884c\u4e3a\u7684\u5408\u6cd5\u6027&#xff0c;\u56e0\u6b64\u73b0\u4ee3\u7cfb\u7edf\u90fd\u4f1a\u9075\u5b88\u6b64\u89c4\u8303\u3002<\/p>\n<\/li>\n<hr \/>\n<h4>\u4e94\u3001\u4ee3\u7801\u793a\u4f8b\u9a8c\u8bc1<\/h4>\n<p>c<\/p>\n<p>\u590d\u5236<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;signal.h&gt;<\/p>\n<p>int main() {<br \/>\n    signal(SIGCHLD, SIG_IGN);  \/\/ \u5173\u952e\u4ee3\u7801<\/p>\n<p>    pid_t pid &#061; fork();<br \/>\n    if (pid &#061;&#061; 0) {<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa<br \/>\n        printf(&#034;Child process %d exiting\\\\n&#034;, getpid());<br \/>\n        exit(0);<br \/>\n    } else {<br \/>\n        \/\/ \u7236\u8fdb\u7a0b\u4e0d\u8c03\u7528wait()<br \/>\n        printf(&#034;Parent process %d sleeping\\\\n&#034;, getpid());<br \/>\n        sleep(30);<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u89c2\u5bdf&#xff1a; \u4f7f\u7528\u00a0ps aux | grep Z\u00a0\u547d\u4ee4\u89c2\u5bdf\u8fdb\u7a0b\u72b6\u6001&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<hr \/>\n<h4>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<li>\n<p>\u4fe1\u606f\u4e22\u5931 \u4f7f\u7528\u00a0SIG_IGN\u00a0\u540e&#xff0c;\u7236\u8fdb\u7a0b\u5c06\u5b8c\u5168\u65e0\u6cd5\u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff08;\u8fd4\u56de\u7801\u7b49&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u79fb\u690d\u6027 \u867d\u7136\u73b0\u4ee3\u7cfb\u7edf\u666e\u904d\u652f\u6301\u8be5\u7279\u6027&#xff0c;\u4f46\u5728\u4e00\u4e9b\u65e7\u7248UNIX\u7cfb\u7edf\u4e0a\u53ef\u80fd\u4e0d\u9002\u7528\u3002 \u66f4\u6807\u51c6\u7684\u5199\u6cd5\u662f\u663e\u5f0f\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u5e76\u5728\u5176\u4e2d\u8c03\u7528\u00a0waitpid&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u590d\u5236<\/p>\n<p> void sigchld_handler(int sig) {<br \/>\n    while(waitpid(-1, NULL, WNOHANG) &gt; 0);<br \/>\n}<br \/>\nsignal(SIGCHLD, sigchld_handler); <\/li>\n<li>\n<p>\u4e0e\u00a0waitpid()\u00a0\u7684\u533a\u522b SIG_IGN\u00a0\u65b9\u5f0f\u9002\u5408&#034;\u4e0d\u5173\u5fc3\u5b50\u8fdb\u7a0b\u7ed3\u679c&#034;\u7684\u573a\u666f&#xff0c;\u800c\u9700\u8981\u6536\u96c6\u5b50\u8fdb\u7a0b\u4fe1\u606f\u65f6\u4ecd\u9700\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u3002<\/p>\n<\/li>\n<hr \/>\n<h4>\u603b\u7ed3<\/h4>\n<p>signal(SIGCHLD, SIG_IGN);\u00a0\u901a\u8fc7\u544a\u8bc9\u5185\u6838&#034;\u7236\u8fdb\u7a0b\u5b8c\u5168\u4e0d\u9700\u8981\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f&#034;&#xff0c;\u4f7f\u5f97\u5185\u6838\u81ea\u52a8\u56de\u6536\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b\u8d44\u6e90&#xff0c;\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u907f\u514d\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002\u8fd9\u79cd\u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7b80\u6d01\u9ad8\u6548&#xff08;\u65e0\u9700\u5faa\u73af\u8c03\u7528\u00a0wait()&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u9002\u5408\u4e0d\u9700\u8981\u6536\u96c6\u5b50\u8fdb\u7a0b\u72b6\u6001\u7684\u573a\u666f<\/p>\n<\/li>\n<li>\n<p>\u662f\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f\u4e4b\u4e00<\/p>\n<\/li>\n<\/ul>\n<h2>3 close(server_fd); \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u76d1\u542c\u5957\u63a5\u5b57 close(client_fd); \/\/ \u5173\u95ed\u4e0d\u9700\u8981\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 \u8fd9\u4e24\u884c\u53c8\u662f\u4ec0\u4e48\u610f\u601d<\/h2>\n<p>\u5df2\u6df1\u5ea6\u601d\u8003&#xff08;\u7528\u65f6 26 \u79d2&#xff09;<\/p>\n<p>\u597d\u7684&#xff0c;\u7528\u6237\u95ee\u7684\u662f\u5173\u4e8eclose(server_fd)\u548cclose(client_fd)\u8fd9\u4e24\u884c\u4ee3\u7801\u7684\u610f\u601d\u3002\u6211\u9700\u8981\u5148\u7406\u89e3\u7528\u6237\u7684\u95ee\u9898\u80cc\u666f\u3002\u7528\u6237\u4e4b\u524d\u95ee\u8fc7\u5173\u4e8eTCP\u670d\u52a1\u5668\u7684\u4ee3\u7801&#xff0c;\u73b0\u5728\u9488\u5bf9\u4ee3\u7801\u4e2d\u7684\u5177\u4f53\u4e24\u884c\u5173\u95ed\u5957\u63a5\u5b57\u7684\u64cd\u4f5c\u6709\u7591\u95ee\u3002\u7528\u6237\u53ef\u80fd\u662f\u5728\u5b66\u4e60\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u6216\u8005\u6b63\u5728\u7f16\u5199\u7c7b\u4f3c\u7684\u670d\u52a1\u5668\u7a0b\u5e8f&#xff0c;\u5bf9\u8d44\u6e90\u7ba1\u7406\u4e0d\u592a\u6e05\u695a\u3002<\/p>\n<\/p>\n<p>\u9996\u5148&#xff0c;\u6211\u8981\u56de\u987e\u4e4b\u524d\u7684\u5bf9\u8bdd\u3002\u7528\u6237\u4e4b\u524d\u5f97\u5230\u4e86\u4e00\u4e2a\u7528C\u5199\u7684TCP\u56de\u663e\u670d\u52a1\u5668\u4ee3\u7801&#xff0c;\u91cc\u9762\u5728\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u4e2d\u5206\u522b\u5173\u95ed\u4e86server_fd\u548cclient_fd\u3002\u7528\u6237\u73b0\u5728\u60f3\u77e5\u9053\u8fd9\u4e24\u884c\u4ee3\u7801\u7684\u5177\u4f53\u4f5c\u7528&#xff0c;\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u522b\u5173\u95ed\u8fd9\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<\/p>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u9700\u8981\u5206\u6790\u8fd9\u4e24\u884c\u4ee3\u7801\u7684\u4e0a\u4e0b\u6587\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d&#xff0c;\u5f53accept\u5230\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u540e&#xff0c;fork\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u8fd9\u4e2a\u5ba2\u6237\u7aef\u3002\u7236\u8fdb\u7a0b\u5728fork\u4e4b\u540e&#xff0c;\u7acb\u5373\u5173\u95edclient_fd&#xff0c;\u800c\u5b50\u8fdb\u7a0b\u5219\u5173\u95edserver_fd&#xff0c;\u7136\u540e\u5904\u7406client_fd\u3002<\/p>\n<\/p>\n<p>\u7528\u6237\u53ef\u80fd\u4e0d\u592a\u7406\u89e3\u4e3a\u4ec0\u4e48\u7236\u5b50\u8fdb\u7a0b\u9700\u8981\u5206\u522b\u5173\u95ed\u4e0d\u540c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5c24\u5176\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u5728fork\u4e4b\u540e\u7684\u884c\u4e3a\u3002\u8fd9\u91cc\u6d89\u53ca\u5230\u8fdb\u7a0b\u95f4\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7ee7\u627f\u4ee5\u53ca\u5f15\u7528\u8ba1\u6570\u7684\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p>\u6211\u9700\u8981\u89e3\u91ca\u6e05\u695a&#xff0c;\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u662f\u4e00\u4e2a\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u5f53fork\u65f6&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff0c;\u6240\u4ee5\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u5404\u81ea\u6301\u6709\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u526f\u672c\u3002\u6bcf\u4e2a\u5957\u63a5\u5b57\u90fd\u6709\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570&#xff0c;\u5f53\u6240\u6709\u6301\u6709\u5b83\u7684\u8fdb\u7a0b\u90fd\u5173\u95ed\u4e86&#xff0c;\u5957\u63a5\u5b57\u624d\u4f1a\u771f\u6b63\u88ab\u91ca\u653e\u3002<\/p>\n<\/p>\n<p>\u5bf9\u4e8e\u7236\u8fdb\u7a0b\u6765\u8bf4&#xff0c;\u5b83\u53ea\u9700\u8981\u7ee7\u7eed\u76d1\u542c\u65b0\u7684\u8fde\u63a5&#xff0c;\u6240\u4ee5\u4e0d\u9700\u8981\u4fdd\u6301\u5ba2\u6237\u7aef\u7684client_fd&#xff0c;\u5173\u95ed\u5b83\u53ef\u4ee5\u91ca\u653e\u8d44\u6e90&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002\u540c\u6837&#xff0c;\u5b50\u8fdb\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u65f6\u4e0d\u9700\u8981\u76d1\u542c\u5957\u63a5\u5b57server_fd&#xff0c;\u5173\u95ed\u5b83\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u5360\u7528&#xff0c;\u540c\u65f6\u9632\u6b62\u5b50\u8fdb\u7a0b\u610f\u5916\u64cd\u4f5c\u76d1\u542c\u5957\u63a5\u5b57\u3002<\/p>\n<\/p>\n<p>\u7528\u6237\u53ef\u80fd\u5b58\u5728\u7684\u7591\u95ee\u70b9\u5305\u62ec&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5173\u95ed&#xff1f;\u4e0d\u5173\u95ed\u4f1a\u600e\u6837&#xff1f;\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5982\u4f55\u7ba1\u7406\u7684&#xff1f;\u8fd9\u65f6\u5019\u9700\u8981\u8be6\u7ec6\u89e3\u91ca\u5f15\u7528\u8ba1\u6570\u7684\u673a\u5236&#xff0c;\u4ee5\u53ca\u5982\u679c\u4e0d\u5173\u95ed\u7684\u8bdd\u53ef\u80fd\u5bfc\u81f4\u7684\u95ee\u9898&#xff0c;\u6bd4\u5982\u8d44\u6e90\u8017\u5c3d&#xff0c;\u65e0\u6cd5\u63a5\u53d7\u65b0\u8fde\u63a5&#xff0c;\u6216\u8005\u5ba2\u6237\u7aef\u8fde\u63a5\u65e0\u6cd5\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p>\u53e6\u5916&#xff0c;\u7528\u6237\u53ef\u80fd\u5bf9\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\u7684\u8d44\u6e90\u7ba1\u7406\u4e0d\u592a\u719f\u6089&#xff0c;\u9700\u8981\u5f3a\u8c03\u5728fork\u4e4b\u540e&#xff0c;\u7236\u5b50\u8fdb\u7a0b\u5e94\u8be5\u5404\u81ea\u5173\u95ed\u4e0d\u9700\u8981\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8fd9\u662f\u4e00\u79cd\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef&#xff0c;\u786e\u4fdd\u8d44\u6e90\u53ca\u65f6\u91ca\u653e&#xff0c;\u907f\u514d\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p>\u603b\u7ed3\u6765\u8bf4&#xff0c;\u7528\u6237\u9700\u8981\u7406\u89e3\u8fd9\u4e24\u4e2aclose\u64cd\u4f5c\u7684\u5fc5\u8981\u6027&#xff0c;\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u5e2e\u52a9\u6b63\u786e\u7ba1\u7406\u670d\u52a1\u5668\u8d44\u6e90&#xff0c;\u9632\u6b62\u6587\u4ef6\u63cf\u8ff0\u7b26\u6cc4\u6f0f&#xff0c;\u786e\u4fdd\u670d\u52a1\u5668\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<p>\u5728TCP\u670d\u52a1\u5668\u7684\u591a\u8fdb\u7a0b\u5b9e\u73b0\u4e2d&#xff0c;close(server_fd)\u00a0\u548c\u00a0close(client_fd)\u00a0\u8fd9\u4e24\u884c\u4ee3\u7801\u6d89\u53caUNIX\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ba1\u7406\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u7684\u6838\u5fc3\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u6df1\u5165\u5206\u6790&#xff1a;<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u672c\u8d28<\/h4>\n<li>\n<p>\u5185\u6838\u8d44\u6e90<\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982\u00a0server_fd\u00a0\u548c\u00a0client_fd&#xff09;\u672c\u8d28\u4e0a\u662f\u5185\u6838\u4e2d\u4e00\u4e2a\u6253\u5f00\u8d44\u6e90\u7684\u5f15\u7528\u53e5\u67c4<\/p>\n<\/li>\n<li>\n<p>\u5957\u63a5\u5b57\u5728\u5185\u6838\u4e2d\u7531\u5f15\u7528\u8ba1\u6570\u7ba1\u7406&#xff0c;\u5f53\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\u65f6\u624d\u4f1a\u771f\u6b63\u5173\u95ed<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>fork\u7684\u7279\u6027<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528\u00a0fork()\u00a0\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868<\/p>\n<\/li>\n<li>\n<p>\u7236\u5b50\u8fdb\u7a0b\u4e2d\u7684\u76f8\u540c\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5411\u540c\u4e00\u4e2a\u5185\u6838\u5957\u63a5\u5b57\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u4e8c\u3001\u4ee3\u7801\u573a\u666f\u5206\u6790<\/h4>\n<h5>\u539f\u59cb\u4ee3\u7801\u7247\u6bb5&#xff1a;<\/h5>\n<p>c<\/p>\n<p>\u590d\u5236<\/p>\n<p>pid_t pid &#061; fork();<br \/>\nif (pid &#061;&#061; 0) {    \/\/ \u5b50\u8fdb\u7a0b<br \/>\n    close(server_fd);  \/\/ \u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n    handle_client(client_fd);<br \/>\n    exit(EXIT_SUCCESS);<br \/>\n} else {            \/\/ \u7236\u8fdb\u7a0b<br \/>\n    close(client_fd);  \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n} <\/p>\n<hr \/>\n<h4>\u4e09\u3001close(server_fd)\u00a0\u5728\u5b50\u8fdb\u7a0b\u4e2d\u7684\u610f\u4e49<\/h4>\n<li>\n<p>\u8d44\u6e90\u91ca\u653e\u5fc5\u8981\u6027<\/p>\n<ul>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u4ec5\u9700\u5904\u7406\u5df2\u63a5\u53d7\u7684\u5ba2\u6237\u7aef\u8fde\u63a5&#xff08;client_fd&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u76d1\u542c\u5957\u63a5\u5b57&#xff08;server_fd&#xff09;\u5bf9\u5b50\u8fdb\u7a0b\u65e0\u7528<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u4e0d\u5173\u95ed&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u6301\u6709\u00a0server_fd\u00a0\u7684\u526f\u672c<\/p>\n<\/li>\n<li>\n<p>\u5373\u4f7f\u7236\u8fdb\u7a0b\u7ed3\u675f&#xff0c;\u53ea\u8981\u5b50\u8fdb\u7a0b\u5b58\u5728&#xff0c;\u76d1\u542c\u5957\u63a5\u5b57\u5c31\u4e0d\u4f1a\u5173\u95ed<\/p>\n<\/li>\n<li>\n<p>\u53ef\u80fd\u5bfc\u81f4\u540e\u7eed\u65e0\u6cd5\u91cd\u542f\u670d\u52a1&#xff08;\u7aef\u53e3\u5360\u7528&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7cfb\u7edf\u9650\u5236\u89c4\u907f<\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u8fdb\u7a0b\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u9650\u5236&#xff08;ulimit -n&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u957f\u671f\u8fd0\u884c\u7684\u5b50\u8fdb\u7a0b\u79ef\u7d2f\u672a\u5173\u95ed\u7684\u00a0server_fd\u00a0\u53ef\u80fd\u8017\u5c3d\u8d44\u6e90<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u56db\u3001close(client_fd)\u00a0\u5728\u7236\u8fdb\u7a0b\u4e2d\u7684\u610f\u4e49<\/h4>\n<li>\n<p>\u804c\u8d23\u5206\u79bb\u539f\u5219<\/p>\n<ul>\n<li>\n<p>\u7236\u8fdb\u7a0b\u4e13\u804c\u63a5\u53d7\u65b0\u8fde\u63a5&#xff08;accept()&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5df2\u63a5\u53d7\u7684\u8fde\u63a5\u7531\u5b50\u8fdb\u7a0b\u5168\u6743\u5904\u7406<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5f15\u7528\u8ba1\u6570\u7ba1\u7406<\/p>\n<ul>\n<li>\n<p>\u539f\u59cb\u72b6\u6001&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7236\u8fdb\u7a0b\u6301\u6709\u00a0client_fd&#xff08;\u5f15\u7528\u8ba1\u6570&#061;1&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u6301\u6709\u00a0client_fd&#xff08;\u5f15\u7528\u8ba1\u6570&#061;2&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7236\u8fdb\u7a0b\u6267\u884c\u00a0close(client_fd)\u00a0\u540e&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f15\u7528\u8ba1\u6570\u51cf\u4e3a1&#xff08;\u4ec5\u5b50\u8fdb\u7a0b\u6301\u6709&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6\u4f1a\u518d\u6b21\u51cf\u4e3a0&#xff0c;\u89e6\u53d1\u771f\u6b63\u5173\u95ed<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u9884\u9632\u7ade\u6001\u6761\u4ef6<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u5173\u95ed\u00a0client_fd&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7236\u8fdb\u7a0b\u610f\u5916\u64cd\u4f5c\u8be5\u63cf\u8ff0\u7b26\u53ef\u80fd\u5e72\u6270\u5b50\u8fdb\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u7236\u8fdb\u7a0b\u5d29\u6e83\u4f1a\u5bfc\u81f4\u5ba2\u6237\u7aef\u8fde\u63a5\u5f02\u5e38&#xff08;\u5f15\u7528\u8ba1\u6570\u4e0d\u5f52\u96f6&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u4e94\u3001\u6df1\u5ea6\u673a\u5236\u56fe\u89e3<\/h4>\n<p>\u590d\u5236<\/p>\n<p>\u521d\u59cb\u72b6\u6001&#xff1a;<br \/>\n\u7236\u8fdb\u7a0b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff1a;<br \/>\n[0] stdin<br \/>\n[1] stdout<br \/>\n[2] stderr<br \/>\n[3] server_fd  &#8211;&gt; \u76d1\u542c\u5957\u63a5\u5b57&#xff08;\u5f15\u7528\u8ba1\u6570&#061;1&#xff09;<\/p>\n<p>After accept():<br \/>\n\u7236\u8fdb\u7a0b\u65b0\u589e&#xff1a;<br \/>\n[4] client_fd  &#8211;&gt; \u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff08;\u5f15\u7528\u8ba1\u6570&#061;1&#xff09;<\/p>\n<p>After fork():<br \/>\n\u5b50\u8fdb\u7a0b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff1a;<br \/>\n[0] stdin<br \/>\n[1] stdout<br \/>\n[2] stderr<br \/>\n[3] server_fd  &#8211;&gt; \u540c\u4e00\u76d1\u542c\u5957\u63a5\u5b57&#xff08;\u5f15\u7528\u8ba1\u6570&#061;2&#xff09;<br \/>\n[4] client_fd  &#8211;&gt; \u540c\u4e00\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff08;\u5f15\u7528\u8ba1\u6570&#061;2&#xff09;<\/p>\n<p>After close()\u64cd\u4f5c&#xff1a;<br \/>\n\u7236\u8fdb\u7a0b&#xff1a;<br \/>\nclose(4) &#8211;&gt; \u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5f15\u7528\u8ba1\u6570&#061;1<br \/>\n\u5b50\u8fdb\u7a0b&#xff1a;<br \/>\nclose(3) &#8211;&gt; \u76d1\u542c\u5957\u63a5\u5b57\u5f15\u7528\u8ba1\u6570&#061;1 <\/p>\n<hr \/>\n<h4>\u516d\u3001\u4e0d\u5173\u95ed\u7684\u540e\u679c\u6f14\u793a<\/h4>\n<p>\u5047\u8bbe\u6ce8\u91ca\u6389\u8fd9\u4e24\u4e2a\u00a0close()&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u590d\u5236<\/p>\n<p>\/\/ close(server_fd);  \/\/ \u5b50\u8fdb\u7a0b\u672a\u5173\u95ed<br \/>\n\/\/ close(client_fd);  \/\/ \u7236\u8fdb\u7a0b\u672a\u5173\u95ed <\/p>\n<li>\n<p>\u76d1\u542c\u5957\u63a5\u5b57\u6cc4\u6f0f<\/p>\n<ul>\n<li>\n<p>\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u6301\u6709\u00a0server_fd<\/p>\n<\/li>\n<li>\n<p>\u5373\u4f7f\u4e3b\u8fdb\u7a0b\u60f3\u5173\u95ed\u670d\u52a1&#xff0c;\u4e5f\u65e0\u6cd5\u771f\u6b63\u91ca\u653e\u7aef\u53e3<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u8fde\u63a5\u6cc4\u6f0f<\/p>\n<ul>\n<li>\n<p>\u7236\u8fdb\u7a0b\u6301\u7eed\u79ef\u7d2f\u00a0client_fd<\/p>\n<\/li>\n<li>\n<p>\u8fbe\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u9650\u540e\u65e0\u6cd5\u63a5\u53d7\u65b0\u8fde\u63a5<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u65e0\u6cd5\u6b63\u5e38\u5173\u95ed<\/p>\n<ul>\n<li>\n<p>\u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;client_fd\u00a0\u5f15\u7528\u8ba1\u6570\u4ecd\u4e3a1&#xff08;\u7236\u8fdb\u7a0b\u6301\u6709&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u8fde\u63a5\u4fdd\u6301\u6253\u5f00\u76f4\u5230\u7236\u8fdb\u7a0b\u9000\u51fa<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u4e03\u3001\u6700\u4f73\u5b9e\u8df5\u6269\u5c55<\/h4>\n<li>\n<p>\u9632\u5fa1\u6027\u5173\u95ed \u5728\u9519\u8bef\u5904\u7406\u8def\u5f84\u4e2d\u4e5f\u5e94\u6dfb\u52a0\u00a0close()\u00a0\u64cd\u4f5c&#xff1a;<\/p>\n<p>c<\/p>\n<p>\u590d\u5236<\/p>\n<p> if (connect() &lt; 0) {<br \/>\n    close(fd);<br \/>\n    \/\/ \u9519\u8bef\u5904\u7406<br \/>\n} <\/li>\n<li>\n<p>\u73b0\u4ee3\u66ff\u4ee3\u65b9\u6848 \u4f7f\u7528\u00a0fork()\u00a0&#043;\u00a0close()\u00a0\u7684\u4f20\u7edf\u65b9\u5f0f\u4e4b\u5916&#xff0c;\u66f4\u63a8\u8350\u4ee5\u4e0b\u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>socketpair()\u00a0&#043;\u00a0fd passing&#xff08;\u9ad8\u7ea7IPC&#xff09;<\/p>\n<\/li>\n<li>\n<p>IO\u591a\u8def\u590d\u7528&#xff08;select\/poll\/epoll&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u7ebf\u7a0b\u6c60<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8bca\u65ad\u5de5\u5177<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u00a0lsof -p [PID]\u00a0\u67e5\u770b\u8fdb\u7a0b\u6301\u6709\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<\/li>\n<li>\n<p>netstat -tulpn\u00a0\u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5<\/p>\n<\/li>\n<li>\n<p>\/proc\/[PID]\/fd\u00a0\u76ee\u5f55\u76f4\u63a5\u89c2\u5bdf\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u603b\u7ed3<\/h4>\n<p>\u8fd9\u4e24\u4e2a\u00a0close()\u00a0\u64cd\u4f5c\u7684\u672c\u8d28\u662f&#xff1a;<\/p>\n<li>\n<p>\u660e\u786e\u8fdb\u7a0b\u804c\u8d23&#xff1a;\u7236\u5b50\u8fdb\u7a0b\u5404\u53f8\u5176\u804c&#xff0c;\u5173\u95ed\u4e0d\u9700\u8981\u7684\u8d44\u6e90\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u7cbe\u7ec6\u63a7\u5236\u751f\u547d\u5468\u671f&#xff1a;\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u673a\u5236\u786e\u4fdd\u8d44\u6e90\u53ca\u65f6\u91ca\u653e<\/p>\n<\/li>\n<li>\n<p>\u7cfb\u7edf\u5065\u58ee\u6027\u4fdd\u969c&#xff1a;\u9884\u9632\u8d44\u6e90\u6cc4\u6f0f\u5bfc\u81f4\u7684\u7ea7\u8054\u6545\u969c<\/p>\n<\/li>\n<p>\u8fd9\u79cd\u6a21\u5f0f\u4f53\u73b0\u4e86UNIX\u54f2\u5b66\u4e2d&#034;\u505a\u4e00\u4ef6\u4e8b\u5e76\u505a\u597d&#034;\u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u662f\u6784\u5efa\u53ef\u9760\u7f51\u7edc\u670d\u52a1\u7684\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u901a\u8fc7\u544a\u8bc9\u5185\u6838&quot;\u7236\u8fdb\u7a0b\u5b8c\u5168\u4e0d\u9700\u8981\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f&quot;\uff0c\u4f7f\u5f97\u5185\u6838\u81ea\u52a8\u56de\u6536\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b\u8d44\u6e90\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u907f\u514d\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002\u8fd9\u79cd\u65b9\u5f0f\uff1a\u7b80\u6d01\u9ad8\u6548\uff08\u65e0\u9700\u5faa\u73af\u8c03\u7528wait()\u9002\u5408\u4e0d\u9700\u8981\u6536\u96c6\u5b50\u8fdb\u7a0b\u72b6\u6001\u7684\u573a\u666f\u662f\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f\u4e4b\u4e00\u8fd9\u4e24\u4e2aclose()\u660e\u786e\u8fdb\u7a0b\u804c\u8d23\uff1a\u7236\u5b50\u8fdb\u7a0b\u5404\u53f8\u5176\u804c\uff0c\u5173\u95ed\u4e0d\u9700\u8981\u7684\u8d44\u6e90\u5f15\u7528\u7cbe\u7ec6\u63a7\u5236\u751f\u547d\u5468\u671f\uff1a\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u673a\u5236\u786e\u4fdd\u8d44\u6e90\u53ca\u65f6\u91ca\u653e\u7cfb\u7edf\u5065\u58ee\u6027\u4fdd\u969c\uff1a\u9884\u9632\u8d44\u6e90\u6cc4\u6f0f\u5bfc\u81f4\u7684\u7ea7\u8054\u6545\u969c\u8fd9\u79cd\u6a21\u5f0f\u4f53\u73b0\u4e86UNIX\u54f2\u5b66\u4e2d&quot;\u505a\u4e00\u4ef6\u4e8b\u5e76\u505a\u597d&quot;\u7684\u8bbe\u8ba1\u601d\u60f3\uff0c\u662f\u6784\u5efa\u53ef\u9760\u7f51\u7edc\u670d\u52a1\u7684\u57fa\u7840\u3002<\/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":[58,43,78],"topic":[],"class_list":["post-15002","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \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\/15002.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u901a\u8fc7\u544a\u8bc9\u5185\u6838&quot;\u7236\u8fdb\u7a0b\u5b8c\u5168\u4e0d\u9700\u8981\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f&quot;\uff0c\u4f7f\u5f97\u5185\u6838\u81ea\u52a8\u56de\u6536\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b\u8d44\u6e90\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u907f\u514d\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002\u8fd9\u79cd\u65b9\u5f0f\uff1a\u7b80\u6d01\u9ad8\u6548\uff08\u65e0\u9700\u5faa\u73af\u8c03\u7528wait()\u9002\u5408\u4e0d\u9700\u8981\u6536\u96c6\u5b50\u8fdb\u7a0b\u72b6\u6001\u7684\u573a\u666f\u662f\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f\u4e4b\u4e00\u8fd9\u4e24\u4e2aclose()\u660e\u786e\u8fdb\u7a0b\u804c\u8d23\uff1a\u7236\u5b50\u8fdb\u7a0b\u5404\u53f8\u5176\u804c\uff0c\u5173\u95ed\u4e0d\u9700\u8981\u7684\u8d44\u6e90\u5f15\u7528\u7cbe\u7ec6\u63a7\u5236\u751f\u547d\u5468\u671f\uff1a\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u673a\u5236\u786e\u4fdd\u8d44\u6e90\u53ca\u65f6\u91ca\u653e\u7cfb\u7edf\u5065\u58ee\u6027\u4fdd\u969c\uff1a\u9884\u9632\u8d44\u6e90\u6cc4\u6f0f\u5bfc\u81f4\u7684\u7ea7\u8054\u6545\u969c\u8fd9\u79cd\u6a21\u5f0f\u4f53\u73b0\u4e86UNIX\u54f2\u5b66\u4e2d&quot;\u505a\u4e00\u4ef6\u4e8b\u5e76\u505a\u597d&quot;\u7684\u8bbe\u8ba1\u601d\u60f3\uff0c\u662f\u6784\u5efa\u53ef\u9760\u7f51\u7edc\u670d\u52a1\u7684\u57fa\u7840\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/15002.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T13:42:14+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=\"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\/15002.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/15002.html\",\"name\":\"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:42:14+00:00\",\"dateModified\":\"2025-04-18T13:42:14+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/15002.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/15002.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15002.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09\"}]},{\"@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":"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \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\/15002.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u901a\u8fc7\u544a\u8bc9\u5185\u6838&quot;\u7236\u8fdb\u7a0b\u5b8c\u5168\u4e0d\u9700\u8981\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f&quot;\uff0c\u4f7f\u5f97\u5185\u6838\u81ea\u52a8\u56de\u6536\u7ec8\u6b62\u7684\u5b50\u8fdb\u7a0b\u8d44\u6e90\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u907f\u514d\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002\u8fd9\u79cd\u65b9\u5f0f\uff1a\u7b80\u6d01\u9ad8\u6548\uff08\u65e0\u9700\u5faa\u73af\u8c03\u7528wait()\u9002\u5408\u4e0d\u9700\u8981\u6536\u96c6\u5b50\u8fdb\u7a0b\u72b6\u6001\u7684\u573a\u666f\u662f\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f\u4e4b\u4e00\u8fd9\u4e24\u4e2aclose()\u660e\u786e\u8fdb\u7a0b\u804c\u8d23\uff1a\u7236\u5b50\u8fdb\u7a0b\u5404\u53f8\u5176\u804c\uff0c\u5173\u95ed\u4e0d\u9700\u8981\u7684\u8d44\u6e90\u5f15\u7528\u7cbe\u7ec6\u63a7\u5236\u751f\u547d\u5468\u671f\uff1a\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u673a\u5236\u786e\u4fdd\u8d44\u6e90\u53ca\u65f6\u91ca\u653e\u7cfb\u7edf\u5065\u58ee\u6027\u4fdd\u969c\uff1a\u9884\u9632\u8d44\u6e90\u6cc4\u6f0f\u5bfc\u81f4\u7684\u7ea7\u8054\u6545\u969c\u8fd9\u79cd\u6a21\u5f0f\u4f53\u73b0\u4e86UNIX\u54f2\u5b66\u4e2d&quot;\u505a\u4e00\u4ef6\u4e8b\u5e76\u505a\u597d&quot;\u7684\u8bbe\u8ba1\u601d\u60f3\uff0c\u662f\u6784\u5efa\u53ef\u9760\u7f51\u7edc\u670d\u52a1\u7684\u57fa\u7840\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/15002.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:42:14+00:00","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\/15002.html","url":"https:\/\/www.wsisp.com\/helps\/15002.html","name":"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:42:14+00:00","dateModified":"2025-04-18T13:42:14+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/15002.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/15002.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/15002.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux TCP\u56de\u663e\u670d\u52a1\u5668\u5b9e\u73b0\uff08deepseek\u6559\u6211\u5199\u4ee3\u7801\uff09"}]},{"@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\/15002","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=15002"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/15002\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=15002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=15002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=15002"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=15002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}