{"id":86031,"date":"2026-07-28T02:56:57","date_gmt":"2026-07-27T18:56:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/86031.html"},"modified":"2026-07-28T02:56:57","modified_gmt":"2026-07-27T18:56:57","slug":"tcp%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8ei-o%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8%ef%bc%88select%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/86031.html","title":{"rendered":"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\uff09"},"content":{"rendered":"<p>\u5728Linux\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u6784\u5efa\u4e00\u4e2a\u80fd\u591f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684TCP\u670d\u52a1\u5668\u662f\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u3002\u4f20\u7edf\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u5728\u8c03\u7528accept\u6216recv\u65f6\u4f1a\u963b\u585e&#xff0c;\u5bfc\u81f4\u65e0\u6cd5\u53ca\u65f6\u54cd\u5e94\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u63a2\u8ba8TCP\u5e76\u53d1\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u601d\u8def&#xff0c;\u91cd\u70b9\u4ecb\u7ecdI\/O\u591a\u8def\u590d\u7528\u4e2d\u7684select\u51fd\u6570&#xff0c;\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002\u540c\u65f6&#xff0c;\u8fd8\u4f1a\u8865\u5145Linux\u56db\u79cdI\/O\u6a21\u578b\u7684\u7b80\u5355\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u7406\u89e3\u4e0d\u540cI\/O\u65b9\u5f0f\u7684\u7279\u70b9\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u95ee\u9898&#xff1a;\u963b\u585eI\/O\u4e0b\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u5668<\/h3>\n<p>\u8003\u8651\u4e00\u4e2a\u6700\u57fa\u672c\u7684TCP\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>c<\/p>\n<p>\/\/ \u521b\u5efasocket\u3001bind\u3001listen&#8230;<br \/>\nwhile (1) {<br \/>\n    int client_fd &#061; accept(server_fd, &#8230;); \/\/ \u963b\u585e\u76f4\u5230\u6709\u8fde\u63a5<br \/>\n    char buf[1024];<br \/>\n    recv(client_fd, buf, sizeof(buf), 0);    \/\/ \u963b\u585e\u7b49\u5f85\u6570\u636e<br \/>\n    \/\/ \u5904\u7406\u6570\u636e&#8230;<br \/>\n    close(client_fd);<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u6a21\u578b\u5b58\u5728\u4e25\u91cd\u7f3a\u9677&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u67d0\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u540e\u4e00\u76f4\u4e0d\u53d1\u9001\u6570\u636e&#xff0c;recv\u5c06\u4e00\u76f4\u963b\u585e&#xff0c;\u540e\u7eed\u7684\u8fde\u63a5\u8bf7\u6c42\u65e0\u6cd5\u88abaccept\u3002<\/p>\n<\/li>\n<li>\n<p>\u65e0\u6cd5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5df2\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6839\u672c\u539f\u56e0\u5728\u4e8e\u963b\u585eI\/O&#xff1a;\u5f53\u7a0b\u5e8f\u6267\u884c\u4e00\u4e2aI\/O\u64cd\u4f5c&#xff08;\u5982accept\u3001recv&#xff09;\u65f6&#xff0c;\u5982\u679c\u6570\u636e\u5c1a\u672a\u51c6\u5907\u597d&#xff0c;\u8fdb\u7a0b\u4f1a\u88ab\u6302\u8d77&#xff0c;\u76f4\u5230\u5185\u6838\u5b8c\u6210\u64cd\u4f5c\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u89e3\u51b3\u65b9\u6848\u6982\u8ff0<\/h3>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898&#xff0c;\u901a\u5e38\u6709\u4e24\u79cd\u601d\u8def&#xff1a;<\/p>\n<li>\n<p>\u591a\u7ebf\u7a0b\/\u591a\u8fdb\u7a0b\u6a21\u578b<br \/>\n\u4e3a\u6bcf\u4e2a\u5ba2\u6237\u7aef\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b&#xff0c;\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u90e8\u4f7f\u7528\u963b\u585eI\/O\u3002\u8fd9\u6837&#xff0c;\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u963b\u585e\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u5ba2\u6237\u7aef\u3002<br \/>\n\u4f18\u70b9&#xff1a;\u7f16\u7a0b\u7b80\u5355\u76f4\u89c2\u3002<br \/>\n\u7f3a\u70b9&#xff1a;\u8d44\u6e90\u5f00\u9500\u5927&#xff08;\u6bcf\u4e2a\u8fde\u63a5\u5360\u7528\u4e00\u4e2a\u7ebf\u7a0b\/\u8fdb\u7a0b&#xff09;&#xff0c;\u5f53\u8fde\u63a5\u6570\u5de8\u5927\u65f6\u7cfb\u7edf\u96be\u4ee5\u627f\u53d7\u3002<\/p>\n<\/li>\n<li>\n<p>I\/O\u591a\u8def\u590d\u7528\u6a21\u578b<br \/>\n\u901a\u8fc7\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u540c\u65f6\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4e00\u65e6\u67d0\u4e2a\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff08;\u53ef\u8bfb\/\u53ef\u5199\/\u5f02\u5e38&#xff09;&#xff0c;\u5c31\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u5904\u7406\u3002<br \/>\n\u4f18\u70b9&#xff1a;\u53ea\u9700\u4e00\u4e2a\u7ebf\u7a0b\u5373\u53ef\u7ba1\u7406\u5927\u91cf\u8fde\u63a5&#xff0c;\u8d44\u6e90\u6d88\u8017\u5c0f\u3002<br \/>\n\u7f3a\u70b9&#xff1a;\u7f16\u7a0b\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/li>\n<p>\u5728Linux\u4e2d&#xff0c;I\/O\u591a\u8def\u590d\u7528\u4e3b\u8981\u6709\u4e09\u79cd\u5b9e\u73b0&#xff1a;select\u3001poll\u3001epoll\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001Linux\u7684\u56db\u79cdI\/O\u6a21\u578b<\/h3>\n<p>\u5728\u6df1\u5165select\u4e4b\u524d&#xff0c;\u6709\u5fc5\u8981\u56de\u987e\u4e00\u4e0bLinux\u4e0b\u7684I\/O\u6a21\u578b&#xff1a;<\/p>\n<table>\n<tr>\u6a21\u578b\u63cf\u8ff0\u5178\u578b\u51fd\u6570<\/tr>\n<tbody>\n<tr>\n<td>\u963b\u585eI\/O<\/td>\n<td>\u8fdb\u7a0b\u53d1\u8d77\u7cfb\u7edf\u8c03\u7528\u540e\u4e00\u76f4\u7b49\u5f85&#xff0c;\u76f4\u5230\u6570\u636e\u51c6\u5907\u597d\u3002<\/td>\n<td>read,\u00a0write,\u00a0accept,\u00a0recv<\/td>\n<\/tr>\n<tr>\n<td>\u975e\u963b\u585eI\/O<\/td>\n<td>\u7cfb\u7edf\u8c03\u7528\u7acb\u5373\u8fd4\u56de&#xff0c;\u82e5\u6570\u636e\u672a\u51c6\u5907\u597d\u5219\u8fd4\u56de\u9519\u8bef&#xff0c;\u8fdb\u7a0b\u9700\u8f6e\u8be2\u3002<\/td>\n<td>\u8bbe\u7f6e\u00a0O_NONBLOCK\u00a0\u540e\u4f7f\u7528\u00a0read\u00a0\u7b49<\/td>\n<\/tr>\n<tr>\n<td>\u5f02\u6b65I\/O<\/td>\n<td>\u8fdb\u7a0b\u53d1\u8d77\u64cd\u4f5c\u540e\u7acb\u5373\u8fd4\u56de&#xff0c;\u5185\u6838\u5b8c\u6210\u6570\u636e\u62f7\u8d1d\u540e\u901a\u8fc7\u4fe1\u53f7\u901a\u77e5\u8fdb\u7a0b\u3002<\/td>\n<td>aio_read,\u00a0fcntl\u00a0\u914d\u5408\u4fe1\u53f7<\/td>\n<\/tr>\n<tr>\n<td>I\/O\u591a\u8def\u590d\u7528<\/td>\n<td>\u540c\u65f6\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5185\u6838\u901a\u77e5\u5c31\u7eea\u72b6\u6001&#xff0c;\u8fdb\u7a0b\u518d\u53d1\u8d77I\/O\u64cd\u4f5c\u3002<\/td>\n<td>select,\u00a0poll,\u00a0epoll<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0b\u9762\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u8fd9\u56db\u79cd\u6a21\u578b\u7684\u884c\u4e3a\u3002<\/p>\n<h4>3.1 \u963b\u585eI\/O\u793a\u4f8b<\/h4>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    char buf[1024];<br \/>\n    printf(&#034;\u963b\u585e\u7b49\u5f85\u8f93\u5165&#xff1a;\\\\n&#034;);<br \/>\n    int n &#061; read(0, buf, sizeof(buf));  \/\/ \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u6570\u636e<br \/>\n    buf[n] &#061; &#039;\\\\0&#039;;<br \/>\n    printf(&#034;\u8bfb\u53d6\u5230&#xff1a;%s&#034;, buf);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u6267\u884c\u540e\u7a0b\u5e8f\u505c\u7559\u5728read\u8c03\u7528\u5904&#xff0c;\u76f4\u5230\u7528\u6237\u8f93\u5165\u6570\u636e\u3002<\/p>\n<h4>3.2 \u975e\u963b\u585eI\/O\u793a\u4f8b<\/h4>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;errno.h&gt;<\/p>\n<p>int main() {<br \/>\n    char buf[1024];<br \/>\n    int flags &#061; fcntl(0, F_GETFL);<br \/>\n    fcntl(0, F_SETFL, flags | O_NONBLOCK); \/\/ \u8bbe\u7f6e\u975e\u963b\u585e<\/p>\n<p>    printf(&#034;\u975e\u963b\u585e\u8f6e\u8be2&#xff0c;\u8f93\u5165 &#039;quit&#039; \u9000\u51fa\\\\n&#034;);<br \/>\n    while (1) {<br \/>\n        int n &#061; read(0, buf, sizeof(buf));<br \/>\n        if (n &gt; 0) {<br \/>\n            buf[n] &#061; &#039;\\\\0&#039;;<br \/>\n            printf(&#034;\u8bfb\u53d6\u5230&#xff1a;%s&#034;, buf);<br \/>\n            if (buf[0] &#061;&#061; &#039;q&#039; &amp;&amp; buf[1] &#061;&#061; &#039;u&#039; &amp;&amp; buf[2] &#061;&#061; &#039;i&#039; &amp;&amp; buf[3] &#061;&#061; &#039;t&#039;)<br \/>\n                break;<br \/>\n        } else if (errno &#061;&#061; EAGAIN) {<br \/>\n            \/\/ \u65e0\u6570\u636e&#xff0c;\u505a\u5176\u4ed6\u4e8b\u60c5<br \/>\n            printf(&#034;\u65e0\u6570\u636e&#xff0c;\u7ee7\u7eed\u8f6e\u8be2&#8230;\\\\n&#034;);<br \/>\n            sleep(1);<br \/>\n        } else {<br \/>\n            perror(&#034;read error&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u975e\u963b\u585e\u6a21\u5f0f\u4e0b&#xff0c;\u6ca1\u6709\u6570\u636e\u65f6read\u7acb\u5373\u8fd4\u56de-1\u5e76\u7f6eerrno\u4e3aEAGAIN&#xff0c;\u7a0b\u5e8f\u53ef\u8f6e\u8be2\u3002<\/p>\n<h4>3.3 \u5f02\u6b65I\/O\u793a\u4f8b&#xff08;\u4fe1\u53f7\u9a71\u52a8&#xff09;<\/h4>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;signal.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>#define MAX_LEN 1024<\/p>\n<p>void sigio_handler(int sig) {<br \/>\n    char buf[MAX_LEN];<br \/>\n    int n &#061; read(0, buf, sizeof(buf));<br \/>\n    if (n &gt; 0) {<br \/>\n        buf[n] &#061; &#039;\\\\0&#039;;<br \/>\n        printf(&#034;\\\\n\u5f02\u6b65\u6536\u5230&#xff1a;%s&#034;, buf);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u8bbe\u7f6e\u6807\u51c6\u8f93\u5165\u4e3a\u5f02\u6b65\u6a21\u5f0f<br \/>\n    int flags &#061; fcntl(0, F_GETFL);<br \/>\n    fcntl(0, F_SETFL, flags | O_ASYNC);<br \/>\n    fcntl(0, F_SETOWN, getpid());  \/\/ \u8bbe\u7f6e\u63a5\u6536\u4fe1\u53f7\u7684\u8fdb\u7a0b<br \/>\n    signal(SIGIO, sigio_handler);<\/p>\n<p>    printf(&#034;\u5f02\u6b65I\/O\u793a\u4f8b&#xff0c;\u8f93\u5165\u6570\u636e\u540e\u5185\u6838\u53d1\u9001SIGIO\u4fe1\u53f7\\\\n&#034;);<br \/>\n    while (1) {<br \/>\n        printf(&#034;\u4e3b\u7a0b\u5e8f\u505a\u5176\u4ed6\u4e8b\u60c5&#8230;\\\\n&#034;);<br \/>\n        sleep(2);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u5f53\u7528\u6237\u8f93\u5165\u6570\u636e\u65f6&#xff0c;\u5185\u6838\u53d1\u9001SIGIO\u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<h4>3.4 I\/O\u591a\u8def\u590d\u7528\u793a\u4f8b&#xff08;select&#xff09;<\/h4>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    fd_set readfds;<br \/>\n    struct timeval tv;<br \/>\n    int ret;<\/p>\n<p>    FD_ZERO(&amp;readfds);<br \/>\n    FD_SET(0, &amp;readfds);  \/\/ \u76d1\u89c6\u6807\u51c6\u8f93\u5165<\/p>\n<p>    tv.tv_sec &#061; 5;        \/\/ \u8d85\u65f65\u79d2<br \/>\n    tv.tv_usec &#061; 0;<\/p>\n<p>    printf(&#034;\u7b49\u5f85\u8f93\u5165&#xff0c;\u8d85\u65f65\u79d2&#8230;\\\\n&#034;);<br \/>\n    ret &#061; select(1, &amp;readfds, NULL, NULL, &amp;tv);<br \/>\n    if (ret &#061;&#061; -1) {<br \/>\n        perror(&#034;select&#034;);<br \/>\n    } else if (ret &#061;&#061; 0) {<br \/>\n        printf(&#034;\u8d85\u65f6&#xff0c;\u65e0\u8f93\u5165\\\\n&#034;);<br \/>\n    } else {<br \/>\n        if (FD_ISSET(0, &amp;readfds)) {<br \/>\n            char buf[1024];<br \/>\n            int n &#061; read(0, buf, sizeof(buf));<br \/>\n            buf[n] &#061; &#039;\\\\0&#039;;<br \/>\n            printf(&#034;\u8bfb\u53d6\u5230&#xff1a;%s&#034;, buf);<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>select\u540c\u65f6\u76d1\u89c6\u6807\u51c6\u8f93\u5165&#xff0c;\u82e5\u6709\u6570\u636e\u5219\u8bfb&#xff0c;\u5426\u5219\u8d85\u65f6\u9000\u51fa\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001select \u51fd\u6570\u8be6\u89e3<\/h3>\n<h4>4.1 \u51fd\u6570\u539f\u578b<\/h4>\n<p>c<\/p>\n<p>#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;sys\/time.h&gt;<\/p>\n<p>int select(int nfds, fd_set *readfds, fd_set *writefds,<br \/>\n           fd_set *exceptfds, struct timeval *timeout);<\/p>\n<h4>4.2 \u53c2\u6570\u8bf4\u660e<\/h4>\n<ul>\n<li>\n<p>nfds&#xff1a;\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u6700\u5927\u6570\u503c\u52a01&#xff08;\u5373\u00a0max_fd &#043; 1&#xff09;&#xff0c;\u7528\u4e8e\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>readfds&#xff1a;\u6307\u5411\u53ef\u8bfb\u63cf\u8ff0\u7b26\u96c6\u5408\u7684\u6307\u9488&#xff0c;\u82e5\u67d0\u4e2a\u63cf\u8ff0\u7b26\u53ef\u8bfb&#xff0c;\u5219\u5bf9\u5e94\u4f4d\u88ab\u8bbe\u7f6e\u3002\u6211\u4eec\u901a\u5e38\u4f20\u5165\u5e0c\u671b\u76d1\u89c6\u53ef\u8bfb\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u96c6\u5408\u3002<\/p>\n<\/li>\n<li>\n<p>writefds&#xff1a;\u53ef\u5199\u63cf\u8ff0\u7b26\u96c6\u5408\u3002<\/p>\n<\/li>\n<li>\n<p>exceptfds&#xff1a;\u5f02\u5e38\u63cf\u8ff0\u7b26\u96c6\u5408\u3002<\/p>\n<\/li>\n<li>\n<p>timeout&#xff1a;\u6307\u5b9a\u7b49\u5f85\u65f6\u95f4&#xff1a;<\/p>\n<ul>\n<li>\n<p>NULL&#xff1a;\u65e0\u9650\u7b49\u5f85&#xff0c;\u76f4\u5230\u6709\u63cf\u8ff0\u7b26\u5c31\u7eea\u3002<\/p>\n<\/li>\n<li>\n<p>\u56fa\u5b9a\u65f6\u95f4&#xff1a;\u7b49\u5f85\u6307\u5b9a\u65f6\u95f4&#xff0c;\u82e5\u8d85\u65f6\u5219\u8fd4\u56de0\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u4e3a0&#xff1a;\u7acb\u5373\u8fd4\u56de&#xff08;\u8f6e\u8be2\u6a21\u5f0f&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.3 \u8fd4\u56de\u503c<\/h4>\n<ul>\n<li>\n<p>\u6210\u529f&#xff1a;\u8fd4\u56de\u5c31\u7eea&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u5f02\u5e38&#xff09;\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u603b\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u8d85\u65f6&#xff1a;\u8fd4\u56de0\u3002<\/p>\n<\/li>\n<li>\n<p>\u51fa\u9519&#xff1a;\u8fd4\u56de-1\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>4.4 \u64cd\u4f5c fd_set \u7684\u5b8f<\/h4>\n<p>c<\/p>\n<p>void FD_ZERO(fd_set *set);          \/\/ \u6e05\u7a7a\u96c6\u5408<br \/>\nvoid FD_SET(int fd, fd_set *set);   \/\/ \u5c06fd\u52a0\u5165\u96c6\u5408<br \/>\nvoid FD_CLR(int fd, fd_set *set);   \/\/ \u5c06fd\u4ece\u96c6\u5408\u4e2d\u79fb\u9664<br \/>\nint  FD_ISSET(int fd, fd_set *set); \/\/ \u5224\u65adfd\u662f\u5426\u5728\u96c6\u5408\u4e2d&#xff08;\u5373\u662f\u5426\u5c31\u7eea&#xff09;<\/p>\n<hr \/>\n<h3>\u4e94\u3001\u4f7f\u7528 select \u5b9e\u73b0 TCP \u5e76\u53d1\u670d\u52a1\u5668<\/h3>\n<h4>5.1 \u8bbe\u8ba1\u601d\u8def<\/h4>\n<li>\n<p>\u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57\u00a0listen_fd&#xff0c;\u5e76\u5c06\u5176\u52a0\u5165\u8bfb\u96c6\u5408\u00a0readfds\u3002<\/p>\n<\/li>\n<li>\n<p>\u5faa\u73af\u8c03\u7528\u00a0select&#xff0c;\u76d1\u89c6\u6240\u6709\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u548c\u76d1\u542c\u5957\u63a5\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p>\u82e5\u76d1\u542c\u5957\u63a5\u5b57\u5c31\u7eea&#xff0c;\u8bf4\u660e\u6709\u65b0\u8fde\u63a5\u5230\u6765&#xff0c;\u8c03\u7528\u00a0accept\u00a0\u63a5\u53d7\u8fde\u63a5&#xff0c;\u5e76\u5c06\u65b0\u5957\u63a5\u5b57\u52a0\u5165\u8bfb\u96c6\u5408\u3002<\/p>\n<\/li>\n<li>\n<p>\u82e5\u67d0\u4e2a\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u5c31\u7eea&#xff0c;\u8c03\u7528\u00a0recv\u00a0\u8bfb\u53d6\u6570\u636e\u5e76\u5904\u7406&#xff1b;\u82e5\u5bf9\u65b9\u5173\u95ed\u8fde\u63a5&#xff0c;\u5219\u5173\u95ed\u8be5\u5957\u63a5\u5b57\u5e76\u4ece\u96c6\u5408\u4e2d\u79fb\u9664\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u590d\u6b65\u9aa42~4\u3002<\/p>\n<\/li>\n<h4>5.2 \u793a\u4f8b\u4ee3\u7801<\/h4>\n<p>c<\/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;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;errno.h&gt;<\/p>\n<p>#define PORT 8888<br \/>\n#define MAX_CLIENTS 10<\/p>\n<p>int main() {<br \/>\n    int listen_fd, client_fd, max_fd, activity, i, valread;<br \/>\n    int client_sockets[MAX_CLIENTS] &#061; {0};<br \/>\n    struct sockaddr_in server_addr, client_addr;<br \/>\n    socklen_t addr_len &#061; sizeof(client_addr);<br \/>\n    char buffer[1024];<\/p>\n<p>    \/\/ 1. \u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n    if ((listen_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &#061;&#061; 0) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u7aef\u53e3\u590d\u7528<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    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>    \/\/ 2. \u7ed1\u5b9a<br \/>\n    if (bind(listen_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u76d1\u542c<br \/>\n    if (listen(listen_fd, 3) &lt; 0) {<br \/>\n        perror(&#034;listen&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    printf(&#034;Listening on port %d\\\\n&#034;, PORT);<\/p>\n<p>    fd_set readfds;<\/p>\n<p>    while (1) {<br \/>\n        FD_ZERO(&amp;readfds);<br \/>\n        FD_SET(listen_fd, &amp;readfds);<br \/>\n        max_fd &#061; listen_fd;<\/p>\n<p>        for (i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) {<br \/>\n            int sd &#061; client_sockets[i];<br \/>\n            if (sd &gt; 0) {<br \/>\n                FD_SET(sd, &amp;readfds);<br \/>\n                if (sd &gt; max_fd)<br \/>\n                    max_fd &#061; sd;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u7b49\u5f85\u4e8b\u4ef6<br \/>\n        activity &#061; select(max_fd &#043; 1, &amp;readfds, NULL, NULL, NULL);<br \/>\n        if (activity &lt; 0 &amp;&amp; errno !&#061; EINTR) {<br \/>\n            perror(&#034;select error&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u5904\u7406\u65b0\u8fde\u63a5<br \/>\n        if (FD_ISSET(listen_fd, &amp;readfds)) {<br \/>\n            client_fd &#061; accept(listen_fd, (struct sockaddr *)&amp;client_addr, &amp;addr_len);<br \/>\n            if (client_fd &lt; 0) {<br \/>\n                perror(&#034;accept&#034;);<br \/>\n                continue;<br \/>\n            }<br \/>\n            printf(&#034;\u65b0\u8fde\u63a5: fd&#061;%d, ip&#061;%s, port&#061;%d\\\\n&#034;,<br \/>\n                   client_fd, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));<\/p>\n<p>            for (i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) {<br \/>\n                if (client_sockets[i] &#061;&#061; 0) {<br \/>\n                    client_sockets[i] &#061; client_fd;<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u6570\u636e<br \/>\n        for (i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) {<br \/>\n            int sd &#061; client_sockets[i];<br \/>\n            if (FD_ISSET(sd, &amp;readfds)) {<br \/>\n                valread &#061; read(sd, buffer, sizeof(buffer) &#8211; 1);<br \/>\n                if (valread &#061;&#061; 0) {<br \/>\n                    \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n                    getpeername(sd, (struct sockaddr *)&amp;client_addr, &amp;addr_len);<br \/>\n                    printf(&#034;\u5ba2\u6237\u7aef\u65ad\u5f00: ip&#061;%s, port&#061;%d\\\\n&#034;,<br \/>\n                           inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));<br \/>\n                    close(sd);<br \/>\n                    client_sockets[i] &#061; 0;<br \/>\n                } else {<br \/>\n                    buffer[valread] &#061; &#039;\\\\0&#039;;<br \/>\n                    printf(&#034;\u6536\u5230: %s&#034;, buffer);<br \/>\n                    send(sd, buffer, valread, 0);  \/\/ \u56de\u663e<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(listen_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>5.3 \u5173\u952e\u70b9\u8bf4\u660e<\/h4>\n<ul>\n<li>\n<p>\u4f7f\u7528\u00a0client_sockets\u00a0\u6570\u7ec4\u4fdd\u5b58\u6240\u6709\u5df2\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u6b21\u8c03\u7528\u00a0select\u00a0\u524d\u91cd\u65b0\u6784\u9020\u00a0readfds&#xff0c;\u5e76\u8ba1\u7b97\u00a0max_fd\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u00a0FD_ISSET\u00a0\u5224\u65ad\u662f\u76d1\u542c\u5957\u63a5\u5b57\u5c31\u7eea&#xff08;\u65b0\u8fde\u63a5&#xff09;\u8fd8\u662f\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5c31\u7eea&#xff08;\u6570\u636e\u53ef\u8bfb&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5\u65f6&#xff0c;read\u00a0\u8fd4\u56de0&#xff0c;\u6b64\u65f6\u5173\u95ed\u5957\u63a5\u5b57\u5e76\u4ece\u6570\u7ec4\u4e2d\u6e05\u9664\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u516d\u3001select \u7684\u4f18\u7f3a\u70b9<\/h3>\n<h4>\u4f18\u70b9<\/h4>\n<ul>\n<li>\n<p>\u8de8\u5e73\u53f0\u6027\u597d&#xff08;\u51e0\u4e4e\u6240\u6709Unix-like\u7cfb\u7edf\u90fd\u652f\u6301&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u53e3\u7b80\u5355&#xff0c;\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u7f3a\u70b9<\/h4>\n<ul>\n<li>\n<p>\u5355\u4e2a\u8fdb\u7a0b\u53ef\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u53d7\u00a0FD_SETSIZE\u00a0\u9650\u5236&#xff08;\u901a\u5e38\u4e3a1024&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u6b21\u8c03\u7528\u00a0select\u00a0\u90fd\u9700\u8981\u5c06\u6574\u4e2a\u00a0fd_set\u00a0\u4ece\u7528\u6237\u6001\u62f7\u8d1d\u5230\u5185\u6838\u6001&#xff0c;\u5f00\u9500\u968f\u63cf\u8ff0\u7b26\u6570\u91cf\u589e\u52a0\u800c\u589e\u5927\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u540e\u9700\u8981\u904d\u5386\u6240\u6709\u63cf\u8ff0\u7b26\u6765\u68c0\u67e5\u54ea\u4e9b\u5c31\u7eea&#xff0c;\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<p>\u901a\u8fc7\u00a0select\u00a0\u5b9e\u73b0I\/O\u591a\u8def\u590d\u7528&#xff0c;\u6211\u4eec\u53ef\u4ee5\u7528\u5355\u7ebf\u7a0b\u7ba1\u7406\u591a\u4e2aTCP\u8fde\u63a5&#xff0c;\u6709\u6548\u907f\u514d\u4e86\u963b\u585eI\/O\u5e26\u6765\u7684\u5e76\u53d1\u95ee\u9898\u3002\u5c3d\u7ba1\u00a0select\u00a0\u6709\u6027\u80fd\u74f6\u9888&#xff0c;\u4f46\u5b83\u662f\u7406\u89e3\u591a\u8def\u590d\u7528\u6a21\u578b\u7684\u57fa\u7840\u3002\u638c\u63e1\u5b83\u4e4b\u540e&#xff0c;\u518d\u5b66\u4e60\u00a0epoll\u00a0\u7b49\u9ad8\u9636\u673a\u5236\u5c06\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<p>\u540c\u65f6&#xff0c;\u901a\u8fc7\u5bf9\u6bd4\u963b\u585e\u3001\u975e\u963b\u585e\u3001\u5f02\u6b65\u548c\u591a\u8def\u590d\u7528\u56db\u79cdI\/O\u6a21\u578b\u7684\u7b80\u5355\u793a\u4f8b&#xff0c;\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u7406\u89e3\u6bcf\u79cd\u6a21\u578b\u7684\u5de5\u4f5c\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u53ef\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u6784\u5efa\u4e00\u4e2a\u80fd\u591f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684TCP\u670d\u52a1\u5668\u662f\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u3002\u4f20\u7edf\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u5728\u8c03\u7528accept\u6216recv\u65f6\u4f1a\u963b\u585e&#xff0c;\u5bfc\u81f4\u65e0\u6cd5\u53ca\u65f6\u54cd\u5e94\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u63a2\u8ba8TCP\u5e76\u53d1\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u601d\u8def&#xff0c;\u91cd\u70b9\u4ecb\u7ecdI\/O\u591a\u8def\u590d\u7528\u4e2d\u7684select\u51fd\u6570&#xff0c;\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002\u540c\u65f6&#xff0c;\u8fd8\u4f1a\u8865\u5145Linux\u56db\u79cdI\/O\u6a21\u578b\u7684\u7b80\u5355\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u7406\u89e3\u4e0d\u540cI\/O\u65b9\u5f0f\u7684\u7279\u70b9\u3002\u4e00<\/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":[205,43,191],"topic":[],"class_list":["post-86031","post","type-post","status-publish","format-standard","hentry","category-server","tag-tcp-ip","tag-43","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\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\/86031.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728Linux\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u6784\u5efa\u4e00\u4e2a\u80fd\u591f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684TCP\u670d\u52a1\u5668\u662f\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u3002\u4f20\u7edf\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u5728\u8c03\u7528accept\u6216recv\u65f6\u4f1a\u963b\u585e&#xff0c;\u5bfc\u81f4\u65e0\u6cd5\u53ca\u65f6\u54cd\u5e94\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u63a2\u8ba8TCP\u5e76\u53d1\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u601d\u8def&#xff0c;\u91cd\u70b9\u4ecb\u7ecdI\/O\u591a\u8def\u590d\u7528\u4e2d\u7684select\u51fd\u6570&#xff0c;\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002\u540c\u65f6&#xff0c;\u8fd8\u4f1a\u8865\u5145Linux\u56db\u79cdI\/O\u6a21\u578b\u7684\u7b80\u5355\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u7406\u89e3\u4e0d\u540cI\/O\u65b9\u5f0f\u7684\u7279\u70b9\u3002\u4e00\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/86031.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T18:56:57+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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/86031.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/86031.html\",\"name\":\"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T18:56:57+00:00\",\"dateModified\":\"2026-07-27T18:56:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/86031.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/86031.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/86031.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\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":"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\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\/86031.html","og_locale":"zh_CN","og_type":"article","og_title":"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728Linux\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u6784\u5efa\u4e00\u4e2a\u80fd\u591f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684TCP\u670d\u52a1\u5668\u662f\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u3002\u4f20\u7edf\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u5728\u8c03\u7528accept\u6216recv\u65f6\u4f1a\u963b\u585e&#xff0c;\u5bfc\u81f4\u65e0\u6cd5\u53ca\u65f6\u54cd\u5e94\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u63a2\u8ba8TCP\u5e76\u53d1\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u601d\u8def&#xff0c;\u91cd\u70b9\u4ecb\u7ecdI\/O\u591a\u8def\u590d\u7528\u4e2d\u7684select\u51fd\u6570&#xff0c;\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002\u540c\u65f6&#xff0c;\u8fd8\u4f1a\u8865\u5145Linux\u56db\u79cdI\/O\u6a21\u578b\u7684\u7b80\u5355\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u7406\u89e3\u4e0d\u540cI\/O\u65b9\u5f0f\u7684\u7279\u70b9\u3002\u4e00","og_url":"https:\/\/www.wsisp.com\/helps\/86031.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T18:56:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/86031.html","url":"https:\/\/www.wsisp.com\/helps\/86031.html","name":"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T18:56:57+00:00","dateModified":"2026-07-27T18:56:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/86031.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/86031.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/86031.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"TCP\u5e76\u53d1\u670d\u52a1\u5668\u4e0eI\/O\u591a\u8def\u590d\u7528\uff08select\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\/86031","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=86031"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/86031\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=86031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=86031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=86031"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=86031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}