{"id":102463,"date":"2026-09-08T20:26:02","date_gmt":"2026-09-08T12:26:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/102463.html"},"modified":"2026-09-08T20:26:02","modified_gmt":"2026-09-08T12:26:02","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-linux-io-%e6%a8%a1%e5%9e%8b%ef%bc%88%e4%ba%8c%ef%bc%89%ef%bc%9a%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8-select","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/102463.html","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8%20IO-toc\" style=\"margin-left:0px\">\u591a\u8def\u590d\u7528 IO<\/p>\n<p id=\"select%20%E7%9A%84%E6%A0%B8%E5%BF%83%E5%AE%9A%E4%BD%8D-toc\" style=\"margin-left:0px\">select \u7684\u6838\u5fc3\u5b9a\u4f4d<\/p>\n<p id=\"select%20%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:0px\">select \u63a5\u53e3<\/p>\n<p id=\"%E5%9F%BA%E4%BA%8E%20Select%20%E7%9A%84%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%20%E2%80%94%E2%80%94%20EchoServer-toc\" style=\"margin-left:0px\">\u57fa\u4e8e Select \u7684\u591a\u8def\u590d\u7528\u670d\u52a1\u5668 \u2014\u2014 EchoServer<\/p>\n<p id=\"SelectServer.hpp-toc\" style=\"margin-left:40px\">SelectServer.hpp<\/p>\n<p id=\"Socket.hpp-toc\" style=\"margin-left:40px\">Socket.hpp<\/p>\n<p id=\"select%20%E7%89%B9%E7%82%B9-toc\" style=\"margin-left:0px\">select \u7279\u70b9<\/p>\n<p id=\"select%20%E7%BC%BA%E7%82%B9-toc\" style=\"margin-left:0px\">select \u7f3a\u70b9<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8%20IO\">\u591a\u8def\u590d\u7528 IO<\/h2>\n<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff1a;\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b&#xff08;\u4e00&#xff09;&#xff1a;\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u4e0e\u4fe1\u53f7\u9a71\u52a8 IO\u00a0\u4e2d&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u548c\u4fe1\u53f7\u9a71\u52a8 IO\u3002\u5176\u4e2d&#xff0c;\u975e\u963b\u585e IO \u867d\u7136\u80fd\u907f\u514d\u8fdb\u7a0b\u56e0\u4e3a\u7b49\u5f85\u6570\u636e\u800c\u963b\u585e&#xff0c;\u4f46\u5982\u679c\u9700\u8981\u540c\u65f6\u7ba1\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5c31\u9700\u8981\u4e0d\u65ad\u8f6e\u8be2\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u9020\u6210\u5927\u91cf\u7684 CPU \u6d6a\u8d39\u3002<\/p>\n<p>\u90a3\u4e48&#xff0c;\u6709\u6ca1\u6709\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u8ba9\u6211\u4eec\u53ea\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5c31\u80fd\u591f\u540c\u65f6\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5e76\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea\u65f6\u901a\u77e5\u8fdb\u7a0b\u5462&#xff1f;<\/p>\n<p>\u591a\u8def\u590d\u7528 IO \u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u800c\u51fa\u73b0\u7684\u3002<\/p>\n<p>\u591a\u8def\u590d\u7528 I\/O&#xff1a;\u53ea\u4f7f\u7528\u4e00\u4e2a\u6267\u884c\u6d41&#xff0c;\u5c31\u53ef\u4ee5\u540c\u65f6\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u5f53\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684 IO \u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u591a\u8def\u590d\u7528\u673a\u5236\u4f1a\u8fd4\u56de\u8fd9\u4e9b\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6267\u884c\u6d41\u5c31\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c I\/O \u64cd\u4f5c\u3002<\/p>\n<p>\u8865\u5145\u4e00&#xff1a;<\/p>\n<p>\u4e0e\u975e\u963b\u585e IO \u4e0d\u540c&#xff0c;\u591a\u8def\u590d\u7528 IO \u4e0d\u9700\u8981\u5e94\u7528\u7a0b\u5e8f\u4e0d\u65ad\u8f6e\u8be2\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u800c\u662f\u5c06\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ea4\u7ed9\u5185\u6838\u7edf\u4e00\u76d1\u63a7\u3002\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u963b\u585e\u5728\u591a\u8def\u590d\u7528\u63a5\u53e3\u4e0a&#xff0c;\u76f4\u5230\u81f3\u5c11\u6709\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u518d\u5bf9\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u8865\u5145\u4e8c&#xff1a;<\/p>\n<p>\u6267\u884c\u6d41\u662f\u7a0b\u5e8f\u6267\u884c\u6307\u4ee4\u7684\u57fa\u672c\u5355\u4f4d&#xff0c;\u672c\u8d28\u4e0a\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6267\u884c\u6d41\u6216\u591a\u4e2a\u6267\u884c\u6d41&#xff0c;\u4e5f\u5c31\u662f\u4e00\u4e2a\u7ebf\u7a0b\u6216\u591a\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u5bf9\u4e8e\u5355\u7ebf\u7a0b\u8fdb\u7a0b\u6765\u8bb2&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u88ab\u79f0\u4e3a\u4e00\u4e2a\u6267\u884c\u6d41\u3002<\/p>\n<p>\u6240\u4ee5\u8bf4&#xff1a;\u591a\u8def\u590d\u7528 IO \u53ef\u4ee5\u8ba9\u4e00\u4e2a\u6267\u884c\u6d41\u540c\u65f6\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u8865\u5145\u4e09&#xff1a;<\/p>\n<p>\u4e8b\u4ef6\u5c31\u7eea&#xff1a;\u5bf9\u4e8e Socket \u7684\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u5982\u679c\u63a5\u6536\u7f13\u51b2\u533a\u4e0a\u6709\u6570\u636e&#xff0c;\u5c31\u79f0\u4e3a\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u3002\u5bf9\u4e8e Socket \u7684\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u5982\u679c\u53d1\u9001\u7f13\u51b2\u533a\u4e0a\u6709\u5269\u4f59\u7a7a\u95f4&#xff0c;\u5c31\u79f0\u4e3a\u5199\u4e8b\u4ef6\u5c31\u7eea\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u65b0\u521b\u5efa\u7684 Socket&#xff0c;\u4e00\u822c\u60c5\u51b5\u4e0b&#xff0c;\u5b83\u7684\u8bfb\u4e8b\u4ef6\u4e0d\u5c31\u7eea&#xff0c;\u5b83\u7684\u5199\u4e8b\u4ef6\u5c31\u7eea\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u6ce8&#xff1a;IO \u7531\u4e24\u90e8\u5206\u7ec4\u6210&#xff1a;1. \u7b49\u5f85\u4e8b\u4ef6\u5c31\u7eea 2.\u5c06\u6570\u636e\u4ece\u5185\u6838\u7a7a\u95f4\u62f7\u8d1d\u5230\u7528\u6237\u7a7a\u95f4<\/span><\/p>\n<p><span style=\"color:#fe2c24\">\u591a\u8def\u590d\u7528 IO \u53ea\u8d1f\u8d23\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u5c31\u7eea&#xff0c;\u5373\u53ea\u4f1a\u5e2e\u8fdb\u7a0b\u5b8c\u6210\u7b2c\u4e00\u6b65&#xff0c;\u5b83\u5e76\u4e0d\u63d0\u8fdb\u7a0b\u5b8c\u6210\u7b2c\u4e8c\u6b65&#xff0c;\u5373\u4e0d\u4f1a\u5c06\u6570\u636e\u4ece\u5185\u6838\u7a7a\u95f4\u62f7\u8d1d\u5230\u7528\u6237\u7a7a\u95f4<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"950\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908122600-6a9ffed81e787.png\" width=\"1655\" \/><\/p>\n<p>\u5728 Linux \u63d0\u4f9b\u4e86 <span style=\"color:#fe2c24\">select\u3001poll\u3001epoll<\/span> \u7b49\u591a\u8def\u590d\u7528\u673a\u5236\u3002\u672c\u6587\u9996\u5148\u4ece\u6700\u7ecf\u5178\u7684 select\u00a0\u5f00\u59cb&#xff0c;\u7406\u89e3 I\/O \u591a\u8def\u590d\u7528\u7a76\u7adf\u89e3\u51b3\u4e86\u4ec0\u4e48\u95ee\u9898&#xff0c;\u4ee5\u53ca select\u00a0\u5728\u7f16\u7801\u4e0a\u662f\u5982\u4f55\u5b9e\u73b0\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u540c\u65f6\u76d1\u63a7\u7684\u3002<\/p>\n<h2 id=\"select%20%E7%9A%84%E6%A0%B8%E5%BF%83%E5%AE%9A%E4%BD%8D\">select \u7684\u6838\u5fc3\u5b9a\u4f4d<\/h2>\n<p><span style=\"color:#fe2c24\">select \u662f\u4e00\u79cd\u00a0IO \u591a\u8def\u590d\u7528\u673a\u5236&#xff0c;\u7528\u4e8e\u540c\u65f6\u76d1\u63a7\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684 I\/O \u4e8b\u4ef6\u662f\u5426\u5c31\u7eea\u3002<\/span><\/p>\n<p><span style=\"color:#fe2c24\">\u5b83\u672c\u8eab\u53ea\u8d1f\u8d23\u7b49\u5f85\u548c\u68c0\u6d4b\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u662f\u5426\u5c31\u7eea&#xff0c;\u5e76\u4e0d\u4f1a\u66ff\u5e94\u7528\u7a0b\u5e8f\u5b8c\u6210\u771f\u6b63\u7684 IO \u64cd\u4f5c\u3002<\/span><\/p>\n<h2 id=\"select%20%E6%8E%A5%E5%8F%A3\">select \u63a5\u53e3<\/h2>\n<p>       #include &lt;sys\/select.h&gt;<br \/>\n       #include &lt;sys\/time.h&gt;<br \/>\n       #include &lt;sys\/types.h&gt;<br \/>\n       #include &lt;unistd.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<p>\u53c2\u6570&#xff1a;<\/p>\n<p>int nfds&#xff1a;\u9700\u8981\u76d1\u542c\u7684\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6700\u5927\u503c &#043; 1\u00a0<\/p>\n<p>\u4f8b\u5982&#xff1a;\u9700\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;3 5 7 8 9 12\u00a0<\/p>\n<p>\u6b64\u5904&#xff1a;nfds&#xff1a;12 &#043; 1<\/p>\n<p>struct timeval *timeout&#xff1a;\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570<\/p>\n<p>\u8f93\u5165\u53c2\u6570&#xff1a;\u63a7\u5236\u8fdb\u7a0b\u5728 select \u5904\u7684\u6700\u957f\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<p>\u5728\u00a0timeout \u65f6\u95f4\u5185&#xff0c;\u8fdb\u7a0b\u963b\u585e\u5728 select \u5904\u7b49\u5f85\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea&#xff0c;select \u8fd4\u56de\u3002\u8d85\u8fc7\u4e86 timeout \u65f6\u95f4&#xff0c;select \u4f9d\u65e7\u8fd4\u56de\u3002<\/p>\n<p>\u8f93\u51fa\u53c2\u6570&#xff1a;select \u8fd4\u56de\u540e&#xff0c;timeout \u8868\u793a\u5269\u4f59\u7684\u7b49\u5f85\u65f6\u95f4<\/p>\n<p>struct timeval { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0long \u00a0 \u00a0tv_sec; \u00a0 \u00a0 \u00a0 \u00a0 \/* seconds *\/ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0long \u00a0 \u00a0tv_usec; \u00a0 \u00a0 \u00a0 \u00a0\/* microseconds *\/<\/p>\n<p>};<\/p>\n<p>long tv_sec&#xff1a;\u5355\u4f4d\u662f\u79d2<\/p>\n<p>long tv_usec&#xff1a;\u5355\u4f4d\u662f\u5fae\u79d2<\/p>\n<p>\u6ce8&#xff1a;1 \u79d2\u00a0&#061;&#061; 1e3 \u6beb\u79d2 &#061;&#061; 1e6 \u5fae\u79d2 \u00a0<\/p>\n<p> \u4f8b\u5982&#xff1a;<\/p>\n<p>timeout.tv_sec &#061; 2;<\/p>\n<p>timeout.tv_usec &#061; 0;<\/p>\n<p>select(&#8230;, &amp;timeout);<\/p>\n<p>\u610f\u601d\u662f&#xff1a;\u5f53\u8fdb\u7a0b\u8c03\u7528 select \u65f6&#xff0c;\u5f00\u59cb\u8ba1\u65f6&#xff0c;<br \/>\n\u5982\u679c\u5728 2 S\u5185\u6ca1\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u8fdb\u7a0b\u4f1a\u5728 select \u8fd4\u56de&#xff0c;<br \/>\n\u6b64\u65f6 timeout.tv_sec &#061; 0, timeout.tv_usec &#061; 0;<br \/>\n\u5982\u679c\u5728 1 S\u5185\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u8fdb\u7a0b\u4f1a\u76f4\u63a5\u5728 select \u8fd4\u56de&#xff0c;<br \/>\n\u6b64\u65f6 timeout.tv_sec &#061; 1, timeout.tv_usec &#061; 0; <\/p>\n<p>\u7279\u6b8a\u60c5\u51b5&#xff1a;<\/p>\n<p>timeout &#061; nullptr \u8868\u793a&#xff1a;\u6ca1\u6709\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff0c;\u8fdb\u7a0b\u5c06\u4e00\u76f4\u963b\u585e\u5728 select \u5904&#xff0c;\u76f4\u5230\u81f3\u5c11\u6709\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea\u3002\u7b49\u4ef7\u4e8e\u963b\u585e IO\u00a0<\/p>\n<p>timeout.tv_sec &#061; 0, timeout.tv_usec &#061; 0 \u8868\u793a&#xff1a;\u4e0d\u8fdb\u884c\u7b49\u5f85&#xff0c;\u68c0\u67e5\u6240\u6709\u88ab\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u5373\u4f7f\u6ca1\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u4f9d\u65e7\u8fd4\u56de\u3002\u7b49\u4ef7\u4e8e\u975e\u963b\u585e IO<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;<\/p>\n<p>\u8fd4\u56de\u503c &gt;\u00a00 &#xff1a;\u5728\u88ab\u76d1\u89c6\u7684\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d&#xff0c;\u4e8b\u4ef6\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570<\/p>\n<p>\u8fd4\u56de\u503c &#061;&#061; 0&#xff1a;\u5728 timeout \u65f6\u95f4\u5185&#xff0c;\u6ca1\u6709\u6587\u4ef6\u63cf\u8ff0\u4e8b\u4ef6\u5c31\u7eea\u3002\u5bf9\u4e8e timeout &#061; nullptr &#xff0c;\u4e0d\u5b58\u5728\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u8fd4\u56de\u503c &#061;&#061; -1&#xff1a;select \u8c03\u7528\u51fa\u9519<\/p>\n<p>fd_set *readfds&#xff1a;\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570<\/p>\n<p> \/* fd_set for select and pselect.  *\/<br \/>\ntypedef struct<br \/>\n  {<br \/>\n    \/* XPG4.2 requires this member name.  Otherwise avoid the name<br \/>\n       from the global namespace.  *\/<br \/>\n#ifdef __USE_XOPEN<br \/>\n    __fd_mask fds_bits[__FD_SETSIZE \/ __NFDBITS];<br \/>\n# define __FDS_BITS(set) ((set)-&gt;fds_bits)<br \/>\n#else<br \/>\n    __fd_mask __fds_bits[__FD_SETSIZE \/ __NFDBITS];<br \/>\n# define __FDS_BITS(set) ((set)-&gt;__fds_bits)<br \/>\n#endif<br \/>\n  } fd_set;<\/p>\n<p>fd_set \u672c\u8d28\u7c7b\u578b\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u5728\u5185\u6838\u4e2d\u7684\u5b9e\u73b0\u5c31\u662f\u4e00\u4e2a\u4f4d\u56fe\u3002<\/p>\n<p>\u8f93\u5165\u53c2\u6570&#xff1a;<\/p>\n<p>\u6bd4\u7279\u4f4d\u7684\u7f16\u53f7&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7f16\u53f7\u00a0<\/p>\n<p>\u6bd4\u7279\u4f4d\u7684\u72b6\u6001&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u9700\u8981\u88ab\u76d1\u63a7&#xff0c;\u5982\u679c\u662f 1&#xff0c;\u8868\u793a\u9700\u8981&#xff0c;\u5426\u5219\u8868\u793a\u4e0d\u9700\u8981<\/p>\n<p>\u7528\u6237\u544a\u8bc9\u5185\u6838&#xff0c;\u4f60\u8981\u5e2e\u6211\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408<\/p>\n<p>\u8f93\u51fa\u53c2\u6570&#xff1a;<\/p>\n<p>\u6bd4\u7279\u4f4d\u7684\u7f16\u53f7&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7f16\u53f7\u00a0<\/p>\n<p>\u6bd4\u7279\u4f4d\u7684\u72b6\u6001&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u4e8b\u4ef6\u662f\u5426\u5c31\u7eea&#xff0c;\u5982\u679c\u662f 1&#xff0c;\u8868\u793a\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5426\u5219\u8868\u793a\u8bfb\u4e8b\u4ef6\u4e0d\u5c31\u7eea<\/p>\n<p>\u5185\u6838\u544a\u8bc9\u7528\u6237&#xff0c;\u88ab\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u4e8b\u4ef6\u6ca1\u6709\u5c31\u7eea\u3002<\/p>\n<p>fd_set *writefds \u8868\u793a\u5199\u4e8b\u4ef6\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408 fd_set *exceptfds \u8868\u793a\u5f02\u5e38\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408<\/p>\n<p>\u4f4d\u56fe\u7684\u76f8\u5173\u64cd\u4f5c\u63a5\u53e3<\/p>\n<p>        void FD_CLR(int fd, fd_set *set);<br \/>\n       int  FD_ISSET(int fd, fd_set *set);<br \/>\n       void FD_SET(int fd, fd_set *set);<br \/>\n       void FD_ZERO(fd_set *set); <\/p>\n<h2 id=\"%E5%9F%BA%E4%BA%8E%20Select%20%E7%9A%84%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%20%E2%80%94%E2%80%94%20EchoServer\">\u57fa\u4e8e Select \u7684\u591a\u8def\u590d\u7528\u670d\u52a1\u5668 \u2014\u2014 EchoServer<\/h2>\n<h3 id=\"SelectServer.hpp\">SelectServer.hpp<\/h3>\n<p>#pragma once<\/p>\n<p>#include &#034;Socket.hpp&#034;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;sys\/select.h&gt;<\/p>\n<p>class SelectServer<br \/>\n{<br \/>\n    const static int defaultfd &#061; -1;<br \/>\n    const static int default_backlog &#061; 5;<\/p>\n<p>    const static int size &#061; sizeof(fd_set) * 8;<\/p>\n<p>public:<br \/>\n    SelectServer(in_port_t port, int backlog &#061; default_backlog)<br \/>\n        : _port(port), _backlog(backlog), _listenfd(std::make_unique&lt;TcpSocket&gt;()), _isrunning(false)<br \/>\n    {<br \/>\n        _listenfd-&gt;BuildTcpListenSocket(port, backlog);<br \/>\n        \/\/ \u8f85\u52a9\u6570\u7ec4\u521d\u59cb\u5316<br \/>\n        for (int i &#061; 0; i &lt; size; &#043;&#043;i)<br \/>\n            _rfds[i] &#061; defaultfd;<br \/>\n        _rfds[0] &#061; _listenfd-&gt;Fd();<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        _isrunning &#061; true;<br \/>\n        while (_isrunning)<br \/>\n        {<br \/>\n            fd_set rfds;<br \/>\n            FD_ZERO(&amp;rfds);<br \/>\n            \/\/ \u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u627e\u5230\u5386\u53f2\u6240\u9700\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5e76\u627e\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6700\u5927\u503c<br \/>\n            int maxfd &#061; defaultfd;<br \/>\n            for (int i &#061; 0; i &lt; size; &#043;&#043;i)<br \/>\n            {<br \/>\n                if (_rfds[i] &#061;&#061; defaultfd)<br \/>\n                    continue;<br \/>\n                FD_SET(_rfds[i], &amp;rfds);<br \/>\n                if (maxfd &lt; _rfds[i])<br \/>\n                    maxfd &#061; _rfds[i];<br \/>\n            }<br \/>\n            struct timeval timeout;<br \/>\n            timeout.tv_sec &#061; 1;<br \/>\n            timeout.tv_usec &#061; 0;<br \/>\n            \/\/ \u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea<br \/>\n            int n &#061; select(maxfd &#043; 1, &amp;rfds, nullptr, nullptr, &amp;timeout);<\/p>\n<p>            switch (n)<br \/>\n            {<br \/>\n            case 0: \/\/ \u963b\u585e IO \u4e0d\u5b58\u5728<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; &#034;select timeout&#034;;<br \/>\n                continue;<br \/>\n                break;<br \/>\n            case -1:<br \/>\n                LOG(LogLevel::FATAL) &lt;&lt; &#034;select error&#034;;<br \/>\n                exit(SELECT_ERR);<br \/>\n                break;<br \/>\n            default:<br \/>\n                \/\/ \u6709\u8bfb\u4e8b\u4ef6\u5c31\u7eea<br \/>\n                HandleEvents(rfds);<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n        _isrunning &#061; false;<br \/>\n    }<br \/>\n    \/\/ \u4e8b\u4ef6\u6d3e\u53d1\u5668<br \/>\n    void HandleEvents(fd_set &amp;rfds)<br \/>\n    {<br \/>\n        for (int i &#061; 0; i &lt; size; &#043;&#043;i)<br \/>\n        {<br \/>\n            if (_rfds[i] &#061;&#061; defaultfd)<br \/>\n                continue;<br \/>\n            \/\/ \u8981\u5173\u5fc3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u4e8b\u4ef6\u5c31\u7eea<br \/>\n            if (FD_ISSET(_rfds[i], &amp;rfds))<br \/>\n            {<br \/>\n                \/\/ \u8fde\u63a5\u7ba1\u7406<br \/>\n                if (_rfds[i] &#061;&#061; _listenfd-&gt;Fd())<br \/>\n                {<br \/>\n                    Accepter();<br \/>\n                }<br \/>\n                else \/\/ \u6570\u636e\u5904\u7406<br \/>\n                {<br \/>\n                    Recver(i);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u8fde\u63a5\u7ba1\u7406\u5668<br \/>\n    void Accepter()<br \/>\n    {<br \/>\n        IntAddr client;<br \/>\n        int fd &#061; _listenfd-&gt;Accpet(&amp;client);<br \/>\n        if (fd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::WARNING) &lt;&lt; &#034;accept error&#034;;<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/ \u6dfb\u52a0\u5230\u8f85\u52a9\u6570\u7ec4\u4e2d<br \/>\n        int pos &#061; 0;<br \/>\n        while (pos &lt; size)<br \/>\n        {<br \/>\n            if (_rfds[pos] &#061;&#061; -1)<br \/>\n                break;<br \/>\n            &#043;&#043;pos;<br \/>\n        }<br \/>\n        if (pos &#061;&#061; size)<br \/>\n        {<br \/>\n            LOG(LogLevel::WARNING) &lt;&lt; &#034;rfds is full&#034;;<br \/>\n            return;<br \/>\n        }<br \/>\n        _rfds[pos] &#061; fd;<br \/>\n    }<\/p>\n<p>    void Recver(int pos)<br \/>\n    {<br \/>\n        \/\/ \u5b58\u5728 TCP \u7c98\u5305\u95ee\u9898<br \/>\n        char buffer[1024];<br \/>\n        ssize_t n &#061; recv(_rfds[pos], buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            buffer[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;client say&#064; &#034; &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else if (n &#061;&#061; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;client quit!&#034;;<br \/>\n            close(_rfds[pos]);<br \/>\n            _rfds[pos] &#061; -1;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            LOG(LogLevel::WARNING) &lt;&lt; &#034;recv error&#034;;<br \/>\n            close(_rfds[pos]);<br \/>\n            _rfds[pos] &#061; -1;<br \/>\n        }<br \/>\n    }<br \/>\n    void Stop()<br \/>\n    {<br \/>\n        _isrunning &#061; false;<br \/>\n    }<\/p>\n<p>    ~SelectServer()<br \/>\n    {<br \/>\n    }<\/p>\n<p>private:<br \/>\n    in_port_t _port;<br \/>\n    int _backlog;<br \/>\n    std::unique_ptr&lt;Socket&gt; _listenfd;<br \/>\n    bool _isrunning;<br \/>\n    int _rfds[size];<br \/>\n}; <\/p>\n<h3 id=\"Socket.hpp\">Socket.hpp<\/h3>\n<p>#pragma once <\/p>\n<p>#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Error.hpp&#034;<br \/>\n#include &#034;IntAddr.hpp&#034;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;cstdlib&gt;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>\/\/ \u7ee7\u627f\u6a21\u677f<\/p>\n<p>class Socket<br \/>\n{<br \/>\npublic:<br \/>\n    virtual void CreateSockfd() &#061; 0;<br \/>\n    virtual void BindSockfd(in_port_t port) &#061; 0;<br \/>\n    virtual void ListenSockfd(int backlog) &#061; 0;<br \/>\n    virtual int Accpet(IntAddr *client_addr) &#061; 0;<br \/>\n    virtual int Connect(const IntAddr &amp;server) &#061; 0;<br \/>\n    virtual void Close() &#061; 0;<br \/>\n    virtual ssize_t Recv(std::string *message) &#061; 0;<br \/>\n    virtual ssize_t Send(const std::string &amp;message) &#061; 0;<br \/>\n    virtual int Fd() &#061; 0;<br \/>\n    void BuildTcpListenSocket(in_port_t port, int backlog)<br \/>\n    {<br \/>\n        CreateSockfd();<br \/>\n        BindSockfd(port);<br \/>\n        ListenSockfd(backlog);<br \/>\n    }<br \/>\n    void BuildTcpSocket()<br \/>\n    {<br \/>\n        CreateSockfd();<br \/>\n    }<\/p>\n<p>};<\/p>\n<p>const static int default_sockfd &#061; -1;<\/p>\n<p>class TcpSocket : public Socket<br \/>\n{<br \/>\npublic:<br \/>\n    TcpSocket()<br \/>\n    :_sockfd(default_sockfd)<br \/>\n    {}<br \/>\n    TcpSocket(int sockfd)<br \/>\n    :_sockfd(sockfd)<br \/>\n    {}<br \/>\n    void CreateSockfd() override<br \/>\n    {<br \/>\n        _sockfd &#061; ::socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if(_sockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;socket error&#034;;<br \/>\n            exit(SOCKET_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;socket success&#034;;<br \/>\n    }<br \/>\n    void BindSockfd(in_port_t port) override<br \/>\n    {<br \/>\n        IntAddr server(port);<br \/>\n        int n &#061; ::bind(_sockfd, server.NetAddrPtr(), server.NetAddrLen());<br \/>\n        if(n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n            exit(BIND_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;bind success&#034;;<br \/>\n    }<br \/>\n    void ListenSockfd(int backlog) override<br \/>\n    {<br \/>\n        int n &#061; ::listen(_sockfd, backlog);<br \/>\n        if(n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;listen error&#034;;<br \/>\n            exit(LISTEN_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;listen success&#034;;<br \/>\n    }<br \/>\n    int Accpet(IntAddr *client_addr) override<br \/>\n    {<br \/>\n        struct sockaddr_in client;<br \/>\n        socklen_t len &#061; sizeof(client);<br \/>\n        int sockfd &#061; ::accept(_sockfd, CONV(client), &amp;len);<\/p>\n<p>        return sockfd;<br \/>\n    }<br \/>\n    void Close() override<br \/>\n    {<br \/>\n        ::close(_sockfd);<br \/>\n    }<\/p>\n<p>    ssize_t Recv(std::string *message) override<br \/>\n    {<br \/>\n        char buffer[1024];<br \/>\n        ssize_t n &#061; ::recv(_sockfd, buffer, sizeof(buffer &#8211; 1), 0);<br \/>\n        if(n &gt; 0)<br \/>\n        {<br \/>\n            buffer[n] &#061; 0;<br \/>\n            *message &#043;&#061; buffer;<br \/>\n        }<br \/>\n        return n;<br \/>\n    }<br \/>\n    ssize_t Send(const std::string &amp;message) override<br \/>\n    {<br \/>\n        return ::send(_sockfd, message.c_str(), message.size(), 0);<br \/>\n    }<br \/>\n    int Connect(const IntAddr &amp;server) override<br \/>\n    {<br \/>\n        return ::connect(_sockfd, server.NetAddrPtr(), server.NetAddrLen());<br \/>\n    }<\/p>\n<p>    int Fd() override<br \/>\n    {<br \/>\n        return _sockfd;<br \/>\n    }<\/p>\n<p>    ~TcpSocket(){}<br \/>\nprivate:<br \/>\n    int _sockfd;<br \/>\n}; <\/p>\n<h2 id=\"select%20%E7%89%B9%E7%82%B9\">select \u7279\u70b9<\/h2>\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u6216\u8005\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u907f\u514d\u4e86\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6216\u8005\u8fdb\u7a0b<\/p>\n<p>\u5f53\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;select \u8fd4\u56de&#xff0c;\u7a0b\u5e8f\u518d\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406<\/p>\n<h2 id=\"select%20%E7%BC%BA%E7%82%B9\" style=\"background-color:transparent\">select \u7f3a\u70b9<\/h2>\n<p>&#xff08;1&#xff09;\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u5b58\u5728\u9650\u5236<\/p>\n<p>select \u901a\u5e38\u53d7 FD_SETSIZE \u9650\u5236\u3002Linux \u4e2d\u5e38\u89c1\u9ed8\u8ba4\u503c\u662f 1024&#xff0c;\u56e0\u6b64\u4e0d\u9002\u5408\u5927\u91cf\u8fde\u63a5\u7684\u573a\u666f\u3002<\/p>\n<p>&#xff08;2&#xff09;\u6bcf\u6b21\u8c03\u7528 select \u524d\u90fd\u9700\u8981\u91cd\u65b0\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408<\/p>\n<p>select\u00a0\u4f1a\u4fee\u6539\u4f20\u5165\u7684 fd_set&#xff0c;\u8fd4\u56de\u540e\u5176\u4e2d\u53ea\u4fdd\u7559\u5df2\u7ecf\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u670d\u52a1\u5668\u901a\u5e38\u9700\u8981\u4f7f\u7528\u8f85\u52a9\u6570\u7ec4\u4fdd\u5b58\u5386\u53f2\u4e0a\u9700\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5e76\u5728\u6bcf\u6b21\u8c03\u7528 select\u00a0\u524d&#xff0c;\u6839\u636e\u8f85\u52a9\u6570\u7ec4\u91cd\u65b0\u521d\u59cb\u5316 fd_set\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)<\/p>\n<p>&#xff08;3&#xff09;\u5e94\u7528\u5c42\u9700\u8981\u904d\u5386\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408<\/p>\n<p>select \u8fd4\u56de\u540e&#xff0c;\u5e94\u7528\u5c42\u5e76\u4e0d\u77e5\u9053\u5177\u4f53\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea&#xff0c;\u9700\u8981\u904d\u5386\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408&#xff0c;\u9010\u4e2a\u5224\u65ad\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u751f\u4e86\u4e8b\u4ef6\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)<\/p>\n<p>&#xff08;4&#xff09;\u5185\u6838\u4e5f\u9700\u8981\u904d\u5386\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<p>select \u5c06\u9700\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u4ea4\u7ed9\u5185\u6838\u540e&#xff0c;\u5185\u6838\u9700\u8981\u904d\u5386\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u68c0\u67e5\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u5df2\u7ecf\u5c31\u7eea\u3002<\/p>\n<p>       int select(int nfds, fd_set *readfds, fd_set *writefds,<br \/>\n                  fd_set *exceptfds, struct timeval *timeout); <\/p>\n<p>\u56e0\u6b64&#xff0c;\u5728 select \u7684\u53c2\u6570\u5217\u8868\u4e2d&#xff1a;nfds \u7684\u503c\u4e3a\u9700\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6700\u5927\u503c &#043; 1<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(N)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u591a\u8def\u590d\u7528 IO<br \/>\nselect \u7684\u6838\u5fc3\u5b9a\u4f4d<br \/>\nselect \u63a5\u53e3<br \/>\n\u57fa\u4e8e Select \u7684\u591a\u8def\u590d\u7528\u670d\u52a1\u5668 \u2014\u2014 EchoServer<br \/>\nSelectServer.hpp<br \/>\nSocket.hpp<br \/>\nselect \u7279\u70b9<br \/>\nselect \u7f3a\u70b9 \u591a\u8def\u590d\u7528 IO<br \/>\n\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff1a;\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b&#xff08;\u4e00&#xff09;&#xff1a;\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u4e0e\u4fe1\u53f7\u9a71\u52a8 IO\u00a0\u4e2d&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u548c\u4fe1\u53f7\u9a71<\/p>\n","protected":false},"author":2,"featured_media":102462,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,205,43,78,44],"topic":[],"class_list":["post-102463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-tcp-ip","tag-43","tag-78","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \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\/102463.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u591a\u8def\u590d\u7528 IO select \u7684\u6838\u5fc3\u5b9a\u4f4d select \u63a5\u53e3 \u57fa\u4e8e Select \u7684\u591a\u8def\u590d\u7528\u670d\u52a1\u5668 \u2014\u2014 EchoServer SelectServer.hpp Socket.hpp select \u7279\u70b9 select \u7f3a\u70b9 \u591a\u8def\u590d\u7528 IO \u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff1a;\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b&#xff08;\u4e00&#xff09;&#xff1a;\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u4e0e\u4fe1\u53f7\u9a71\u52a8 IO\u00a0\u4e2d&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u548c\u4fe1\u53f7\u9a71\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/102463.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-08T12:26:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908122600-6a9ffed81e787.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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/102463.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/102463.html\",\"name\":\"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-08T12:26:02+00:00\",\"dateModified\":\"2026-09-08T12:26:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/102463.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/102463.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/102463.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select\"}]},{\"@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":"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \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\/102463.html","og_locale":"zh_CN","og_type":"article","og_title":"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u591a\u8def\u590d\u7528 IO select \u7684\u6838\u5fc3\u5b9a\u4f4d select \u63a5\u53e3 \u57fa\u4e8e Select \u7684\u591a\u8def\u590d\u7528\u670d\u52a1\u5668 \u2014\u2014 EchoServer SelectServer.hpp Socket.hpp select \u7279\u70b9 select \u7f3a\u70b9 \u591a\u8def\u590d\u7528 IO \u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff1a;\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b&#xff08;\u4e00&#xff09;&#xff1a;\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u4e0e\u4fe1\u53f7\u9a71\u52a8 IO\u00a0\u4e2d&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86\u963b\u585e IO\u3001\u975e\u963b\u585e IO \u548c\u4fe1\u53f7\u9a71","og_url":"https:\/\/www.wsisp.com\/helps\/102463.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-08T12:26:02+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908122600-6a9ffed81e787.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/102463.html","url":"https:\/\/www.wsisp.com\/helps\/102463.html","name":"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-08T12:26:02+00:00","dateModified":"2026-09-08T12:26:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/102463.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/102463.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/102463.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u6df1\u5165\u7406\u89e3 Linux IO \u6a21\u578b\uff08\u4e8c\uff09\uff1a\u591a\u8def\u590d\u7528\u2014\u2014select"}]},{"@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\/102463","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=102463"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/102463\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/102462"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=102463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=102463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=102463"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=102463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}