{"id":42498,"date":"2025-06-09T12:31:01","date_gmt":"2025-06-09T04:31:01","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/42498.html"},"modified":"2025-06-09T12:31:01","modified_gmt":"2025-06-09T04:31:01","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%88%86%e6%9e%90-reactor%e7%ba%bf%e7%a8%8b%e6%a8%a1%e5%9e%8b%e5%92%8c%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e5%ba%8f%e7%ab%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/42498.html","title":{"rendered":"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0)"},"content":{"rendered":"<h2>1 \u5f15\u8a00<\/h2>\n<p>\u6700\u8fd1\u5728\u62dc\u8bfb\u9648\u7855\u7684\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b[1]&#xff0c;\u76ee\u524d\u8bfb\u5230\u7b2c3\u7ae0&#xff0c;\u4e3b\u8981\u8bb2\u5e38\u7528\u7684\u670d\u52a1\u7aef\u7684\u7f16\u7a0b\u6a21\u578b&#xff0c;\u5176\u4e2d\u63d0\u5230\u4e86\u5f88\u591a\u7ebf\u7a0b\u6a21\u578b\u4ee5\u53ca\u4e0e\u6027\u80fd\u76f8\u5173\u7684\u5206\u6790&#xff0c;\u8bfb\u4e86\u4e4b\u540e\u60f3\u5b9e\u8df5\u4e00\u4e0b\u4e66\u4e2d\u63d0\u5230\u7684\u7406\u8bba&#xff0c;\u4e5f\u987a\u4fbf\u590d\u4e60\u4e00\u4e0b\u7f51\u7edc\u7f16\u7a0b\u7684\u76f8\u5173\u5185\u5bb9&#xff0c;\u4e8e\u662f\u641c\u7d22\u4e86\u5f88\u591a\u5173\u4e8eReactor\u548cProactor\u76f8\u5173\u7684\u5185\u5bb9&#xff0c;\u5bf9\u4e8e\u91cc\u9762\u6982\u5ff5\u7684\u8bb2\u8ff0\u662f\u53ef\u4ee5\u7406\u89e3\u7684&#xff0c;\u4f46\u662f\u5982\u679c\u5177\u8c61\u5316\u5230\u4ee3\u7801\u4e2d\u8fd8\u662f\u65e0\u6cd5\u7b2c\u4e00\u65f6\u95f4\u5728\u8111\u4e2d\u5f62\u6210\u6982\u5ff5\u3002\u56e0\u6b64\u60f3\u7ed3\u5408\u4e66\u4e2d\u7b2c\u4e09\u7ae0\u7684\u8bb2\u89e3&#xff0c;\u634b\u4e00\u634b\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6a21\u578b\u4ee5\u53ca\u6027\u80fd\u5206\u6790&#xff0c;\u901a\u8fc7C&#043;&#043;\u4ee3\u7801\u9010\u6b65\u5f04\u660e\u767d\u5404\u79cd\u670d\u52a1\u7aef\u6a21\u578b\u548cIO\u6a21\u578b\u3002\u6ca1\u770b\u8fc7\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b\u7684\u8bfb\u8005\u4e0d\u5fc5\u62c5\u5fc3&#xff0c;\u672c\u6587\u53ea\u662f\u4ee5\u4e66\u4e3a\u7ebf\u7d22\u5c3d\u91cf\u5168\u9762\u7684\u8bb2\u8ff0\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u8bbe\u8ba1&#xff0c;\u6d89\u53ca\u5230\u4e66\u4e2d\u7684\u5185\u5bb9\u65f6\u4f1a\u5f15\u7528\u76f8\u5173\u7684\u539f\u6587\u3002<\/p>\n<h2>2 \u9605\u8bfb\u5bfc\u5f15<\/h2>\n<ul>\n<li>\u9605\u8bfb\u672c\u6587\u9996\u5148\u8981\u4e86\u89e3\u4e0d\u540c\u7684IO\u6a21\u578b&#xff0c;\u63a8\u8350\u9605\u8bfb\u7406\u89e3\u4e00\u4e0b5\u79cdIO\u6a21\u578b\u3001\u963b\u585eIO\u548c\u975e\u963b\u585eIO\u3001\u540c\u6b65IO\u548c\u5f02\u6b65IO [2]&#xff1b;<\/li>\n<li>\u5176\u6b21\u8981\u4e86\u89e3socket\u7f16\u7a0b&#xff0c;\u8981\u4e86\u89e3socket\u7684\u7cfb\u7edf\u8c03\u7528\u7684\u529f\u80fd&#xff1b;<\/li>\n<li>\u672c\u6587\u4e2d\u7684\u4ee3\u7801\u4ec5\u4ec5\u662f\u4e3a\u4e86\u65b9\u4fbf\u5c55\u73b0\u591a\u7ebf\u7a0b\u800c\u5b9e\u73b0&#xff0c;\u56e0\u6b64\u4f1a\u7f3a\u4e4f\u5173\u4e8e\u5f02\u5e38\u5904\u7406\u6216\u8005\u65e5\u5fd7\u7cfb\u7edf\u7b49\u6a21\u5757\u7684\u4ee3\u7801&#xff1b;<\/li>\n<li>\u6240\u6709\u7684\u4ee3\u7801\u5747\u53ef\u5728C&#043;&#043;\u5b9e\u73b0\u7684\u591a\u7ebf\u7a0b\u5206\u5e03\u5f0f\u5b58\u50a8\u670d\u52a1\u5668\u4ee3\u7801\u83b7\u53d6&#xff0c;\u6bcf\u4e00\u7ae0\u8282\u5747\u6709\u5bf9\u5e94\u3002<\/li>\n<\/ul>\n<p>\u7d22\u5f15&#xff1a;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u7d22\u5f15)<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6b63\u5f0f\u5f00\u59cb&#xff1a;<\/p>\n<h2>3 \u963b\u585e\u670d\u52a1\u5668<\/h2>\n<h3>3.1 \u5355\u7ebf\u7a0b\u963b\u585e\u670d\u52a1\u5668<\/h3>\n<p>\u3010\u4ee3\u7801reactor\/single_thread\u3011<\/p>\n<p>\u4f60\u662f\u4e00\u4e2a\u5356\u714e\u997c\u679c\u5b50\u7684\u5c0f\u8001\u677f&#xff0c;\u6bcf\u5929\u63a8\u7740\u5c0f\u8f66\u5230\u516c\u53f8\u95e8\u53e3&#xff0c;\u6bcf\u5230\u4e2d\u5348\u4f60\u7684\u5c0f\u8f66\u524d\u603b\u80fd\u6392\u8d77\u4e00\u4e2a\u4e0d\u957f\u4e0d\u77ed\u7684\u961f\u4f0d\u3002\u6765\u4e00\u4e2a\u987e\u5ba2\u5c31\u644a\u4e00\u4e2a\u714e\u997c\u679c\u5b50&#xff0c;\u5e76\u4e14\u4f60\u53ea\u80fd\u644a\u5b8c\u5f53\u524d\u8fd9\u4e2a\u987e\u5ba2\u5e76\u4e14\u4ea4\u7ed9\u4ed6\u624d\u80fd\u7ed9\u4e0b\u4e00\u4e2a\u987e\u5ba2\u644a&#xff0c;\u5c31\u50cf\u4e2a&#034;\u65e0\u60c5\u7684\u644a\u714e\u997c\u679c\u5b50\u670d\u52a1\u5668&#034;&#xff0c;\u8fd9\u5c31\u662f\u6700\u57fa\u7840\u7684\u5355\u7ebf\u7a0b\u963b\u585e\u670d\u52a1\u5668\u3002<\/p>\n<p>\u8bf4\u714e\u997c\u679c\u5b50\u53ea\u662f\u7ed9\u8bfb\u8005\u7c7b\u6bd4\u4e00\u4e0b&#xff0c;\u4e0b\u9762\u7528\u5177\u4f53\u7684\u4ee3\u7801\u8fdb\u884c\u8bb2\u89e3&#xff1a; \u9996\u5148\u6765\u521b\u5efa\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u4ee3\u7801&#xff0c;\u9996\u5148\u670d\u52a1\u7aef\u5c31\u5229\u7528linux\u63d0\u4f9b\u7684socket\u76f8\u5173\u7684\u7cfb\u7edf\u8c03\u7528\u505a\u4e00\u4e2atcp\u670d\u52a1\u5668&#xff0c;\u6574\u4f53\u670d\u52a1\u7aef\u8981\u505a\u7684\u662f\u63a5\u6536\u5ba2\u6237\u7aef\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef&#034;hello world&#034;&#xff0c;\u5176\u4e2dhandle_request()\u51fd\u6570\u662f\u5904\u7406\u5ba2\u6237\u7aef\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b&#xff0c;\u5f53\u524d\u662f\u6253\u5370\u5230\u5c4f\u5e55&#xff0c;\u4e3a\u4e86\u66f4\u76f4\u89c2\u7684\u5c55\u793a\u4e0d\u540c\u7c7b\u578b\u670d\u52a1\u5668\u6a21\u578b\u7684\u5904\u7406\u8017\u65f6&#xff0c;\u6211\u4eec\u7ed9\u670d\u52a1\u7aef\u7684\u5904\u7406\u52a0\u4e00\u4e2asleep 1\u6beb\u79d2&#xff0c;\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\u7ebf\u7a0b\u7684\u521b\u5efa\u4e5f\u662f\u6709\u5f00\u9500\u7684&#xff0c;\u5982\u679c\u670d\u52a1\u7aef\u7684\u5904\u7406\u53ea\u662f\u6253\u5370\u90a3\u4e48\u521b\u5efa\u7ebf\u7a0b\u7684\u65f6\u95f4\u5bf9\u4e8e\u603b\u4f53\u4e0a\u7684\u8017\u65f6\u4f1a\u6709\u5f88\u5927\u7684\u5f71\u54cd&#xff0c;\u6211\u4eec\u53ef\u80fd\u65e0\u6cd5\u6e05\u6670\u7684\u611f\u77e5\u5230\u591a\u7ebf\u7a0b\u548c\u5355\u7ebf\u7a0b\u7684\u533a\u522b\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">SERVER_SLEEP_MILLISEC<\/span> <span class=\"token expression\"><span class=\"token number\">1<\/span><\/span><\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">handle_request<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> connfd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> buffer<span class=\"token punctuation\">[<\/span><span class=\"token number\">512<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">memset<\/span><span class=\"token punctuation\">(<\/span>buffer<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">512<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> res <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>connfd<span class=\"token punctuation\">,<\/span> buffer<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>buffer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token double-colon punctuation\">::<\/span>this_thread<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">sleep_for<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token double-colon punctuation\">::<\/span>chrono<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">milliseconds<\/span><span class=\"token punctuation\">(<\/span>SERVER_SLEEP_MILLISEC<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Server Received: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> buffer <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">char<\/span> response<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;hello world&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    res <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>connfd<span class=\"token punctuation\">,<\/span> response<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">shutdown<\/span><span class=\"token punctuation\">(<\/span>connfd<span class=\"token punctuation\">,<\/span> SHUT_RD<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>connfd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> sockfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span> SOCK_STREAM<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> serv_addr<span class=\"token punctuation\">;<\/span><br \/>\n    serv_addr<span class=\"token punctuation\">.<\/span>sin_family <span class=\"token operator\">&#061;<\/span> AF_INET<span class=\"token punctuation\">;<\/span><br \/>\n    serv_addr<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">.<\/span>s_addr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htonl<\/span><span class=\"token punctuation\">(<\/span>INADDR_ANY<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    serv_addr<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8080<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>serv_addr<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>serv_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> cli_addr<span class=\"token punctuation\">;<\/span><br \/>\n        socklen_t cli_len <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>cli_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> connfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">accept<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>cli_addr<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>cli_len<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">handle_request<\/span><span class=\"token punctuation\">(<\/span>connfd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u63a5\u7740\u6211\u4eec\u6765\u6784\u5efa\u5ba2\u6237\u7aefclient&#xff0c;\u9996\u5148\u5bf9client\u7f16\u53f7\u5e76\u5c06&#034;i am client&#034;\u548c\u7f16\u53f7\u7ec4\u5408\u6210\u5b57\u7b26\u4e32\u53d1\u9001\u7ed9\u670d\u52a1\u7aef&#xff1a;<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">MAX_CLIENT<\/span> <span class=\"token expression\"><span class=\"token number\">1000<\/span><\/span><\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">request_server<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> sockfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span> SOCK_STREAM<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> serv_addr<span class=\"token punctuation\">;<\/span><br \/>\n    serv_addr<span class=\"token punctuation\">.<\/span>sin_family <span class=\"token operator\">&#061;<\/span> AF_INET<span class=\"token punctuation\">;<\/span><br \/>\n    serv_addr<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8080<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">inet_pton<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span><span class=\"token punctuation\">(<\/span>serv_addr<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> res <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">connect<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>serv_addr<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>serv_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">char<\/span> request<span class=\"token punctuation\">[<\/span><span class=\"token number\">128<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;i am client &#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> num<span class=\"token punctuation\">[<\/span><span class=\"token number\">128<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">sprintf<\/span><span class=\"token punctuation\">(<\/span>num<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;%d&#034;<\/span><span class=\"token punctuation\">,<\/span> i<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">strcat<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">,<\/span> num<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    res <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> request<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">char<\/span> response<span class=\"token punctuation\">[<\/span><span class=\"token number\">512<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">memset<\/span><span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">512<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    res <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> response<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Client Received: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> response <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">shutdown<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> SHUT_RD<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/* for loop request server *\/<\/span><br \/>\n    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> MAX_CLIENT<span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">request_server<\/span><span class=\"token punctuation\">(<\/span>i<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/* multi-threaded request server *\/<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>std<span class=\"token double-colon punctuation\">::<\/span>thread<span class=\"token operator\">&gt;<\/span> threads<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> MAX_CLIENT<span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        threads<span class=\"token punctuation\">.<\/span><span class=\"token function\">emplace_back<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token double-colon punctuation\">::<\/span>thread<span class=\"token punctuation\">{<\/span>request_server<span class=\"token punctuation\">,<\/span> i<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">auto<\/span><span class=\"token operator\">&amp;<\/span> p <span class=\"token operator\">:<\/span> threads<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        p<span class=\"token punctuation\">.<\/span><span class=\"token function\">join<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c\u4e00\u4e0b&#xff1a;<\/p>\n<p>g&#043;&#043; server.cc <span class=\"token parameter variable\">-o<\/span> server <span class=\"token parameter variable\">-std<\/span><span class=\"token operator\">&#061;<\/span>c&#043;&#043;11 <span class=\"token parameter variable\">-I<\/span> \/usr\/include\/ <span class=\"token parameter variable\">-lpthread<\/span><br \/>\ng&#043;&#043; client.cc <span class=\"token parameter variable\">-o<\/span> client <span class=\"token parameter variable\">-std<\/span><span class=\"token operator\">&#061;<\/span>c&#043;&#043;11 <span class=\"token parameter variable\">-I<\/span> \/usr\/include\/ <span class=\"token parameter variable\">-lpthread<\/span><br \/>\n.\/server <span class=\"token operator\">&amp;<\/span> <span class=\"token comment\"># \u5982\u679c\u60f3\u5728\u670d\u52a1\u7aef\u770b\u5230\u5ba2\u6237\u7aef\u7684\u5185\u5bb9\u5f00\u4e24\u4e2a\u7a97\u53e3&#xff0c;\u52a0\u4e86&amp;\u8868\u793a\u8fdb\u7a0b\u540e\u53f0\u8fd0\u884c<\/span><br \/>\n.\/client<\/p>\n<p>\u4e4b\u540e\u7684\u6240\u6709\u8ba8\u8bba\u90fd\u662f\u57fa\u4e8e\u4ee5\u4e0a\u8fd9\u4e24\u6bb5server\u548cclient\u7aef\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u7684main\u51fd\u6570\u91cc&#xff0c;\u5ba2\u6237\u7aef\u53d1\u90011000\u4e2a\u8bf7\u6c42&#xff0c;\u5206\u522b\u4f7f\u7528for\u5faa\u73af\u548c\u591a\u7ebf\u7a0b&#xff0c;\u89c2\u5bdf\u4e8c\u8005\u7684\u8017\u65f6&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-684663847c2ca.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u53ef\u4ee5\u53d1\u73b0\u591a\u7ebf\u7a0b\u8bbf\u95ee\u4ec5\u4ec5\u6bd4for\u5faa\u73af\u5feb\u4e86\u4e00\u70b9\u70b9&#xff0c;\u8fd9\u662f\u56e0\u4e3a\u670d\u52a1\u5668\u662f\u5355\u7ebf\u7a0b\u7684&#xff0c;\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u5904\u7406\u903b\u8f91\u90fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d&#xff0c;\u5e76\u4e14read\u662f\u963b\u585e\u7684&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u7684\u6570\u636e\u4e00\u76f4\u4e0d\u5230&#xff0c;\u90a3\u4e48\u670d\u52a1\u7aef\u7ebf\u7a0b\u5c31\u7b49\u5728read&#xff0c;\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-684663848786c.jpg\" alt=\"\u8bf7\u6dfb\u52a0\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>3.2 \u591a\u7ebf\u7a0b\u963b\u585e\u670d\u52a1\u5668<\/h3>\n<p>\u3010\u4ee3\u7801&#xff1a;reactor\/multi_thread_sync_block\u3011<\/p>\n<p>\u4f60\u7684\u751f\u610f\u8d8a\u6765\u8d8a\u7ea2\u706b&#xff0c;\u6392\u961f\u7684\u4eba\u4e5f\u8d8a\u6765\u8d8a\u591a&#xff0c;\u4f46\u662f\u6709\u4e9b\u4eba\u770b\u6392\u961f\u592a\u4e45\u5403\u4e0d\u4e0a\u4f60\u7684\u714e\u997c\u679c\u5b50\u5c31\u53bb\u65c1\u8fb9\u5403\u9e21\u86cb\u704c\u997c\u4e86&#xff0c;\u4f60\u770b\u7740\u751f\u610f\u88ab\u9e21\u86cb\u704c\u997c\u5206\u8d70\u5fc3\u91cc\u4e0d\u592a\u723d&#xff0c;\u4e8e\u662f\u72e0\u72e0\u5fc3\u6708\u85aa2\u4e07\u62db\u8058\u4e86\u4e00\u4e2a\u725b\u9a6c\u5e2e\u4f60\u644a&#xff0c;\u4f60\u7684\u644a\u5b50\u4e8e\u662f\u6709\u4e24\u4e2a\u4eba\u53ef\u4ee5\u540c\u65f6\u505a\u714e\u997c\u679c\u5b50&#xff0c;\u901f\u5ea6\u4e00\u4e0b\u4e0a\u53bb\u4e86\u3002<\/p>\n<p>\u6309\u7167\u714e\u997c\u679c\u5b50\u7684\u601d\u8def&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u628a\u6765\u4e00\u4e2a\u8fde\u63a5\u5c31\u5904\u7406\u4e00\u4e2a\u8fde\u63a5\u53d8\u6210\u6765\u4e00\u4e2a\u8fde\u63a5\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u5904\u7406\u8fd9\u4e2a\u8fde\u63a5&#xff0c;\u5404\u4e2a\u8fde\u63a5\u7684\u5904\u7406\u4e92\u4e0d\u5f71\u54cd&#xff0c;\u8fd9\u6837\u5c31\u53ef\u4ee5\u540c\u65f6\u54cd\u5e94\u591a\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e86&#xff0c;\u670d\u52a1\u7aef\u5faa\u73af\u5904\u7406\u8fde\u63a5\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u6362\u6210&#xff1a;<\/p>\n<p><span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> cli_addr<span class=\"token punctuation\">;<\/span><br \/>\n    socklen_t cli_len <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>cli_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> connfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">accept<\/span><span class=\"token punctuation\">(<\/span>sockfd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>cli_addr<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>cli_len<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token double-colon punctuation\">::<\/span>thread <span class=\"token function\">t<\/span><span class=\"token punctuation\">(<\/span>handle_request<span class=\"token punctuation\">,<\/span> connfd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    t<span class=\"token punctuation\">.<\/span><span class=\"token function\">detach<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u770b\u4e00\u4e0b1000\u4e2a\u7ebf\u7a0b\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u8017\u65f6&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-68466384944b4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u76f8\u6bd4\u4e8e\u5355\u7ebf\u7a0b\u7684\u670d\u52a1\u5668&#xff0c;\u901f\u5ea6\u5feb\u4e86\u4e0d\u662f\u4e00\u661f\u534a\u70b9&#xff0c;\u5176\u6a21\u578b\u5982\u4e0b\u56fe&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-684663849f0d1.jpg\" alt=\"\u8bf7\u6dfb\u52a0\u56fe\u7247\u63cf\u8ff0\" \/> \u90a3\u6709\u4e86\u591a\u7ebf\u7a0b\u662f\u4e0d\u662f\u5c31\u9ad8\u6795\u65e0\u5fe7\u4e86\u5462&#xff1f;\u670d\u52a1\u7aef\u60f3\u63a5\u6536\u591a\u5c11\u8bf7\u6c42\u5c31\u80fd\u63a5\u6536\u591a\u5c11\u5417&#xff1f;\u57283.1\u8282\u6700\u5f00\u59cb\u63d0\u5230\u521b\u5efa\u7ebf\u7a0b\u662f\u4e00\u4e2a\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u64cd\u4f5c&#xff08;\u8fd9\u4e2a\u95ee\u9898\u5c06\u4f1a\u5728\u540e\u9762\u8bb2\u7ebf\u7a0b\u6c60\u65f6\u8fdb\u884c\u8bb2\u89e3&#xff09;&#xff0c;\u9ed8\u8ba4\u5206\u914d\u7ed9\u7ebf\u7a0b\u6808\u7684\u5927\u5c0f\u5c31\u662f1MB&#xff0c;\u90a3\u4e481024\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u5360\u75281GB\u7684\u5185\u5b58&#xff0c;\u56e0\u6b64\u7ebf\u7a0b\u4e5f\u662f\u975e\u5e38\u5b9d\u8d35\u7684\u8d44\u6e90&#xff0c;\u4e0d\u53ef\u80fd\u65e0\u9650\u6269\u5c55\u3002\u5176\u6b21\u5982\u679c\u5355\u4e2a\u670d\u52a1\u7aef\u7684\u7ebf\u7a0b\u963b\u585e\u5728read&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42&#xff0c;\u5e76\u4e14\u65e0\u6cd5\u7ee7\u7eed\u5176\u4ed6\u7684\u5904\u7406\u903b\u8f91&#xff0c;\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u3002\u5f53\u6709\u8db3\u591f\u591a\u7684\u7ebf\u7a0b\u90fd\u5904\u4e8e\u963b\u585e\u65f6&#xff0c;\u670d\u52a1\u7aef\u5c06\u65e0\u6cd5\u63a5\u6536\u66f4\u591a\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u90a3\u4e48\u63a5\u4e0b\u6765\u8be5\u600e\u4e48\u505a\u5462&#xff1f;\u4e14\u542c\u4e0b\u56de\u5206\u89e3\u3002\u3002\u3002 \u4e0b\u4e00\u7bc7\u94fe\u63a5&#xff1a;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u4e00) [1]\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b [2] \u7406\u89e3\u4e00\u4e0b5\u79cdIO\u6a21\u578b\u3001\u963b\u585eIO\u548c\u975e\u963b\u585eIO\u3001\u540c\u6b65IO\u548c\u5f02\u6b65IO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb932\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u6700\u8fd1\u5728\u62dc\u8bfb\u9648\u7855\u7684\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b[1]\uff0c\u76ee\u524d\u8bfb\u5230\u7b2c3\u7ae0\uff0c\u4e3b\u8981\u8bb2\u5e38\u7528\u7684\u670d\u52a1\u7aef\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u5176\u4e2d\u63d0\u5230\u4e86\u5f88\u591a\u7ebf\u7a0b\u6a21\u578b\u4ee5\u53ca\u4e0e\u6027\u80fd\u76f8\u5173\u7684\u5206\u6790\uff0c\u8bfb\u4e86\u4e4b\u540e\u60f3\u5b9e\u8df5\u4e00\u4e0b\u4e66\u4e2d\u63d0\u5230\u7684\u7406\u8bba\uff0c\u4e5f\u987a\u4fbf\u590d\u4e60\u4e00\u4e0b\u7f51\u7edc\u7f16\u7a0b\u7684\u76f8\u5173\u5185\u5bb9\uff0c\u4e8e\u662f\u641c\u7d22\u4e86\u5f88\u591a\u5173\u4e8eReactor\u548cProactor\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5bf9\u4e8e\u91cc\u9762\u6982\u5ff5\u7684\u8bb2\u8ff0\u662f\u53ef\u4ee5\u7406\u89e3\u7684\uff0c\u4f46\u662f\u5982\u679c\u5177\u8c61\u5316\u5230\u4ee3\u7801\u4e2d\u8fd8\u662f\u65e0\u6cd5\u7b2c\u4e00\u65f6\u95f4\u5728\u8111\u4e2d\u5f62\u6210\u6982\u5ff5\u3002\u56e0\u6b64\u60f3\u7ed3\u5408\u4e66\u4e2d\u7b2c\u4e09\u7ae0\u7684\u8bb2\u89e3\uff0c\u634b\u4e00\u634b\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6a21\u578b\u4ee5\u53ca\u6027\u80fd\u5206\u6790\uff0c\u901a\u8fc7C++\u4ee3\u7801\u9010\u6b65\u5f04\u660e\u767d\u5404\u79cd\u670d\u52a1\u7aef\u6a21\u578b\u548cIO\u6a21\u578b\u3002<\/p>\n","protected":false},"author":2,"featured_media":42494,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,252,43],"topic":[],"class_list":["post-42498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-252","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \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\/42498.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb932\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u6700\u8fd1\u5728\u62dc\u8bfb\u9648\u7855\u7684\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b[1]\uff0c\u76ee\u524d\u8bfb\u5230\u7b2c3\u7ae0\uff0c\u4e3b\u8981\u8bb2\u5e38\u7528\u7684\u670d\u52a1\u7aef\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u5176\u4e2d\u63d0\u5230\u4e86\u5f88\u591a\u7ebf\u7a0b\u6a21\u578b\u4ee5\u53ca\u4e0e\u6027\u80fd\u76f8\u5173\u7684\u5206\u6790\uff0c\u8bfb\u4e86\u4e4b\u540e\u60f3\u5b9e\u8df5\u4e00\u4e0b\u4e66\u4e2d\u63d0\u5230\u7684\u7406\u8bba\uff0c\u4e5f\u987a\u4fbf\u590d\u4e60\u4e00\u4e0b\u7f51\u7edc\u7f16\u7a0b\u7684\u76f8\u5173\u5185\u5bb9\uff0c\u4e8e\u662f\u641c\u7d22\u4e86\u5f88\u591a\u5173\u4e8eReactor\u548cProactor\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5bf9\u4e8e\u91cc\u9762\u6982\u5ff5\u7684\u8bb2\u8ff0\u662f\u53ef\u4ee5\u7406\u89e3\u7684\uff0c\u4f46\u662f\u5982\u679c\u5177\u8c61\u5316\u5230\u4ee3\u7801\u4e2d\u8fd8\u662f\u65e0\u6cd5\u7b2c\u4e00\u65f6\u95f4\u5728\u8111\u4e2d\u5f62\u6210\u6982\u5ff5\u3002\u56e0\u6b64\u60f3\u7ed3\u5408\u4e66\u4e2d\u7b2c\u4e09\u7ae0\u7684\u8bb2\u89e3\uff0c\u634b\u4e00\u634b\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6a21\u578b\u4ee5\u53ca\u6027\u80fd\u5206\u6790\uff0c\u901a\u8fc7C++\u4ee3\u7801\u9010\u6b65\u5f04\u660e\u767d\u5404\u79cd\u670d\u52a1\u7aef\u6a21\u578b\u548cIO\u6a21\u578b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/42498.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-09T04:31:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-684663847c2ca.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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/42498.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/42498.html\",\"name\":\"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-09T04:31:01+00:00\",\"dateModified\":\"2025-06-09T04:31:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/42498.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/42498.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/42498.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \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\/42498.html","og_locale":"zh_CN","og_type":"article","og_title":"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb932\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf30\u6b21\u3002\u6700\u8fd1\u5728\u62dc\u8bfb\u9648\u7855\u7684\u300aLinux\u591a\u7ebf\u7a0b\u670d\u52a1\u7aef\u7f16\u7a0b\u300b[1]\uff0c\u76ee\u524d\u8bfb\u5230\u7b2c3\u7ae0\uff0c\u4e3b\u8981\u8bb2\u5e38\u7528\u7684\u670d\u52a1\u7aef\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u5176\u4e2d\u63d0\u5230\u4e86\u5f88\u591a\u7ebf\u7a0b\u6a21\u578b\u4ee5\u53ca\u4e0e\u6027\u80fd\u76f8\u5173\u7684\u5206\u6790\uff0c\u8bfb\u4e86\u4e4b\u540e\u60f3\u5b9e\u8df5\u4e00\u4e0b\u4e66\u4e2d\u63d0\u5230\u7684\u7406\u8bba\uff0c\u4e5f\u987a\u4fbf\u590d\u4e60\u4e00\u4e0b\u7f51\u7edc\u7f16\u7a0b\u7684\u76f8\u5173\u5185\u5bb9\uff0c\u4e8e\u662f\u641c\u7d22\u4e86\u5f88\u591a\u5173\u4e8eReactor\u548cProactor\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5bf9\u4e8e\u91cc\u9762\u6982\u5ff5\u7684\u8bb2\u8ff0\u662f\u53ef\u4ee5\u7406\u89e3\u7684\uff0c\u4f46\u662f\u5982\u679c\u5177\u8c61\u5316\u5230\u4ee3\u7801\u4e2d\u8fd8\u662f\u65e0\u6cd5\u7b2c\u4e00\u65f6\u95f4\u5728\u8111\u4e2d\u5f62\u6210\u6982\u5ff5\u3002\u56e0\u6b64\u60f3\u7ed3\u5408\u4e66\u4e2d\u7b2c\u4e09\u7ae0\u7684\u8bb2\u89e3\uff0c\u634b\u4e00\u634b\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6a21\u578b\u4ee5\u53ca\u6027\u80fd\u5206\u6790\uff0c\u901a\u8fc7C++\u4ee3\u7801\u9010\u6b65\u5f04\u660e\u767d\u5404\u79cd\u670d\u52a1\u7aef\u6a21\u578b\u548cIO\u6a21\u578b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/42498.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-09T04:31:01+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250609043100-684663847c2ca.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/42498.html","url":"https:\/\/www.wsisp.com\/helps\/42498.html","name":"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-09T04:31:01+00:00","dateModified":"2025-06-09T04:31:01+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/42498.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/42498.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/42498.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u5206\u6790\u2014\u2014Reactor\u7ebf\u7a0b\u6a21\u578b\u548c\u6027\u80fd\u5206\u6790(\u5e8f\u7ae0)"}]},{"@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\/42498","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=42498"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/42498\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/42494"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=42498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=42498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=42498"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=42498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}