{"id":27517,"date":"2025-04-19T19:56:16","date_gmt":"2025-04-19T11:56:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/27517.html"},"modified":"2025-04-19T19:56:16","modified_gmt":"2025-04-19T11:56:16","slug":"c-asio%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b3%e5%90%8c%e6%ad%a5%e8%af%bb%e5%86%99%e7%9a%84%e5%ae%a2%e6%88%b7%e7%ab%af%e5%92%8c%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/27517.html","title":{"rendered":"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u524d\u8a00<\/li>\n<li>\u4e00\u3001\u5ba2\u6237\u7aef\u8bbe\u8ba1<\/li>\n<li>\n<ul>\n<li>\u5ba2\u6237\u7aef\u8bbe\u8ba1\u90e8\u5206\u4ee3\u7801\u8be6\u89e3<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8c\u3001\u670d\u52a1\u7aef\u8bbe\u8ba1<\/li>\n<li>\n<ul>\n<li>\u670d\u52a1\u7aef\u8bbe\u8ba1\u90e8\u5206\u4ee3\u7801\u8be6\u89e3<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u8fd0\u884c\u7a0b\u5e8f<\/li>\n<li>\u603b\u7ed3<\/li>\n<\/ul>\n<hr \/>\n<h2>\u524d\u8a00<\/h2>\n<p>\u4eca\u5929\u662f\u5b66\u6821\u7f51\u7edc\u7f16\u7a0b\u7684\u7b2c\u4e09\u5929&#xff0c;\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86boost::asio\u540c\u6b65\u8bfb\u5199\u7684api\u51fd\u6570&#xff0c;\u73b0\u5728\u5c06\u524d\u9762\u7684api\u4e32\u8054\u8d77\u6765&#xff0c;\u505a\u4e00\u4e2a\u80fd\u8dd1\u8d77\u6765\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002\u4f46\u8bf7\u6ce8\u610f&#xff01;&#xff0c;\u4e4b\u524d\u8bb2\u8fc7\u7684api\u51fd\u6570\u90e8\u5206\u4ee3\u7801\u6211\u4e0b\u9762\u5c06\u4e0d\u4f1a\u518d\u6b64\u8be6\u7ec6\u6ce8\u89e3&#xff0c;\u5982\u6709\u4e0d\u61c2&#xff0c;\u8bf7\u56de\u987e\u4e4b\u524d\u77e5\u8bc6\u70b9<\/p>\n<p>\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u91c7\u7528\u963b\u585e\u7684\u540c\u6b65\u8bfb\u5199\u65b9\u5f0f\u5b8c\u6210\u901a\u4fe1<\/p>\n<hr \/>\n<h2>\u4e00\u3001\u5ba2\u6237\u7aef\u8bbe\u8ba1<\/h2>\n<p>\u5ba2\u6237\u7aef\u8bbe\u8ba1\u57fa\u672c\u601d\u8def\u662f\u6839\u636e\u670d\u52a1\u5668\u5bf9\u7aef\u7684ip\u548c\u7aef\u53e3\u521b\u5efa\u4e00\u4e2aendpoint&#xff0c;\u7136\u540e\u521b\u5efasocket\u8fde\u63a5\u8fd9\u4e2aendpoint&#xff0c;\u4e4b\u540e\u5c31\u53ef\u4ee5\u7528\u540c\u6b65\u8bfb\u5199\u7684\u65b9\u5f0f\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u4e86\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;boost\/asio.hpp&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> MAX_LENGTH <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1024<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u8868\u793a\u53d1\u9001\u548c\u63a5\u6536\u6700\u5927\u957f\u5ea6\u4e3a1024\u5b57\u8282<\/span><br \/>\n<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\">try<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/\u521b\u5efa\u4e0a\u4e0b\u6587\u670d\u52a1<\/span><br \/>\n        boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span>io_context ios<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u521b\u5efaendpoint<\/span><br \/>\n        boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span>ip<span class=\"token double-colon punctuation\">::<\/span>tcp<span class=\"token double-colon punctuation\">::<\/span>endpoint<br \/>\n            <span class=\"token function\">remote_ep<\/span><span class=\"token punctuation\">(<\/span>boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span>ip<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">make_address<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10086<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/127.0.0.1\u662f\u672c\u673a\u7684\u56de\u8def\u5730\u5740 \u56e0\u4e3a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5728\u540c\u4e00\u5730\u5740<\/span><br \/>\n        <span class=\"token comment\">\/\/\u670d\u52a1\u5668\u5c31\u662f\u672c\u673a<\/span><br \/>\n        boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span>ip<span class=\"token double-colon punctuation\">::<\/span>tcp<span class=\"token double-colon punctuation\">::<\/span>socket <span class=\"token function\">sock<\/span><span class=\"token punctuation\">(<\/span>ios<span class=\"token punctuation\">,<\/span> remote_ep<span class=\"token punctuation\">.<\/span><span class=\"token function\">protocol<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        boost<span class=\"token double-colon punctuation\">::<\/span>system<span class=\"token double-colon punctuation\">::<\/span>error_code error <span class=\"token operator\">&#061;<\/span> boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span>error<span class=\"token double-colon punctuation\">::<\/span>host_not_found<span class=\"token punctuation\">;<\/span><br \/>\n        sock<span class=\"token punctuation\">.<\/span><span class=\"token function\">connect<\/span><span class=\"token punctuation\">(<\/span>remote_ep<span class=\"token punctuation\">,<\/span>error<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span><br \/>\n        <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;connect failed,code is:&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> error<span class=\"token punctuation\">.<\/span><span class=\"token function\">value<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span><br \/>\n                <span class=\"token string\">&#034;error message is&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> error<span class=\"token punctuation\">.<\/span><span class=\"token function\">message<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <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>        std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u8bf7\u8f93\u5165\u9700\u8981\u53d1\u9001\u7684\u4fe1\u606f:&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">char<\/span> request<span class=\"token punctuation\">[<\/span>MAX_LENGTH<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token double-colon punctuation\">::<\/span>cin<span class=\"token punctuation\">.<\/span><span class=\"token function\">getline<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">,<\/span> MAX_LENGTH<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        size_t request_length <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u83b7\u53d6\u5b9e\u9645\u8981\u53d1\u9001\u7684\u6570\u636e\u957f\u5ea6<\/span><br \/>\n        <span class=\"token comment\">\/\/\u53d1\u9001\u6570\u636e<\/span><br \/>\n        boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">,<\/span> request_length<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token keyword\">char<\/span> reply<span class=\"token punctuation\">[<\/span>MAX_LENGTH<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5b58\u50a8\u63a5\u6536\u6570\u636e<\/span><br \/>\n        size_t reply_length <span class=\"token operator\">&#061;<\/span> boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> boost<span class=\"token double-colon punctuation\">::<\/span>asio<span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span>reply<span class=\"token punctuation\">,<\/span> reply_length<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;\u56de\u590d:&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token double-colon punctuation\">::<\/span>cout<span class=\"token punctuation\">.<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>reply<span class=\"token punctuation\">,<\/span> reply_length<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;\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span>std<span class=\"token double-colon punctuation\">::<\/span>exception<span class=\"token operator\">&amp;<\/span>e<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token double-colon punctuation\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Exception is: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> e<span class=\"token punctuation\">.<\/span><span class=\"token function\">what<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u5ba2\u6237\u7aef\u8bbe\u8ba1\u90e8\u5206\u4ee3\u7801\u8be6\u89e3<\/h3>\n<p>\u5728\u8fd9\u91cc&#xff0c;\u6211\u89e3\u91ca\u4e00\u4e0b\u4e0a\u8ff0\u90e8\u5206\u4ee3\u7801<\/p>\n<p>(1)\u5173\u4e8ehost_not_found\u51fd\u6570 boost::asio::error::host_not_found \u662f Boost.Asio \u5e93\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u9519\u8bef\u7801&#xff0c;\u7528\u4e8e\u6307\u793a\u5728\u5c1d\u8bd5\u89e3\u6790\u4e3b\u673a\u540d\u65f6\u53d1\u751f\u9519\u8bef\u3002\u5177\u4f53\u6765\u8bf4&#xff0c;\u5f53\u7a0b\u5e8f\u8bd5\u56fe\u5c06\u4e00\u4e2a\u4e3b\u673a\u540d&#xff08;\u5982\u57df\u540d&#xff09;\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684 IP \u5730\u5740\u65f6&#xff0c;\u5982\u679c\u8be5\u4e3b\u673a\u540d\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u89e3\u6790&#xff0c;\u5c31\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u9519\u8bef\u7801\u3002<\/p>\n<p>(2)std::cin.getline(request, MAX_LENGTH);\u5173\u4e8e\u8fd9\u91cc\u7684getline\u51fd\u6570 1.\u5982\u679c\u4f7f\u7528\u7684\u662fcin.getline\u90a3\u4e48&#xff08;&#xff09;\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3achar\u6570\u7ec4&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u5b58\u50a8\u957f\u5ea6length&#xff0c;\u4f46\u5b9e\u9645\u5b58\u50a8\u957f\u5ea6\u4e3alength-1&#xff0c;<\/p>\n<p>2.\u5982\u679c\u4f7f\u7528\u7684\u662fgetline()&#xff0c;\u90a3\u4e48\u4e2d\u95f4\u53c2\u6570\u7b2c\u4e00\u4e2a\u4e3acin\u8868\u793a\u8f93\u5165&#xff0c;\u7b2c\u4e8c\u4e2a\u4e3astring\u5b57\u7b26\u4e32<\/p>\n<p>(3).cout.write\u51fd\u6570 \u8fd9\u91cc\u76f4\u63a5\u5c06\u8fd4\u56de\u503c\u8f93\u51fa\u5230\u63a7\u5236\u53f0std::cout.write(reply, reply_length) &lt;&lt; std::endl;,\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3achar \u6570\u7ec4&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u957f\u5ea6<\/p>\n<h2>\u4e8c\u3001\u670d\u52a1\u7aef\u8bbe\u8ba1<\/h2>\n<p>session\u51fd\u6570 \u521b\u5efasession\u51fd\u6570&#xff0c;\u8be5\u51fd\u6570\u4e3a\u670d\u52a1\u5668\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff0c;\u6bcf\u5f53\u6211\u4eec\u83b7\u53d6\u5ba2\u6237\u7aef\u8fde\u63a5\u540e\u5c31\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728session\u51fd\u6570\u91cc\u91cc\u8fdb\u884cecho\u65b9\u5f0f\u7684\u8bfb\u5199&#xff0c;\u6240\u8c13echo\u5c31\u662f\u5e94\u7b54\u5f0f\u7684\u5904\u7406<\/p>\n<p>server\u51fd\u6570 server\u51fd\u6570\u6839\u636e\u670d\u52a1\u5668ip\u548c\u7aef\u53e3\u521b\u5efa\u670d\u52a1\u5668acceptor\u7528\u6765\u63a5\u6536\u6570\u636e&#xff0c;\u7528socket\u63a5\u6536\u65b0\u7684\u8fde\u63a5&#xff0c;\u7136\u540e\u4e3a\u8fd9\u4e2asocket\u521b\u5efasession\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;boost\/asio.hpp&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;set&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;memory&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> max_length <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1034<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">typedef<\/span> std<span class=\"token operator\">::<\/span>shared_ptr<span class=\"token operator\">&lt;<\/span>boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>ip<span class=\"token operator\">::<\/span>tcp<span class=\"token operator\">::<\/span>socket<span class=\"token operator\">&gt;<\/span> socket_ptr<span class=\"token punctuation\">;<\/span><br \/>\nstd<span class=\"token operator\">::<\/span>set<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_ptr<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>thread<span class=\"token operator\">&gt;&gt;<\/span> thread_set<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u4e13\u95e8\u5904\u7406\u67d0\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u4e0a\u6765\u8fdb\u884c\u6536\u53d1\u6570\u636e<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">session<\/span><span class=\"token punctuation\">(<\/span>socket_ptr sock<span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/\u8fd9\u4e2asession\u662f\u8dd1\u5728\u7ebf\u7a0b\u4e2d\u7684<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    try<br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/\u5faa\u73af\u5904\u7406  \u5ba2\u6237\u7aef\u53d1\u7ed9\u670d\u52a1\u7aef \u670d\u52a1\u7aef\u518d\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">char<\/span> data<span class=\"token punctuation\">[<\/span>max_length<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u7528\u6765\u7f13\u5b58\u63a5\u6536\u7684\u6570\u636e<\/span><br \/>\n            <span class=\"token function\">memset<\/span><span class=\"token punctuation\">(<\/span>data<span class=\"token punctuation\">,<\/span> <span class=\"token char\">&#039;\\\\0&#039;<\/span><span class=\"token punctuation\">,<\/span> max_length<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            boost<span class=\"token operator\">::<\/span>system<span class=\"token operator\">::<\/span>error_code error<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\">\/\/\u8fd9\u91cc\u6211\u4eec\u7528read<\/span><br \/>\n            <span class=\"token comment\">\/*size_t length &#061; boost::asio::read<br \/>\n            (sock, boost::asio::buffer(data, max_length), error);*\/<\/span><br \/>\n            <span class=\"token comment\">\/\/\u4e0a\u9762\u8fd9\u91cc\u8fd9\u79cd\u5199\u6cd5\u5176\u5b9e\u4e0d\u53cb\u597d \u8fd9\u91cc\u9700\u8981\u8bfb\u53d61024\u4e2a\u5b57\u8282\u7684\u6570\u636e<\/span><br \/>\n             <span class=\"token comment\">\/\/\u624d\u4f1a\u8fd4\u56de&#xff0c;\u4f46\u5982\u679c\u5ba2\u6237\u7aef\u53ea\u53d1\u9001\u4e8610\u4e2a\u5b57\u8282\u7684\u6570\u636e\u5462&#xff1f;<\/span><br \/>\n            <span class=\"token comment\">\/\/\u8fd9\u5c31\u4f1a\u4e00\u76f4\u7b49\u7740\u4e0d\u52a8<\/span><br \/>\n            <span class=\"token class-name\">size_t<\/span> length <span class=\"token operator\">&#061;<\/span> sock<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">read_some<\/span><span class=\"token punctuation\">(<\/span>boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span><span class=\"token function\">buffer<\/span><br \/>\n            <span class=\"token punctuation\">(<\/span>data<span class=\"token punctuation\">,<\/span> max_length<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error <span class=\"token operator\">&#061;&#061;<\/span> boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>error<span class=\"token operator\">::<\/span>eof<span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/eof\u8868\u793a\u5bf9\u7aef\u5173\u95ed<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><br \/>\n                std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;connection closed by peer&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>                <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/\u5176\u4ed6\u9519\u8bef\u5c31\u6bd4\u8f83\u4e25\u91cd\u4e86 \u6211\u4eec\u8981\u628a\u5b83\u629b\u51fa\u53bb<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><br \/>\n                throw boost<span class=\"token operator\">::<\/span>system<span class=\"token operator\">::<\/span><span class=\"token function\">system_error<\/span><span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;receive from &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> sock<span class=\"token operator\">-&gt;<\/span><br \/>\n            <span class=\"token function\">remote_endpoint<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">address<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">to_string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;receive message is &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> data <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/\u5c06\u641c\u7684\u6570\u636e\u56de\u4f20\u7ed9\u5bf9\u65b9<\/span><br \/>\n            boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>sock<span class=\"token punctuation\">,<\/span> boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span><span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span>data<span class=\"token punctuation\">,<\/span> length<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Reply sent to client.&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">catch<\/span> <span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span>exception<span class=\"token operator\">&amp;<\/span> e<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Exception in thread: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span><br \/>\n         e<span class=\"token punctuation\">.<\/span><span class=\"token function\">what<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\\\\n&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">server<\/span><span class=\"token punctuation\">(<\/span>boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>io_context<span class=\"token operator\">&amp;<\/span> io_context<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">short<\/span> port<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u4f20\u5165\u4e0a\u4e0b\u6587  \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7ed1\u5b9a\u672c\u5730\u7684\u5730\u5740<\/span><br \/>\n    boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>ip<span class=\"token operator\">::<\/span>tcp<span class=\"token operator\">::<\/span>acceptor <span class=\"token function\">a<\/span><br \/>\n    <span class=\"token punctuation\">(<\/span>io_context<span class=\"token punctuation\">,<\/span> boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>ip<span class=\"token operator\">::<\/span>tcp<span class=\"token operator\">::<\/span><span class=\"token function\">endpoint<\/span><br \/>\n    <span class=\"token punctuation\">(<\/span>boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>ip<span class=\"token operator\">::<\/span>tcp<span class=\"token operator\">::<\/span><span class=\"token function\">v4<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>port<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/\u6b64\u65f6\u6765\u4e86\u4e00\u4e2a\u5ba2\u6237\u7aef  \u6211\u4eec\u4e13\u95e8\u751f\u6210\u4e00\u4e2asocket\u6765\u5904\u7406\u8fd9\u91cc\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42<\/span><br \/>\n        socket_ptr <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>new boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>ip<span class=\"token operator\">::<\/span>tcp<span class=\"token operator\">::<\/span><span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>io_context<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        a<span class=\"token punctuation\">.<\/span><span class=\"token function\">accept<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>socket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5355\u72ec\u521b\u5efa\u4e00\u4e2asession \u8ba9\u8fd9\u4e2asocket\u5728\u8fd9\u4e2asession\u53bb\u5904\u7406\u8fd9\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42<\/span><br \/>\n        <span class=\"token comment\">\/\/\u8fd9\u4e2asession\u662f\u8dd1\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u91cc\u9762 \u8fd9\u6837\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u548c\u8bf7\u6c42<\/span><br \/>\n        <span class=\"token keyword\">auto<\/span> t <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>make_shared<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>thread<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">(<\/span>session<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        thread_set<span class=\"token punctuation\">.<\/span><span class=\"token function\">insert<\/span><span class=\"token punctuation\">(<\/span>t<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><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    try<br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        boost<span class=\"token operator\">::<\/span>asio<span class=\"token operator\">::<\/span>io_context ioc<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">server<\/span><span class=\"token punctuation\">(<\/span>ioc<span class=\"token punctuation\">,<\/span> <span class=\"token number\">10086<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5982\u679c\u4e3b\u7ebf\u7a0bacceptor\u5d29\u4e86 \u4f46\u5b50\u7ebf\u7a0bsocket\u8fd8\u5728\u670d\u52a1\u6bcf\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u63a5\u6536<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5b50\u7ebf\u7a0b\u4e0d\u80fd\u53d7\u5f71\u54cd \u6240\u6709\u6211\u4eec\u8981\u7b49\u5f85\u5b50\u7ebf\u7a0b\u7684\u5b8c\u6210\u624d\u80fd\u5173\u95ed<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">auto<\/span><span class=\"token operator\">&amp;<\/span> t <span class=\"token operator\">:<\/span> thread_set<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            t<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">join<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/join()&#xff1a;<\/span><br \/>\n            <span class=\"token comment\">\/\/\u963b\u585e\u5f53\u524d\u7ebf\u7a0b&#xff0c;\u76f4\u5230\u88ab\u8c03\u7528\u7684\u7ebf\u7a0b\u5b8c\u6210\u3002<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">catch<\/span> <span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span>exception<span class=\"token operator\">&amp;<\/span> e<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Exception1 &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> e<span class=\"token punctuation\">.<\/span><span class=\"token function\">what<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <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<h3>\u670d\u52a1\u7aef\u8bbe\u8ba1\u90e8\u5206\u4ee3\u7801\u8be6\u89e3<\/h3>\n<p>(1).\u9996\u5148\u6211\u4eec\u8fd9\u91cc\u7528\u4e86\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\u6765\u5199&#xff0c;\u5728server\u4e2d&#xff0c;\u5f53\u6709\u4e00\u4f4d\u5ba2\u6237\u7aef\u6765\u4e86&#xff0c;\u6211\u4eec\u5c31\u751f\u6210\u4e00\u4e2a\u5355\u72ec\u7684socket\u53bb\u4e3a\u4ed6\u670d\u52a1\u5e76\u4e14\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u7684\u96c6\u5408&#xff0c;\u7136\u540e\u5c06\u8fd9\u4e2asocket\u548c\u8fd9\u4e2a\u5ba2\u6237\u7aef\u5305\u88c5\u4e3a\u4e00\u4e2a\u7ebf\u7a0bsession&#xff0c;\u8fd9\u6837\u4e0d\u4f1a\u5bfc\u81f4\u5982\u679c\u6211\u8fd9\u4e2asocket\u8fd8\u6ca1\u5b8c&#xff0c;\u53c8\u6765\u4e86\u4e00\u4e2a\u5ba2\u6237\u7aef\u65e0\u6cd5\u89e3\u51b3\u7684\u60c5\u51b5<\/p>\n<p>(2).\u4ee3\u7801\u4e2d&#xff0c;socket_ptr\u662f\u4e00\u4e2astd::shared_ptr&lt;boost::asio::ip::tcp::socket&gt;\u3002\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u5bf9\u8c61&#xff0c;\u5e76\u901a\u8fc7\u667a\u80fd\u6307\u9488\u8fdb\u884c\u7ba1\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5957\u63a5\u5b57\u8d44\u6e90\u5728\u4e0d\u518d\u9700\u8981\u65f6\u88ab\u6b63\u786e\u91ca\u653e\u3002 \u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d&#xff0c;\u624b\u52a8\u7ba1\u7406\u52a8\u6001\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ade\u6001\u6761\u4ef6\u6216\u6b7b\u9501\u7b49\u95ee\u9898\u3002\u667a\u80fd\u6307\u9488\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c\u7ba1\u7406\u5f15\u7528\u8ba1\u6570&#xff0c;\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4e5f\u80fd\u5b89\u5168\u5730\u4f7f\u7528\u3002<\/p>\n<h2>\u4e09\u3001\u8fd0\u884c\u7a0b\u5e8f<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419115614-68038f5e47aef.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u540c\u6b65\u8bfb\u5199\u7684\u4f18\u52a3 1 \u540c\u6b65\u8bfb\u5199\u7684\u7f3a\u9677\u5728\u4e8e\u8bfb\u5199\u662f\u963b\u585e\u7684&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u5bf9\u7aef\u4e0d\u53d1\u9001\u6570\u636e\u670d\u52a1\u5668\u7684read\u64cd\u4f5c\u662f\u963b\u585e\u7684&#xff0c;\u8fd9\u5c06\u5bfc\u81f4\u670d\u52a1\u5668\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u72b6\u6001\u3002 2 \u53ef\u4ee5\u901a\u8fc7\u5f00\u8f9f\u65b0\u7684\u7ebf\u7a0b\u4e3a\u65b0\u751f\u6210\u7684\u8fde\u63a5\u5904\u7406\u8bfb\u5199&#xff0c;\u4f46\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5f00\u8f9f\u7684\u7ebf\u7a0b\u662f\u6709\u9650\u7684&#xff0c;\u7ea6\u4e3a2048\u4e2a\u7ebf\u7a0b&#xff0c;\u5728Linux\u73af\u5883\u53ef\u4ee5\u901a\u8fc7unlimit\u589e\u52a0\u4e00\u4e2a\u8fdb\u7a0b\u5f00\u8f9f\u7684\u7ebf\u7a0b\u6570&#xff0c;\u4f46\u662f\u7ebf\u7a0b\u8fc7\u591a\u4e5f\u4f1a\u5bfc\u81f4\u5207\u6362\u6d88\u8017\u7684\u65f6\u95f4\u7247\u8f83\u591a\u3002 3 \u8be5\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e3a\u5e94\u7b54\u5f0f&#xff0c;\u5b9e\u9645\u573a\u666f\u4e3a\u5168\u53cc\u5de5\u901a\u4fe1\u6a21\u5f0f&#xff0c;\u53d1\u9001\u548c\u63a5\u6536\u8981\u72ec\u7acb\u5206\u5f00\u3002 4 \u8be5\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u672a\u8003\u8651\u7c98\u5305\u5904\u7406\u3002 \u7efc\u4e0a\u6240\u8ff0&#xff0c;\u662f\u6211\u4eec\u8fd9\u4e2a\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u5b58\u5728\u7684\u95ee\u9898&#xff0c;\u4e3a\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898&#xff0c;\u6211\u4eec\u5728\u63a5\u4e0b\u91cc\u7684\u6587\u7ae0\u91cc\u505a\u4e0d\u65ad\u5b8c\u5584\u548c\u6539\u8fdb&#xff0c;\u4e3b\u8981\u4ee5\u5f02\u6b65\u8bfb\u5199\u6539\u8fdb\u4e0a\u8ff0\u65b9\u6848\u3002 \u5f53\u7136\u540c\u6b65\u8bfb\u5199\u7684\u65b9\u5f0f\u4e5f\u6709\u5176\u4f18\u70b9&#xff0c;\u6bd4\u5982\u5ba2\u6237\u7aef\u8fde\u63a5\u6570\u4e0d\u591a&#xff0c;\u800c\u4e14\u670d\u52a1\u5668\u5e76\u53d1\u6027\u4e0d\u9ad8\u7684\u573a\u666f&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u540c\u6b65\u8bfb\u5199\u7684\u65b9\u5f0f\u3002\u4f7f\u7528\u540c\u6b65\u8bfb\u5199\u80fd\u7b80\u5316\u7f16\u7801\u96be\u5ea6\u3002<\/p>\n<p>\u672c\u6587\u7ae0\u4e3b\u8981\u4ee3\u7801\u6ce8\u89e3\u6211\u90fd\u5199\u5230\u4ee3\u7801\u65c1\u7684\u6ce8\u89e3\u4e86&#xff0c;\u8bf7\u5927\u5bb6\u591a\u4ed4\u7ec6\u770b&#xff0c;\u6709\u9519\u8bef\u6216\u8005\u6709\u4e0d\u660e\u767d\u7684\u5730\u65b9\u8bf7\u53d1\u8868\u5728\u8bc4\u8bba\u533a&#xff0c;\u4e00\u8d77\u8ba8\u8bba&#xff0c;\u6b64\u540c\u6b65\u7684\u65b9\u5f0f\u5199\u7684\u5927\u5bb6\u4e86\u89e3\u5373\u53ef&#xff0c;\u73b0\u5728\u57fa\u672c\u4e0a\u4e0d\u4f1a\u7528\u8fd9\u79cd\u5199\u6cd5&#xff0c;\u63a5\u4e0b\u6765\u51e0\u5929\u6211\u4eec\u5c06\u5b66\u4e60\u5f02\u6b65<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb963\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u4eca\u5929\u662f\u5b66\u6821\u7f51\u7edc\u7f16\u7a0b\u7684\u7b2c\u4e09\u5929\uff0c\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86boost::asio\u540c\u6b65\u8bfb\u5199\u7684api\u51fd\u6570\uff0c\u73b0\u5728\u5c06\u524d\u9762\u7684api\u4e32\u8054\u8d77\u6765\uff0c\u505a\u4e00\u4e2a\u80fd\u8dd1\u8d77\u6765\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002\u4f46\u8bf7\u6ce8\u610f\uff01\u4e4b\u524d\u8bb2\u8fc7\u7684api\u51fd\u6570\u90e8\u5206\u4ee3\u7801\u6211\u4e0b\u9762\u5c06\u4e0d\u4f1a\u518d\u6b64\u8be6\u7ec6\u6ce8\u89e3\uff0c\u5982\u6709\u4e0d\u61c2\uff0c\u8bf7\u56de\u987e\u4e4b\u524d\u77e5\u8bc6\u70b9\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u91c7\u7528\u963b\u585e\u7684\u540c\u6b65\u8bfb\u5199\u65b9\u5f0f\u5b8c\u6210\u901a\u4fe1\u540c\u6b65\u8bfb\u5199\u7684\u4f18\u52a31 \u540c\u6b65\u8bfb\u5199\u7684\u7f3a\u9677\u5728\u4e8e\u8bfb\u5199\u662f\u963b\u585e\u7684\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u5bf9\u7aef\u4e0d\u53d1\u9001\u6570\u636e\u670d\u52a1\u5668\u7684read\u64cd\u4f5c\u662f\u963b\u585e\u7684\uff0c\u8fd9\u5c06\u5bfc\u81f4\u670d\u52a1\u5668\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u72b6\u6001\u30022\u53ef\u4ee5\u901a\u8fc7\u5f00\u8f9f\u65b0\u7684\u7ebf\u7a0b\u4e3a\u65b0\u751f\u6210\u7684\u8fde\u63a5\u5904\u7406\u8bfb\u5199\u3002_\u5982\u4f55\u4f7f\u7528asio\u5e93\u901a\u8fc7udp (c++)\u8fde\u7eed\u53d1\u9001\u5927\u91cf\u5b57\u8282<\/p>\n","protected":false},"author":2,"featured_media":27516,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2205,43,44],"topic":[],"class_list":["post-27517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-casio","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \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\/27517.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb963\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u4eca\u5929\u662f\u5b66\u6821\u7f51\u7edc\u7f16\u7a0b\u7684\u7b2c\u4e09\u5929\uff0c\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86boost::asio\u540c\u6b65\u8bfb\u5199\u7684api\u51fd\u6570\uff0c\u73b0\u5728\u5c06\u524d\u9762\u7684api\u4e32\u8054\u8d77\u6765\uff0c\u505a\u4e00\u4e2a\u80fd\u8dd1\u8d77\u6765\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002\u4f46\u8bf7\u6ce8\u610f\uff01\u4e4b\u524d\u8bb2\u8fc7\u7684api\u51fd\u6570\u90e8\u5206\u4ee3\u7801\u6211\u4e0b\u9762\u5c06\u4e0d\u4f1a\u518d\u6b64\u8be6\u7ec6\u6ce8\u89e3\uff0c\u5982\u6709\u4e0d\u61c2\uff0c\u8bf7\u56de\u987e\u4e4b\u524d\u77e5\u8bc6\u70b9\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u91c7\u7528\u963b\u585e\u7684\u540c\u6b65\u8bfb\u5199\u65b9\u5f0f\u5b8c\u6210\u901a\u4fe1\u540c\u6b65\u8bfb\u5199\u7684\u4f18\u52a31 \u540c\u6b65\u8bfb\u5199\u7684\u7f3a\u9677\u5728\u4e8e\u8bfb\u5199\u662f\u963b\u585e\u7684\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u5bf9\u7aef\u4e0d\u53d1\u9001\u6570\u636e\u670d\u52a1\u5668\u7684read\u64cd\u4f5c\u662f\u963b\u585e\u7684\uff0c\u8fd9\u5c06\u5bfc\u81f4\u670d\u52a1\u5668\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u72b6\u6001\u30022\u53ef\u4ee5\u901a\u8fc7\u5f00\u8f9f\u65b0\u7684\u7ebf\u7a0b\u4e3a\u65b0\u751f\u6210\u7684\u8fde\u63a5\u5904\u7406\u8bfb\u5199\u3002_\u5982\u4f55\u4f7f\u7528asio\u5e93\u901a\u8fc7udp (c++)\u8fde\u7eed\u53d1\u9001\u5927\u91cf\u5b57\u8282\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/27517.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-19T11:56:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419115614-68038f5e47aef.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\/27517.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/27517.html\",\"name\":\"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T11:56:16+00:00\",\"dateModified\":\"2025-04-19T11:56:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/27517.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/27517.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/27517.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b\"}]},{\"@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":"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \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\/27517.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb963\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u4eca\u5929\u662f\u5b66\u6821\u7f51\u7edc\u7f16\u7a0b\u7684\u7b2c\u4e09\u5929\uff0c\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86boost::asio\u540c\u6b65\u8bfb\u5199\u7684api\u51fd\u6570\uff0c\u73b0\u5728\u5c06\u524d\u9762\u7684api\u4e32\u8054\u8d77\u6765\uff0c\u505a\u4e00\u4e2a\u80fd\u8dd1\u8d77\u6765\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002\u4f46\u8bf7\u6ce8\u610f\uff01\u4e4b\u524d\u8bb2\u8fc7\u7684api\u51fd\u6570\u90e8\u5206\u4ee3\u7801\u6211\u4e0b\u9762\u5c06\u4e0d\u4f1a\u518d\u6b64\u8be6\u7ec6\u6ce8\u89e3\uff0c\u5982\u6709\u4e0d\u61c2\uff0c\u8bf7\u56de\u987e\u4e4b\u524d\u77e5\u8bc6\u70b9\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u91c7\u7528\u963b\u585e\u7684\u540c\u6b65\u8bfb\u5199\u65b9\u5f0f\u5b8c\u6210\u901a\u4fe1\u540c\u6b65\u8bfb\u5199\u7684\u4f18\u52a31 \u540c\u6b65\u8bfb\u5199\u7684\u7f3a\u9677\u5728\u4e8e\u8bfb\u5199\u662f\u963b\u585e\u7684\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u5bf9\u7aef\u4e0d\u53d1\u9001\u6570\u636e\u670d\u52a1\u5668\u7684read\u64cd\u4f5c\u662f\u963b\u585e\u7684\uff0c\u8fd9\u5c06\u5bfc\u81f4\u670d\u52a1\u5668\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u72b6\u6001\u30022\u53ef\u4ee5\u901a\u8fc7\u5f00\u8f9f\u65b0\u7684\u7ebf\u7a0b\u4e3a\u65b0\u751f\u6210\u7684\u8fde\u63a5\u5904\u7406\u8bfb\u5199\u3002_\u5982\u4f55\u4f7f\u7528asio\u5e93\u901a\u8fc7udp (c++)\u8fde\u7eed\u53d1\u9001\u5927\u91cf\u5b57\u8282","og_url":"https:\/\/www.wsisp.com\/helps\/27517.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T11:56:16+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419115614-68038f5e47aef.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\/27517.html","url":"https:\/\/www.wsisp.com\/helps\/27517.html","name":"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T11:56:16+00:00","dateModified":"2025-04-19T11:56:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/27517.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/27517.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/27517.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ asio\u7f51\u7edc\u7f16\u7a0b(3)\u540c\u6b65\u8bfb\u5199\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u793a\u4f8b"}]},{"@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\/27517","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=27517"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/27517\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/27516"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=27517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=27517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=27517"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=27517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}