{"id":45586,"date":"2025-07-30T00:36:20","date_gmt":"2025-07-29T16:36:20","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/45586.html"},"modified":"2025-07-30T00:36:20","modified_gmt":"2025-07-29T16:36:20","slug":"%e5%a4%9a%e8%b7%afi-o%e8%bd%ac%e6%8e%a5%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%88select%e3%80%81poll%e3%80%81epoll%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/45586.html","title":{"rendered":"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\uff09"},"content":{"rendered":"<p>\u591a\u8defIO\u8f6c\u63a5\u670d\u52a1\u5668\u4e5f\u53eb\u505a\u591a\u4efb\u52a1IO\u670d\u52a1\u5668\u3002\u8be5\u7c7b\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u4e3b\u65e8\u601d\u60f3\u662f&#xff0c;\u4e0d\u518d\u7531\u5e94\u7528\u7a0b\u5e8f\u81ea\u5df1\u76d1\u89c6\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u53d6\u800c\u4ee3\u4e4b\u7531\u5185\u6838\u66ff\u5e94\u7528\u7a0b\u5e8f\u76d1\u89c6\u6587\u4ef6\u3002<\/p>\n<p>IO \u591a\u8def\u8f6c\u63a5\u65b9\u5f0f\u6bd4\u8f83&#xff1a;<\/p>\n<p>\n\u5e38\u89c1\u7684 IO \u591a\u8def\u8f6c\u63a5\u65b9\u5f0f\u6709&#xff1a;select\u3001poll\u3001epoll&#xff0c;\u4ed6\u4eec\u7684\u533a\u522b\u4e3a&#xff1a;<\/p>\n<ul>\n<li>select \u53ef\u4ee5\u8de8\u5e73\u53f0&#xff0c;Linux\u3001Mac\u3001Windows \u90fd\u652f\u6301&#xff0c;\u800c poll \u548c epoll \u53ea\u80fd\u5728 Linux \u4e0a\u4f7f\u7528<\/li>\n<li>epoll \u5e95\u5c42\u4e3a\u7ea2\u9ed1\u6811&#xff0c;select \u548c poll \u5e95\u5c42\u4e3a\u7ebf\u6027\u8868&#xff0c;epoll \u7684\u6548\u7387\u8f83\u9ad8<\/li>\n<li>select \u8fde\u63a5\u7684\u8bbe\u5907\u4e0a\u9650\u4e3a 1024&#xff0c;poll \u548c epoll \u6ca1\u4e0a\u9650&#xff0c;\u53d6\u51b3\u4e8e\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u914d\u7f6e<\/li>\n<\/ul>\n<p>\nIO \u591a\u8def\u8f6c\u63a5\u672c\u8d28&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u5728\u670d\u52a1\u5668\u7aef\u6709\u4e24\u7c7b\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5206\u522b\u5bf9\u5e94\u4e00\u4e2a\u8bfb\u7f13\u51b2\u533a\u548c\u5199\u7f13\u51b2\u533a<\/li>\n<li>\u7528\u4e8e\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u8bfb\u7f13\u51b2\u533a\u4e3b\u8981\u7528\u6765\u5b58\u50a8\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5f53\u8c03\u7528 accept \u65f6\u4f1a\u68c0\u6d4b\u8fd9\u4e2a\u8bfb\u7f13\u51b2\u533a\u662f\u5426\u6709\u8fde\u63a5\u8bf7\u6c42<\/li>\n<li>\u7528\u4e8e\u901a\u4fe1\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u8bfb\u7f13\u51b2\u533a\u7528\u4e8e\u5b58\u50a8\u5ba2\u6237\u7aef\u53d1\u9001\u6765\u7684\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u8c03\u7528 read \u65b9\u6cd5\u80fd\u591f\u5c06\u6570\u636e\u8bfb\u53d6\u51fa\u6765&#xff0c;\u5199\u7f13\u51b2\u533a\u7528\u4e8e\u670d\u52a1\u5668\u901a\u8fc7 write \u5199\u5165\u7684\u6570\u636e<\/li>\n<li>\u5f53\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u65f6&#xff0c;accpet\u3001read\u3001write \u53ea\u8981\u6709\u4e00\u4e2a\u963b\u585e&#xff0c;\u5c31\u4e0d\u80fd\u7ee7\u7eed\u8fd0\u884c\u4e86<\/li>\n<li>IO \u591a\u8def\u8f6c\u63a5\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u672c\u8be5\u7531\u7528\u6237\u8fdb\u884c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\/\u5199\u7f13\u51b2\u533a\u7684\u68c0\u6d4b\u4ea4\u7ed9\u4e86\u5185\u6838&#xff0c;\u5185\u6838\u53ef\u4ee5\u540c\u65f6\u68c0\u6d4b\u82e5\u5e72\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ee5\u53ca\u5b83\u4eec\u7684\u8bfb\/\u5199\u7f13\u51b2\u533a&#xff0c;\u68c0\u6d4b\u8bfb\u7f13\u51b2\u533a\u662f\u5426\u6709\u6570\u636e\/\u68c0\u6d4b\u5199\u7f13\u51b2\u533a\u662f\u5426\u6709\u5269\u4f59\u7684\u7a7a\u95f4&#xff0c;\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6&#xff0c;\u5185\u6838\u4f1a\u544a\u77e5\u7528\u6237\u76f8\u5173\u4fe1\u606f&#xff08;\u53ef\u64cd\u4f5c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;&#xff0c;\u6b64\u65f6 accpet\u3001read\u3001write \u5c31\u4e0d\u4f1a\u963b\u585e\u4e86&#xff0c;\u82e5\u5185\u6838\u901a\u77e5\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5728\u7528\u6237\u7a7a\u95f4\u5904\u7406\u65f6\u662f\u6309\u987a\u5e8f\u5904\u7406\u7684<\/li>\n<\/ul>\n<h2>select<\/h2>\n<li>\n<p>select\u80fd\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570\u53d7\u9650\u4e8eFD_SETSIZE,\u4e00\u822c\u4e3a1024&#xff0c;\u5355\u7eaf\u6539\u53d8\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570\u5e76\u4e0d\u80fd\u6539\u53d8select\u76d1\u542c\u6587\u4ef6\u4e2a\u6570<\/p>\n<\/li>\n<li>\n<p>\u89e3\u51b31024\u4ee5\u4e0b\u5ba2\u6237\u7aef\u65f6\u4f7f\u7528select\u662f\u5f88\u5408\u9002\u7684&#xff0c;\u4f46\u5982\u679c\u94fe\u63a5\u5ba2\u6237\u7aef\u8fc7\u591a&#xff0c;select\u91c7\u7528\u7684\u662f\u8f6e\u8be2\u6a21\u578b&#xff0c;\u4f1a\u5927\u5927\u964d\u4f4e\u670d\u52a1\u5668\u54cd\u5e94\u6548\u7387&#xff0c;\u4e0d\u5e94\u5728select\u4e0a\u6295\u5165\u66f4\u591a\u7cbe\u529b<\/p>\n<\/li>\n<p>#include &lt;sys\/select.h&gt;<\/p>\n<p>\/* According to earlier standards *\/<\/p>\n<p>#include &lt;sys\/time.h&gt;<\/p>\n<p>#include &lt;sys\/types.h&gt;<\/p>\n<p>#include &lt;unistd.h&gt;<\/p>\n<\/p>\n<p>\/\/ \u8fd4\u56de\u503c\u5c31\u7eea\u63cf\u8ff0\u7b26\u7684\u6570\u76ee<\/p>\n<p>int select(int nfds, fd_set *readfds, fd_set *writefds,<\/p>\n<p>fd_set *exceptfds, struct timeval *timeout);<\/p>\n<p>nfds: \u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u91cc\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u52a01&#xff0c;\u56e0\u4e3a\u6b64\u53c2\u6570\u4f1a\u544a\u8bc9\u5185\u6838\u68c0\u6d4b\u524d\u591a\u5c11\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001<\/p>\n<p>readfds&#xff1a; \u76d1\u63a7\u6709\u8bfb\u6570\u636e\u5230\u8fbe\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u4f20\u5165\u4f20\u51fa\u53c2\u6570<\/p>\n<p>writefds&#xff1a; \u76d1\u63a7\u5199\u6570\u636e\u5230\u8fbe\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u4f20\u5165\u4f20\u51fa\u53c2\u6570<\/p>\n<p>exceptfds&#xff1a; \u76d1\u63a7\u5f02\u5e38\u53d1\u751f\u8fbe\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408,\u5982\u5e26\u5916\u6570\u636e\u5230\u8fbe\u5f02\u5e38&#xff0c;\u4f20\u5165\u4f20\u51fa\u53c2\u6570<\/p>\n<p>timeout&#xff1a; \u5b9a\u65f6\u963b\u585e\u76d1\u63a7\u65f6\u95f4&#xff0c;3\u79cd\u60c5\u51b5<\/p>\n<p>1.NULL&#xff0c;\u6c38\u8fdc\u7b49\u4e0b\u53bb<\/p>\n<p>2.\u8bbe\u7f6etimeval&#xff0c;\u7b49\u5f85\u56fa\u5b9a\u65f6\u95f4<\/p>\n<p>3.\u8bbe\u7f6etimeval\u91cc\u65f6\u95f4\u5747\u4e3a0&#xff0c;\u68c0\u67e5\u63cf\u8ff0\u5b57\u540e\u7acb\u5373\u8fd4\u56de&#xff0c;\u8f6e\u8be2<\/p>\n<\/p>\n<p>struct timeval {<\/p>\n<p>long tv_sec; \/* seconds *\/<\/p>\n<p>long tv_usec; \/* microseconds *\/<\/p>\n<p>};<\/p>\n<\/p>\n<p>void FD_CLR(int fd, fd_set *set); \/\/\u628a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u91ccfd\u6e050<\/p>\n<p>int FD_ISSET(int fd, fd_set *set); \/\/\u6d4b\u8bd5\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u91ccfd\u662f\u5426\u7f6e1<\/p>\n<p>void FD_SET(int fd, fd_set *set); \/\/\u628a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u91ccfd\u4f4d\u7f6e1<\/p>\n<p>void FD_ZERO(fd_set *set); \/\/\u628a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u91cc\u6240\u6709\u4f4d\u6e050<\/p>\n<p>\u793a\u4f8b\u56fe\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"536\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163610-6888f87a0de08.png\" width=\"1114\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0\">\u8fd9\u4e2a\u56fe\u7684\u610f\u601d\u662f&#xff0c;\u5e94\u7528\u5c42\u5148\u628a\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u505a\u6807\u8bb01&#xff0c;\u4e4b\u540e\u518d\u5c06\u5176\u62f7\u8d1d\u4e00\u4efd&#xff0c;\u5c06\u62f7\u8d1d\u7684\u8fd9\u4e00\u4efd\u6587\u4ef6\u63cf\u8ff0\u7b26\u5728\u62f7\u8d1d\u5230\u5185\u6838\u4e2d&#xff0c;\u8ba9\u5185\u6838\u76d1\u542c\u8fd9\u4e9b\u505a\u6807\u8bb0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5982\u679c\u88ab\u76d1\u542c\u7684\u6587\u4ef6\u6ca1\u6709\u53d8\u5316&#xff0c;\u90a3\u4e48\u5185\u6838\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7684\u6807\u8bb0\u5c31\u4f1a\u88ab\u62b9\u6d88&#xff0c;\u7136\u540e\u5728\u5c06\u6539\u53d8\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u590d\u5236\u5230\u5e94\u7528\u5c42&#xff0c;\u8ba9\u5176\u5bf9\u6539\u53d8\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u8bfb\u53d6&#xff0c;<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u4f8b\u5982\u5e94\u7528\u5c42\u51c6\u5907\u76d1\u542c4567\u8fd9\u56db\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u590d\u5236\u5230\u5185\u6838\u53bb\u76d1\u542c&#xff0c;\u5185\u6838\u53d1\u73b0\u53ea\u67095\u53f7\u53d1\u751f\u4e86\u6539\u53d8&#xff0c;\u6240\u4ee5\u544a\u77e5\u5e94\u7528\u5c42\u53bb5\u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u53d6\u6570\u636e&#xff0c;\u5982\u679c\u662flfd\u53734\u53d1\u751f\u4e86\u53d8\u5316&#xff0c;\u5c31\u8bf4\u660e\u6709\u65b0\u7684\u8fde\u63a5\u4ea7\u751f\u4e86<\/p>\n<p style=\"margin-left:0;margin-right:0\">\n<h3 style=\"background-color:transparent\">server<\/h3>\n<p>\u6d41\u7a0b\u56fe\u793a&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3820\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163611-6888f87b7dd63.png\" width=\"1248\" \/><\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/select.h&gt; \/\/ select\u591a\u8def\u590d\u7528API<br \/>\n#include &lt;sys\/types.h&gt;\/\/ \u57fa\u672c\u7cfb\u7edf\u6570\u636e\u7c7b\u578b<br \/>\n#include &lt;unistd.h&gt;\/\/ POSIX API&#xff08;read\/write\/close\u7b49&#xff09;<br \/>\n#include &#034;wrap.h&#034;\/\/ \u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u5c01\u88c5<br \/>\n#include &lt;sys\/time.h&gt;<\/p>\n<p>#define PORT 8888 \/\/ \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n\/\/ \u521b\u5efaTCP\u5957\u63a5\u5b57\u5e76\u7ed1\u5b9a\u7aef\u53e3<br \/>\nint lfd &#061; tcp4bind(PORT, NULL);<br \/>\n\/\/ \u8bbe\u7f6e\u76d1\u542c\u961f\u5217\u957f\u5ea6\u4e3a128<br \/>\nListen(lfd, 128);<\/p>\n<p>int maxfd &#061; lfd; \/\/ \u521d\u59cb\u5316\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5f53\u524d\u53ea\u6709\u76d1\u542c\u5957\u63a5\u5b57&#xff09;<br \/>\nfd_set oldset, rset; \/\/ \u5b9a\u4e49\u4e24\u4e2afd_set&#xff1a;<br \/>\n \/\/ oldset&#xff1a;\u6c38\u4e45\u8bb0\u5f55\u6240\u6709\u9700\u76d1\u63a7\u7684fd<br \/>\n \/\/ rset&#xff1a;\u6bcf\u6b21select\u8c03\u7528\u4f20\u5165\u7684\u4e34\u65f6\u96c6\u5408<\/p>\n<p>FD_ZERO(&amp;oldset); \/\/ \u6e05\u7a7a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408<br \/>\nFD_ZERO(&amp;rset);<br \/>\nFD_SET(lfd, &amp;oldset); \/\/ \u5c06\u76d1\u542c\u5957\u63a5\u5b57\u52a0\u5165\u76d1\u63a7\u96c6\u5408<\/p>\n<p>while (1)<br \/>\n{<br \/>\nrset &#061; oldset; \/\/ \u590d\u5236\u6c38\u4e45\u96c6\u5408\u5230\u4e34\u65f6\u96c6\u5408&#xff08;select\u4f1a\u4fee\u6539\u4f20\u5165\u7684\u96c6\u5408&#xff09;<\/p>\n<p>\/\/ \u6838\u5fc3&#xff1a;\u963b\u585e\u76d1\u542c\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u53ef\u8bfb\u4e8b\u4ef6<br \/>\nint n &#061; select(maxfd &#043; 1, &amp;rset, NULL, NULL, NULL);<\/p>\n<p>\/\/ \u9519\u8bef\u5904\u7406<br \/>\nif (n &lt; 0)<br \/>\n{<br \/>\nperror(&#034;select error&#034;);<br \/>\nbreak;<br \/>\n}<br \/>\nelse if (n &#061;&#061; 0)<br \/>\n{ \/\/ \u65e0\u4e8b\u4ef6\u53d1\u751f&#xff08;\u8d85\u65f6&#xff09;<br \/>\ncontinue;<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406\u76d1\u542c\u5957\u63a5\u5b57\u4e8b\u4ef6&#xff08;\u65b0\u8fde\u63a5\u5230\u8fbe&#xff09;\u67e5\u770blfd\u76d1\u542c\u63cf\u8ff0\u7b26\u662f\u5426\u5728\u5c31\u7eea\u7684rset\u96c6\u5408\u4e2d&#xff0c;\u5728\u8868\u793a\u6709\u65b0\u8fde\u63a5<br \/>\nif (FD_ISSET(lfd, &amp;rset))<br \/>\n{ \/\/ \u68c0\u67e5\u76d1\u542c\u5957\u63a5\u5b57\u662f\u5426\u5c31\u7eea<br \/>\nstruct sockaddr_in cliaddr;<br \/>\nsocklen_t len &#061; sizeof(cliaddr);<br \/>\nchar ip[16] &#061; &#034;&#034;;<\/p>\n<p>\/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\nint cfd &#061; Accept(lfd, (struct sockaddr *)&amp;cliaddr, &amp;len);<br \/>\nprintf(&#034;new client ip&#061;%s port&#061;%d\\\\n&#034;,<br \/>\n   inet_ntop(AF_INET, &amp;cliaddr.sin_addr.s_addr, ip, 16),<br \/>\n   ntohs(cliaddr.sin_port));<\/p>\n<p>\/\/ \u5c06\u65b0\u8fde\u63a5\u52a0\u5165\u6c38\u4e45\u76d1\u63a7\u96c6\u5408<br \/>\nFD_SET(cfd, &amp;oldset);<br \/>\n\/\/ \u66f4\u65b0\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\nif (cfd &gt; maxfd)<br \/>\nmaxfd &#061; cfd;<\/p>\n<p>\/\/ \u82e5\u5df2\u65e0\u5176\u4ed6\u4e8b\u4ef6&#xff0c;\u8df3\u8fc7\u540e\u7eed\u5904\u7406<br \/>\nif (&#8211;n &#061;&#061; 0)<br \/>\ncontinue;<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u7684\u6570\u636e\u4e8b\u4ef6<br \/>\n\/\/\u5728 Unix\/Linux \u4e2d&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u5206\u914d\u3002lfd \u662f\u670d\u52a1\u5668\u542f\u52a8\u65f6\u6700\u65e9\u521b\u5efa\u7684\u5957\u63a5\u5b57&#xff0c;\u5176\u503c\u901a\u5e38\u4e3a3&#xff08;0-2 \u88ab\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u5360\u7528&#xff09;&#xff0c;\u540e\u7eed cfd \u4f9d\u6b21\u9012\u589e&#xff08;4, 5, &#8230;&#xff09;<br \/>\n        \/\/\u56e0\u6b64 lfd &#043; 1 \u81ea\u7136\u6307\u5411\u7b2c\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u5957\u63a5\u5b57\u3002<br \/>\nfor (int i &#061; lfd &#043; 1; i &lt;&#061; maxfd; i&#043;&#043;)<br \/>\n{<br \/>\nif (FD_ISSET(i, &amp;rset))<br \/>\n{ \/\/ \u68c0\u67e5\u5f53\u524dfd\u662f\u5426\u5c31\u7eea<br \/>\nchar buf[1500] &#061; &#034;&#034;;<br \/>\nint ret &#061; Read(i, buf, sizeof(buf)); \/\/ \u8bfb\u53d6\u6570\u636e<\/p>\n<p>\/\/ \u9519\u8bef\u5904\u7406<br \/>\nif (ret &lt; 0)<br \/>\n{<br \/>\nperror(&#034;read error&#034;);<br \/>\nclose(i);<br \/>\nFD_CLR(i, &amp;oldset); \/\/ \u4ece\u76d1\u63a7\u96c6\u5408\u79fb\u9664<br \/>\n}<br \/>\n\/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\nelse if (ret &#061;&#061; 0)<br \/>\n{<br \/>\nstruct sockaddr_in remote_addr;<br \/>\nsocklen_t len &#061; sizeof(remote_addr);<br \/>\ngetpeername(i, (struct sockaddr *)&amp;remote_addr, &amp;len);<br \/>\nint remote_port &#061; ntohs(remote_addr.sin_port);<\/p>\n<p>printf(&#034;client%d close\\\\n&#034;,remote_port);<br \/>\nclose(i);<br \/>\nFD_CLR(i, &amp;oldset);<br \/>\n}<br \/>\n\/\/ \u6b63\u5e38\u6570\u636e\u5904\u7406<br \/>\nelse<br \/>\n{<br \/>\nstruct sockaddr_in remote_addr;<br \/>\nsocklen_t len &#061; sizeof(remote_addr);<br \/>\ngetpeername(i, (struct sockaddr *)&amp;remote_addr, &amp;len);<br \/>\nint remote_port &#061; ntohs(remote_addr.sin_port);<\/p>\n<p>printf(&#034;\u5ba2\u6237\u7aef%d:%s\\\\n&#034;, remote_port,buf);<br \/>\nWrite(i, buf, ret); \/\/ \u56de\u663e\u6570\u636e<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>\u51e0\u4e2a\u95ee\u9898&#xff1f;<\/p>\n<p>1.select(maxfd &#043; 1, &amp;rset, NULL, NULL, NULL);\u4e3a\u4ec0\u4e48\u8981maxfd &#043; 1&#xff1f;<\/p>\n<p>fd_set rset;<br \/>\nint maxfd &#061; 5; \u00a0\/\/ \u5f53\u524d\u6700\u5927FD\u4e3a5<br \/>\nFD_ZERO(&amp;rset);<br \/>\nFD_SET(3, &amp;rset); \u00a0\/\/ \u76d1\u63a7FD&#061;3<br \/>\nFD_SET(5, &amp;rset); \u00a0\/\/ \u76d1\u63a7FD&#061;5<\/p>\n<p>\/\/ \u5185\u6838\u4f1a\u68c0\u67e50~5\u7684FD&#xff08;\u51716\u4e2a&#xff09;&#xff0c;\u4f46\u4ec5FD&#061;3\u548c5\u5b9e\u9645\u88ab\u76d1\u63a7<br \/>\nselect(5 &#043; 1, &amp;rset, NULL, NULL, NULL);<\/p>\n<p>\u82e5\u8bef\u4f20\u00a0maxfd&#061;5&#xff08;\u672a&#043;1&#xff09;&#xff0c;\u5185\u6838\u53ef\u80fd\u6f0f\u68c0FD&#061;5&#xff0c;\u5bfc\u81f4\u6570\u636e\u5c31\u7eea\u5374\u672a\u88ab\u89e6\u53d1<\/p>\n<\/p>\n<p>2.int n &#061; select(maxfd &#043; 1, &amp;rset, NULL, NULL, NULL);\u8fd9\u91cc\u662f\u600e\u6837\u904d\u5386\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u7684&#xff1f;\u662f\u4ece0\u5f00\u59cb\u904d\u5386rset\u91cc\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5417&#xff1f;<\/p>\n<ul>\n<li>\u5728\u00a0select\u00a0\u51fd\u6570\u4e2d&#xff0c;\u5185\u6838\u904d\u5386\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff08;rset&#xff09;\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u7ebf\u6027\u626b\u63cf\u4f4d\u56fe&#xff0c;\u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26 \u200b0\u200b \u5f00\u59cb&#xff0c;\u4f9d\u6b21\u68c0\u67e5\u6bcf\u4e2a\u6bd4\u7279\u4f4d\u662f\u5426\u88ab\u7f6e\u4f4d&#xff08;\u5373\u662f\u5426\u4e3a1&#xff09;&#xff0c;\u76f4\u5230\u8fbe\u5230\u00a0maxfd &#043; 1\u00a0\u6307\u5b9a\u7684\u8303\u56f4\u3002\u4ece0\u5230\u00a0maxfd&#xff0c;\u65e0\u8bba\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u6253\u5f00\u6216\u6d3b\u8dc3\u3002\u8fd9\u79cd\u8bbe\u8ba1\u7b80\u5355\u4f46\u6548\u7387\u4f4e&#xff0c;\u662f\u00a0select\u00a0\u88ab\u00a0epoll\u00a0\u53d6\u4ee3\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00<\/li>\n<\/ul>\n<p>3.select(maxfd &#043; 1, &amp;rset, NULL, NULL, NULL);\u91cc\u7684rset\u4f5c\u7528\u662f\u4ec0\u4e48<\/p>\n<ul>\n<li>\u5728\u00a0select\u00a0\u51fd\u6570\u4e2d&#xff0c;rset\u00a0\u662f\u4e00\u4e2a\u00a0fd_set\u00a0\u7c7b\u578b\u7684\u4f4d\u56fe\u96c6\u5408&#xff0c;\u5176\u6838\u5fc3\u4f5c\u7528\u662f\u6807\u8bc6\u9700\u8981\u76d1\u63a7\u7684\u53ef\u8bfb\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;FD&#xff09;\u96c6\u5408&#xff0c;\u5e76\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6807\u8bb0\u54ea\u4e9bFD\u5df2\u5c31\u7eea\u53ef\u8bfb\u3002<\/li>\n<\/ul>\n<p>4.for (int i &#061; lfd &#043; 1; i &lt;&#061; maxfd; i&#043;&#043;) \u4e3a\u4ec0\u4e48\u4ece lfd &#043; 1\u5f00\u59cb\u904d\u5386&#xff1f;\u200b<\/p>\n<ul>\n<li>\u200blfd\u00a0\u901a\u5e38\u662f\u8f83\u5c0f\u7684\u503c&#xff1a;<br \/>\n\u5728 Unix\/Linux \u4e2d&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u5206\u914d\u3002lfd\u00a0\u662f\u670d\u52a1\u5668\u542f\u52a8\u65f6\u6700\u65e9\u521b\u5efa\u7684\u5957\u63a5\u5b57&#xff0c;\u5176\u503c\u901a\u5e38\u4e3a3&#xff08;0-2 \u88ab\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u5360\u7528&#xff09;&#xff0c;\u540e\u7eed\u00a0cfd\u00a0\u4f9d\u6b21\u9012\u589e&#xff08;4, 5, &#8230;&#xff09;\u3002<\/li>\n<li>\n\u56e0\u6b64\u00a0lfd &#043; 1\u00a0\u81ea\u7136\u6307\u5411\u7b2c\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u5957\u63a5\u5b57\u3002<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">client<\/h3>\n<p>\u5ba2\u6237\u7aef\u4f7f\u7528&#xff1a;<\/p>\n<p>nc 127.0.0.1 8888<\/p>\n<p>\u6a21\u62df\u5ba2\u6237\u7aef\u94fe\u63a5\u670d\u52a1\u5668<\/p>\n<p>\u7ed3\u679c\u663e\u793a\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"911\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163616-6888f880eab11.png\" width=\"2131\" \/><\/p>\n<p>\u4f46\u8fd9\u6837\u6709\u4e2a\u95ee\u9898&#xff0c;\u65e0\u8bba\u8fde\u63a5\u670d\u52a1\u5668\u7684\u5ba2\u6237\u7aef\u662f\u5426\u6d3b\u8dc3&#xff0c;\u904d\u5386\u65f6\u90fd\u4f1a\u904d\u5386\u8fd9\u4e9b\u8fde\u63a5\u7684\u5ba2\u6237\u7aef&#xff0c;\u6240\u4ee5\u8fd9\u5c31\u4f1a\u5f15\u53d1\u4e00\u4e2a\u95ee\u9898&#xff08;<span style=\"color:red\">\u5927\u91cf\u5e76\u53d1<\/span><span style=\"color:red\">,<\/span><span style=\"color:red\">\u5c11\u4e86\u6d3b\u8dc3<\/span>&#xff09;&#xff1a;<\/p>\n<p style=\"margin-left:0px;margin-right:0px\">\u5047\u8bbe\u73b0\u5728 4-1023\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u9700\u8981\u76d1\u542c,\u4f46\u662f5-1000\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u5173\u95ed\u4e86&#xff0c;\u904d\u5386\u65f6\u8fd8\u662f\u8981\u4ece4-1023\u8fdb\u884c\u904d\u5386&#xff0c;\u5b9e\u9645\u53ea\u9700\u8981\u904d\u53864\u30011001-1023\u5373\u53ef\u3002<\/p>\n<p style=\"margin-left:0px;margin-right:0px\">\u5047\u8bbe\u73b0\u5728 4-1023\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u9700\u8981\u76d1\u542c,\u4f46\u662f\u53ea\u6709 5,1002 \u53d1\u6765\u6d88\u606f&#xff0c;\u904d\u5386\u65f6\u8fd8\u662f\u8981\u4ece4-1023\u8fdb\u884c\u904d\u5386&#xff0c;\u5b9e\u9645\u53ea\u9700\u8981\u904d\u53865\u30011002\u5373\u53ef\u3002<\/p>\n<h3 style=\"margin-left:0px;margin-right:0px\">select\u8fdb\u9636\u4f18\u5316\u7248<\/h3>\n<p style=\"margin-left:0px;margin-right:0px\">\u4e4b\u524d\u7684\u4ee3\u7801&#xff0c;\u5982\u679c\u6700\u5927fd\u662f1023&#xff0c;\u6bcf\u6b21\u786e\u5b9a\u6709\u4e8b\u4ef6\u53d1\u751f\u7684fd\u65f6&#xff0c;\u5c31\u8981\u626b\u63cf3-1023\u7684\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8fd9\u770b\u8d77\u6765\u5f88\u8822\u3002<span style=\"color:#0d0016\">\u4e8e\u662f\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u628a\u8981\u76d1\u542c\u7684\u6d3b\u8dc3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5b58\u4e0b\u6765&#xff0c;\u6bcf\u6b21\u626b\u63cf\u8fd9\u4e2a\u6570\u7ec4\u5c31\u884c\u4e86\u3002\u770b\u8d77\u6765\u79d1\u5b66\u5f97\u591a\u3002<\/span><\/p>\n<h3 style=\"margin-left:0px;margin-right:0px\">server<\/h3>\n<p>\/\/\u8fdb\u9636\u7248select&#xff0c;\u901a\u8fc7\u6570\u7ec4\u9632\u6b62\u904d\u53861024\u4e2a\u63cf\u8ff0\u7b26<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;ctype.h&gt;<\/p>\n<p>#include &#034;wrap.h&#034;<\/p>\n<p>#define SERV_PORT 8888<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    int i, j, n, maxi;<\/p>\n<p>    int nready, client[FD_SETSIZE];                 \/* \u81ea\u5b9a\u4e49\u6570\u7ec4client, \u9632\u6b62\u904d\u53861024\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26  FD_SETSIZE\u9ed8\u8ba4\u4e3a1024 *\/<br \/>\n    int maxfd, listenfd, connfd, sockfd;<br \/>\n    char buf[BUFSIZ], str[INET_ADDRSTRLEN];         \/* #define INET_ADDRSTRLEN 16 *\/<br \/>\n    struct sockaddr_in clie_addr, serv_addr;<br \/>\n    socklen_t clie_addr_len;<br \/>\n    fd_set rset, allset;                            \/* rset \u8bfb\u4e8b\u4ef6\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408 allset\u7528\u6765\u6682\u5b58 *\/<\/p>\n<p>    listenfd &#061; Socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    \/\/\u7aef\u53e3\u590d\u7528<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    bzero(&amp;serv_addr, sizeof(serv_addr));<br \/>\n    serv_addr.sin_family&#061; AF_INET;<br \/>\n    serv_addr.sin_addr.s_addr &#061; htonl(INADDR_ANY);<br \/>\n    serv_addr.sin_port&#061; htons(SERV_PORT);<\/p>\n<p>    Bind(listenfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr));<br \/>\n    Listen(listenfd, 128);<\/p>\n<p>    maxfd &#061; listenfd;                                           \/* \u8d77\u521d listenfd \u5373\u4e3a\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26 *\/<\/p>\n<p>    maxi &#061; -1;                                                  \/* \u5c06\u6765\u7528\u4f5cclient[]\u7684\u4e0b\u6807, \u521d\u59cb\u503c\u6307\u54110\u4e2a\u5143\u7d20\u4e4b\u524d\u4e0b\u6807\u4f4d\u7f6e *\/<br \/>\n    for (i &#061; 0; i &lt; FD_SETSIZE; i&#043;&#043;)<br \/>\n        client[i] &#061; -1;                                         \/* \u7528-1\u521d\u59cb\u5316client[] *\/<\/p>\n<p>    FD_ZERO(&amp;allset);<br \/>\n    FD_SET(listenfd, &amp;allset);                                  \/* \u6784\u9020select\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6 *\/<\/p>\n<p>    while (1) {<br \/>\n        rset &#061; allset;                                          \/* \u6bcf\u6b21\u5faa\u73af\u65f6\u90fd\u4ece\u65b0\u8bbe\u7f6eselect\u76d1\u63a7\u4fe1\u53f7\u96c6 *\/<\/p>\n<p>        nready &#061; select(maxfd&#043;1, &amp;rset, NULL, NULL, NULL);      \/\/2  1&#8211;lfd  1&#8211;connfd<br \/>\n        if (nready &lt; 0)<br \/>\n            perr_exit(&#034;select error&#034;);<\/p>\n<p>        if (FD_ISSET(listenfd, &amp;rset)) {                        \/* \u8bf4\u660e\u6709\u65b0\u7684\u5ba2\u6237\u7aef\u94fe\u63a5\u8bf7\u6c42 *\/<\/p>\n<p>            clie_addr_len &#061; sizeof(clie_addr);<br \/>\n            connfd &#061; Accept(listenfd, (struct sockaddr *)&amp;clie_addr, &amp;clie_addr_len);       \/* Accept \u4e0d\u4f1a\u963b\u585e *\/<br \/>\n            printf(&#034;received from %s at PORT %d\\\\n&#034;,<br \/>\n                    inet_ntop(AF_INET, &amp;clie_addr.sin_addr, str, sizeof(str)),<br \/>\n                    ntohs(clie_addr.sin_port));<\/p>\n<p>            for (i &#061; 0; i &lt; FD_SETSIZE; i&#043;&#043;)<br \/>\n                if (client[i] &lt; 0) {                            \/* \u627eclient[]\u4e2d\u6ca1\u6709\u4f7f\u7528\u7684\u4f4d\u7f6e *\/<br \/>\n                    client[i] &#061; connfd;                         \/* \u4fdd\u5b58accept\u8fd4\u56de\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5230client[]\u91cc *\/<br \/>\n                    break;<br \/>\n                }<\/p>\n<p>            if (i &#061;&#061; FD_SETSIZE) {                              \/* \u8fbe\u5230select\u80fd\u76d1\u63a7\u7684\u6587\u4ef6\u4e2a\u6570\u4e0a\u9650 1024 *\/<br \/>\n                fputs(&#034;too many clients\\\\n&#034;, stderr);<br \/>\n                exit(1);<br \/>\n            }<\/p>\n<p>            FD_SET(connfd, &amp;allset);                            \/* \u5411\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408allset\u6dfb\u52a0\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26connfd *\/<\/p>\n<p>            if (connfd &gt; maxfd)<br \/>\n                maxfd &#061; connfd;                                 \/* select\u7b2c\u4e00\u4e2a\u53c2\u6570\u9700\u8981 *\/<\/p>\n<p>            if (i &gt; maxi)<br \/>\n                maxi &#061; i;                                       \/* \u4fdd\u8bc1maxi\u5b58\u7684\u603b\u662fclient[]\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4e0b\u6807 *\/<\/p>\n<p>            if (&#8211;nready &#061;&#061; 0)<br \/>\n                continue;<br \/>\n        } <\/p>\n<p>        for (i &#061; 0; i &lt;&#061; maxi; i&#043;&#043;) {                               \/* \u68c0\u6d4b\u54ea\u4e2aclients \u6709\u6570\u636e\u5c31\u7eea *\/<\/p>\n<p>            if ((sockfd &#061; client[i]) &lt; 0)<br \/>\n                continue;\/\/\u6570\u7ec4\u5185\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5982\u679c\u88ab\u91ca\u653e\u6709\u53ef\u80fd\u53d8\u6210-1<br \/>\n            if (FD_ISSET(sockfd, &amp;rset)) {<\/p>\n<p>                if ((n &#061; Read(sockfd, buf, sizeof(buf))) &#061;&#061; 0) {    \/* \u5f53client\u5173\u95ed\u94fe\u63a5\u65f6,\u670d\u52a1\u5668\u7aef\u4e5f\u5173\u95ed\u5bf9\u5e94\u94fe\u63a5 *\/<br \/>\n                    Close(sockfd);<br \/>\n                    FD_CLR(sockfd, &amp;allset);                        \/* \u89e3\u9664select\u5bf9\u6b64\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7 *\/<br \/>\n                    client[i] &#061; -1;<br \/>\n                } else if (n &gt; 0) {<br \/>\n                    for (j &#061; 0; j &lt; n; j&#043;&#043;)<br \/>\n                        buf[j] &#061; toupper(buf[j]);<br \/>\n                    Write(sockfd, buf, n);<br \/>\n                    Write(STDOUT_FILENO, buf, n);<br \/>\n                }<br \/>\n                if (&#8211;nready &#061;&#061; 0)<br \/>\n                    break;                                          \/* \u8df3\u51fafor, \u4f46\u8fd8\u5728while\u4e2d *\/<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    Close(listenfd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"background-color:transparent;margin-left:0px;margin-right:0px\">\u82e5\u00a0lfd &#061; 3&#xff0c;maxfd &#061; 100&#xff0c;\u4f46\u4ec5 5 \u4e2a\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u57fa\u7840\u7248\u4ecd\u9700\u5faa\u73af 97 \u6b21&#xff08;4~100&#xff09;&#xff0c;\u800c\u8fdb\u9636\u7248\u4ec5\u9700\u5faa\u73af 5 \u6b21&#xff08;\u6570\u7ec4\u4e2d\u7684\u6d3b\u8dc3 FD&#xff09;\u3002<\/p>\n<h3 style=\"background-color:transparent;margin-left:0px;margin-right:0px\">\u5c40\u9650\u6027<\/h3>\n<p style=\"margin-left:0px;margin-right:0px\">\n\u867d\u7136\u4f7f\u7528 select \u8fd9\u79cd IO \u591a\u8def\u8f6c\u63a5\u6280\u672f\u53ef\u4ee5\u964d\u4f4e\u7cfb\u7edf\u5f00\u9500&#xff0c;\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387&#xff0c;\u4f46\u662f\u5b83\u4e5f\u6709\u5c40\u9650\u6027&#xff1a;<\/p>\n<p style=\"margin-left:0px;margin-right:0px\">\n<p style=\"margin-left:0px\">\u5f85\u68c0\u6d4b\u96c6\u5408&#xff08;\u7b2c 2\u30013\u30014 \u4e2a\u53c2\u6570&#xff09;\u9700\u8981\u9891\u7e41\u7684\u5728\u7528\u6237\u533a\u548c\u5185\u6838\u533a\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u7684\u62f7\u8d1d&#xff0c;\u6548\u7387\u4f4e<\/p>\n<p style=\"margin-left:0px\">\u5185\u6838\u5bf9\u4e8e select \u4f20\u9012\u8fdb\u6765\u7684\u5f85\u68c0\u6d4b\u96c6\u5408\u7684\u68c0\u6d4b\u65b9\u5f0f\u662f\u7ebf\u6027\u7684<\/p>\n<p style=\"margin-left:0px\">\u68c0\u6d4b\u6548\u7387\u4e0e\u96c6\u5408\u5185\u5f85\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6709\u5173&#xff1a;\u5982\u679c\u96c6\u5408\u5185\u5f85\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5f88\u591a&#xff0c;\u68c0\u6d4b\u6548\u7387\u4f1a\u6bd4\u8f83\u4f4e&#xff1b;\u5982\u679c\u96c6\u5408\u5185\u5f85\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u76f8\u5bf9\u8f83\u5c11&#xff0c;\u68c0\u6d4b\u6548\u7387\u4f1a\u6bd4\u8f83\u9ad8<\/p>\n<p style=\"margin-left:0px\">\u4f7f\u7528 select \u80fd\u591f\u68c0\u6d4b\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570\u6709\u4e0a\u9650&#xff0c;\u9ed8\u8ba4\u662f 1024&#xff0c;\u8fd9\u662f\u5728\u5185\u6838\u4e2d\u88ab\u5199\u6b7b\u4e86\u7684<\/p>\n<p style=\"margin-left:0px\">\n<h2 style=\"background-color:transparent\">poll<\/h2>\n<p>poll \u7684\u673a\u5236\u4e0e select \u7c7b\u4f3c&#xff0c;\u4e0e select \u5728\u672c\u8d28\u4e0a\u6ca1\u6709\u591a\u5927\u5dee\u522b&#xff0c;\u4f7f\u7528\u65b9\u6cd5\u4e5f\u7c7b\u4f3c&#xff0c;\u4e0b\u9762\u7684\u662f\u5bf9\u4e8e\u4e8c\u8005\u7684\u5bf9\u6bd4&#xff1a;<\/p>\n<ul>\n<li>\u5185\u6838\u5bf9\u5e94\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u68c0\u6d4b\u4e5f\u662f\u4ee5\u7ebf\u6027\u7684\u65b9\u5f0f\u8fdb\u884c\u8f6e\u8be2&#xff0c;\u6839\u636e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u8fdb\u884c\u5904\u7406<\/li>\n<li>poll \u548c select \u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u4f1a\u5728\u68c0\u6d4b\u8fc7\u7a0b\u4e2d\u9891\u7e41\u7684\u8fdb\u884c\u7528\u6237\u533a\u548c\u5185\u6838\u533a\u7684\u62f7\u8d1d&#xff0c;\u5b83\u7684\u5f00\u9500\u968f\u7740\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u589e\u52a0\u800c\u7ebf\u6027\u589e\u5927&#xff0c;\u4ece\u800c\u6548\u7387\u4e5f\u4f1a\u8d8a\u6765\u8d8a\u4f4e\u3002<\/li>\n<li>select \u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570\u4e0a\u9650\u662f 1024&#xff0c;poll \u6ca1\u6709\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u9650\u5236<\/li>\n<li>select \u53ef\u4ee5\u8de8\u5e73\u53f0\u4f7f\u7528&#xff0c;poll \u53ea\u80fd\u5728 Linux \u5e73\u53f0\u4f7f\u7528<\/li>\n<li>select \u901a\u8fc7 fd_set&#xff08;\u4f4d\u56fe\u96c6\u5408&#xff09;\u6765\u8bb0\u5f55\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;poll \u4f7f\u7528\u4e00\u4e2a\u6574\u578b\u6570\u6765\u8bb0\u5f55<\/li>\n<\/ul>\n<p>\npoll \u51fd\u6570<\/p>\n<p>#include &lt;poll.h&gt;<br \/>\n\/\/ \u6bcf\u4e2a\u59d4\u6258poll\u68c0\u6d4b\u7684fd\u90fd\u5bf9\u5e94\u8fd9\u6837\u4e00\u4e2a\u7ed3\u6784\u4f53<br \/>\nstruct pollfd {<br \/>\n\u00a0 \u00a0 int \u00a0 fd; \u00a0 \u00a0 \u00a0 \u00a0 \/* \u59d4\u6258\u5185\u6838\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 *\/<br \/>\n\u00a0 \u00a0 short events; \u00a0 \u00a0 \/* \u59d4\u6258\u5185\u6838\u68c0\u6d4b\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4ec0\u4e48\u4e8b\u4ef6 *\/<br \/>\n\u00a0 \u00a0 short revents; \u00a0 \u00a0\/* \u6587\u4ef6\u63cf\u8ff0\u7b26\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6 -&gt; \u4f20\u51fa *\/ \u00a0\/\/ \u4e0d\u9700\u8981\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n};<\/p>\n<p>struct pollfd myfd[100]; \u00a0 \/\/ \u53ef\u80fd\u9700\u8981\u68c0\u6d4b\u82e5\u5e72\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8981\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d<br \/>\nint poll(struct pollfd *fds, nfds_t nfds, int timeout);<br \/>\n\u00a0<\/p>\n<p>\n\u53c2\u6570\u542b\u4e49&#xff1a;<\/p>\n<p>\nfds: \u8fd9\u662f\u4e00\u4e2a struct pollfd \u7c7b\u578b\u7684\u6570\u7ec4, \u91cc\u8fb9\u5b58\u50a8\u4e86\u5f85\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4fe1\u606f&#xff0c;\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u6709\u4e09\u4e2a\u6210\u5458&#xff1a;<\/p>\n<p>fd&#xff1a;\u59d4\u6258\u5185\u6838\u68c0\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\nevents&#xff1a;\u59d4\u6258\u5185\u6838\u68c0\u6d4b\u7684 fd \u4e8b\u4ef6&#xff08;\u8f93\u5165\u3001\u8f93\u51fa\u3001\u9519\u8bef&#xff09;&#xff0c;\u6bcf\u4e00\u4e2a\u4e8b\u4ef6\u6709\u591a\u4e2a\u53d6\u503c<br \/>\nrevents&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u4f20\u51fa\u53c2\u6570&#xff0c;\u6570\u636e\u7531\u5185\u6838\u5199\u5165&#xff0c;\u5b58\u50a8\u5185\u6838\u68c0\u6d4b\u4e4b\u540e\u7684\u7ed3\u679c&#xff08;\u4e0d\u9700\u8981\u521d\u59cb\u5316&#xff0c;\u6839\u636e events \u59d4\u6258\u5185\u6838\u68c0\u6d4b\u7684\u65f6\u95f4\u4f20\u51fa\u7ed3\u679c&#xff09;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163619-6888f88302b0c.png\" \/><\/p>\n<p>nfds: \u8fd9\u662f\u7b2c\u4e00\u4e2a\u53c2\u6570\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u4e2a\u6709\u6548\u5143\u7d20\u7684\u4e0b\u6807 &#043; 1&#xff08;\u4e5f\u53ef\u4ee5\u6307\u5b9a\u53c2\u6570 1 \u6570\u7ec4\u7684\u5143\u7d20\u603b\u4e2a\u6570&#xff09;<\/p>\n<p>timeout: \u6307\u5b9a poll \u51fd\u6570\u7684\u963b\u585e\u65f6\u957f<\/p>\n<p>-1&#xff1a;\u4e00\u76f4\u963b\u585e&#xff0c;\u76f4\u5230\u68c0\u6d4b\u7684\u96c6\u5408\u4e2d\u6709\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u6709\u4e8b\u4ef6\u4ea7\u751f&#xff09;\u89e3\u9664\u963b\u585e<br \/>\n0&#xff1a;\u4e0d\u963b\u585e&#xff0c;\u4e0d\u7ba1\u68c0\u6d4b\u96c6\u5408\u4e2d\u6709\u6ca1\u6709\u5df2\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u51fd\u6570\u9a6c\u4e0a\u8fd4\u56de<br \/>\n\u5927\u4e8e 0&#xff1a;\u963b\u585e\u6307\u5b9a\u7684\u6beb\u79d2&#xff08;ms&#xff09;\u6570\u4e4b\u540e&#xff0c;\u89e3\u9664\u963b\u585e<\/p>\n<p>\n\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u96c6\u5408\u4e2d\u5df2\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u603b\u4e2a\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de-1<\/p>\n<\/p>\n<h3>server<\/h3>\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;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;poll.h&gt;<br \/>\n#include &lt;errno.h&gt;<br \/>\n#include &#034;wrap.h&#034; \/\/ \u81ea\u5b9a\u4e49\u7684\u5305\u88f9\u51fd\u6570\u5934\u6587\u4ef6&#xff08;\u5982Socket\u3001Bind\u7b49&#xff09;<\/p>\n<p>#define MAXLINE 80     \/\/ \u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n#define SERV_PORT 6666 \/\/ \u670d\u52a1\u5668\u7aef\u53e3<br \/>\n#define OPEN_MAX 1024  \/\/ \u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    int i, j, maxi, listenfd, connfd, sockfd;<br \/>\n    int nready;                              \/\/ poll\u8fd4\u56de\u7684\u5c31\u7eea\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf<br \/>\n    ssize_t n;                               \/\/ \u8bfb\u53d6\u7684\u5b57\u8282\u6570<br \/>\n    char buf[MAXLINE], str[INET_ADDRSTRLEN]; \/\/ \u7f13\u51b2\u533a\u548cIP\u5730\u5740\u5b57\u7b26\u4e32<br \/>\n    socklen_t clilen;                        \/\/ \u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6<br \/>\n    struct pollfd client[OPEN_MAX];          \/\/ poll\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u7ec4<br \/>\n    struct sockaddr_in cliaddr, servaddr;    \/\/ \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5730\u5740\u7ed3\u6784<\/p>\n<p>    \/* 1. \u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57 *\/<br \/>\n    listenfd &#061; Socket(AF_INET, SOCK_STREAM, 0); \/\/ IPv4 TCP\u5957\u63a5\u5b57<\/p>\n<p>    \/* 2. \u7ed1\u5b9a\u670d\u52a1\u5668\u5730\u5740 *\/<br \/>\n    bzero(&amp;servaddr, sizeof(servaddr));           \/\/ \u6e05\u7a7a\u7ed3\u6784\u4f53<br \/>\n    servaddr.sin_family &#061; AF_INET;                \/\/ IPv4<br \/>\n    servaddr.sin_addr.s_addr &#061; htonl(INADDR_ANY); \/\/ \u76d1\u542c\u6240\u6709\u672c\u5730IP<br \/>\n    servaddr.sin_port &#061; htons(SERV_PORT);         \/\/ \u8bbe\u7f6e\u7aef\u53e3<br \/>\n    Bind(listenfd, (struct sockaddr *)&amp;servaddr, sizeof(servaddr));<\/p>\n<p>    \/* 3. \u5f00\u59cb\u76d1\u542c *\/<br \/>\n    Listen(listenfd, 20); \/\/ \u76d1\u542c\u961f\u5217\u6700\u5927\u957f\u5ea6\u4e3a20<\/p>\n<p>    \/* 4. \u521d\u59cb\u5316poll\u76d1\u63a7\u6570\u7ec4 *\/<br \/>\n    client[0].fd &#061; listenfd;       \/\/ \u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n    client[0].events &#061; POLLRDNORM; \/\/ \u76d1\u542c\u666e\u901a\u8bfb\u4e8b\u4ef6&#xff08;\u65b0\u8fde\u63a5&#xff09;<br \/>\n    for (i &#061; 1; i &lt; OPEN_MAX; i&#043;&#043;)<br \/>\n        client[i].fd &#061; -1; \/\/ \u5176\u4f59\u5143\u7d20\u521d\u59cb\u5316\u4e3a-1&#xff08;\u8868\u793a\u7a7a\u95f2&#xff09;<br \/>\n    maxi &#061; 0;              \/\/ \u5f53\u524dclient\u6570\u7ec4\u4e2d\u6709\u6548\u5143\u7d20\u7684\u6700\u5927\u4e0b\u6807<\/p>\n<p>    \/* 5. \u4e3b\u5faa\u73af&#xff1a;\u5904\u7406poll\u4e8b\u4ef6 *\/<br \/>\n    for (;;)<br \/>\n    {<br \/>\n        \/\/ \u963b\u585e\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u76d1\u63a7maxi&#043;1\u4e2a\u63cf\u8ff0\u7b26&#xff08;\u4ece0\u5230maxi&#xff09;&#xff0c;\u65e0\u9650\u7b49\u5f85<br \/>\n        nready &#061; poll(client, maxi &#043; 1, -1);<\/p>\n<p>        \/* 5.1 \u5904\u7406\u76d1\u542c\u5957\u63a5\u5b57&#xff08;\u65b0\u8fde\u63a5&#xff09; *\/<br \/>\n        if (client[0].revents &amp; POLLRDNORM) \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u53ef\u8bfb&#xff08;\u6709\u65b0\u8fde\u63a5&#xff09;<br \/>\n        {<br \/>\n            clilen &#061; sizeof(cliaddr);<br \/>\n            connfd &#061; Accept(listenfd, (struct sockaddr *)&amp;cliaddr, &amp;clilen);<br \/>\n            printf(&#034;received from %s at PORT %d\\\\n&#034;,<br \/>\n                   inet_ntop(AF_INET, &amp;cliaddr.sin_addr, str, sizeof(str)),<br \/>\n                   ntohs(cliaddr.sin_port)); \/\/ \u6253\u5370\u5ba2\u6237\u7aefIP\u548c\u7aef\u53e3<\/p>\n<p>            \/* \u5c06\u65b0\u8fde\u63a5\u52a0\u5165client\u6570\u7ec4 *\/<br \/>\n            for (i &#061; 1; i &lt; OPEN_MAX; i&#043;&#043;)<br \/>\n            {<br \/>\n                if (client[i].fd &lt; 0)<br \/>\n                {                          \/\/ \u627e\u5230\u7b2c\u4e00\u4e2a\u7a7a\u95f2\u4f4d\u7f6e<br \/>\n                    client[i].fd &#061; connfd; \/\/ \u5b58\u50a8\u65b0\u8fde\u63a5\u7684\u63cf\u8ff0\u7b26<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            if (i &#061;&#061; OPEN_MAX) \/\/ \u8d85\u8fc7\u6700\u5927\u8fde\u63a5\u6570\u9650\u5236<br \/>\n                perr_exit(&#034;too many clients&#034;);<\/p>\n<p>            client[i].events &#061; POLLRDNORM; \/\/ \u5bf9\u65b0\u8fde\u63a5\u76d1\u63a7\u8bfb\u4e8b\u4ef6<br \/>\n            if (i &gt; maxi)<br \/>\n                maxi &#061; i; \/\/ \u66f4\u65b0\u6700\u5927\u6709\u6548\u4e0b\u6807<\/p>\n<p>            if (&#8211;nready &lt;&#061; 0) \/\/ \u5982\u679c\u6ca1\u6709\u66f4\u591a\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u7ee7\u7eedpoll<br \/>\n                continue;<br \/>\n        }<\/p>\n<p>        \/* 5.2 \u5904\u7406\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u7684\u6570\u636e *\/<br \/>\n        for (i &#061; 1; i &lt;&#061; maxi; i&#043;&#043;) \/\/ \u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u8fde\u63a5<br \/>\n        {<br \/>\n            if ((sockfd &#061; client[i].fd) &lt; 0) \/\/ \u8df3\u8fc7\u65e0\u6548\u63cf\u8ff0\u7b26<br \/>\n                continue;<\/p>\n<p>            \/* \u68c0\u67e5\u8bfb\u4e8b\u4ef6\u6216\u9519\u8bef\u4e8b\u4ef6 *\/<br \/>\n            if (client[i].revents &amp; (POLLRDNORM | POLLERR))<br \/>\n            {<br \/>\n                n &#061; Read(sockfd, buf, MAXLINE); \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n                if (n &lt; 0)<br \/>\n                {<br \/>\n                    \/\/ \u8bfb\u53d6\u9519\u8bef<br \/>\n                    if (errno &#061;&#061; ECONNRESET) \/\/ \u5ba2\u6237\u7aef\u53d1\u9001RST\u91cd\u7f6e\u8fde\u63a5<br \/>\n                    {<br \/>\n                        printf(&#034;client[%d] aborted connection\\\\n&#034;, i);<br \/>\n                        Close(sockfd);<br \/>\n                        client[i].fd &#061; -1; \/\/ \u91cd\u7f6e\u4e3a\u672a\u4f7f\u7528<br \/>\n                    }<br \/>\n                    else<br \/>\n                    {<br \/>\n                        perr_exit(&#034;read error&#034;); \/\/ \u5176\u4ed6\u9519\u8bef\u76f4\u63a5\u9000\u51fa<br \/>\n                    }<br \/>\n                }<br \/>\n                else if (n &#061;&#061; 0) \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n                {<br \/>\n                    printf(&#034;client[%d] closed connection\\\\n&#034;, i);<br \/>\n                    Close(sockfd);<br \/>\n                    client[i].fd &#061; -1;<br \/>\n                }<br \/>\n                else \/\/ \u6b63\u5e38\u8bfb\u53d6\u6570\u636e<br \/>\n                {<br \/>\n                    for (j &#061; 0; j &lt; n; j&#043;&#043;) \/\/ \u8f6c\u4e3a\u5927\u5199<br \/>\n                        buf[j] &#061; toupper(buf[j]);<br \/>\n                    Writen(sockfd, buf, n); \/\/ \u56de\u5199\u7ed9\u5ba2\u6237\u7aef<br \/>\n                }<\/p>\n<p>                if (&#8211;nready &lt;&#061; 0) \/\/ \u6ca1\u6709\u66f4\u591a\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u8df3\u51fa\u5faa\u73af<br \/>\n                    break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3 style=\"background-color:transparent\">client<\/h3>\n<p>\u5ba2\u6237\u7aef\u4f7f\u7528&#xff1a;<\/p>\n<p>nc 127.0.0.1 6666<\/p>\n<p>\u6a21\u62df\u5ba2\u6237\u7aef\u94fe\u63a5\u670d\u52a1\u5668<\/p>\n<\/p>\n<h2 style=\"background-color:transparent\">epoll<\/h2>\n<p>epoll\u662fLinux\u4e0b\u591a\u8def\u590d\u7528IO\u63a5\u53e3select\/poll\u7684\u589e\u5f3a\u7248\u672c&#xff0c;\u5b83\u80fd\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u5728\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u4e2d\u53ea\u6709\u5c11\u91cf\u6d3b\u8dc3\u7684\u60c5\u51b5\u4e0b\u7684\u7cfb\u7edfCPU\u5229\u7528\u7387&#xff0c;\u56e0\u4e3a\u5b83\u4f1a\u590d\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u6765\u4f20\u9012\u7ed3\u679c\u800c\u4e0d\u7528\u8feb\u4f7f\u5f00\u53d1\u8005\u6bcf\u6b21\u7b49\u5f85\u4e8b\u4ef6\u4e4b\u524d\u90fd\u5fc5\u987b\u91cd\u65b0\u51c6\u5907\u8981\u88ab\u4fa6\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u53e6\u4e00\u70b9\u539f\u56e0\u5c31\u662f\u83b7\u53d6\u4e8b\u4ef6\u7684\u65f6\u5019&#xff0c;\u5b83\u65e0\u987b\u904d\u5386\u6574\u4e2a\u88ab\u4fa6\u542c\u7684\u63cf\u8ff0\u7b26\u96c6&#xff0c;\u53ea\u8981\u904d\u5386\u90a3\u4e9b\u88ab\u5185\u6838IO\u4e8b\u4ef6\u5f02\u6b65\u5524\u9192\u800c\u52a0\u5165Ready\u961f\u5217\u7684\u63cf\u8ff0\u7b26\u96c6\u5408\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u76ee\u524depell\u662flinux\u5927\u89c4\u6a21\u5e76\u53d1\u7f51\u7edc\u7a0b\u5e8f\u4e2d\u7684\u70ed\u95e8\u9996\u9009\u6a21\u578b\u3002<\/p>\n<p>epoll\u9664\u4e86\u63d0\u4f9bselect\/poll\u90a3\u79cdIO\u4e8b\u4ef6\u7684\u7535\u5e73\u89e6\u53d1&#xff08;Level Triggered&#xff09;\u5916&#xff0c;\u8fd8\u63d0\u4f9b\u4e86\u8fb9\u6cbf\u89e6\u53d1&#xff08;Edge Triggered&#xff09;&#xff0c;\u8fd9\u5c31\u4f7f\u5f97\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u6709\u53ef\u80fd\u7f13\u5b58IO\u72b6\u6001&#xff0c;\u51cf\u5c11epoll_wait\/epoll_pwait\u7684\u8c03\u7528&#xff0c;\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528cat\u547d\u4ee4\u67e5\u770b\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u6253\u5f00\u7684socket\u63cf\u8ff0\u7b26\u4e0a\u9650\u3002<\/p>\n<p>cat \/proc\/sys\/fs\/file-max<\/p>\n<p>\u5982\u6709\u9700\u8981&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u4fee\u6539\u8be5\u4e0a\u9650\u503c\u3002<\/p>\n<p>sudo vi \/etc\/security\/limits.conf<\/p>\n<p>\u5728\u6587\u4ef6\u5c3e\u90e8\u5199\u5165\u4ee5\u4e0b\u914d\u7f6e,soft\u8f6f\u9650\u5236&#xff0c;hard\u786c\u9650\u5236\u3002\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p>* soft nofile 65536<\/p>\n<p>* hard nofile 100000<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"475\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163619-6888f8837b9a6.png\" width=\"818\" \/><\/p>\n<\/p>\n<h3>\u57fa\u7840API<\/h3>\n<p>1.\u521b\u5efa\u4e00\u4e2aepoll\u53e5\u67c4&#xff0c;\u53c2\u6570size\u7528\u6765\u544a\u8bc9\u5185\u6838\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e2a\u6570&#xff0c;\u8ddf\u5185\u5b58\u5927\u5c0f\u6709\u5173\u3002<\/p>\n<p>#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int epoll_create(int size) size&#xff1a;\u76d1\u542c\u6570\u76ee<\/p>\n<p>2.\u63a7\u5236\u67d0\u4e2aepoll\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6&#xff1a;\u6ce8\u518c\u3001\u4fee\u6539\u3001\u5220\u9664\u3002<\/p>\n<p>#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)<\/p>\n<p>epfd&#xff1a; \u4e3aepoll_creat\u7684\u53e5\u67c4<\/p>\n<\/p>\n<p>op&#xff1a; \u8868\u793a\u52a8\u4f5c&#xff0c;\u75283\u4e2a\u5b8f\u6765\u8868\u793a&#xff1a;<\/p>\n<p>EPOLL_CTL_ADD (\u6ce8\u518c\u65b0\u7684fd\u5230epfd)&#xff0c;<\/p>\n<p>EPOLL_CTL_MOD (\u4fee\u6539\u5df2\u7ecf\u6ce8\u518c\u7684fd\u7684\u76d1\u542c\u4e8b\u4ef6)&#xff0c;<\/p>\n<p>EPOLL_CTL_DEL (\u4eceepfd\u5220\u9664\u4e00\u4e2afd)&#xff1b;<\/p>\n<\/p>\n<p>event&#xff1a; \u544a\u8bc9\u5185\u6838\u9700\u8981\u76d1\u542c\u7684\u4e8b\u4ef6<\/p>\n<\/p>\n<p>struct epoll_event {<\/p>\n<p>__uint32_t events; \/* Epoll events *\/<\/p>\n<p>epoll_data_t data; \/* User data variable *\/<\/p>\n<p>};<\/p>\n<\/p>\n<p>typedef union epoll_data {<\/p>\n<p>void *ptr;<\/p>\n<p>int fd;<\/p>\n<p>uint32_t u32;<\/p>\n<p>uint64_t u64;<\/p>\n<p>} epoll_data_t;<\/p>\n<\/p>\n<p>EPOLLIN &#xff1a; \u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u8bfb&#xff08;\u5305\u62ec\u5bf9\u7aefSOCKET\u6b63\u5e38\u5173\u95ed&#xff09;<\/p>\n<p>EPOLLOUT&#xff1a; \u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u5199<\/p>\n<p>EPOLLPRI&#xff1a; \u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6709\u7d27\u6025\u7684\u6570\u636e\u53ef\u8bfb&#xff08;\u8fd9\u91cc\u5e94\u8be5\u8868\u793a\u6709\u5e26\u5916\u6570\u636e\u5230\u6765&#xff09;<\/p>\n<p>EPOLLERR&#xff1a; \u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u751f\u9519\u8bef<\/p>\n<p>EPOLLHUP&#xff1a; \u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u6302\u65ad&#xff1b;<\/p>\n<p>EPOLLET&#xff1a; \u5c06EPOLL\u8bbe\u4e3a\u8fb9\u7f18\u89e6\u53d1(Edge Triggered)\u6a21\u5f0f&#xff0c;\u8fd9\u662f\u76f8\u5bf9\u4e8e\u6c34\u5e73\u89e6\u53d1(Level Triggered)\u800c\u8a00\u7684<\/p>\n<p>EPOLLONESHOT&#xff1a;\u53ea\u76d1\u542c\u4e00\u6b21\u4e8b\u4ef6&#xff0c;\u5f53\u76d1\u542c\u5b8c\u8fd9\u6b21\u4e8b\u4ef6\u4e4b\u540e&#xff0c;\u5982\u679c\u8fd8\u9700\u8981\u7ee7\u7eed\u76d1\u542c\u8fd9\u4e2asocket\u7684\u8bdd&#xff0c;\u9700\u8981\u518d\u6b21\u628a\u8fd9\u4e2asocket\u52a0\u5165\u5230EPOLL\u961f\u5217\u91cc<\/p>\n<p>3.\u7b49\u5f85\u6240\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u6709\u4e8b\u4ef6\u7684\u4ea7\u751f&#xff0c;\u7c7b\u4f3c\u4e8eselect()\u8c03\u7528\u3002<\/p>\n<p>#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)<\/p>\n<p>events&#xff1a; \u7528\u6765\u5b58\u5185\u6838\u5f97\u5230\u4e8b\u4ef6\u7684\u96c6\u5408&#xff0c;<\/p>\n<p>maxevents&#xff1a; \u544a\u4e4b\u5185\u6838\u8fd9\u4e2aevents\u6709\u591a\u5927&#xff0c;\u8fd9\u4e2amaxevents\u7684\u503c\u4e0d\u80fd\u5927\u4e8e\u521b\u5efaepoll_create()\u65f6\u7684size&#xff0c;<\/p>\n<p>timeout&#xff1a; \u662f\u8d85\u65f6\u65f6\u95f4<\/p>\n<p>-1&#xff1a; \u963b\u585e<\/p>\n<p>0&#xff1a; \u7acb\u5373\u8fd4\u56de&#xff0c;\u975e\u963b\u585e<\/p>\n<p>&gt;0&#xff1a; \u6307\u5b9a\u6beb\u79d2<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a; \u6210\u529f\u8fd4\u56de\u6709\u591a\u5c11\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u65f6\u95f4\u5230\u65f6\u8fd4\u56de0&#xff0c;\u51fa\u9519\u8fd4\u56de-1<\/p>\n<h3 style=\"background-color:transparent\">server<\/h3>\n<p>\u5904\u7406\u6d41\u7a0b&#xff1a;<\/p>\n<p>\n1.\u521b\u5efa epoll \u5b9e\u4f8b\u5bf9\u8c61 epoll_create<\/p>\n<p>2.\u5c06\u7528\u4e8e\u76d1\u542c\u7684\u5957\u63a5\u5b57\u6dfb\u52a0\u5230 epoll \u5b9e\u4f8b\u4e2d epoll_ctl<\/p>\n<p>3.\u68c0\u6d4b\u6dfb\u52a0\u5230 epoll \u5b9e\u4f8b\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u5df2\u5c31\u7eea&#xff0c;\u5e76\u5c06\u8fd9\u4e9b\u5df2\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u5904\u7406 epoll_wait<\/p>\n<ul>\n<li>\u5982\u679c\u662f\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u548c\u65b0\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5&#xff0c;\u5c06\u5f97\u5230\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230 epoll \u5b9e\u4f8b\u4e2d<\/li>\n<li>\u5982\u679c\u662f\u901a\u4fe1\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u548c\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u901a\u4fe1&#xff0c;\u5982\u679c\u8fde\u63a5\u5df2\u65ad\u5f00&#xff0c;\u5c06\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ece epoll \u5b9e\u4f8b\u4e2d\u5220\u9664<br \/>\n\u00a0<\/li>\n<\/ul>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &#034;wrap.h&#034;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n\/\/\u521b\u5efa\u5957\u63a5\u5b57 \u7ed1\u5b9a<br \/>\nint lfd &#061; tcp4bind(8000,NULL);<br \/>\n\/\/\u76d1\u542c<br \/>\nListen(lfd,128);<br \/>\n\/\/\u521b\u5efa\u6811<br \/>\nint epfd &#061; epoll_create(1);<br \/>\n\/\/\u5c06lfd\u4e0a\u6811<br \/>\nstruct epoll_event ev,evs[1024];<br \/>\nev.data.fd &#061; lfd;<br \/>\nev.events &#061; EPOLLIN;<br \/>\nepoll_ctl(epfd,EPOLL_CTL_ADD,lfd,&amp;ev);<br \/>\n\/\/while\u76d1\u542c<br \/>\nwhile(1)<br \/>\n{<br \/>\nint nready &#061; epoll_wait(epfd,evs,1024,-1);\/\/\u76d1\u542c<br \/>\nprintf(&#034;epoll wait _________________\\\\n&#034;);<br \/>\nif(nready &lt;0)<br \/>\n{<br \/>\nperror(&#034;&#034;);<br \/>\nbreak;<br \/>\n}<br \/>\nelse if( nready &#061;&#061; 0)<br \/>\n{<br \/>\ncontinue;<br \/>\n}<br \/>\nelse\/\/\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d8\u5316<br \/>\n{<br \/>\nint i &#061; 0;<br \/>\nfor( i&#061;0;i&lt;nready;i&#043;&#043;)<br \/>\n{<br \/>\n\/\/\u5224\u65adlfd\u53d8\u5316,\u5e76\u4e14\u662f\u8bfb\u4e8b\u4ef6\u53d8\u5316<br \/>\nif(evs[i].data.fd &#061;&#061; lfd &amp;&amp; evs[i].events &amp; EPOLLIN)<br \/>\n{<br \/>\nstruct sockaddr_in cliaddr;<br \/>\nchar ip[16]&#061;&#034;&#034;;<br \/>\nsocklen_t len &#061; sizeof(cliaddr);<br \/>\nint cfd &#061; Accept(lfd,(struct sockaddr *)&amp;cliaddr,&amp;len);\/\/\u63d0\u53d6\u65b0\u7684\u8fde\u63a5<\/p>\n<p>printf(&#034;new client ip&#061;%s port &#061;%d\\\\n&#034;,inet_ntop(AF_INET,&amp;cliaddr.sin_addr.s_addr,ip,16)<br \/>\n,ntohs(cliaddr.sin_port));<br \/>\n\/\/\u8bbe\u7f6ecfd\u4e3a\u975e\u963b\u585e<br \/>\nint flags &#061; fcntl(cfd,F_GETFL);\/\/\u83b7\u53d6\u7684cfd\u7684\u6807\u5fd7\u4f4d<br \/>\nflags |&#061; O_NONBLOCK;<br \/>\nfcntl(cfd,F_SETFL,flags);<br \/>\n\/\/\u5c06cfd\u4e0a\u6811<br \/>\nev.data.fd &#061;cfd;<br \/>\nev.events &#061;EPOLLIN | EPOLLET;\/\/\u8bbe\u7f6e\u4e3a\u8fb9\u6cbf\u89e6\u53d1<br \/>\nepoll_ctl(epfd,EPOLL_CTL_ADD,cfd,&amp;ev);<\/p>\n<p>}<br \/>\nelse if( evs[i].events &amp; EPOLLIN)\/\/cfd\u53d8\u5316 ,\u800c\u4e14\u662f\u8bfb\u4e8b\u4ef6\u53d8\u5316<br \/>\n{<br \/>\nwhile(1)<br \/>\n{<br \/>\nchar buf[4]&#061;&#034;&#034;;<br \/>\n\/\/\u5982\u679c\u8bfb\u4e00\u4e2a\u7f13\u51b2\u533a,\u7f13\u51b2\u533a\u6ca1\u6709\u6570\u636e,\u5982\u679c\u662f\u5e26\u963b\u585e,\u5c31\u963b\u585e\u7b49\u5f85,\u5982\u679c<br \/>\n\/\/\u662f\u975e\u963b\u585e,\u8fd4\u56de\u503c\u7b49\u4e8e-1,\u5e76\u4e14\u4f1a\u5c06errno \u503c\u8bbe\u7f6e\u4e3aEAGAIN<br \/>\nint n &#061; read(evs[i].data.fd,buf,sizeof(buf));<br \/>\nif(n &lt; 0)\/\/\u51fa\u9519,cfd\u4e0b\u6811<br \/>\n{<br \/>\n\/\/\u5982\u679c\u7f13\u51b2\u533a\u8bfb\u5e72\u51c0\u4e86,\u8fd9\u4e2a\u65f6\u5019\u5e94\u8be5\u8df3\u51fawhile(1)\u5faa\u73af,\u7ee7\u7eed\u76d1\u542c<br \/>\nif(errno &#061;&#061; EAGAIN)<br \/>\n{<br \/>\nbreak;<\/p>\n<p>}<br \/>\n\/\/\u666e\u901a\u9519\u8bef<br \/>\nperror(&#034;&#034;);<br \/>\nclose(evs[i].data.fd);\/\/\u5c06cfd\u5173\u95ed<br \/>\nepoll_ctl(epfd,EPOLL_CTL_DEL,evs[i].data.fd,&amp;evs[i]);<br \/>\nbreak;<br \/>\n}<br \/>\nelse if(n &#061;&#061; 0)\/\/\u5ba2\u6237\u7aef\u5173\u95ed ,<br \/>\n{<br \/>\nprintf(&#034;client close\\\\n&#034;);<br \/>\nclose(evs[i].data.fd);\/\/\u5c06cfd\u5173\u95ed<br \/>\nepoll_ctl(epfd,EPOLL_CTL_DEL,evs[i].data.fd,&amp;evs[i]);\/\/\u4e0b\u6811<br \/>\nbreak;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/printf(&#034;%s\\\\n&#034;,buf);<br \/>\nwrite(STDOUT_FILENO,buf,4);<br \/>\nwrite(evs[i].data.fd,buf,n);<\/p>\n<p>}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<h3 style=\"background-color:transparent\">client<\/h3>\n<p>\u5ba2\u6237\u7aef\u4f7f\u7528&#xff1a;<\/p>\n<p>nc 127.0.0.1 8000<\/p>\n<p>\u6a21\u62df\u5ba2\u6237\u7aef\u94fe\u63a5\u670d\u52a1\u5668<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb869\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u591a\u8defIO\u8f6c\u63a5\u670d\u52a1\u5668\u4e5f\u53eb\u505a\u591a\u4efb\u52a1IO\u670d\u52a1\u5668\u3002\u8be5\u7c7b\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u4e3b\u65e8\u601d\u60f3\u662f\uff0c\u4e0d\u518d\u7531\u5e94\u7528\u7a0b\u5e8f\u81ea\u5df1\u76d1\u89c6\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u53d6\u800c\u4ee3\u4e4b\u7531\u5185\u6838\u66ff\u5e94\u7528\u7a0b\u5e8f\u76d1\u89c6\u6587\u4ef6\u3002<\/p>\n","protected":false},"author":2,"featured_media":45581,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,192,884,100,43],"topic":[],"class_list":{"0":"post-45586","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-linux","10":"tag-socket","11":"tag-100","12":"tag-43"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\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\/45586.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb869\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u591a\u8defIO\u8f6c\u63a5\u670d\u52a1\u5668\u4e5f\u53eb\u505a\u591a\u4efb\u52a1IO\u670d\u52a1\u5668\u3002\u8be5\u7c7b\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u4e3b\u65e8\u601d\u60f3\u662f\uff0c\u4e0d\u518d\u7531\u5e94\u7528\u7a0b\u5e8f\u81ea\u5df1\u76d1\u89c6\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u53d6\u800c\u4ee3\u4e4b\u7531\u5185\u6838\u66ff\u5e94\u7528\u7a0b\u5e8f\u76d1\u89c6\u6587\u4ef6\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/45586.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T16:36:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163610-6888f87a0de08.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45586.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/45586.html\",\"name\":\"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T16:36:20+00:00\",\"dateModified\":\"2025-07-29T16:36:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/45586.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/45586.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45586.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\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":"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\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\/45586.html","og_locale":"zh_CN","og_type":"article","og_title":"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb869\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u591a\u8defIO\u8f6c\u63a5\u670d\u52a1\u5668\u4e5f\u53eb\u505a\u591a\u4efb\u52a1IO\u670d\u52a1\u5668\u3002\u8be5\u7c7b\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u4e3b\u65e8\u601d\u60f3\u662f\uff0c\u4e0d\u518d\u7531\u5e94\u7528\u7a0b\u5e8f\u81ea\u5df1\u76d1\u89c6\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u53d6\u800c\u4ee3\u4e4b\u7531\u5185\u6838\u66ff\u5e94\u7528\u7a0b\u5e8f\u76d1\u89c6\u6587\u4ef6\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/45586.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T16:36:20+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729163610-6888f87a0de08.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/45586.html","url":"https:\/\/www.wsisp.com\/helps\/45586.html","name":"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T16:36:20+00:00","dateModified":"2025-07-29T16:36:20+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/45586.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/45586.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/45586.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u591a\u8defI\/O\u8f6c\u63a5\u670d\u52a1\u5668\uff08select\u3001poll\u3001epoll\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\/45586","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=45586"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/45586\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/45581"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=45586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=45586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=45586"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=45586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}