{"id":34716,"date":"2025-04-30T13:30:46","date_gmt":"2025-04-30T05:30:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/34716.html"},"modified":"2025-04-30T13:30:46","modified_gmt":"2025-04-30T05:30:46","slug":"%e3%80%90%e7%bd%91%e7%bb%9c%e3%80%91%e9%ab%98%e7%ba%a7io-poll%e7%89%88%e6%9c%actcp%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/34716.html","title":{"rendered":"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E5%89%8D%E8%A8%80-toc\" style=\"margin-left:0px\">\u524d\u8a00<\/p>\n<p id=\"%E4%B8%80%EF%BC%8Cpoll%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:0px\">\u4e00&#xff0c;poll\u51fd\u6570<\/p>\n<p id=\"%C2%A01.1.%E5%8F%82%E6%95%B0%E4%B8%80%3Afds-toc\" style=\"margin-left:40px\">\u00a01.1.\u53c2\u6570\u4e00:fds<\/p>\n<p id=\"1.2.%E5%8F%82%E6%95%B0%E4%BA%8C%EF%BC%8Cnfds-toc\" style=\"margin-left:40px\">1.2.\u53c2\u6570\u4e8c&#xff0c;nfds<\/p>\n<p id=\"1.3.%E5%8F%82%E6%95%B0%E4%B8%89%EF%BC%8Ctimeout-toc\" style=\"margin-left:40px\">1.3.\u53c2\u6570\u4e09&#xff0c;timeout<\/p>\n<p id=\"1.4.%E8%BF%94%E5%9B%9E%E5%80%BC-toc\" style=\"margin-left:40px\">1.4.\u8fd4\u56de\u503c<\/p>\n<p id=\"1.5.poll%E5%87%BD%E6%95%B0%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-toc\" style=\"margin-left:40px\">1.5.poll\u51fd\u6570\u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/p>\n<p id=\"%E4%BA%8C%EF%BC%8Cpoll%E7%89%88TCP%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%BC%96%E5%86%99-toc\" style=\"margin-left:0px\">\u4e8c&#xff0c;poll\u7248TCP\u670d\u52a1\u5668\u7f16\u5199<\/p>\n<p id=\"2.1.%E7%BC%96%E5%86%99-toc\" style=\"margin-left:40px\">2.1.\u7f16\u5199<\/p>\n<p id=\"2.2.poll%E7%9A%84%E4%BC%98%E7%BC%BA%E7%82%B9-toc\" style=\"margin-left:40px\">2.2.poll\u7684\u4f18\u7f3a\u70b9<\/p>\n<p id=\"2.3.%E6%BA%90%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:40px\">2.3.\u6e90\u4ee3\u7801<\/p>\n<hr id=\"hr-toc\" \/>\n<h2>\u524d\u8a00<\/h2>\n<p>\u7531\u4e8eselect\u51fd\u6570\u6709\u4e0b\u9762\u51e0\u4e2a\u7279\u522b\u660e\u663e\u7684\u7f3a\u70b9&#xff0c;\u5c31\u63a8\u6f14\u51fa\u4e86\u6539\u8fdb\u7248\u672c\u2014\u2014poll\u51fd\u6570<\/p>\n<ul>\n<li>\u6bd4\u5982select\u76d1\u89c6\u7684fd\u662f\u6709\u4e0a\u9650\u7684&#xff0c;\u6211\u7684\u4e91\u670d\u52a1\u5668\u5185\u6838\u7248\u672c\u4e0b\u6700\u5927\u4e0a\u9650\u662f1024\u4e2afd&#xff0c;\u4e3b\u8981\u8fd8\u662f\u56e0\u4e3afd_set\u4ed6\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u4f4d\u56fe\u7ed3\u6784&#xff0c;\u4f4d\u56fe\u4e2d\u7684\u6570\u7ec4\u5f00\u8f9f\u4e4b\u540e\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86&#xff0c;\u8fd9\u662f\u5185\u6838\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u9664\u975e\u4f60\u4fee\u6539\u5185\u6838\u53c2\u6570&#xff0c;\u5426\u5219\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86&#xff0c;\u6240\u4ee5\u4e00\u65e6select\u76d1\u89c6\u7684fd\u6570\u91cf\u8d85\u8fc71024&#xff0c;\u5219select\u4f1a\u62a5\u9519\u3002<\/li>\n<li>\u9664\u6b64\u4e4b\u5916&#xff0c;select\u5927\u90e8\u5206\u7684\u53c2\u6570\u90fd\u662f\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u7528\u6237\u548c\u5185\u6838\u90fd\u4f1a\u4e0d\u65ad\u7684\u4fee\u6539\u8fd9\u4e9b\u53c2\u6570\u7684\u503c&#xff0c;\u5bfc\u81f4\u6bcf\u6b21\u8c03\u7528select\u524d&#xff0c;\u90fd\u9700\u8981\u91cd\u65b0\u8bbe\u7f6efd_set\u4f4d\u56fe\u4e2d\u7684\u5185\u5bb9&#xff0c;\u8fd9\u5728\u7528\u6237\u5c42\u9762\u4e0a\u4f1a\u5e26\u6765\u5f88\u591a\u4e0d\u5fc5\u8981\u7684\u904d\u5386&#043;\u62f7\u8d1d\u7684\u6210\u672c\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0poll\u63a5\u53e3\u4e3b\u8981\u89e3\u51b3\u4e86select\u63a5\u53e3\u7684\u4e24\u4e2a\u95ee\u9898&#xff0c;\u4e00\u4e2a\u662fselect\u76d1\u89c6\u7684fd\u6709\u4e0a\u9650&#xff0c;\u53e6\u4e00\u4e2a\u662fselect\u6bcf\u6b21\u8c03\u7528\u524d\u90fd\u9700\u8981\u501f\u52a9\u7b2c\u4e09\u65b9\u6570\u7ec4&#xff0c;\u5411fd_set\u91cc\u9762\u91cd\u65b0\u8bbe\u7f6e\u5173\u5fc3\u7684fd\u3002\u00a0<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0select() \u548c poll() \u7cfb\u7edf\u8c03\u7528\u7684\u672c\u8d28\u4e00\u6837&#xff0c;poll() \u7684\u673a\u5236\u4e0e select() \u7c7b\u4f3c&#xff0c;\u4e0e select() \u5728\u672c\u8d28\u4e0a\u6ca1\u6709\u591a\u5927\u5dee\u522b&#xff0c;\u7ba1\u7406\u591a\u4e2a\u63cf\u8ff0\u7b26\u4e5f\u662f\u8fdb\u884c\u8f6e\u8be2&#xff0c;\u6839\u636e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u8fdb\u884c\u5904\u7406&#xff0c;<span style=\"background-color:#ffd900\">\u4f46\u662f poll() \u6ca1\u6709\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u9650\u5236&#xff08;\u4f46\u662f\u6570\u91cf\u8fc7\u5927\u540e\u6027\u80fd\u4e5f\u662f\u4f1a\u4e0b\u964d&#xff09;\u3002<\/span><\/p>\n<p><span style=\"background-color:#ffd900\">\u53ea\u8981\u6211\u4eec\u7406\u89e3\u4e86\u6211\u4eec\u7684select&#xff0c;poll\u4e5f\u5c31\u4e0d\u5728\u8bdd\u4e0b<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0poll() \u548c select() \u540c\u6837\u5b58\u5728\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f&#xff0c;\u5305\u542b\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u7ec4\u88ab\u6574\u4f53\u590d\u5236\u4e8e\u7528\u6237\u6001\u548c\u5185\u6838\u7684\u5730\u5740\u7a7a\u95f4\u4e4b\u95f4&#xff0c;\u800c\u4e0d\u8bba\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u5c31\u7eea&#xff0c;\u5b83\u7684\u5f00\u9500\u968f\u7740\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u589e\u52a0\u800c\u7ebf\u6027\u589e\u5927\u3002<\/p>\n<h2 id=\"%E4%B8%80%EF%BC%8Cpoll%E5%87%BD%E6%95%B0\">\u4e00&#xff0c;poll\u51fd\u6570<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"101\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053043-6811b5831e899.png\" width=\"677\" \/><\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u548cselect\u4e00\u6a21\u4e00\u6837\u3002\u90fd\u662f\u76d1\u89c6\u5e76\u7b49\u5f85\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c5e\u6027\u53d8\u5316<\/p>\n<p><span style=\"color:#fe2c24\">poll\u51fd\u6570\u5904\u7406\u7684\u8fd8\u662fIO\u8fc7\u7a0b\u91cc\u9762\u7684\u7b49\u5f85\u8fc7\u7a0b&#xff01;&#xff01;&#xff01;<\/span><\/p>\n<h3 id=\"%C2%A01.1.%E5%8F%82%E6%95%B0%E4%B8%80%3Afds\">\u00a01.1.\u53c2\u6570\u4e00:fds<\/h3>\n<p>\u8fd9\u4e2a\u53c2\u6570\u5c31\u9700\u8981\u6211\u4eec\u77e5\u9053struct pollfd\u7ed3\u6784\u4f53\u662f\u4ec0\u4e48\u4e1c\u897f&#xff01;&#xff01;&#xff01;<\/p>\n<p>struct pollfd{<br \/>\nint fd;\/\/\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\nshort events;\/\/\u7b49\u5f85\u7684\u4e8b\u4ef6<br \/>\nshort revents;\/\/\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n}; <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0poll\u00a0\u51fd\u6570\u662f Unix\/Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;file descriptors&#xff09;\u72b6\u6001\u53d8\u5316\u7684\u4e00\u79cd\u673a\u5236\u3002poll\u00a0\u51fd\u6570\u80fd\u591f\u540c\u65f6\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4ee5\u68c0\u67e5\u5176\u4e0a\u662f\u5426\u53d1\u751f\u4e86\u611f\u5174\u8da3\u7684\u4e8b\u4ef6&#xff08;\u5982\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u9519\u8bef\u7b49&#xff09;\u3002<span style=\"color:#956fe7\">\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd&#xff0c;poll\u00a0\u51fd\u6570\u4f7f\u7528\u4e86\u4e00\u4e2a\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u6570\u7ec4\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570&#xff0c;\u6bcf\u4e2a\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u4ee3\u8868\u4e86\u4e00\u4e2a\u88ab\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u76f8\u5173\u7684\u4e8b\u4ef6\u3002<\/span><\/p>\n<p>\u4e0b\u9762\u662f\u5bf9\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u5404\u6210\u5458\u7684\u8be6\u7ec6\u89e3\u91ca&#xff1a;<\/p>\n<ul>\n<li><span style=\"background-color:#ffd900\">int fd;<\/span>&#xff1a;<span style=\"color:#fe2c24\">\u8fd9\u4e2a\u6210\u5458\u53d8\u91cf\u662f\u4e00\u4e2a\u6574\u6570&#xff0c;\u8868\u793a\u88ab\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/span>\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u4e00\u4e2a\u975e\u8d1f\u6574\u6570&#xff0c;\u5b83\u662f\u4e00\u4e2a\u7d22\u5f15\u503c&#xff0c;\u6307\u5411\u5185\u6838\u4e2d\u6253\u5f00\u6587\u4ef6\u7684\u8868\u9879\u3002\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5bf9\u6253\u5f00\u7684\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u5728\u00a0poll\u00a0\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4e2d&#xff0c;\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\u3001\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u6216\u8005\u7ba1\u9053&#xff08;pipe&#xff09;\u7b49\u3002<\/li>\n<li><span style=\"background-color:#ffd900\">short events;<\/span>&#xff1a;\u8fd9\u4e2a\u6210\u5458\u53d8\u91cf\u662f\u4e00\u4e2a\u4f4d\u63a9\u7801&#xff08;bitmask&#xff09;&#xff0c;<span style=\"color:#fe2c24\">\u7528\u4e8e\u6307\u5b9a\u6211\u4eec\u60f3\u8981\u76d1\u89c6\u7684\u3001\u5728\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u53ef\u80fd\u53d1\u751f\u7684\u4e8b\u4ef6\u3002<\/span>\u8fd9\u4e9b\u4e8b\u4ef6\u53ef\u4ee5\u662f\u4ee5\u4e0b\u51e0\u79cd\u4e4b\u4e00&#xff08;\u6216\u5b83\u4eec\u7684\u7ec4\u5408&#xff0c;\u901a\u8fc7\u4f4d\u6216\u64cd\u4f5c\u7b26\u00a0|\u00a0\u5b9e\u73b0&#xff09;&#xff1a;<\/li>\n<\/ul>\n<li>POLLIN&#xff1a;\u6709\u6570\u636e\u53ef\u8bfb\u3002<\/li>\n<li>POLLOUT&#xff1a;\u5199\u6570\u636e\u4e0d\u4f1a\u963b\u585e\u3002<\/li>\n<li>POLLERR&#xff1a;\u53d1\u751f\u9519\u8bef\u3002<\/li>\n<li>POLLHUP&#xff1a;\u6302\u8d77&#xff08;hang up&#xff09;\u3002<\/li>\n<li>POLLNVAL&#xff1a;\u65e0\u6548\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u53ef\u80fd\u4f9d\u8d56\u4e8e\u7279\u5b9a\u5b9e\u73b0\u7684\u6807\u5fd7\u3002<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"346\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053043-6811b583efc62.png\" width=\"1081\" \/><\/p>\n<p>\u76f8\u6bd4\u4e8eselect&#xff0c;poll\u5c06\u8f93\u5165\u548c\u8f93\u51fa\u4e8b\u4ef6\u8fdb\u884c\u4e86\u5206\u79bb\u00a0<\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u5185\u6838\u5e2e\u6211\u4eec\u5173\u6ce8 0\u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u8bfb\u4e8b\u4ef6<\/p>\n<p> struct pollfd rfds;<br \/>\nrfds.fd &#061; 0;              \/\/\u5e0c\u671b\u5185\u6838\u5e2e\u6211\u4eec\u5173\u6ce80\u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6<br \/>\nrfds.events |&#061; POLLIN;    \/\/\u5e0c\u671b\u5185\u6838\u5173\u6ce80\u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u8bfb\u4e8b\u4ef6<br \/>\n\/\/rfds.events &#061; POLLIN | POLLOUT;    \/\/\u5e0c\u671b\u65e2\u76d1\u542c\u8bfb\u4e8b\u4ef6\u53c8\u76d1\u542c\u5199\u4e8b\u4ef6<br \/>\nrfds.revents &#061; 0;         \/\/\u8fd9\u4e2a\u53c2\u6570\u7528\u4e8e\u5185\u6838\u901a\u77e5\u6211\u4eec\u6709\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff0c;\u8ba9\u6211\u4eec\u8d76\u7d27\u6765\u53d6 <\/p>\n<p>\u5982\u679c\u6211\u4eec\u8981\u5224\u65ad\u8bfb\u4e8b\u4ef6\u662f\u5426\u5c31\u7eea<\/p>\n<p> if(rfds.revents &amp; POLLIN){<br \/>\n    std::cout &lt;&lt; &#034;\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u4e86&#8230;&#034; &lt;&lt; std::endl;<br \/>\n} <\/p>\n<ul>\n<li><span style=\"background-color:#ffd900\">short revents;<\/span>&#xff1a;<span style=\"color:#fe2c24\">\u8fd9\u4e2a\u6210\u5458\u53d8\u91cf\u5728\u00a0poll\u00a0\u51fd\u6570\u8c03\u7528\u8fd4\u56de\u65f6\u88ab\u586b\u5145&#xff0c;\u5b83\u4e5f\u662f\u4e00\u4e2a\u4f4d\u63a9\u7801&#xff0c;\u8868\u793a\u5728\u8c03\u7528\u00a0poll\u00a0\u671f\u95f4&#xff0c;\u5b9e\u9645\u53d1\u751f\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6\u3002<\/span>\u4e0e\u00a0events\u00a0\u4e0d\u540c&#xff0c;revents\u00a0\u662f\u7531\u00a0poll\u00a0\u51fd\u6570\u81ea\u52a8\u586b\u5199\u7684&#xff0c;\u7528\u6237\u4e0d\u9700\u8981&#xff08;\u4e5f\u4e0d\u5e94\u8be5&#xff09;\u5728\u8c03\u7528\u00a0poll\u00a0\u4e4b\u524d\u8bbe\u7f6e\u5b83\u3002revents\u00a0\u7684\u503c\u53ef\u80fd\u5305\u542b\u00a0events\u00a0\u4e2d\u6307\u5b9a\u7684\u4efb\u4f55\u4e8b\u4ef6&#xff0c;\u6216\u8005\u7531\u4e8e\u67d0\u79cd\u539f\u56e0&#xff08;\u5982\u9519\u8bef\u6216\u6302\u8d77&#xff09;\u800c\u5305\u542b\u5176\u4ed6\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p>events\u548crevents\u7684\u53d6\u503c\u90fd\u662f\u4ee5\u5b8f\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4e49\u7684&#xff0c;\u5b83\u4eec\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5f53\u4e2d\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u6bd4\u7279\u4f4d\u662f1&#xff0c;\u4e14\u4e3a1\u7684\u6bd4\u7279\u4f4d\u662f\u5404\u4e0d\u76f8\u540c\u7684\u3002\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"726\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053044-6811b584466e5.png\" width=\"1200\" \/><\/p>\n<ul>\n<li>\u5728\u8c03\u7528poll\u51fd\u6570\u4e4b\u524d&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u201c\u6216\u201d\u8fd0\u7b97\u7b26\u5c06\u8981\u68c0\u6d4b\u7684\u4e8b\u4ef6\u6dfb\u52a0\u5230events\u6210\u5458\u5f53\u4e2d\u3002<\/li>\n<li>\u5728poll\u51fd\u6570\u8fd4\u56de\u540e&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u201c\u4e0e\u201d\u8fd0\u7b97\u7b26\u68c0\u6d4brevents\u6210\u5458\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u4e8b\u4ef6&#xff0c;\u4ee5\u5f97\u77e5\u5bf9\u5e94\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7279\u5b9a\u4e8b\u4ef6\u662f\u5426\u5c31\u7eea\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u6ce8\u610f&#xff1a;\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684 events \u57df\u662f\u7531\u7528\u6237\u6765\u8bbe\u7f6e&#xff0c;\u544a\u8bc9\u5185\u6838\u6211\u4eec\u5173\u6ce8\u7684\u662f\u4ec0\u4e48&#xff0c;\u800c revents \u57df\u662f\u8fd4\u56de\u65f6\u5185\u6838\u8bbe\u7f6e\u7684&#xff0c;\u4ee5\u8bf4\u660e\u5bf9\u8be5\u63cf\u8ff0\u7b26\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u4ef6\u00a0<\/span><\/p>\n<ul>\n<li>\u4f7f\u7528\u00a0poll\u00a0\u51fd\u6570\u65f6&#xff0c;\u4f60\u901a\u5e38\u4f1a\u521b\u5efa\u4e00\u4e2a\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u6570\u7ec4&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u4f60\u60f3\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u4e8b\u4ef6\u3002<\/li>\n<li>\u7136\u540e&#xff0c;\u4f60\u8c03\u7528\u00a0poll\u00a0\u51fd\u6570&#xff0c;\u5e76\u5c06\u8fd9\u4e2a\u6570\u7ec4\u548c\u6570\u7ec4\u7684\u5927\u5c0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>poll\u00a0\u51fd\u6570\u4f1a\u963b\u585e&#xff08;\u6216\u6839\u636e\u9700\u8981\u7acb\u5373\u8fd4\u56de&#xff09;&#xff0c;\u76f4\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u53d1\u751f\u4e86\u8bf7\u6c42\u7684\u4e8b\u4ef6&#xff0c;\u6216\u8005\u8d85\u8fc7\u4e86\u6307\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/li>\n<li><span style=\"color:#956fe7\">\u6700\u540e&#xff0c;\u4f60\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6bcf\u4e2a\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u7684\u00a0revents\u00a0\u6210\u5458\u6765\u786e\u5b9a\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u53d1\u751f\u4e86\u54ea\u4e9b\u4e8b\u4ef6\u3002<\/span><\/li>\n<\/ul>\n<h3 id=\"1.2.%E5%8F%82%E6%95%B0%E4%BA%8C%EF%BC%8Cnfds\">1.2.\u53c2\u6570\u4e8c&#xff0c;nfds<\/h3>\n<p>nfds_t\u7c7b\u578b\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nfds_t\u00a0\u7c7b\u578b\u662f\u5728 Unix\/Linux \u7cfb\u7edf\u7f16\u7a0b\u4e2d&#xff0c;\u7279\u522b\u662f\u5728\u4f7f\u7528\u00a0poll\u3001ppoll\u3001select\u00a0\u7b49\u7cfb\u7edf\u8c03\u7528\u65f6&#xff0c;\u7528\u6765\u8868\u793a\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u4e2a\u7c7b\u578b\u7684\u786e\u5207\u5b9a\u4e49\u53ef\u80fd\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u7cfb\u7edf\u548c\u5e93\u5b9e\u73b0\u800c\u6709\u6240\u4e0d\u540c&#xff0c;\u4f46\u901a\u5e38\u5b83\u662f\u4e00\u4e2a\u8db3\u591f\u5927\u7684\u6574\u6570\u7c7b\u578b&#xff0c;\u4ee5\u5bb9\u7eb3\u7cfb\u7edf\u53ef\u80fd\u652f\u6301\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0<span style=\"color:#fe2c24\">\u5728\u5927\u591a\u6570\u73b0\u4ee3 Unix-like \u7cfb\u7edf\u4e2d&#xff0c;nfds_t\u00a0\u901a\u5e38\u662f\u00a0unsigned int\u00a0\u6216\u00a0unsigned long\u00a0\u7684\u522b\u540d&#xff0c;\u4f46\u8fd9\u4e0d\u662f\u4e00\u4e2a\u56fa\u5b9a\u7684\u89c4\u5219&#xff0c;\u56e0\u6b64\u6700\u597d\u67e5\u770b\u4f60\u7684\u7cfb\u7edf\u6216\u5e93\u7684\u6587\u6863\u4ee5\u83b7\u53d6\u786e\u5207\u7684\u5b9a\u4e49\u3002<\/span><\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728 glibc&#xff08;GNU C Library&#xff09;\u4e2d&#xff0c;nfds_t\u00a0\u7684\u5b9a\u4e49\u53ef\u80fd\u770b\u8d77\u6765\u50cf\u8fd9\u6837&#xff08;\u5c3d\u7ba1\u8fd9\u53d6\u51b3\u4e8e glibc \u7684\u7248\u672c\u548c\u914d\u7f6e&#xff09;&#xff1a;<\/p>\n<p>\u00a0typedef unsigned long nfds_t;\u00a0 <\/p>\n<p>\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b&#xff0c;\u5b83\u53ef\u80fd\u662f&#xff1a;<\/p>\n<p>\u00a0typedef unsigned int nfds_t;\u00a0 <\/p>\n<p>\u5f53\u4f60\u4f7f\u7528\u00a0poll\u00a0\u51fd\u6570\u65f6&#xff0c;\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u00a0nfds_t\u00a0\u7c7b\u578b\u7684\u503c\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570&#xff0c;\u8fd9\u4e2a\u503c\u8868\u793a\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002\u8fd9\u4e2a\u503c\u5e94\u8be5\u5927\u4e8e\u6216\u7b49\u4e8e\u6570\u7ec4\u4e2d\u5b9e\u9645\u5143\u7d20\u7684\u6570\u91cf&#xff0c;\u56e0\u4e3a\u00a0poll\u00a0\u4f1a\u68c0\u67e5\u8fd9\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u00a0pollfd\u00a0\u7ed3\u6784\u4f53\u3002<\/p>\n<p>\u00a0#include &lt;poll.h&gt; \u00a0<br \/>\n\u00a0 \u00a0int main() { \u00a0<br \/>\n\u00a0struct pollfd fds[2]; \u00a0<br \/>\n\u00a0\/\/ \u521d\u59cb\u5316 fds \u6570\u7ec4&#8230; \u00a0<br \/>\n\u00a0 \u00a0nfds_t nfds &#061; sizeof(fds) \/ sizeof(fds[0]); \u00a0<br \/>\n\u00a0int timeout &#061; -1; \/\/ \u65e0\u9650\u7b49\u5f85 \u00a0<br \/>\n\u00a0 \u00a0int ret &#061; poll(fds, nfds, timeout); \u00a0<br \/>\n\u00a0\/\/ \u5904\u7406 poll \u7684\u8fd4\u56de\u503c&#8230; \u00a0<br \/>\n\u00a0 \u00a0return 0; \u00a0<br \/>\n\u00a0}\u00a0 <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;nfds\u00a0\u88ab\u8bbe\u7f6e\u4e3a\u00a0fds\u00a0\u6570\u7ec4\u7684\u5927\u5c0f&#xff0c;\u8fd9\u662f\u8c03\u7528\u00a0poll\u00a0\u65f6\u5e94\u8be5\u4f20\u9012\u7684\u6b63\u786e\u503c\u3002\u6ce8\u610f&#xff0c;\u867d\u7136\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u00a0nfds\u00a0\u88ab\u663e\u5f0f\u5730\u8ba1\u7b97\u4e3a\u6570\u7ec4\u7684\u5927\u5c0f&#xff0c;\u4f46\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b&#xff0c;\u4f60\u53ef\u80fd\u5df2\u7ecf\u77e5\u9053\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u90a3\u4e2a\u503c\u3002<\/p>\n<h3 id=\"1.3.%E5%8F%82%E6%95%B0%E4%B8%89%EF%BC%8Ctimeout\">1.3.\u53c2\u6570\u4e09&#xff0c;timeout<\/h3>\n<p>\u8fd9\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u8f93\u5165\u578b\u53c2\u6570&#xff0c;\u5355\u4f4d\u662f\u6beb\u79d2&#xff0c;\u4ee3\u8868\u963b\u585e\u7b49\u5f85\u7684\u65f6\u95f4&#xff0c;\u8d85\u8fc7\u8be5\u65f6\u95f4\u5c31\u4f1a\u53d8\u4e3a\u975e\u963b\u585e\u7b49\u5f85\u3002<\/p>\n<li>timeout &#061; -1&#xff0c;\u4ee3\u8868\u6c38\u4e45\u963b\u585e\u7b49\u5f85<\/li>\n<li>timeout &#061; 0&#xff0c;\u4ee3\u8868\u6c38\u4e45\u975e\u963b\u585e\u7b49\u5f85<\/li>\n<li>timeout &gt; 0&#xff0c;\u4ee3\u8868\u5148\u963b\u585e\u7b49\u5f85 timeout \u6beb\u79d2&#xff0c;\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u53d8\u4e3a\u975e\u963b\u585e\u7b49\u5f85&#xff0c;poll\u51fd\u6570\u8fd4\u56de<\/li>\n<h3 id=\"1.4.%E8%BF%94%E5%9B%9E%E5%80%BC\">1.4.\u8fd4\u56de\u503c<\/h3>\n<p><span style=\"color:#fe2c24\">\u6210\u529f\u65f6&#xff0c;poll() \u8fd4\u56de\u7ed3\u6784\u4f53\u4e2d revents \u57df\u4e0d\u4e3a 0 \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570&#xff1b;\u5982\u679c\u5728\u8d85\u65f6\u524d\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u53d1\u751f&#xff0c;poll()\u8fd4\u56de 0&#xff1b;<\/span><\/p>\n<p>\u5931\u8d25\u65f6&#xff0c;poll() \u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno \u4e3a\u4e0b\u5217\u503c\u4e4b\u4e00&#xff1a;<\/p>\n<li>EBADF&#xff1a;\u4e00\u4e2a\u6216\u591a\u4e2a\u7ed3\u6784\u4f53\u4e2d\u6307\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u65e0\u6548\u3002<\/li>\n<li>EFAULT&#xff1a;fds \u6307\u9488\u6307\u5411\u7684\u5730\u5740\u8d85\u51fa\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3002<\/li>\n<li>EINTR&#xff1a;\u8bf7\u6c42\u7684\u4e8b\u4ef6\u4e4b\u524d\u4ea7\u751f\u4e00\u4e2a\u4fe1\u53f7&#xff0c;\u8c03\u7528\u53ef\u4ee5\u91cd\u65b0\u53d1\u8d77\u3002<\/li>\n<li>EINVAL&#xff1a;nfds \u53c2\u6570\u8d85\u51fa PLIMIT_NOFILE \u503c\u3002<\/li>\n<li>ENOMEM&#xff1a;\u53ef\u7528\u5185\u5b58\u4e0d\u8db3&#xff0c;\u65e0\u6cd5\u5b8c\u6210\u8bf7\u6c42\u3002<\/li>\n<h3 id=\"1.5.poll%E5%87%BD%E6%95%B0%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B\">1.5.poll\u51fd\u6570\u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>#include &lt;stdio.h&gt;  \/\/ \u5f15\u5165\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5e93&#xff0c;\u7528\u4e8eprintf\u7b49\u51fd\u6570<br \/>\n#include &lt;stdlib.h&gt; \/\/ \u5f15\u5165\u6807\u51c6\u5e93&#xff0c;\u7528\u4e8eEXIT_FAILURE\u7b49\u5b8f\u5b9a\u4e49<br \/>\n#include &lt;unistd.h&gt; \/\/ \u5f15\u5165POSIX\u64cd\u4f5c\u7cfb\u7edfAPI&#xff0c;\u7528\u4e8eopen\u3001close\u7b49\u51fd\u6570<br \/>\n#include &lt;fcntl.h&gt;  \/\/ \u5f15\u5165\u6587\u4ef6\u63a7\u5236\u9009\u9879&#xff0c;\u5982O_RDONLY<br \/>\n#include &lt;poll.h&gt;   \/\/ \u5f15\u5165poll\u51fd\u6570\u53ca\u5176\u76f8\u5173\u7ed3\u6784\u4f53pollfd  <\/p>\n<p>int main() {<br \/>\n    \/\/ \u5c1d\u8bd5\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u540d\u4e3a&#034;test.txt&#034;\u7684\u6587\u4ef6<br \/>\n    int fd &#061; open(&#034;test.txt&#034;, O_RDONLY);<br \/>\n    if (fd &lt; 0) {<br \/>\n        \/\/ \u5982\u679c\u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u8fd4\u56de\u5931\u8d25\u72b6\u6001<br \/>\n        perror(&#034;Failed to open file&#034;);<br \/>\n        return EXIT_FAILURE;<br \/>\n    }    <\/p>\n<p>    \/\/ \u5b9a\u4e49\u4e00\u4e2apollfd\u7ed3\u6784\u4f53\u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u4e8b\u4ef6<br \/>\n    struct pollfd fds[1];<br \/>\n    \/\/ \u8bbe\u7f6e\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20&#xff0c;\u6307\u5b9a\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u611f\u5174\u8da3\u7684\u4e8b\u4ef6&#xff08;POLLIN&#xff0c;\u8868\u793a\u53ef\u8bfb&#xff09;<br \/>\n    fds[0].fd &#061; fd;<br \/>\n    fds[0].events &#061; POLLIN;    <\/p>\n<p>    \/\/ \u8bbe\u7f6epoll\u51fd\u6570\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5000\u6beb\u79d2&#xff08;5\u79d2&#xff09;<br \/>\n    int timeout &#061; 5000; \/\/ 5 seconds<br \/>\n    \/\/ \u8c03\u7528poll\u51fd\u6570&#xff0c;\u76d1\u89c6fds\u6570\u7ec4\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8d85\u65f6\u65f6\u95f4\u4e3atimeout<br \/>\n    int ret &#061; poll(fds, 1, timeout);    <\/p>\n<p>    \/\/ \u68c0\u67e5poll\u51fd\u6570\u7684\u8fd4\u56de\u503c<br \/>\n    if (ret &#061;&#061; -1) {<br \/>\n        \/\/ \u5982\u679cpoll\u8c03\u7528\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f&#xff0c;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5e76\u8fd4\u56de\u5931\u8d25\u72b6\u6001<br \/>\n        perror(&#034;poll failed&#034;);<br \/>\n        close(fd);<br \/>\n        return EXIT_FAILURE;<br \/>\n    }    <\/p>\n<p>    \/\/ \u5982\u679cpoll\u8d85\u65f6&#xff0c;\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u53d1\u751f<br \/>\n    if (ret &#061;&#061; 0) {<br \/>\n        printf(&#034;No data within five seconds\\\\n&#034;);<br \/>\n    } else if (fds[0].revents &amp; POLLIN) {<br \/>\n        \/\/ \u5982\u679c\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u53d1\u751f\u4e86POLLIN\u4e8b\u4ef6&#xff08;\u5373\u53ef\u8bfb&#xff09;<br \/>\n        char buf[1024];  \/\/ \u5b9a\u4e49\u4e00\u4e2a\u7f13\u51b2\u533a&#xff0c;\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u7684\u6570\u636e<br \/>\n        ssize_t bytes_read &#061; read(fd, buf, sizeof(buf) &#8211; 1);  \/\/ \u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u8bfb\u53d6\u6570\u636e\u5230\u7f13\u51b2\u533a<br \/>\n        if (bytes_read &gt; 0) {  \/\/ \u5982\u679c\u6210\u529f\u8bfb\u53d6\u5230\u6570\u636e<br \/>\n            buf[bytes_read] &#061; &#039;\\\\0&#039;;  \/\/ \u5728\u8bfb\u53d6\u5230\u7684\u6570\u636e\u672b\u5c3e\u6dfb\u52a0\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26&#039;\\\\0&#039;<br \/>\n            printf(&#034;Read %zd bytes: %s\\\\n&#034;, bytes_read, buf);  \/\/ \u6253\u5370\u8bfb\u53d6\u5230\u7684\u6570\u636e\u53ca\u5176\u957f\u5ea6<br \/>\n        }<br \/>\n    }    <\/p>\n<p>    \/\/ \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u91ca\u653e\u8d44\u6e90<br \/>\n    close(fd);<br \/>\n    \/\/ \u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f<br \/>\n    return EXIT_SUCCESS;<br \/>\n} <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528poll\u51fd\u6570\u6765\u76d1\u89c6\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6&#xff09;\u7684\u72b6\u6001&#xff0c;\u7279\u522b\u662f\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6709\u6570\u636e\u53ef\u8bfb\u3002\u5982\u679c\u6587\u4ef6\u5728\u6307\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\u5185\u53d8\u5f97\u53ef\u8bfb&#xff0c;\u7a0b\u5e8f\u5c06\u8bfb\u53d6\u5e76\u6253\u5370\u6587\u4ef6\u5185\u5bb9&#xff1b;\u5982\u679c\u8d85\u65f6&#xff0c;\u5219\u6253\u5370\u4e00\u6761\u6d88\u606f\u8868\u793a\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u3002<\/p>\n<h2 id=\"%E4%BA%8C%EF%BC%8Cpoll%E7%89%88TCP%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%BC%96%E5%86%99\">\u4e8c&#xff0c;poll\u7248TCP\u670d\u52a1\u5668\u7f16\u5199<\/h2>\n<h3 id=\"2.1.%E7%BC%96%E5%86%99\">2.1.\u7f16\u5199<\/h3>\n<p>\u6211\u4eec\u8fd9\u4e2apoll\u7248TCP\u670d\u52a1\u5668\u548c\u90a3\u4e2aselect\u7248\u672c\u7684\u5dee\u4e0d\u591a&#xff01;&#xff01;&#xff01;\u6240\u4ee5\u6211\u4eec\u57fa\u672c\u5c31\u662f\u5728select\u7248\u672c\u4e0a\u9762\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4fee\u6539<\/p>\n<p>PollServer.hpp\u521d\u59cb\u7248\u672c<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &#034;Socket.hpp&#034;<br \/>\n#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;sys\/time.h&gt;<br \/>\n#include &lt;poll.h&gt; <\/p>\n<p>const uint16_t default_port &#061; 8877;       \/\/ \u9ed8\u8ba4\u7aef\u53e3\u53f7<br \/>\nconst std::string default_ip &#061; &#034;0.0.0.0&#034;; \/\/ \u9ed8\u8ba4IP<br \/>\nconst int default_fd &#061; -1;<br \/>\nconst int fd_num_max&#061;64;<br \/>\nconst int non_event&#061;0;<\/p>\n<p>class PollServer<br \/>\n{<br \/>\npublic:<br \/>\n    PollServer(const uint16_t port &#061; default_port, const std::string ip &#061; default_ip)<br \/>\n        : ip_(ip), port_(port)<br \/>\n    {<br \/>\n        for(int i&#061;0;i&lt;fd_num_max;i&#043;&#043;)<br \/>\n        {<br \/>\n            event_fds[i].fd&#061;default_fd;<br \/>\n            event_fds[i].events&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n            event_fds[i].revents&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n        }<br \/>\n    }<br \/>\n    ~PollServer()<br \/>\n    {<br \/>\n        listensock_.Close();<br \/>\n    }<\/p>\n<p>    bool Init()<br \/>\n    {<br \/>\n        listensock_.Socket();<br \/>\n        listensock_.Bind(port_);<br \/>\n        listensock_.Listen();<\/p>\n<p>        return true;<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<\/p>\n<p>    }<\/p>\n<p>private:<br \/>\n    uint16_t port_;          \/\/ \u7ed1\u5b9a\u7684\u7aef\u53e3\u53f7<br \/>\n    Sock listensock_;        \/\/ \u4e13\u95e8\u7528\u6765listen\u7684<br \/>\n    std::string ip_;         \/\/ ip\u5730\u5740<br \/>\n    struct pollfd event_fds[fd_num_max];<br \/>\n}; <\/p>\n<p>\u4e0a\u9762\u8fd9\u4e9b\u5c31\u662f\u6700\u6700\u57fa\u672c\u7684\u4e1c\u897f&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u5f88\u5bb9\u6613\u5199\u51fa\u4e0b\u9762\u8fd9\u4e9b\u4e1c\u897f&#xff01;&#xff01;<\/p>\n<p> void HandlerEvent()<br \/>\n    {<br \/>\n        for (int n &#061; 0; n &lt; fd_num_max; n&#043;&#043;)<br \/>\n        {<br \/>\n            int fd &#061; event_fds[n].fd;<br \/>\n            if (fd &#061;&#061; default_fd) \/\/ \u65e0\u6548\u7684<br \/>\n                continue;<\/p>\n<p>            if (event_fds[n].revents&amp;POLLIN) \/\/ fd\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n            {<br \/>\n                \/\/ 1.\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n                if (fd &#061;&#061; listensock_.Fd()) \/\/ \u5982\u679c\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff01;&#xff01;&#xff01;<br \/>\n                {<br \/>\n                    Accept();<br \/>\n                }<br \/>\n                \/\/ 2.\u662f\u901a\u4fe1\u7684\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff0c;fd\u4e0d\u662flisten\u5957\u63a5\u5b57<br \/>\n                else \/\/ \u8bfb\u4e8b\u4ef6<br \/>\n                {<br \/>\n                    Receiver(fd,n);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        int listensock &#061; listensock_.Fd();<br \/>\n        event_fds[0].fd&#061;listensock;\/\/\u628alisten\u653e\u5230\u9996\u4e2a\u6570\u7ec4\u4e0b\u6807\u91cc\u9762<br \/>\n        event_fds[0].events&#061;POLLIN;\/\/\u53ea\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n        \/\/revent\u53ef\u4ee5\u4e0d\u8bbe\u7f6e<br \/>\n        int timeout&#061;3000;\/\/3s<br \/>\n        for (;;)<br \/>\n        {<br \/>\n            int n &#061; poll(event_fds,fd_num_max,timeout);<\/p>\n<p>            switch (n)<br \/>\n            {<br \/>\n            case 0:<br \/>\n                std::cout &lt;&lt; &#034;time out&#8230;..&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            case -1:<br \/>\n                std::cout &lt;&lt; &#034;poll error&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            default:<br \/>\n                \/\/ \u6709\u4e8b\u4ef6\u5c31\u7eea<br \/>\n                std::cout &lt;&lt; &#034;get a new link&#034; &lt;&lt; std::endl;<br \/>\n                HandlerEvent(); \/\/ \u5904\u7406\u4e8b\u4ef6<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u4fee\u6539 Accept()\u548cReceive()\u4e86<\/p>\n<p>void Accept()<br \/>\n    {<br \/>\n        \/\/ \u6211\u4eec\u7684\u8fde\u63a5\u4e8b\u4ef6\u5c31\u7eea\u4e86<br \/>\n        std::string clientip;<br \/>\n        uint16_t clientport;<\/p>\n<p>        int sockfd &#061; listensock_.Accept(&amp;clientip, &amp;clientport); \/\/ \u8fd9\u91cc\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57<br \/>\n        \/\/ \u8bf7\u95ee\u8fdb\u7a0b\u4f1a\u963b\u585e\u5728Accept\u8fd9\u91cc\u5417&#xff1f;\u7b54\u6848\u662f\u4e0d\u4f1a\u7684&#xff0c;\u56e0\u4e3a\u4e0a\u5c42\u7684select\u5df2\u7ecf\u5b8c\u6210\u7684\u4e86\u7b49\u5f85\u90e8\u5206&#xff0c;accept\u53ea\u9700\u8981\u5b8c\u6210\u5efa\u7acb\u8fde\u63a5\u5373\u53ef<br \/>\n        if (sockfd &lt; 0)<br \/>\n            return;<br \/>\n        else \/\/ \u628a\u65b0fd\u52a0\u5165\u4f4d\u56fe<br \/>\n        {<br \/>\n            int i &#061; 1;<br \/>\n            for (; i &lt; fd_num_max; i&#043;&#043;) \/\/ \u4e3a\u4ec0\u4e48\u4ece1\u5f00\u59cb&#xff0c;\u56e0\u4e3a\u6211\u4eec0\u53f7\u4e0b\u6807\u5bf9\u5e94\u7684\u662flisten\u5957\u63a5\u5b57&#xff0c;\u6211\u4eec\u4e0d\u8981\u4fee\u6539<br \/>\n            {<br \/>\n                if (event_fds[i].fd !&#061; default_fd) \/\/ \u6ca1\u627e\u5230\u7a7a\u4f4d<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                { \/\/ \u627e\u5230\u7a7a\u4f4d,\u4f46\u4e0d\u80fd\u76f4\u63a5\u6dfb\u52a0<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            if (i !&#061; fd_num_max) \/\/ \u6ca1\u6709\u6ee1<br \/>\n            {<br \/>\n                event_fds[i].fd &#061; sockfd; \/\/ \u628a\u65b0\u8fde\u63a5\u52a0\u5165\u6570\u7ec4<br \/>\n                 event_fds[i].events&#061;POLLIN ;\/\/\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n                 event_fds[i].revents &#061; non_event;\/\/\u91cd\u7f6e\u4e00\u4e0b<\/p>\n<p>                Printfd();<br \/>\n            }<br \/>\n            else \/\/ \u6ee1\u4e86<br \/>\n            {<br \/>\n                close(sockfd); \/\/ \u5904\u7406\u4e0d\u4e86\u4e86&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u9009\u62e9\u5173\u95ed\u8fde\u63a5<br \/>\n                \/\/\u5f53\u7136&#xff0c;\u5982\u679c\u60f3\u8981\u8fde\u63a5\u66f4\u591a\u94fe\u63a5&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u4e0d\u8fc7\u6211\u4eec\u8fd9\u91cc\u5c31\u4e0d\u505a\u4e86<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n     void Printfd()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;online fd list: &#034;;<br \/>\n        for (int i &#061; 0; i &lt; fd_num_max; i&#043;&#043;)<br \/>\n        {<br \/>\n            if (event_fds[i].fd &#061;&#061; default_fd)<br \/>\n                continue;<br \/>\n            else<br \/>\n            {<br \/>\n                std::cout &lt;&lt; event_fds[i].fd &lt;&lt; &#034; &#034;;<br \/>\n            }<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void Receiver(int fd, int i)<br \/>\n    {<br \/>\n        char in_buff[1024];<br \/>\n        int n &#061; read(fd, in_buff, sizeof(in_buff) &#8211; 1);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            in_buff[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;get message: &#034; &lt;&lt; in_buff &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else if (n &#061;&#061; 0) \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u00a0PollServer.hpp<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &#034;Socket.hpp&#034;<br \/>\n#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;sys\/time.h&gt;<br \/>\n#include &lt;poll.h&gt; <\/p>\n<p>const uint16_t default_port &#061; 8877;       \/\/ \u9ed8\u8ba4\u7aef\u53e3\u53f7<br \/>\nconst std::string default_ip &#061; &#034;0.0.0.0&#034;; \/\/ \u9ed8\u8ba4IP<br \/>\nconst int default_fd &#061; -1;<br \/>\nconst int fd_num_max&#061;64;<br \/>\nconst int non_event&#061;0;<\/p>\n<p>class PollServer<br \/>\n{<br \/>\npublic:<br \/>\n    PollServer(const uint16_t port &#061; default_port, const std::string ip &#061; default_ip)<br \/>\n        : ip_(ip), port_(port)<br \/>\n    {<br \/>\n        for(int i&#061;0;i&lt;fd_num_max;i&#043;&#043;)<br \/>\n        {<br \/>\n            event_fds[i].fd&#061;default_fd;<br \/>\n            event_fds[i].events&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n            event_fds[i].revents&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n        }<br \/>\n    }<br \/>\n    ~PollServer()<br \/>\n    {<br \/>\n        listensock_.Close();<br \/>\n    }<\/p>\n<p>    bool Init()<br \/>\n    {<br \/>\n        listensock_.Socket();<br \/>\n        listensock_.Bind(port_);<br \/>\n        listensock_.Listen();<\/p>\n<p>        return true;<br \/>\n    }<\/p>\n<p>    void Accept()<br \/>\n    {<br \/>\n        \/\/ \u6211\u4eec\u7684\u8fde\u63a5\u4e8b\u4ef6\u5c31\u7eea\u4e86<br \/>\n        std::string clientip;<br \/>\n        uint16_t clientport;<\/p>\n<p>        int sockfd &#061; listensock_.Accept(&amp;clientip, &amp;clientport); \/\/ \u8fd9\u91cc\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57<br \/>\n        \/\/ \u8bf7\u95ee\u8fdb\u7a0b\u4f1a\u963b\u585e\u5728Accept\u8fd9\u91cc\u5417&#xff1f;\u7b54\u6848\u662f\u4e0d\u4f1a\u7684&#xff0c;\u56e0\u4e3a\u4e0a\u5c42\u7684select\u5df2\u7ecf\u5b8c\u6210\u7684\u4e86\u7b49\u5f85\u90e8\u5206&#xff0c;accept\u53ea\u9700\u8981\u5b8c\u6210\u5efa\u7acb\u8fde\u63a5\u5373\u53ef<br \/>\n        if (sockfd &lt; 0)<br \/>\n            return;<br \/>\n        else \/\/ \u628a\u65b0fd\u52a0\u5165\u4f4d\u56fe<br \/>\n        {<br \/>\n            int i &#061; 1;<br \/>\n            for (; i &lt; fd_num_max; i&#043;&#043;) \/\/ \u4e3a\u4ec0\u4e48\u4ece1\u5f00\u59cb&#xff0c;\u56e0\u4e3a\u6211\u4eec0\u53f7\u4e0b\u6807\u5bf9\u5e94\u7684\u662flisten\u5957\u63a5\u5b57&#xff0c;\u6211\u4eec\u4e0d\u8981\u4fee\u6539<br \/>\n            {<br \/>\n                if (event_fds[i].fd !&#061; default_fd) \/\/ \u6ca1\u627e\u5230\u7a7a\u4f4d<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                { \/\/ \u627e\u5230\u7a7a\u4f4d,\u4f46\u4e0d\u80fd\u76f4\u63a5\u6dfb\u52a0<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            if (i !&#061; fd_num_max) \/\/ \u6ca1\u6709\u6ee1<br \/>\n            {<br \/>\n                event_fds[i].fd &#061; sockfd; \/\/ \u628a\u65b0\u8fde\u63a5\u52a0\u5165\u6570\u7ec4<br \/>\n                 event_fds[i].events&#061;POLLIN ;\/\/\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n                 event_fds[i].revents &#061; non_event;\/\/\u91cd\u7f6e\u4e00\u4e0b<\/p>\n<p>                Printfd();<br \/>\n            }<br \/>\n            else \/\/ \u6ee1\u4e86<br \/>\n            {<br \/>\n                close(sockfd); \/\/ \u5904\u7406\u4e0d\u4e86\u4e86&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u9009\u62e9\u5173\u95ed\u8fde\u63a5<br \/>\n                \/\/\u5f53\u7136&#xff0c;\u5982\u679c\u60f3\u8981\u8fde\u63a5\u66f4\u591a\u94fe\u63a5&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u4e0d\u8fc7\u6211\u4eec\u8fd9\u91cc\u5c31\u4e0d\u505a\u4e86<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n     void Printfd()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;online fd list: &#034;;<br \/>\n        for (int i &#061; 0; i &lt; fd_num_max; i&#043;&#043;)<br \/>\n        {<br \/>\n            if (event_fds[i].fd &#061;&#061; default_fd)<br \/>\n                continue;<br \/>\n            else<br \/>\n            {<br \/>\n                std::cout &lt;&lt; event_fds[i].fd &lt;&lt; &#034; &#034;;<br \/>\n            }<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void Receiver(int fd, int i)<br \/>\n    {<br \/>\n        char in_buff[1024];<br \/>\n        int n &#061; read(fd, in_buff, sizeof(in_buff) &#8211; 1);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            in_buff[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;get message: &#034; &lt;&lt; in_buff &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else if (n &#061;&#061; 0) \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void HandlerEvent()<br \/>\n    {<br \/>\n        for (int n &#061; 0; n &lt; fd_num_max; n&#043;&#043;)<br \/>\n        {<br \/>\n            int fd &#061; event_fds[n].fd;<br \/>\n            if (fd &#061;&#061; default_fd) \/\/ \u65e0\u6548\u7684<br \/>\n                continue;<\/p>\n<p>            if (event_fds[n].revents&amp;POLLIN) \/\/ fd\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n            {<br \/>\n                \/\/ 1.\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n                if (fd &#061;&#061; listensock_.Fd()) \/\/ \u5982\u679c\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff01;&#xff01;&#xff01;<br \/>\n                {<br \/>\n                    Accept();<br \/>\n                }<br \/>\n                \/\/ 2.\u662f\u901a\u4fe1\u7684\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff0c;fd\u4e0d\u662flisten\u5957\u63a5\u5b57<br \/>\n                else \/\/ \u8bfb\u4e8b\u4ef6<br \/>\n                {<br \/>\n                    Receiver(fd,n);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        int listensock &#061; listensock_.Fd();<br \/>\n        event_fds[0].fd&#061;listensock;\/\/\u628alisten\u653e\u5230\u9996\u4e2a\u6570\u7ec4\u4e0b\u6807\u91cc\u9762<br \/>\n        event_fds[0].events&#061;POLLIN;\/\/\u53ea\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n        \/\/revent\u53ef\u4ee5\u4e0d\u8bbe\u7f6e<br \/>\n        int timeout&#061;3000;\/\/3s<br \/>\n        for (;;)<br \/>\n        {<br \/>\n            int n &#061; poll(event_fds,fd_num_max,timeout);<\/p>\n<p>            switch (n)<br \/>\n            {<br \/>\n            case 0:<br \/>\n                std::cout &lt;&lt; &#034;time out&#8230;..&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            case -1:<br \/>\n                std::cout &lt;&lt; &#034;poll error&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            default:<br \/>\n                \/\/ \u6709\u4e8b\u4ef6\u5c31\u7eea<br \/>\n                std::cout &lt;&lt; &#034;get a new link&#034; &lt;&lt; std::endl;<br \/>\n                HandlerEvent(); \/\/ \u5904\u7406\u4e8b\u4ef6<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t port_;          \/\/ \u7ed1\u5b9a\u7684\u7aef\u53e3\u53f7<br \/>\n    Sock listensock_;        \/\/ \u4e13\u95e8\u7528\u6765listen\u7684<br \/>\n    std::string ip_;         \/\/ ip\u5730\u5740<br \/>\n    struct pollfd event_fds[fd_num_max];<br \/>\n}; <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"216\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053044-6811b584b8edf.png\" width=\"544\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"530\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053044-6811b584e7ab3.png\" width=\"582\" \/><\/p>\n<p>\u6211\u4eec\u518d\u94fe\u63a5\u4e00\u4e2a\u770b\u770b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"195\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053045-6811b58528908.png\" width=\"518\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"380\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053045-6811b58557ca7.png\" width=\"426\" \/><\/p>\n<p>\u975e\u5e38\u5b8c\u7f8e\u4e86\u554a&#xff01;&#xff01;&#xff01;<\/p>\n<h3 id=\"2.2.poll%E7%9A%84%E4%BC%98%E7%BC%BA%E7%82%B9\">2.2.poll\u7684\u4f18\u7f3a\u70b9<\/h3>\n<ul>\n<li><span style=\"background-color:#ffd900\">poll\u4e0d\u662f\u4e5f\u7528\u4e86\u4e00\u4e2a\u6570\u7ec4\u5417&#xff1f;\u4ed6\u662f\u600e\u4e48\u89e3\u51b3\u4e86fd\u6709\u4e0a\u9650\u7684\u95ee\u9898&#xff1f;<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u51ed\u7684\u662f\u8fd9\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u6211\u4eec\u81ea\u5df1\u8bbe\u7f6e\u7684&#xff0c;\u800cselect\u7684\u90a3\u4e2a\u6570\u7ec4\u662f\u5df2\u7ecf\u56fa\u5b9a\u4e86\u7684\u3002<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#956fe7\">\u00a0\u00a0\u00a0\u5176\u5b9epoll\u7684\u4f18\u70b9\u5c31\u662f\u89e3\u51b3\u4e86select\u652f\u6301\u7684fd\u6709\u4e0a\u9650&#xff0c;\u4ee5\u53ca\u7528\u6237\u8f93\u5165\u4fe1\u606f\u548c\u5185\u6838\u8f93\u51fa\u4fe1\u606f\u8026\u5408\u7684\u4e24\u4e2a\u95ee\u9898\u3002<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4f46poll\u7684\u7f3a\u70b9\u5176\u5b9e\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u4f53\u73b0\u51fa\u6765\u4e86\u4e00\u90e8\u5206&#xff0c;\u5185\u6838\u5728\u68c0\u6d4bfd\u662f\u5426\u5c31\u7eea\u65f6&#xff0c;\u9700\u8981\u904d\u5386\u6574\u4e2a\u7ed3\u6784\u4f53\u6570\u7ec4\u68c0\u6d4bevents\u7684\u503c&#xff0c;<span style=\"color:#0d0016\"><span style=\"background-color:#ffd900\">\u540c\u6837\u7528\u6237\u5728\u5904\u7406\u5c31\u7eea\u7684fd\u4e8b\u4ef6\u65f6&#xff0c;\u4e5f\u9700\u8981\u904d\u5386\u6574\u4e2a\u7ed3\u6784\u4f53\u6570\u7ec4\u68c0\u6d4brevents\u7684\u503c&#xff0c;\u5f53rfds\u7ed3\u6784\u4f53\u6570\u7ec4\u8d8a\u6765\u8d8a\u5927\u65f6&#xff0c;\u6bcf\u6b21\u904d\u5386\u6570\u7ec4\u5176\u5b9e\u5c31\u4f1a\u964d\u4f4e\u670d\u52a1\u5668\u7684\u6548\u7387<\/span><\/span>&#xff0c;\u4e3a\u6b64&#xff0c;\u5185\u6838\u63d0\u4f9b\u4e86epoll\u63a5\u53e3\u6765\u89e3\u51b3\u8fd9\u6837\u7684\u95ee\u9898\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0eselect\u76f8\u540c\u7684\u662f&#xff0c;<span style=\"color:#fe2c24\">poll\u4e5f\u9700\u8981\u7528\u6237\u81ea\u5df1\u7ef4\u62a4\u4e00\u4e2a\u7b2c\u4e09\u65b9\u6570\u7ec4\u6765\u5b58\u50a8\u7528\u6237\u9700\u8981\u5173\u5fc3\u7684fd\u53ca\u4e8b\u4ef6&#xff0c;\u53ea\u4e0d\u8fc7poll\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u8c03\u7528\u524d\u90fd\u91cd\u65b0\u8bbe\u7f6e\u5173\u5fc3\u7684fd&#xff0c;\u56e0\u4e3a\u7528\u6237\u7684\u8f93\u5165\u548c\u5185\u6838\u7684\u8f93\u51fa\u662f\u5206\u79bb\u7684&#xff0c;\u5206\u522b\u5728\u7ed3\u6784\u4f53\u7684events\u548crevents\u7684\u4e24\u4e2a\u5b57\u6bb5&#xff0c;\u505a\u5230\u4e86\u8f93\u5165\u548c\u8f93\u51fa\u5206\u79bb\u3002<\/span><\/p>\n<h3 id=\"2.3.%E6%BA%90%E4%BB%A3%E7%A0%81\">2.3.\u6e90\u4ee3\u7801<\/h3>\n<p>PollServer.hpp<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &#034;Socket.hpp&#034;<br \/>\n#include &lt;sys\/select.h&gt;<br \/>\n#include &lt;sys\/time.h&gt;<br \/>\n#include &lt;poll.h&gt; <\/p>\n<p>const uint16_t default_port &#061; 8877;       \/\/ \u9ed8\u8ba4\u7aef\u53e3\u53f7<br \/>\nconst std::string default_ip &#061; &#034;0.0.0.0&#034;; \/\/ \u9ed8\u8ba4IP<br \/>\nconst int default_fd &#061; -1;<br \/>\nconst int fd_num_max&#061;64;<br \/>\nconst int non_event&#061;0;<\/p>\n<p>class PollServer<br \/>\n{<br \/>\npublic:<br \/>\n    PollServer(const uint16_t port &#061; default_port, const std::string ip &#061; default_ip)<br \/>\n        : ip_(ip), port_(port)<br \/>\n    {<br \/>\n        for(int i&#061;0;i&lt;fd_num_max;i&#043;&#043;)<br \/>\n        {<br \/>\n            event_fds[i].fd&#061;default_fd;<br \/>\n            event_fds[i].events&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n            event_fds[i].revents&#061;non_event;\/\/\u6682\u65f6\u4e0d\u5173\u5fc3<br \/>\n        }<br \/>\n    }<br \/>\n    ~PollServer()<br \/>\n    {<br \/>\n        listensock_.Close();<br \/>\n    }<\/p>\n<p>    bool Init()<br \/>\n    {<br \/>\n        listensock_.Socket();<br \/>\n        listensock_.Bind(port_);<br \/>\n        listensock_.Listen();<\/p>\n<p>        return true;<br \/>\n    }<\/p>\n<p>    void Accept()<br \/>\n    {<br \/>\n        \/\/ \u6211\u4eec\u7684\u8fde\u63a5\u4e8b\u4ef6\u5c31\u7eea\u4e86<br \/>\n        std::string clientip;<br \/>\n        uint16_t clientport;<\/p>\n<p>        int sockfd &#061; listensock_.Accept(&amp;clientip, &amp;clientport); \/\/ \u8fd9\u91cc\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57<br \/>\n        \/\/ \u8bf7\u95ee\u8fdb\u7a0b\u4f1a\u963b\u585e\u5728Accept\u8fd9\u91cc\u5417&#xff1f;\u7b54\u6848\u662f\u4e0d\u4f1a\u7684&#xff0c;\u56e0\u4e3a\u4e0a\u5c42\u7684select\u5df2\u7ecf\u5b8c\u6210\u7684\u4e86\u7b49\u5f85\u90e8\u5206&#xff0c;accept\u53ea\u9700\u8981\u5b8c\u6210\u5efa\u7acb\u8fde\u63a5\u5373\u53ef<br \/>\n        if (sockfd &lt; 0)<br \/>\n            return;<br \/>\n        else \/\/ \u628a\u65b0fd\u52a0\u5165\u4f4d\u56fe<br \/>\n        {<br \/>\n            int i &#061; 1;<br \/>\n            for (; i &lt; fd_num_max; i&#043;&#043;) \/\/ \u4e3a\u4ec0\u4e48\u4ece1\u5f00\u59cb&#xff0c;\u56e0\u4e3a\u6211\u4eec0\u53f7\u4e0b\u6807\u5bf9\u5e94\u7684\u662flisten\u5957\u63a5\u5b57&#xff0c;\u6211\u4eec\u4e0d\u8981\u4fee\u6539<br \/>\n            {<br \/>\n                if (event_fds[i].fd !&#061; default_fd) \/\/ \u6ca1\u627e\u5230\u7a7a\u4f4d<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                { \/\/ \u627e\u5230\u7a7a\u4f4d,\u4f46\u4e0d\u80fd\u76f4\u63a5\u6dfb\u52a0<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            if (i !&#061; fd_num_max) \/\/ \u6ca1\u6709\u6ee1<br \/>\n            {<br \/>\n                event_fds[i].fd &#061; sockfd; \/\/ \u628a\u65b0\u8fde\u63a5\u52a0\u5165\u6570\u7ec4<br \/>\n                 event_fds[i].events&#061;POLLIN ;\/\/\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n                 event_fds[i].revents &#061; non_event;\/\/\u91cd\u7f6e\u4e00\u4e0b<\/p>\n<p>                Printfd();<br \/>\n            }<br \/>\n            else \/\/ \u6ee1\u4e86<br \/>\n            {<br \/>\n                close(sockfd); \/\/ \u5904\u7406\u4e0d\u4e86\u4e86&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u9009\u62e9\u5173\u95ed\u8fde\u63a5<br \/>\n                \/\/\u5f53\u7136&#xff0c;\u5982\u679c\u60f3\u8981\u8fde\u63a5\u66f4\u591a\u94fe\u63a5&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u4e0d\u8fc7\u6211\u4eec\u8fd9\u91cc\u5c31\u4e0d\u505a\u4e86<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n     void Printfd()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;online fd list: &#034;;<br \/>\n        for (int i &#061; 0; i &lt; fd_num_max; i&#043;&#043;)<br \/>\n        {<br \/>\n            if (event_fds[i].fd &#061;&#061; default_fd)<br \/>\n                continue;<br \/>\n            else<br \/>\n            {<br \/>\n                std::cout &lt;&lt; event_fds[i].fd &lt;&lt; &#034; &#034;;<br \/>\n            }<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void Receiver(int fd, int i)<br \/>\n    {<br \/>\n        char in_buff[1024];<br \/>\n        int n &#061; read(fd, in_buff, sizeof(in_buff) &#8211; 1);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            in_buff[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;get message: &#034; &lt;&lt; in_buff &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else if (n &#061;&#061; 0) \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            close(fd);               \/\/ \u6211\u670d\u52a1\u5668\u4e5f\u8981\u5173\u95ed<br \/>\n            event_fds[i].fd &#061; default_fd; \/\/ \u91cd\u7f6e\u6570\u7ec4\u5185\u7684\u503c<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void HandlerEvent()<br \/>\n    {<br \/>\n        for (int n &#061; 0; n &lt; fd_num_max; n&#043;&#043;)<br \/>\n        {<br \/>\n            int fd &#061; event_fds[n].fd;<br \/>\n            if (fd &#061;&#061; default_fd) \/\/ \u65e0\u6548\u7684<br \/>\n                continue;<\/p>\n<p>            if (event_fds[n].revents&amp;POLLIN) \/\/ fd\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n            {<br \/>\n                \/\/ 1.\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86<br \/>\n                if (fd &#061;&#061; listensock_.Fd()) \/\/ \u5982\u679c\u662flisten\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff01;&#xff01;&#xff01;<br \/>\n                {<br \/>\n                    Accept();<br \/>\n                }<br \/>\n                \/\/ 2.\u662f\u901a\u4fe1\u7684\u5957\u63a5\u5b57\u5c31\u7eea\u4e86&#xff0c;fd\u4e0d\u662flisten\u5957\u63a5\u5b57<br \/>\n                else \/\/ \u8bfb\u4e8b\u4ef6<br \/>\n                {<br \/>\n                    Receiver(fd,n);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        int listensock &#061; listensock_.Fd();<br \/>\n        event_fds[0].fd&#061;listensock;\/\/\u628alisten\u653e\u5230\u9996\u4e2a\u6570\u7ec4\u4e0b\u6807\u91cc\u9762<br \/>\n        event_fds[0].events&#061;POLLIN;\/\/\u53ea\u5173\u5fc3\u8bfb\u4e8b\u4ef6<br \/>\n        \/\/revent\u53ef\u4ee5\u4e0d\u8bbe\u7f6e<br \/>\n        int timeout&#061;3000;\/\/3s<br \/>\n        for (;;)<br \/>\n        {<br \/>\n            int n &#061; poll(event_fds,fd_num_max,timeout);<\/p>\n<p>            switch (n)<br \/>\n            {<br \/>\n            case 0:<br \/>\n                std::cout &lt;&lt; &#034;time out&#8230;..&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            case -1:<br \/>\n                std::cout &lt;&lt; &#034;poll error&#034; &lt;&lt; std::endl;<br \/>\n                break;<br \/>\n            default:<br \/>\n                \/\/ \u6709\u4e8b\u4ef6\u5c31\u7eea<br \/>\n                std::cout &lt;&lt; &#034;get a new link&#034; &lt;&lt; std::endl;<br \/>\n                HandlerEvent(); \/\/ \u5904\u7406\u4e8b\u4ef6<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t port_;          \/\/ \u7ed1\u5b9a\u7684\u7aef\u53e3\u53f7<br \/>\n    Sock listensock_;        \/\/ \u4e13\u95e8\u7528\u6765listen\u7684<br \/>\n    std::string ip_;         \/\/ ip\u5730\u5740<br \/>\n    struct pollfd event_fds[fd_num_max];<br \/>\n}; <\/p>\n<p>Socket.hpp<\/p>\n<p>#pragma once  <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.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;  <\/p>\n<p>\/\/ \u5b9a\u4e49\u4e00\u4e9b\u9519\u8bef\u4ee3\u7801<br \/>\nenum<br \/>\n{<br \/>\n    SocketErr &#061; 2,    \/\/ \u5957\u63a5\u5b57\u521b\u5efa\u9519\u8bef<br \/>\n    BindErr,          \/\/ \u7ed1\u5b9a\u9519\u8bef<br \/>\n    ListenErr,        \/\/ \u76d1\u542c\u9519\u8bef<br \/>\n};  <\/p>\n<p>\/\/ \u76d1\u542c\u961f\u5217\u7684\u957f\u5ea6<br \/>\nconst int backlog &#061; 10;  <\/p>\n<p>class Sock  \/\/\u670d\u52a1\u5668\u4e13\u95e8\u4f7f\u7528<br \/>\n{<br \/>\npublic:<br \/>\n    Sock() : sockfd_(-1) \/\/ \u521d\u59cb\u5316\u65f6&#xff0c;\u5c06sockfd_\u8bbe\u4e3a-1&#xff0c;\u8868\u793a\u672a\u521d\u59cb\u5316\u7684\u5957\u63a5\u5b57<br \/>\n    {<br \/>\n    }<br \/>\n    ~Sock()<br \/>\n    {<br \/>\n        \/\/ \u6790\u6784\u51fd\u6570\u4e2d\u53ef\u4ee5\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u4f46\u8fd9\u91cc\u9009\u62e9\u4e0d\u5728\u6790\u6784\u51fd\u6570\u4e2d\u5173\u95ed&#xff0c;\u56e0\u4e3a\u6709\u65f6\u9700\u8981\u624b\u52a8\u7ba1\u7406\u8d44\u6e90<br \/>\n    }  <\/p>\n<p>    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    void Socket()<br \/>\n    {<br \/>\n        sockfd_ &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (sockfd_ &lt; 0)<br \/>\n        {<br \/>\n            printf(&#034;socket error, %s: %d&#034;, strerror(errno), errno); \/\/\u9519\u8bef<br \/>\n            exit(SocketErr); \/\/ \u53d1\u751f\u9519\u8bef\u65f6\u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<br \/>\n        int opt&#061;1;<br \/>\n        setsockopt(sockfd_,SOL_SOCKET,SO_REUSEADDR,&amp;opt,sizeof(opt)); \/\/\u670d\u52a1\u5668\u4e3b\u52a8\u5173\u95ed\u540e\u5feb\u901f\u91cd\u542f<br \/>\n    }  <\/p>\n<p>    \/\/ \u5c06\u5957\u63a5\u5b57\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7aef\u53e3\u4e0a<br \/>\n    void Bind(uint16_t port)<br \/>\n    {<br \/>\n        \/\/\u8ba9\u670d\u52a1\u5668\u7ed1\u5b9aIP\u5730\u5740\u4e0e\u7aef\u53e3\u53f7<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));\/\/\u6e05\u96f6<br \/>\n        local.sin_family &#061; AF_INET;  \/\/ \u7f51\u7edc<br \/>\n        local.sin_port &#061; htons(port);  \/\/ \u6211\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u7ed1\u5b9a\u4efb\u610f\u53ef\u7528IP\u5730\u5740<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u76d1\u542c\u6240\u6709\u53ef\u7528\u7684\u7f51\u7edc\u63a5\u53e3  <\/p>\n<p>        if (bind(sockfd_, (struct sockaddr *)&amp;local, sizeof(local)) &lt; 0)  \/\/\u8ba9\u81ea\u5df1\u7ed1\u5b9a\u522b\u4eba<br \/>\n        {<br \/>\n            printf(&#034;bind error, %s: %d&#034;, strerror(errno), errno);<br \/>\n            exit(BindErr);<br \/>\n        }<br \/>\n    }  <\/p>\n<p>    \/\/ \u76d1\u542c\u7aef\u53e3\u4e0a\u7684\u8fde\u63a5\u8bf7\u6c42<br \/>\n    void Listen()<br \/>\n    {<br \/>\n        if (listen(sockfd_, backlog) &lt; 0)<br \/>\n        {<br \/>\n            printf(&#034;listen error, %s: %d&#034;, strerror(errno), errno);<br \/>\n            exit(ListenErr);<br \/>\n        }<br \/>\n    }  <\/p>\n<p>    \/\/ \u63a5\u53d7\u4e00\u4e2a\u8fde\u63a5\u8bf7\u6c42<br \/>\n    int Accept(std::string *clientip, uint16_t *clientport)<br \/>\n    {<br \/>\n        struct sockaddr_in peer;<br \/>\n        socklen_t len &#061; sizeof(peer);<br \/>\n        int newfd &#061; accept(sockfd_, (struct sockaddr*)&amp;peer, &amp;len);  <\/p>\n<p>        if(newfd &lt; 0)<br \/>\n        {<br \/>\n            printf(&#034;accept error, %s: %d&#034;, strerror(errno), errno);<br \/>\n            return -1;<br \/>\n        }  <\/p>\n<p>        char ipstr[64];<br \/>\n        inet_ntop(AF_INET, &amp;peer.sin_addr, ipstr, sizeof(ipstr));<br \/>\n        *clientip &#061; ipstr;<br \/>\n        *clientport &#061; ntohs(peer.sin_port);  <\/p>\n<p>        return newfd; \/\/ \u8fd4\u56de\u65b0\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    }  <\/p>\n<p>    \/\/ \u8fde\u63a5\u5230\u6307\u5b9a\u7684IP\u548c\u7aef\u53e3\u2014\u2014\u5ba2\u6237\u7aef\u624d\u4f1a\u7528\u7684<br \/>\n    bool Connect(const std::string &amp;ip, const uint16_t &amp;port)<br \/>\n    {<br \/>\n        struct sockaddr_in peer;\/\/\u670d\u52a1\u5668\u7684\u4fe1\u606f<br \/>\n        memset(&amp;peer, 0, sizeof(peer));<br \/>\n        peer.sin_family &#061; AF_INET;<br \/>\n        peer.sin_port &#061; htons(port);<\/p>\n<p>        inet_pton(AF_INET, ip.c_str(), &amp;(peer.sin_addr));  <\/p>\n<p>        int n &#061; connect(sockfd_, (struct sockaddr*)&amp;peer, sizeof(peer));<br \/>\n        if(n &#061;&#061; -1)<br \/>\n        {<br \/>\n            std::cerr &lt;&lt; &#034;connect to &#034; &lt;&lt; ip &lt;&lt; &#034;:&#034; &lt;&lt; port &lt;&lt; &#034; error&#034; &lt;&lt; std::endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }  <\/p>\n<p>    \/\/ \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    void Close()<br \/>\n    {<br \/>\n        close(sockfd_);<br \/>\n    }  <\/p>\n<p>    \/\/ \u83b7\u53d6\u5957\u63a5\u5b57\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    int Fd()<br \/>\n    {<br \/>\n        return sockfd_;<br \/>\n    }  <\/p>\n<p>private:<br \/>\n    int sockfd_; \/\/ \u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n}; <\/p>\n<p>main.cc<\/p>\n<p>#include&#034;PollServer.hpp&#034;<br \/>\n#include&lt;memory&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::unique_ptr&lt;PollServer&gt; svr(new PollServer());<br \/>\n    svr-&gt;Init();<br \/>\n    svr-&gt;Start();<br \/>\n} <\/p>\n<p>makefile<\/p>\n<p>poll_server:main.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;11<br \/>\n.PHONY:clean<br \/>\nclean:<br \/>\nrm -rf poll_server <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.2k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u7531\u4e8eselect\u51fd\u6570\u6709\u4e0b\u9762\u51e0\u4e2a\u7279\u522b\u660e\u663e\u7684\u7f3a\u70b9\uff0c\u5c31\u63a8\u6f14\u51fa\u4e86\u6539\u8fdb\u7248\u672c\u2014\u2014poll\u51fd\u6570\u6bd4\u5982select\u76d1\u89c6\u7684fd\u662f\u6709\u4e0a\u9650\u7684\uff0c\u6211\u7684\u4e91\u670d\u52a1\u5668\u5185\u6838\u7248\u672c\u4e0b\u6700\u5927\u4e0a\u9650\u662f1024\u4e2afd\uff0c\u4e3b\u8981\u8fd8\u662f\u56e0\u4e3afd_set\u4ed6\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u4f4d\u56fe\u7ed3\u6784\uff0c\u4f4d\u56fe\u4e2d\u7684\u6570\u7ec4\u5f00\u8f9f\u4e4b\u540e\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u8fd9\u662f\u5185\u6838\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9664\u975e\u4f60\u4fee\u6539\u5185\u6838\u53c2\u6570\uff0c\u5426\u5219\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u6240\u4ee5\u4e00\u65e6select\u76d1\u89c6\u7684fd\u6570\u91cf\u8d85\u8fc71024\uff0c\u5219select\u4f1a\u62a5\u9519\u3002_poll\u51fd\u6570<\/p>\n","protected":false},"author":2,"featured_media":34709,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[192,114,768,205,100,78,191,122],"topic":[],"class_list":["post-34716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-mysql","tag-sql","tag-tcp-ip","tag-100","tag-78","tag-191","tag-122"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \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\/34716.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.2k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u7531\u4e8eselect\u51fd\u6570\u6709\u4e0b\u9762\u51e0\u4e2a\u7279\u522b\u660e\u663e\u7684\u7f3a\u70b9\uff0c\u5c31\u63a8\u6f14\u51fa\u4e86\u6539\u8fdb\u7248\u672c\u2014\u2014poll\u51fd\u6570\u6bd4\u5982select\u76d1\u89c6\u7684fd\u662f\u6709\u4e0a\u9650\u7684\uff0c\u6211\u7684\u4e91\u670d\u52a1\u5668\u5185\u6838\u7248\u672c\u4e0b\u6700\u5927\u4e0a\u9650\u662f1024\u4e2afd\uff0c\u4e3b\u8981\u8fd8\u662f\u56e0\u4e3afd_set\u4ed6\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u4f4d\u56fe\u7ed3\u6784\uff0c\u4f4d\u56fe\u4e2d\u7684\u6570\u7ec4\u5f00\u8f9f\u4e4b\u540e\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u8fd9\u662f\u5185\u6838\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9664\u975e\u4f60\u4fee\u6539\u5185\u6838\u53c2\u6570\uff0c\u5426\u5219\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u6240\u4ee5\u4e00\u65e6select\u76d1\u89c6\u7684fd\u6570\u91cf\u8d85\u8fc71024\uff0c\u5219select\u4f1a\u62a5\u9519\u3002_poll\u51fd\u6570\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/34716.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-30T05:30:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053043-6811b5831e899.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=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34716.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/34716.html\",\"name\":\"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-30T05:30:46+00:00\",\"dateModified\":\"2025-04-30T05:30:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/34716.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/34716.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34716.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668\"}]},{\"@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":"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \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\/34716.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.2k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u7531\u4e8eselect\u51fd\u6570\u6709\u4e0b\u9762\u51e0\u4e2a\u7279\u522b\u660e\u663e\u7684\u7f3a\u70b9\uff0c\u5c31\u63a8\u6f14\u51fa\u4e86\u6539\u8fdb\u7248\u672c\u2014\u2014poll\u51fd\u6570\u6bd4\u5982select\u76d1\u89c6\u7684fd\u662f\u6709\u4e0a\u9650\u7684\uff0c\u6211\u7684\u4e91\u670d\u52a1\u5668\u5185\u6838\u7248\u672c\u4e0b\u6700\u5927\u4e0a\u9650\u662f1024\u4e2afd\uff0c\u4e3b\u8981\u8fd8\u662f\u56e0\u4e3afd_set\u4ed6\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u4f4d\u56fe\u7ed3\u6784\uff0c\u4f4d\u56fe\u4e2d\u7684\u6570\u7ec4\u5f00\u8f9f\u4e4b\u540e\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u8fd9\u662f\u5185\u6838\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9664\u975e\u4f60\u4fee\u6539\u5185\u6838\u53c2\u6570\uff0c\u5426\u5219\u4e0d\u4f1a\u5728\u53d8\u5316\u4e86\uff0c\u6240\u4ee5\u4e00\u65e6select\u76d1\u89c6\u7684fd\u6570\u91cf\u8d85\u8fc71024\uff0c\u5219select\u4f1a\u62a5\u9519\u3002_poll\u51fd\u6570","og_url":"https:\/\/www.wsisp.com\/helps\/34716.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-30T05:30:46+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250430053043-6811b5831e899.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/34716.html","url":"https:\/\/www.wsisp.com\/helps\/34716.html","name":"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-30T05:30:46+00:00","dateModified":"2025-04-30T05:30:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/34716.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/34716.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/34716.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u7f51\u7edc\u3011\u9ad8\u7ea7IO\u2014\u2014poll\u7248\u672cTCP\u670d\u52a1\u5668"}]},{"@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\/34716","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=34716"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/34716\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/34709"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=34716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=34716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=34716"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=34716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}