{"id":1651,"date":"2018-09-13T14:02:41","date_gmt":"2018-09-13T06:02:41","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1651"},"modified":"2018-09-13T14:02:41","modified_gmt":"2018-09-13T06:02:41","slug":"%e5%9f%ba%e4%ba%8e-asio-%e7%9a%84-c-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1651","title":{"rendered":"\u57fa\u4e8e Asio \u7684 C++ \u7f51\u7edc\u7f16\u7a0b"},"content":{"rendered":"<div class=\"post-topheader custom- pt0\">\n<div class=\"mb20\">\n<div class=\"block-for-right-border\">\n<div class=\"row\">\n<div class=\"col-md-12article__author col-sm-12 col-xs-12\">\n<div class=\"post-topheader__info\" data-username=\"adam1q84\" data-userslug=\"adam1q84\" data-useravatar=\"https:\/\/avatar-static.segmentfault.com\/183\/944\/1839442818-569cb34c4505f_big64\">\n<div>\n<div class=\"article__author\">2016-10-20 \u53d1\u5e03<\/div>\n<\/div>\n<h1 id=\"articleTitle\" class=\"h1 post-topheader__info--title\" data-id=\"1190000007225464\"><a href=\"https:\/\/segmentfault.com\/a\/1190000007225464\">\u57fa\u4e8e Asio \u7684 C++ \u7f51\u7edc\u7f16\u7a0b<\/a><\/h1>\n<div class=\"content__tech hidden-xs\">\n<ul class=\"taglist--inline inline-block article__title--tag mr10\">\n<li class=\"tagPopup mb5\"><a class=\"tag\" href=\"https:\/\/segmentfault.com\/t\/network\/blogs\" data-toggle=\"popover\" data-img=\"\" data-placement=\"top\" data-original-title=\"network\" data-id=\"1040000000165699\">network<\/a><\/li>\n<li class=\"tagPopup mb5\"><a class=\"tag\" href=\"https:\/\/segmentfault.com\/t\/asio\/blogs\" data-toggle=\"popover\" data-img=\"\" data-placement=\"top\" data-original-title=\"asio\" data-id=\"1040000000713280\">asio<\/a><\/li>\n<li class=\"tagPopup mb5\"><a class=\"tag\" href=\"https:\/\/segmentfault.com\/t\/boost\/blogs\" data-toggle=\"popover\" data-img=\"\" data-placement=\"top\" data-original-title=\"boost\" data-id=\"1040000000240399\">boost<\/a><\/li>\n<li class=\"tagPopup mb5\"><a class=\"tag\" href=\"https:\/\/segmentfault.com\/t\/c%2B%2B\/blogs\" data-toggle=\"popover\" data-img=\"https:\/\/avatar-static.segmentfault.com\/368\/178\/3681786295-1040000000089741_big64\" data-placement=\"top\" data-original-title=\"c++\" data-id=\"1040000000089741\">c++<\/a><\/li>\n<\/ul>\n<p>11.4k \u6b21\u9605\u8bfb \u00a0\u00b7\u00a0 \u8bfb\u5b8c\u9700\u8981 50 \u5206\u949f<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"visible-lg\">\n<div class=\"side-widget\">\n<div id=\"side-widget-votes-btn\" class=\"stream__item-zan btn btn-default mt0 mb15 ml0 mr0 pt0 pb0 pl0 pr0 \"><span id=\"side-widget-votes-num\" class=\"stream__item-zan-number\">11<\/span><\/div>\n<p><i id=\"side-widget-bookmarks-btn\" class=\"fa fa-bookmark item \"><\/i><i class=\"fa fa-weibo item\"><\/i><i class=\"fa fa-weixin item\" title=\"\" data-toggle=\"popover\" data-placement=\"right\" data-original-title=\"\"><\/i><i class=\"fa fa-twitter item\"><\/i><i class=\"fa fa-facebook item\"><\/i><\/div>\n<\/div>\n<div class=\"article fmt article__content\" data-id=\"1190000007225464\" data-license=\"cc\">\n<p><strong>\u73af\u5883\uff1a<\/strong>\u00a0Boost v1.66, VS 2013 &amp; 2015<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><br \/>\n\u8fd9\u7bc7\u6559\u7a0b\u5f62\u6210\u4e8e Boost v1.62 \u65f6\u4ee3\uff0c\u6700\u8fd1\uff082018\/01\uff09\u9488\u5bf9 v1.66 \u505a\u4e86\u4e00\u6b21\u5927\u7684\u66f4\u65b0\u3002<br \/>\n\u6b64\u5916\uff0c\u5728\u4ee3\u7801\u98ce\u683c\u4e0a\uff0cC++11 \u7528\u5f97\u66f4\u591a\u4e86\u3002<\/p>\n<hr \/>\n<h2 id=\"articleHeader0\">\u6982\u8ff0<\/h2>\n<p>\u8fd1\u671f\u5b66\u4e60 Boost.Asio\uff0c\u4f9d\u846b\u82a6\u753b\u74e2\uff0c\u5199\u4e86\u4e0d\u5c11\u4f8b\u5b50\uff0c\u5bf9\u8fd9\u4e2a\u300c\u8f7b\u91cf\u7ea7\u300d\u7684\u7f51\u7edc\u5e93\u7b97\u662f\u6709\u4e86\u4e00\u5b9a\u7406\u89e3\u3002\u4f46\u662f\u79c9\u7740\u7406\u8bba\u4e0e\u5b9e\u8df5\u7ed3\u5408\u7684\u6001\u5ea6\uff0c\u51b3\u5b9a\u5199\u4e00\u7bc7\u6559\u7a0b\uff0c\u628a\u8111\u5b50\u91cc\u4e00\u77e5\u534a\u89e3\u7684\u4e1c\u897f\uff0c\u8bd5\u56fe\u8bf4\u6e05\u695a\u3002<\/p>\n<p>Asio\uff0c\u5373\u300c\u5f02\u6b65 IO\u300d\uff08Asynchronous Input\/Output\uff09\uff0c\u672c\u662f\u4e00\u4e2a\u00a0<a href=\"http:\/\/think-async.com\/Asio\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">\u72ec\u7acb\u7684 C++ \u7f51\u7edc\u7a0b\u5e8f\u5e93<\/a>\uff0c\u4f3c\u4e4e\u5e76\u4e0d\u4e3a\u4eba\u6240\u77e5\uff0c\u540e\u6765\u56e0\u4e3a\u88ab Boost \u76f8\u4e2d\uff0c\u624d\u58f0\u540d\u9e4a\u8d77\u3002<\/p>\n<p>\u4ece\u8bbe\u8ba1\u4e0a\u6765\u770b\uff0cAsio \u76f8\u4f3c\u4e14\u91cd\u5ea6\u4f9d\u8d56\u4e8e Boost\uff0c\u4e0e thread\u3001bind\u3001smart pointers \u7b49\u7ed3\u5408\u65f6\uff0c\u4f53\u9a8c\u987a\u6ed1\u3002\u4ece\u4f7f\u7528\u4e0a\u6765\u770b\uff0c\u4f9d\u7136\u662f\u91cd\u7ec4\u5408\u800c\u8f7b\u7ee7\u627f\uff0c\u4e00\u8d2f\u7684 C++ \u6807\u51c6\u5e93\u98ce\u683c\u3002<\/p>\n<p>\u4ec0\u4e48\u662f\u300c\u5f02\u6b65 IO\u300d\uff1f<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u4f60\u53d1\u8d77\u4e00\u4e2a IO \u64cd\u4f5c\uff0c\u5374\u4e0d\u7528\u7b49\u5b83\u7ed3\u675f\uff0c\u4f60\u53ef\u4ee5\u7ee7\u7eed\u505a\u5176\u4ed6\u4e8b\u60c5\uff0c\u5f53\u5b83\u7ed3\u675f\u65f6\uff0c\u4f60\u4f1a\u5f97\u5230\u901a\u77e5\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u79cd\u8868\u8ff0\u662f\u4e0d\u7cbe\u786e\u7684\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5e76\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u8fd9\u6837\u7684\u673a\u5236\u3002\u4ee5 Unix \u4e3a\u4f8b\uff0c\u6709\u4e94\u79cd IO \u6a21\u578b\u53ef\u7528\uff1a<\/p>\n<ul>\n<li>\u963b\u585e I\/O<\/li>\n<li>\u975e\u963b\u585e I\/O<\/li>\n<li>I\/O \u591a\u8def\u590d\u7528\uff08multiplexing\uff09\uff08<code>select<\/code>\u00a0\u548c\u00a0<code>poll<\/code>\uff09<\/li>\n<li>\u4fe1\u53f7\u9a71\u52a8 I\/O\uff08<code>SIGIO<\/code>\uff09<\/li>\n<li>\u5f02\u6b65 I\/O\uff08POSIX\u00a0<code>aio_<\/code>\u00a0\u7cfb\u5217\u51fd\u6570\uff09<\/li>\n<\/ul>\n<p>\u8fd9\u4e94\u79cd\u6a21\u578b\u7684\u5b9a\u4e49\u548c\u6bd4\u8f83\uff0c\u8be6\u89c1\u300cUnix Network Programming, Volume 1: The Sockets Networking API\u300d\u4e00\u4e66 6.2 \u8282\uff0c\u6216\u8005\u53ef\u53c2\u8003\u00a0<a href=\"https:\/\/segmentfault.com\/n\/1330000004444307\">\u8fd9\u7bc7\u7b14\u8bb0<\/a>\u3002<\/p>\n<p>Asio \u5c01\u88c5\u7684\u6b63\u662f\u300cI\/O \u591a\u8def\u590d\u7528\u300d\u3002\u5177\u4f53\u4e00\u70b9\uff0c<code>epoll<\/code>\u00a0\u4e4b\u4e8e Linux\uff0c<code>kqueue<\/code>\u00a0\u4e4b\u4e8e Mac \u548c BSD\u3002<code>epoll<\/code>\u00a0\u548c\u00a0<code>kqueue<\/code>\u00a0\u6bd4\u00a0<code>select<\/code>\u00a0\u548c\u00a0<code>poll<\/code>\u00a0\u66f4\u9ad8\u6548\u3002\u5f53\u7136\u5728 Windows \u4e0a\u5c01\u88c5\u7684\u5219\u662f IOCP\uff08\u5b8c\u6210\u7aef\u53e3\uff09\u3002<\/p>\n<p>Asio \u7684\u300cI\/O \u64cd\u4f5c\u300d\uff0c\u4e3b\u8981\u8fd8\u662f\u6307\u300c\u7f51\u7edc IO\u300d\uff0c\u6bd4\u5982 socket \u8bfb\u5199\u3002\u7531\u4e8e\u7f51\u7edc\u4f20\u8f93\u7684\u7279\u6027\uff0c\u300c\u7f51\u7edc IO\u300d\u76f8\u5bf9\u6bd4\u8f83\u8d39\u65f6\uff0c\u8bbe\u8ba1\u826f\u597d\u7684\u670d\u52a1\u5668\uff0c\u4e0d\u53ef\u80fd\u540c\u6b65\u7b49\u5f85\u4e00\u4e2a IO \u64cd\u4f5c\u7684\u7ed3\u675f\uff0c\u8fd9\u592a\u6d6a\u8d39 CPU \u4e86\u3002<\/p>\n<p>\u5bf9\u4e8e\u666e\u901a\u7684\u300c\u6587\u4ef6 IO\u300d\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5e76\u6ca1\u6709\u63d0\u4f9b\u201c\u5f02\u6b65\u201d\u8bfb\u5199\u673a\u5236\uff0clibuv \u7684\u505a\u6cd5\u662f\u7528\u7ebf\u7a0b\u6a21\u62df\u5f02\u6b65\uff0c\u4e3a\u7f51\u7edc\u548c\u6587\u4ef6\u63d0\u4f9b\u4e86\u4e00\u81f4\u7684\u63a5\u53e3\u3002Asio \u5e76\u6ca1\u6709\u8fd9\u6837\u505a\uff0c\u5b83\u4e13\u6ce8\u4e8e\u7f51\u7edc\u3002\u63d0\u4f9b\u673a\u5236\u800c\u4e0d\u662f\u7b56\u7565\uff0c\u8fd9\u5f88\u7b26\u5408 C++ \u54f2\u5b66\u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u793a\u4f8b\uff0c\u7531\u6d45\u5230\u6df1\uff0c\u7531\u7b80\u5355\u5230\u590d\u6742\uff0c\u9010\u4e00\u4ecb\u7ecd Asio \u7684\u7528\u6cd5\u3002<br \/>\n\u7b80\u5355\u8d77\u89c1\uff0c\u5934\u6587\u4ef6\u4e00\u5f8b\u7701\u7565\u3002<\/p>\n<h2 id=\"articleHeader1\">I\/O Context<\/h2>\n<p>\u6bcf\u4e2a Asio \u7a0b\u5e8f\u90fd\u81f3\u5c11\u6709\u4e00\u4e2a\u00a0<code>io_context<\/code>\u00a0\u5bf9\u8c61\uff0c\u5b83\u4ee3\u8868\u4e86\u64cd\u4f5c\u7cfb\u7edf\u7684 I\/O \u670d\u52a1\uff08<code>io_context<\/code>\u00a0\u5728 Boost 1.66 \u4e4b\u524d\u4e00\u76f4\u53eb\u00a0<code>io_service<\/code>\uff09\uff0c\u628a\u4f60\u7684\u7a0b\u5e8f\u548c\u8fd9\u4e9b\u670d\u52a1\u94fe\u63a5\u8d77\u6765\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u7a7a\u6709\u00a0<code>io_context<\/code>\u00a0\u5bf9\u8c61\uff0c\u5374\u6ca1\u6709\u4efb\u4f55\u5f02\u6b65\u64cd\u4f5c\uff0c\u6240\u4ee5\u5b83\u5176\u5b9e\u4ec0\u4e48\u4e5f\u6ca1\u505a\uff0c\u4e5f\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u3002<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n  boost::asio::io_context ioc;\r\n  ioc.run();\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<p><code>io_context.run<\/code>\u00a0\u662f\u4e00\u4e2a\u963b\u585e\uff08blocking\uff09\u8c03\u7528\uff0c\u59d1\u4e14\u628a\u5b83\u60f3\u8c61\u6210\u4e00\u4e2a loop\uff08\u4e8b\u4ef6\u5faa\u73af\uff09\uff0c\u76f4\u5230\u6240\u6709\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\uff0cloop \u624d\u7ed3\u675f\uff0c<code>run<\/code>\u00a0\u624d\u8fd4\u56de\u3002<\/p>\n<h2 id=\"articleHeader2\">Timer<\/h2>\n<p>\u6709\u4e86\u00a0<code>io_context<\/code>\u00a0\u8fd8\u4e0d\u8db3\u4ee5\u5b8c\u6210 I\/O \u64cd\u4f5c\uff0c\u7528\u6237\u4e00\u822c\u4e5f\u4e0d\u8ddf\u00a0<code>io_context<\/code>\u00a0\u76f4\u63a5\u4ea4\u4e92\u3002<\/p>\n<p>\u6839\u636e I\/O \u64cd\u4f5c\u7684\u4e0d\u540c\uff0cAsio \u63d0\u4f9b\u4e86\u4e0d\u540c\u7684 I\/O \u5bf9\u8c61\uff0c\u6bd4\u5982 timer\uff08\u5b9a\u65f6\u5668\uff09\uff0csocket\uff0c\u7b49\u7b49\u3002<br \/>\nTimer \u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd I\/O \u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u7684\u8d85\u65f6\u673a\u5236\uff0c\u4e0b\u9762\u662f\u6700\u7b80\u5355\u7684\u7528\u6cd5\uff1a<\/p>\n<pre class=\"hljs zephir\"><code class=\"cpp\">void <span class=\"hljs-keyword\">Print<\/span>(boost::system::error_code ec) {\r\n  std::cout &lt;&lt; <span class=\"hljs-string\">\"Hello, world!\"<\/span> &lt;&lt; std::endl;\r\n}\r\n\r\n<span class=\"hljs-keyword\">int<\/span> main() {\r\n  boost::asio::io_context ioc;\r\n  boost::asio::deadline_timer timer(ioc, boost::posix_time::seconds(<span class=\"hljs-number\">3<\/span>));\r\n\r\n  timer.async_wait(&amp;<span class=\"hljs-keyword\">Print<\/span>);\r\n\r\n  ioc.run();\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<p>\u5148\u521b\u5efa\u4e00\u4e2a\u00a0<code>deadline_timer<\/code>\uff0c\u6307\u5b9a\u65f6\u95f4 3 \u79d2\uff0c\u7136\u540e\u5f02\u6b65\u7b49\u5f85\u8fd9\u4e2a timer\uff0c3 \u79d2\u540e\uff0ctimer \u8d85\u65f6\u7ed3\u675f\uff0c<code>Print<\/code>\u00a0\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u6240\u6709 I\/O \u5bf9\u8c61\u90fd\u4f9d\u8d56\u00a0<code>io_context<\/code>\uff0c\u4e00\u822c\u5728\u6784\u9020\u65f6\u6307\u5b9a\u3002<\/li>\n<li><code>async_wait<\/code>\u00a0\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f46\u662f\u8fd9\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6267\u884c\uff0c\u8981\u7b49\u5230\u00a0<code>io_context.run<\/code>\u00a0\u65f6\u624d\u5f00\u59cb\u3002<\/li>\n<li>Timer \u9664\u4e86\u5f02\u6b65\u7b49\u5f85\uff08<code>async_wait<\/code>\uff09\uff0c\u8fd8\u53ef\u4ee5\u540c\u6b65\u7b49\u5f85\uff08<code>wait<\/code>\uff09\u3002\u540c\u6b65\u7b49\u5f85\u662f\u963b\u585e\u7684\uff0c\u76f4\u5230 timer \u8d85\u65f6\u7ed3\u675f\u3002\u57fa\u672c\u4e0a\u6240\u6709 I\/O \u5bf9\u8c61\u7684\u64cd\u4f5c\u90fd\u6709\u540c\u6b65\u548c\u5f02\u6b65\u4e24\u4e2a\u7248\u672c\uff0c\u4e5f\u8bb8\u662f\u51fa\u4e8e\u8bbe\u8ba1\u4e0a\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<li><code>async_wait<\/code>\u00a0\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u65f6\u5b83\u4f1a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5\u4e5f\u53eb Completion Handler\uff0c\u7b80\u79f0 handler\uff0c\u53ef\u4ee5\u7406\u89e3\u6210\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<li>\u6240\u6709 I\/O \u5bf9\u8c61\u7684\u00a0<code>async_xyz<\/code>\u00a0\u51fd\u6570\u90fd\u6709 handler \u53c2\u6570\uff0c\u5bf9\u4e8e handler \u7684\u7b7e\u540d\uff0c\u4e0d\u540c\u7684\u5f02\u6b65\u64cd\u4f5c\u6709\u4e0d\u540c\u7684\u8981\u6c42\uff0c\u9664\u4e86\u5b98\u65b9\u6587\u6863\u91cc\u7684\u8bf4\u660e\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u67e5\u770b Boost \u6e90\u7801\u3002<\/li>\n<\/ul>\n<p><code>async_wait<\/code>\u00a0\u7684 handler \u7b7e\u540d\u4e3a\u00a0<code>void (boost::system::error_code)<\/code>\uff0c\u5982\u679c\u8981\u4f20\u9012\u989d\u5916\u7684\u53c2\u6570\uff0c\u5c31\u5f97\u7528\u00a0<code>bind<\/code>\u3002<br \/>\n\u4e0d\u59a8\u4fee\u6539\u4e00\u4e0b\u00a0<code>Print<\/code>\uff0c\u8ba9\u5b83\u6bcf\u9694\u4e00\u79d2\u6253\u5370\u4e00\u6b21\u8ba1\u6570\uff0c\u4ece\u00a0<code>0<\/code>\u00a0\u9012\u589e\u5230\u00a0<code>3<\/code>\u3002<\/p>\n<pre class=\"hljs zephir\"><code class=\"cpp\">void <span class=\"hljs-keyword\">Print<\/span>(boost::system::error_code ec,\r\n           boost::asio::deadline_timer* timer,\r\n           <span class=\"hljs-keyword\">int<\/span>* count) {\r\n  <span class=\"hljs-keyword\">if<\/span> (*count &lt; <span class=\"hljs-number\">3<\/span>) {\r\n    std::cout &lt;&lt; *count &lt;&lt; std::endl;\r\n    ++(*count);\r\n\r\n    timer-&gt;expires_at(timer-&gt;expires_at() + boost::posix_time::seconds(<span class=\"hljs-number\">1<\/span>));\r\n    \r\n    timer-&gt;async_wait(boost::bind(<span class=\"hljs-keyword\">Print<\/span>, boost::placeholders::_1, timer, count));\r\n  }\r\n}<\/code><\/pre>\n<p>\u4e0e\u524d\u7248\u76f8\u6bd4\uff0c<code>Print<\/code>\u00a0\u591a\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u4ee5\u4fbf\u8bbf\u95ee\u5f53\u524d\u8ba1\u6570\u53ca\u91cd\u542f timer\u3002<\/p>\n<pre class=\"hljs zephir\"><code class=\"cpp\"><span class=\"hljs-keyword\">int<\/span> main() {\r\n  boost::asio::io_context ioc;\r\n  boost::asio::deadline_timer timer(ioc, boost::posix_time::seconds(<span class=\"hljs-number\">1<\/span>));\r\n  <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\r\n  timer.async_wait(boost::bind(<span class=\"hljs-keyword\">Print<\/span>, boost::placeholders::_1, &amp;timer, &amp;count));\r\n\r\n  ioc.run();\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<p>\u8c03\u7528\u00a0<code>bind<\/code>\u00a0\u65f6\uff0c\u4f7f\u7528\u4e86\u5360\u4f4d\u7b26\uff08placeholder\uff09<code>boost::placeholders::_1<\/code>\u3002\u6570\u5b57\u5360\u4f4d\u7b26\u5171\u6709 9 \u4e2a\uff0c<code>_1<\/code>\u00a0&#8211;\u00a0<code>_9<\/code>\u3002\u5360\u4f4d\u7b26\u4e5f\u6709\u5f88\u591a\u79cd\u5199\u6cd5\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8be6\u8ff0\u4e86\u3002<\/p>\n<h2 id=\"articleHeader3\">Echo Server<\/h2>\n<p>Socket \u4e5f\u662f\u4e00\u79cd I\/O \u5bf9\u8c61\uff0c\u8fd9\u4e00\u70b9\u524d\u9762\u5df2\u7ecf\u63d0\u53ca\u3002\u76f8\u6bd4\u4e8e timer\uff0csocket \u66f4\u4e3a\u5e38\u7528\uff0c\u6bd5\u7adf Asio \u662f\u4e00\u4e2a\u7f51\u7edc\u7a0b\u5e8f\u5e93\u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u7ecf\u5178\u7684 Echo \u7a0b\u5e8f\u4e3a\u4f8b\uff0c\u5b9e\u73b0\u4e00\u4e2a TCP Server\u3002\u6240\u8c13 Echo\uff0c\u5c31\u662f Server \u628a Client \u53d1\u6765\u7684\u5185\u5bb9\u539f\u5c01\u4e0d\u52a8\u53d1\u56de\u7ed9 Client\u3002<\/p>\n<p>\u5148\u4ece\u540c\u6b65\u65b9\u5f0f\u5f00\u59cb\uff0c\u5f02\u6b65\u592a\u590d\u6742\uff0c\u6162\u6162\u6765\u3002<\/p>\n<h3 id=\"articleHeader4\">\u540c\u6b65\u65b9\u5f0f<\/h3>\n<pre class=\"hljs zephir\"><code class=\"cpp\"><span class=\"hljs-comment\">\/\/ echo_server_sync.cpp<\/span>\r\nusing boost::asio::ip::tcp;\r\n\r\nenum {\r\n  BUF_SIZE = <span class=\"hljs-number\">1024<\/span>\r\n};\r\n\r\nvoid Session(tcp::socket socket) {\r\n  <span class=\"hljs-keyword\">try<\/span> {\r\n    <span class=\"hljs-keyword\">while<\/span> (<span class=\"hljs-keyword\">true<\/span>) {\r\n      boost::array&lt;<span class=\"hljs-keyword\">char<\/span>, BUF_SIZE&gt; data;\r\n\r\n      boost::system::error_code ec;\r\n      size_t length = socket.read_some(boost::asio::buffer(data), ec);\r\n\r\n      <span class=\"hljs-keyword\">if<\/span> (ec == boost::asio::error::eof) {\r\n        std::cout &lt;&lt; <span class=\"hljs-string\">\"Connection closed cleanly by peer\\n\"<\/span>;\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n      } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (ec) {\r\n        <span class=\"hljs-comment\">\/\/ Some other error.<\/span>\r\n        <span class=\"hljs-keyword\">throw<\/span> boost::system::system_error(ec);\r\n      }\r\n\r\n      boost::asio::write(socket, boost::asio::buffer(data, length));\r\n    }\r\n  } <span class=\"hljs-keyword\">catch<\/span> (std::exception&amp; e) {\r\n    std::cerr &lt;&lt; <span class=\"hljs-string\">\"Exception: \"<\/span> &lt;&lt;  e.what() &lt;&lt; std::endl;\r\n  }\r\n}\r\n\r\n<span class=\"hljs-keyword\">int<\/span> main(<span class=\"hljs-keyword\">int<\/span> argc, <span class=\"hljs-keyword\">char<\/span>* argv[]) {\r\n  <span class=\"hljs-keyword\">if<\/span> (argc != <span class=\"hljs-number\">2<\/span>) {\r\n    std::cerr &lt;&lt; <span class=\"hljs-string\">\"Usage: \"<\/span> &lt;&lt; argv[<span class=\"hljs-number\">0<\/span>] &lt;&lt; <span class=\"hljs-string\">\" &lt;port&gt;\"<\/span> &lt;&lt; std::endl;\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\r\n  }\r\n\r\n  unsigned short port = std::atoi(argv[<span class=\"hljs-number\">1<\/span>]);\r\n\r\n  boost::asio::io_context ioc;\r\n  tcp::acceptor acceptor(ioc, tcp::endpoint(tcp::v4(), port));\r\n\r\n  <span class=\"hljs-keyword\">try<\/span> {\r\n    <span class=\"hljs-keyword\">while<\/span> (<span class=\"hljs-keyword\">true<\/span>) {\r\n      Session(acceptor.accept());\r\n    }\r\n  } <span class=\"hljs-keyword\">catch<\/span> (std::exception&amp; e) {\r\n    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;\r\n  }\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<p>\u542f\u52a8\u65f6\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6307\u5b9a\u7aef\u53e3\u53f7\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"hljs elixir\"><code><span class=\"hljs-variable\">$\u00a0<\/span>echo_server_sync\u00a0<span class=\"hljs-number\">8080<\/span><\/code><\/pre>\n<p>\u5148\u7528\u00a0<code>netcat<\/code>\u00a0\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n<pre class=\"hljs elixir\"><code><span class=\"hljs-variable\">$ <\/span>nc localhost <span class=\"hljs-number\">8080<\/span>\r\nhello\r\nhello<\/code><\/pre>\n<p>\u4ee5\u4e0b\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li><code>tcp::acceptor<\/code>\u00a0\u4e5f\u662f\u4e00\u79cd I\/O \u5bf9\u8c61\uff0c\u7528\u6765\u63a5\u6536 TCP \u8fde\u63a5\uff0c\u8fde\u63a5\u7aef\u53e3\u7531\u00a0<code>tcp::endpoint<\/code>\u00a0\u6307\u5b9a\u3002<\/li>\n<li>\u6570\u636e buffer \u4ee5\u00a0<code>boost::array&lt;char, BUF_SIZE&gt;<\/code>\u00a0\u8868\u793a\uff0c\u4e5f\u53ef\u4ee5\u7528\u00a0<code>char data[BUF_SIZE]<\/code>\uff0c\u6216\u00a0<code>std::vector&lt;char&gt; data(BUF_SIZE)<\/code>\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7528\u00a0<code>std::vector<\/code>\u00a0\u662f\u6700\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u652f\u6301\u00a0<a href=\"http:\/\/blog.think-async.com\/2006\/11\/buffer-debugging.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Buffer Debugging<\/a>\u3002<\/li>\n<li>\u540c\u6b65\u65b9\u5f0f\u4e0b\uff0c\u6ca1\u6709\u8c03\u7528\u00a0<code>io_context.run<\/code>\uff0c\u56e0\u4e3a\u00a0<code>accept<\/code>\u3001<code>read_some<\/code>\u00a0\u548c\u00a0<code>write<\/code>\u00a0\u90fd\u662f\u963b\u585e\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u4e00\u4e2a Client \u8fde\u63a5\uff0c\u4f46\u662f\u53ef\u4ee5\u8fde\u7eed echo\uff0c\u9664\u975e Client \u65ad\u5f00\u8fde\u63a5\u3002<\/li>\n<li>\u5199\u56de\u6570\u636e\u65f6\uff0c\u6ca1\u6709\u76f4\u63a5\u8c03\u7528\u00a0<code>socket.write_some<\/code>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u4fdd\u8bc1\u4e00\u6b21\u5199\u5b8c\u6240\u6709\u6570\u636e\uff0c\u4f46\u662f\u00a0<code>boost::asio::write<\/code>\u00a0\u53ef\u4ee5\u3002\u6211\u89c9\u5f97\u8fd9\u662f Asio \u63a5\u53e3\u8bbe\u8ba1\u4e0d\u5468\uff0c\u5e94\u8be5\u63d0\u4f9b\u00a0<code>socket.write<\/code>\u3002<\/li>\n<li><code>Session<\/code>\u00a0\u7684 socket \u53c2\u6570\u7adf\u7136\u662f\u4f20\u503c\uff1f<code>acceptor.accept()<\/code>\u00a0\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 socket \u5bf9\u8c61\uff0c\u5c31\u76f4\u63a5\u4e22\u7ed9\u4e86\u00a0<code>Session<\/code>\uff0c\u8fde\u5c40\u90e8\u53d8\u91cf\u90fd\u4e0d\u9700\u8981\u3002\u5176\u5b9e\u4e5f\u53ef\u4ee5\u5199\u5f97\u9ebb\u70e6\u4e00\u4e9b\uff1a<\/li>\n<\/ul>\n<p><code>Session<\/code>\u00a0\u4f20\u5f15\u7528\uff1a<\/p>\n<pre class=\"hljs lasso\"><code class=\"cpp\"><span class=\"hljs-literal\">void<\/span> Session(tcp<span class=\"hljs-type\">::socket<\/span>&amp; socket) {\r\n  <span class=\"hljs-params\">...<\/span><\/code><\/pre>\n<p>\u8c03\u7528\u00a0<code>Session<\/code>\u00a0\u65f6\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\uff1a<\/p>\n<pre class=\"hljs perl\"><code class=\"cpp\">      tcp::<span class=\"hljs-keyword\">socket<\/span> <span class=\"hljs-keyword\">socket<\/span>(ioc);\r\n      acceptor.accept(<span class=\"hljs-keyword\">socket<\/span>);\r\n      Session(<span class=\"hljs-keyword\">socket<\/span>);<\/code><\/pre>\n<p>\u6216<\/p>\n<pre class=\"hljs perl\"><code class=\"cpp\">      tcp::<span class=\"hljs-keyword\">socket<\/span> <span class=\"hljs-keyword\">socket<\/span> = acceptor.accept();\r\n      Session(<span class=\"hljs-keyword\">socket<\/span>);<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u4f20\u503c\u81ea\u6709\u5999\u5904\uff0c\u8fd9\u91cc\u9762\u6709 RVO\uff08Return Value Optimization\uff09\u8fd9\u79cd\u975e\u5e38\u6280\u5de7\u6027\uff08tricky\uff09\u7684\u4e1c\u897f\uff01<\/p>\n<h3 id=\"articleHeader5\">\u5f02\u6b65\u65b9\u5f0f<\/h3>\n<p>\u5f02\u6b65\u65b9\u5f0f\u4e0b\uff0c\u56f0\u96be\u5728\u4e8e\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u53ef\u4ee5\u7528\u00a0<code>shared_ptr<\/code>\u00a0\u89e3\u51b3\u3002<\/p>\n<p>\u4e3a\u4e86\u540c\u65f6\u5904\u7406\u591a\u4e2a Client \u8fde\u63a5\uff0c\u9700\u8981\u4fdd\u7559\u6bcf\u4e2a\u8fde\u63a5\u7684 socket \u5bf9\u8c61\uff0c\u4e8e\u662f\u62bd\u8c61\u51fa\u4e00\u4e2a\u8868\u793a\u8fde\u63a5\u7684\u7c7b\uff0c\u53eb\u00a0<code>Session<\/code>\uff1a<\/p>\n<pre class=\"hljs rust\"><code class=\"cpp\">class Session : public std::enable_shared_from_this&lt;Session&gt; {\r\npublic:\r\n  Session(tcp::socket socket)\r\n      : socket_(std::<span class=\"hljs-keyword\">move<\/span>(socket)) {\r\n  }\r\n\r\n  void Start() {\r\n    DoRead();\r\n  }\r\n\r\n  void DoRead() {\r\n    auto <span class=\"hljs-keyword\">self<\/span>(shared_from_this());\r\n    socket_.async_read_some(\r\n        boost::asio::buffer(buffer_),\r\n        [this, <span class=\"hljs-keyword\">self<\/span>](boost::system::error_code ec, std::size_t length) {\r\n          <span class=\"hljs-keyword\">if<\/span> (!ec) {\r\n            DoWrite(length);\r\n          }\r\n        });\r\n  }\r\n\r\n  void DoWrite(std::size_t length) {\r\n    auto <span class=\"hljs-keyword\">self<\/span>(shared_from_this());\r\n    boost::asio::async_write(\r\n        socket_,\r\n        boost::asio::buffer(buffer_, length),\r\n        [this, <span class=\"hljs-keyword\">self<\/span>](boost::system::error_code ec, std::size_t <span class=\"hljs-comment\">\/*length*\/<\/span>) {\r\n          <span class=\"hljs-keyword\">if<\/span> (!ec) {\r\n            DoRead();\r\n          }\r\n        });\r\n  }\r\n\r\nprivate:\r\n  tcp::socket socket_;\r\n  std::array&lt;<span class=\"hljs-built_in\">char<\/span>, BUF_SIZE&gt; buffer_;\r\n};<\/code><\/pre>\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u5728\u4ee3\u7801\u98ce\u683c\u4e0a\uff0c\u6211\u505a\u4e86\u4e00\u4e9b\u6539\u8fdb\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4f18\u5148\u4f7f\u7528 STL\uff0c\u6bd4\u5982\u00a0<code>std::enable_shared_from_this<\/code>\uff0c<code>std::bind<\/code>\uff0c<code>std::array<\/code>\uff0c\u7b49\u7b49\u3002<\/li>\n<li>\u5b9a\u4e49 handler \u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff08lambda \u8868\u8fbe\u5f0f\uff09\u3002<\/li>\n<li>\u4ee5\u00a0<code>std::size_t<\/code>\u00a0(C++) \u4ee3\u66ff\u00a0<code>size_t<\/code>\u00a0(C)\u3002<\/li>\n<\/ul>\n<p>\u521a\u5f00\u59cb\uff0c\u4f60\u53ef\u80fd\u4f1a\u4e0d\u4e60\u60ef\uff0c\u6211\u4e5f\u662f\u8fd9\u6837\uff0c\u8fc7\u4e86\u597d\u4e45\u624d\u6162\u6162\u62e5\u62b1 C++11 \u4e43\u81f3 C++14\u3002<\/p>\n<p><code>Session<\/code>\u00a0\u6709\u4e24\u4e2a\u6210\u5458\u53d8\u91cf\uff0c<code>socket_<\/code>\u00a0\u4e0e client \u901a\u4fe1\uff0c<code>buffer_<\/code>\u00a0\u662f\u63a5\u6536 client \u6570\u636e\u7684\u7f13\u5b58\u3002\u53ea\u8981\u00a0<code>Session<\/code>\u00a0\u5bf9\u8c61\u5728\uff0csocket \u5c31\u5728\uff0c\u8fde\u63a5\u5c31\u4e0d\u65ad\u3002<br \/>\nSocket \u5bf9\u8c61\u662f\u6784\u9020\u65f6\u4f20\u8fdb\u6765\u7684\uff0c\u800c\u4e14\u662f\u00a0<strong>move<\/strong>\u00a0\u8fdb\u6765\u7684\uff08<code>std::move<\/code>\uff09\u3002Move \u8bed\u4e49\u662f C++11 \u7684\u6982\u5ff5\uff0c\u5c31\u4e0d\u8d58\u8ff0\u4e86\u3002<\/p>\n<p>\u867d\u7136\u8fd8\u6ca1\u770b\u5230\u00a0<code>Session<\/code>\u00a0\u5bf9\u8c61\u662f\u5982\u4f55\u521b\u5efa\u7684\uff0c\u4f46\u53ef\u4ee5\u80af\u5b9a\u7684\u662f\uff0c\u5b83\u5fc5\u987b\u7528\u00a0<code>std::shared_ptr<\/code>\u00a0\u8fdb\u884c\u5c01\u88c5\uff0c\u8fd9\u6837\u624d\u80fd\u4fdd\u8bc1\u5f02\u6b65\u6a21\u5f0f\u4e0b\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5728\u00a0<code>Session::DoRead<\/code>\u00a0\u548c\u00a0<code>Session::DoWrite<\/code>\u00a0\u4e2d\uff0c\u56e0\u4e3a\u8bfb\u5199\u90fd\u662f\u5f02\u6b65\u7684\uff0c\u540c\u6837\u4e3a\u4e86\u9632\u6b62\u5f53\u524d\u00a0<code>Session<\/code>\u00a0\u4e0d\u88ab\u9500\u6bc1\uff08\u56e0\u4e3a\u8d85\u51fa\u4f5c\u7528\u57df\uff09\uff0c\u4fbf\u589e\u52a0\u5b83\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5373\u00a0<code>auto self(shared_from_this());<\/code>\u00a0\u8fd9\u4e00\u53e5\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u81f3\u4e8e\u8bfb\u5199\u7684\u903b\u8f91\uff0c\u57fa\u672c\u4e0a\u5c31\u662f\u628a\u00a0<code>read_some<\/code>\u00a0\u6362\u6210\u00a0<code>async_read_some<\/code>\uff0c\u628a\u00a0<code>write<\/code>\u00a0\u6362\u6210\u00a0<code>async_write<\/code>\uff0c\u7136\u540e\u4ee5\u533f\u540d\u51fd\u6570\u4f5c\u4e3a Completion Handler\u3002<\/p>\n<p>\u63a5\u6536 Client \u8fde\u63a5\u7684\u4ee3\u7801\uff0c\u63d0\u53d6\u51fa\u6765\uff0c\u62bd\u8c61\u6210\u4e00\u4e2a\u7c7b\u00a0<code>Server<\/code>\uff1a<\/p>\n<pre class=\"hljs php\"><code class=\"cpp\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Server<\/span> <\/span>{\r\n<span class=\"hljs-keyword\">public<\/span>:\r\n  Server(boost::asio::io_context&amp; ioc, short port)\r\n      : acceptor_(ioc, tcp::endpoint(tcp::v4(), port)) {\r\n    DoAccept();\r\n  }\r\n\r\n<span class=\"hljs-keyword\">private<\/span>:\r\n  void DoAccept() {\r\n    acceptor_.async_accept(\r\n        [this](boost::system::error_code ec, tcp::socket socket) {\r\n          <span class=\"hljs-keyword\">if<\/span> (!ec) {\r\n            std::make_shared&lt;Session&gt;(std::move(socket))-&gt;Start();\r\n          }\r\n          DoAccept();\r\n        });\r\n  }\r\n\r\n<span class=\"hljs-keyword\">private<\/span>:\r\n  tcp::acceptor acceptor_;\r\n};<\/code><\/pre>\n<p>\u540c\u6837\uff0c<code>async_accept<\/code>\u00a0\u66ff\u6362\u4e86\u00a0<code>accept<\/code>\u3002<code>async_accept<\/code>\u00a0\u4e0d\u518d\u963b\u585e\uff0c<code>DoAccept<\/code>\u00a0\u5373\u523b\u5c31\u4f1a\u8fd4\u56de\u3002<br \/>\n\u4e3a\u4e86\u4fdd\u8bc1\u00a0<code>Session<\/code>\u00a0\u5bf9\u8c61\u7ee7\u7eed\u5b58\u5728\uff0c\u4f7f\u7528\u00a0<code>std::shared_ptr<\/code>\u00a0\u4ee3\u66ff\u666e\u901a\u7684\u6808\u5bf9\u8c61\uff0c\u540c\u65f6\u628a\u65b0\u63a5\u6536\u7684 socket \u5bf9\u8c61 move \u8fdb\u53bb\u3002<\/p>\n<p>\u6700\u540e\u662f\u00a0<code>main()<\/code>\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span> argc, <span class=\"hljs-keyword\">char<\/span>* argv[])<\/span> <\/span>{\r\n  <span class=\"hljs-keyword\">if<\/span> (argc != <span class=\"hljs-number\">2<\/span>) {\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cerr<\/span> &lt;&lt; <span class=\"hljs-string\">\"Usage: \"<\/span> &lt;&lt; argv[<span class=\"hljs-number\">0<\/span>] &lt;&lt; <span class=\"hljs-string\">\" &lt;port&gt;\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\r\n  }\r\n\r\n  <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">short<\/span> port = <span class=\"hljs-built_in\">std<\/span>::atoi(argv[<span class=\"hljs-number\">1<\/span>]);\r\n\r\n  boost::asio::io_context ioc;\r\n  <span class=\"hljs-function\">Server <span class=\"hljs-title\">server<\/span><span class=\"hljs-params\">(ioc, port)<\/span><\/span>;\r\n\r\n  ioc.run();\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<h2 id=\"articleHeader6\">Echo Client<\/h2>\n<p>\u867d\u7136\u7528\u00a0<code>netcat<\/code>\u00a0\u6d4b\u8bd5 Echo Server \u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u662f\u81ea\u5df1\u52a8\u624b\u5199\u4e00\u4e2a Echo Client \u4ecd\u7136\u5341\u5206\u5fc5\u8981\u3002<br \/>\n\u8fd8\u662f\u5148\u8003\u8651\u540c\u6b65\u65b9\u5f0f\u3002<\/p>\n<h3 id=\"articleHeader7\">\u540c\u6b65\u65b9\u5f0f<\/h3>\n<p>\u9996\u5148\u901a\u8fc7\u00a0<code>host<\/code>\u00a0\u548c\u00a0<code>port<\/code>\u00a0\u89e3\u6790\u51fa endpoints\uff08\u5bf9\uff0c\u662f\u590d\u6570\uff01\uff09\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">tcp::<span class=\"hljs-function\">resolver <span class=\"hljs-title\">resolver<\/span><span class=\"hljs-params\">(ioc)<\/span><\/span>;\r\ntcp::resolver::results_type endpoints = resolver.resolve(tcp::v4(), host, port);<\/code><\/pre>\n<p><code>host<\/code>\u00a0\u548c\u00a0<code>port<\/code>\u00a0\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6307\u5b9a\uff0c\u6bd4\u5982\u00a0<code>localhost<\/code>\u00a0\u548c\u00a0<code>8080<\/code>\u3002<\/p>\n<p>\u63a5\u7740\u521b\u5efa socket\uff0c\u5efa\u7acb\u8fde\u63a5\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">tcp::<span class=\"hljs-function\">socket <span class=\"hljs-title\">socket<\/span><span class=\"hljs-params\">(ioc)<\/span><\/span>;\r\nboost::asio::connect(socket, endpoints);<\/code><\/pre>\n<p>\u8fd9\u91cc\u6ca1\u6709\u76f4\u63a5\u8c03\u7528\u00a0<code>socket.connect<\/code>\uff0c\u56e0\u4e3a\u00a0<code>endpoints<\/code>\u00a0\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\uff0c<code>boost::asio::connect<\/code>\u00a0\u4f1a\u6328\u4e2a\u5c1d\u8bd5\uff0c\u9010\u4e00\u8c03\u7528\u00a0<code>socket.connect<\/code>\u00a0\u76f4\u5230\u6210\u529f\u8fde\u63a5\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u6837\u8bf4\u4e0d\u592a\u4e25\u8c28\uff0c\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c<code>resolve<\/code>\u00a0\u5728\u6ca1\u6709\u6307\u5b9a protocol \u65f6\uff0c\u786e\u5b9e\u4f1a\u8fd4\u56de\u591a\u4e2a endpoints\uff0c\u4e00\u4e2a\u662f IPv6\uff0c\u4e00\u4e2a\u662f IPv4\u3002\u4f46\u662f\u6211\u4eec\u5df2\u7ecf\u6307\u5b9a\u4e86 protocol \u4e3a\u00a0<code>tcp::v4()<\/code>\uff1a<\/p>\n<pre class=\"hljs css\"><code class=\"cpp\"><span class=\"hljs-selector-tag\">resolver<\/span><span class=\"hljs-selector-class\">.resolve<\/span>(<span class=\"hljs-selector-tag\">tcp<\/span><span class=\"hljs-selector-pseudo\">::v4()<\/span>,\u00a0<span class=\"hljs-selector-tag\">host<\/span>,\u00a0<span class=\"hljs-selector-tag\">port<\/span>)<\/code><\/pre>\n<p>\u6240\u4ee5\uff0cendpoint \u5e94\u8be5\u53ea\u6709\u4e00\u4e2a\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4ece\u6807\u51c6\u8f93\u5165\uff08std::cin\uff09\u8bfb\u4e00\u884c\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u53d1\u9001\u7ed9 server\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">    <span class=\"hljs-keyword\">char<\/span> request[BUF_SIZE];\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> request_length = <span class=\"hljs-number\">0<\/span>;\r\n    <span class=\"hljs-keyword\">do<\/span> {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Enter message: \"<\/span>;\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cin<\/span>.getline(request, BUF_SIZE);\r\n      request_length = <span class=\"hljs-built_in\">strlen<\/span>(request);\r\n    } <span class=\"hljs-keyword\">while<\/span> (request_length == <span class=\"hljs-number\">0<\/span>);\r\n\r\n    boost::asio::write(socket, boost::asio::buffer(request, request_length));<\/code><\/pre>\n<p><code>do...while<\/code>\u00a0\u662f\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u76f4\u63a5 Enter \u5bfc\u81f4\u8f93\u5165\u4e3a\u7a7a\u3002<br \/>\n<code>boost::asio::write<\/code>\u00a0\u662f\u963b\u585e\u8c03\u7528\uff0c\u53d1\u9001\u5b8c\u624d\u8fd4\u56de\u3002<\/p>\n<p>\u4ece Server \u540c\u6b65\u63a5\u6536\u6570\u636e\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u00a0<code>boost::asio::read<\/code>\uff08\u5bf9\u5e94\u4e8e\u00a0<code>boost::asio::write<\/code>\uff09\uff1b<\/li>\n<li>\u4f7f\u7528\u00a0<code>socket.read_some<\/code>\u3002<\/li>\n<\/ul>\n<p>\u4e24\u8005\u7684\u5dee\u522b\u662f\uff0c<code>boost::asio::read<\/code>\u00a0\u8bfb\u5230\u6307\u5b9a\u957f\u5ea6\u65f6\uff0c\u5c31\u4f1a\u8fd4\u56de\uff0c\u4f60\u9700\u8981\u77e5\u9053\u4f60\u60f3\u8bfb\u591a\u5c11\uff1b\u800c\u00a0<code>socket.read_some<\/code>\u00a0\u4e00\u65e6\u8bfb\u5230\u4e00\u4e9b\u6570\u636e\u5c31\u4f1a\u8fd4\u56de\uff0c\u6240\u4ee5\u5fc5\u987b\u653e\u5728\u5faa\u73af\u91cc\uff0c\u7136\u540e\u624b\u52a8\u5224\u65ad\u662f\u5426\u5df2\u7ecf\u8bfb\u5230\u60f3\u8981\u7684\u957f\u5ea6\uff0c\u5426\u5219\u65e0\u6cd5\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e24\u79cd\u5b9e\u73b0\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4f7f\u7528\u00a0<code>boost::asio::read<\/code>\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">    <span class=\"hljs-keyword\">char<\/span> reply[BUF_SIZE];\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> reply_length = boost::asio::read(\r\n        socket,\r\n        boost::asio::buffer(reply, request_length));\r\n\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span>.write(reply, reply_length);<\/code><\/pre>\n<p>\u4f7f\u7528\u00a0<code>socket.read_some<\/code>\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> total_reply_length = <span class=\"hljs-number\">0<\/span>;\r\n    <span class=\"hljs-keyword\">while<\/span> (<span class=\"hljs-literal\">true<\/span>) {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">array<\/span>&lt;<span class=\"hljs-keyword\">char<\/span>, BUF_SIZE&gt; reply;\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> reply_length = socket.read_some(boost::asio::buffer(reply));\r\n\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span>.write(reply.data(), reply_length);\r\n\r\n      total_reply_length += reply_length;\r\n      <span class=\"hljs-keyword\">if<\/span> (total_reply_length &gt;= request_length) {\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n      }\r\n    }<\/code><\/pre>\n<p>\u4e0d\u96be\u770b\u51fa\uff0c<code>socket.read_some<\/code>\u00a0\u7528\u8d77\u6765\u66f4\u4e3a\u590d\u6742\u3002<br \/>\nEcho \u7a0b\u5e8f\u7684\u7279\u6b8a\u4e4b\u5904\u5c31\u662f\uff0c\u4f60\u53ef\u4ee5\u5047\u5b9a Server \u4f1a\u539f\u5c01\u4e0d\u52a8\u7684\u628a\u8bf7\u6c42\u53d1\u56de\u6765\uff0c\u6240\u4ee5\u4f60\u77e5\u9053 Client \u8981\u8bfb\u591a\u5c11\u3002<br \/>\n\u4f46\u662f\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u8981\u8bfb\u591a\u5c11\u6570\u636e\u3002<br \/>\n\u6240\u4ee5\uff0c<code>socket.read_some<\/code>\u00a0\u53cd\u5012\u66f4\u4e3a\u5b9e\u7528\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a\u5404\u51fd\u6570\u6307\u5b9a\u8f93\u51fa\u53c2\u6570\u00a0<code>boost::system::error_code<\/code>\uff0c\u800c\u662f\u4f7f\u7528\u4e86\u5f02\u5e38\uff0c\u628a\u6574\u4e2a\u4ee3\u7801\u5757\u653e\u5728\u00a0<code>try...catch<\/code>\u00a0\u4e2d\u3002<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\"><span class=\"hljs-keyword\">try<\/span> {\r\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\r\n} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-built_in\">std<\/span>::exception&amp; e) {\r\n  <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cerr<\/span> &lt;&lt; e.what() &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\r\n}<\/code><\/pre>\n<p>Asio \u7684 API \u57fa\u672c\u90fd\u901a\u8fc7\u91cd\u8f7d\uff08overload\uff09\uff0c\u63d0\u4f9b\u4e86\u00a0<code>error_code<\/code>\u00a0\u548c\u00a0<code>exception<\/code>\u00a0\u4e24\u79cd\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u3002<br \/>\n\u4f7f\u7528\u5f02\u5e38\u66f4\u6613\u4e8e\u9519\u8bef\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u4f46\u662f\u00a0<code>try...catch<\/code>\u00a0\u8be5\u5305\u542b\u591a\u5c11\u4ee3\u7801\uff0c\u5e76\u4e0d\u662f\u90a3\u4e48\u660e\u663e\uff0c\u65b0\u624b\u5f88\u5bb9\u6613\u8bef\u7528\uff0c\u4ec0\u4e48\u90fd\u5f80\u00a0<code>try...catch<\/code>\u00a0\u91cc\u653e\u3002<\/p>\n<p><strong>\u4e00\u822c\u6765\u8bf4\uff0c\u5f02\u6b65\u65b9\u5f0f\u4e0b\uff0c\u4f7f\u7528\u00a0<code>error_code<\/code>\u00a0\u66f4\u65b9\u4fbf\u4e00\u4e9b\u3002\u6240\u4ee5 Handler \u7684\u53c2\u6570\u90fd\u6709\u00a0<code>error_code<\/code><\/strong><\/p>\n<h3 id=\"articleHeader8\">\u5f02\u6b65\u65b9\u5f0f<\/h3>\n<p>\u5c31 Client \u6765\u8bf4\uff0c\u5f02\u6b65\u4e5f\u8bb8\u5e76\u975e\u5fc5\u8981\uff0c\u9664\u975e\u60f3\u540c\u65f6\u8fde\u63a5\u591a\u4e2a Server\u3002<\/p>\n<p>\u5f02\u6b65\u8bfb\u5199\u524d\u9762\u5df2\u7ecf\u6d89\u53ca\uff0c\u6211\u4eec\u5c31\u5148\u770b\u00a0<code>async_resolve<\/code>\u00a0\u548c\u00a0<code>async_connect<\/code>\u3002<\/p>\n<p>\u9996\u5148\uff0c\u62bd\u53d6\u51fa\u4e00\u4e2a\u7c7b\u00a0<code>Client<\/code>\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\"><span class=\"hljs-keyword\">class<\/span> Client {\r\n<span class=\"hljs-keyword\">public<\/span>:\r\n  Client(boost::asio::io_context&amp; ioc,\r\n         <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span>&amp; host,\r\n        <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span>&amp; port)\r\n      : socket_(ioc)\r\n      , resolver_(<span class=\"hljs-keyword\">new<\/span> tcp::resolver(ioc)) {\r\n  }\r\n\r\n<span class=\"hljs-keyword\">private<\/span>:\r\n  tcp::socket socket_;\r\n\r\n  boost::scoped_ptr&lt;tcp::resolver&gt; resolver_;\r\n  tcp::resolver::results_type endpoints_;\r\n\r\n  <span class=\"hljs-keyword\">char<\/span> cin_buf_[BUF_SIZE];\r\n  <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">array<\/span>&lt;<span class=\"hljs-keyword\">char<\/span>, BUF_SIZE&gt; buf_;\r\n};<\/code><\/pre>\n<p><code>resolver_<\/code>\u00a0\u662f\u4e3a\u4e86\u00a0<code>async_resolve<\/code>\uff1b\u00a0<code>endpoints_<\/code>\u00a0\u662f\u4e3a\u4e86\u00a0<code>async_connect<\/code>\u3002<br \/>\n\u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\uff0c\u751f\u547d\u5468\u671f\u4fbf\u5f97\u5230\u4e86\u4fdd\u8bc1\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u51fd\u6570\u7ed3\u675f\u800c\u5931\u6548\u3002<\/p>\n<p>\u4e0b\u9762\u6765\u770b\u00a0<code>async_resolve<\/code>\u00a0\u5b9e\u73b0\uff08\u4ee3\u7801\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff09\uff1a<\/p>\n<pre class=\"hljs clean\"><code class=\"cpp\">  Client(...) {\r\n    auto handler = std::bind(&amp;Client::HandleResolve,\r\n                             this,\r\n                             std::placeholders::_1,\r\n                             std::placeholders::_2);\r\n    resolver_-&gt;async_resolve(tcp::v4(), host, port, handler);\r\n  }<\/code><\/pre>\n<p><code>async_resolve<\/code>\u00a0\u7684 handler\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">  <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">HandleResolve<\/span><span class=\"hljs-params\">(boost::system::error_code ec,\r\n                     tcp::resolver::results_type results)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">if<\/span> (ec) {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cerr<\/span> &lt;&lt; <span class=\"hljs-string\">\"Resolve: \"<\/span> &lt;&lt; ec.message() &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\r\n    } <span class=\"hljs-keyword\">else<\/span> {\r\n      endpoints_ = results;\r\n      DoConnect(endpoints_.begin());\r\n    }\r\n  }<\/code><\/pre>\n<p><code>HandleResolve<\/code>\u00a0\u7ed3\u679c\u5b58\u5728\u6210\u5458\u53d8\u91cf\u4e2d\uff08<code>endpoints_<\/code>\uff09\uff0c\u540e\u7eed\u00a0<code>DoConnect<\/code>\u00a0\u9700\u8981\u7528\u5230\u3002<\/p>\n<p><code>async_connect<\/code>\u00a0\u53ca\u5176 handler\uff1a<\/p>\n<pre class=\"hljs lasso\"><code class=\"cpp\">  <span class=\"hljs-literal\">void<\/span> DoConnect(tcp<span class=\"hljs-type\">::resolver<\/span><span class=\"hljs-type\">::results_type<\/span><span class=\"hljs-type\">::iterator<\/span> endpoint_it) {\r\n    <span class=\"hljs-keyword\">if<\/span> (endpoint_it != endpoints_.end()) {\r\n      socket_.async_connect(endpoint_it-&gt;endpoint(),\r\n                            std<span class=\"hljs-type\">::bind<\/span>(&amp;Client<span class=\"hljs-type\">::HandleConnect<\/span>,\r\n                                      this,\r\n                                      std<span class=\"hljs-type\">::placeholders<\/span><span class=\"hljs-type\">::_1<\/span>,\r\n                                      endpoint_it));\r\n    }\r\n  }\r\n\r\n  <span class=\"hljs-literal\">void<\/span> HandleConnect(boost<span class=\"hljs-type\">::system<\/span><span class=\"hljs-type\">::error_code<\/span> ec,\r\n                     tcp<span class=\"hljs-type\">::resolver<\/span><span class=\"hljs-type\">::results_type<\/span><span class=\"hljs-type\">::iterator<\/span> endpoint_it) {\r\n    <span class=\"hljs-keyword\">if<\/span> (ec) {\r\n      std<span class=\"hljs-type\">::cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Connect error: \"<\/span> &lt;&lt; ec.message() &lt;&lt; std<span class=\"hljs-type\">::endl<\/span>;\r\n      socket_.close();\r\n\r\n      <span class=\"hljs-comment\">\/\/ Try the next available endpoint.<\/span>\r\n      DoConnect(++endpoint_it);\r\n    } <span class=\"hljs-keyword\">else<\/span> {\r\n      DoWrite();\r\n    }\r\n  }<\/code><\/pre>\n<p>\u56e0\u4e3a endpoints \u53ef\u80fd\u6709\u591a\u4e2a\uff08\u6bd4\u5982\u6ca1\u6709\u6307\u5b9a protocol \u7684\u8bdd\uff09\uff0c\u6240\u4ee5\u00a0<code>HandleConnect<\/code>\u00a0\u5728\u5931\u8d25\u65f6\uff0c\u5f97\u5c1d\u8bd5\u4e0b\u4e00\u4e2a endpoint\uff08<code>DoConnect(++endpoint_it)<\/code>\uff09\u3002\u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0csocket \u5fc5\u987b\u5148 close\uff08<code>socket_.close()<\/code>\uff09\u3002<\/p>\n<p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u8c03\u7528\u00a0<code>DoWrite<\/code>\uff0c\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u4e00\u884c\u6570\u636e\uff0c\u7136\u540e\u5f02\u6b65\u53d1\u9001\u7ed9 Server\u3002<br \/>\n\u4e0b\u9762\u662f\u5f02\u6b65\u8bfb\u5199\u76f8\u5173\u7684\u51fd\u6570\uff0c\u4e00\u5e76\u7ed9\u51fa\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\">  <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">DoWrite<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> len = <span class=\"hljs-number\">0<\/span>;\r\n    <span class=\"hljs-keyword\">do<\/span> {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Enter message: \"<\/span>;\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cin<\/span>.getline(cin_buf_, BUF_SIZE);\r\n      len = <span class=\"hljs-built_in\">strlen<\/span>(cin_buf_);\r\n    } <span class=\"hljs-keyword\">while<\/span> (len == <span class=\"hljs-number\">0<\/span>);\r\n\r\n    boost::asio::async_write(socket_,\r\n                             boost::asio::buffer(cin_buf_, len),\r\n                             <span class=\"hljs-built_in\">std<\/span>::bind(&amp;Client::HandleWrite,\r\n                                       <span class=\"hljs-keyword\">this<\/span>,\r\n                                       <span class=\"hljs-built_in\">std<\/span>::placeholders::_1));\r\n  }\r\n\r\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">HandleWrite<\/span><span class=\"hljs-params\">(boost::system::error_code ec)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">if<\/span> (!ec) {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Reply is: \"<\/span>;\r\n\r\n      socket_.async_read_some(boost::asio::buffer(buf_),\r\n                              <span class=\"hljs-built_in\">std<\/span>::bind(&amp;Client::HandleRead,\r\n                                        <span class=\"hljs-keyword\">this<\/span>,\r\n                                        <span class=\"hljs-built_in\">std<\/span>::placeholders::_1,\r\n                                        <span class=\"hljs-built_in\">std<\/span>::placeholders::_2));\r\n    }\r\n  }\r\n\r\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">HandleRead<\/span><span class=\"hljs-params\">(boost::system::error_code ec,\r\n                  <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">size_t<\/span> length)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">if<\/span> (!ec) {\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span>.write(buf_.data(), length);\r\n      <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\r\n\r\n      <span class=\"hljs-comment\">\/\/ \u5982\u679c\u60f3\u7ee7\u7eed\u4e0b\u4e00\u8f6e\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u8c03\u7528 DoWrite()\u3002<\/span>\r\n    }\r\n  }<\/code><\/pre>\n<p>\u5f02\u6b65\u8bfb\u5199\u5728\u5f02\u6b65 Server \u90a3\u4e00\u8282\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\u4e86\u3002<\/p>\n<p>\u6700\u540e\u662f\u00a0<code>main()<\/code>\uff1a<\/p>\n<pre class=\"hljs cpp\"><code class=\"cpp\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span> argc, <span class=\"hljs-keyword\">char<\/span>* argv[])<\/span> <\/span>{\r\n  <span class=\"hljs-keyword\">if<\/span> (argc != <span class=\"hljs-number\">3<\/span>) {\r\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cerr<\/span> &lt;&lt; <span class=\"hljs-string\">\"Usage: \"<\/span> &lt;&lt; argv[<span class=\"hljs-number\">0<\/span>] &lt;&lt; <span class=\"hljs-string\">\" &lt;host&gt; &lt;port&gt;\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\r\n  }\r\n\r\n  <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-keyword\">char<\/span>* host = argv[<span class=\"hljs-number\">1<\/span>];\r\n  <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-keyword\">char<\/span>* port = argv[<span class=\"hljs-number\">2<\/span>];\r\n\r\n  boost::asio::io_context ioc;\r\n  <span class=\"hljs-function\">Client <span class=\"hljs-title\">client<\/span><span class=\"hljs-params\">(ioc, host, port)<\/span><\/span>;\r\n\r\n  ioc.run();\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u5f02\u6b65\u65b9\u5f0f\u7684 Echo Client \u5c31\u7b97\u5b9e\u73b0\u4e86\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u6587\u7ae0\u592a\u957f\uff0cAsio \u7684\u4ecb\u7ecd\u6682\u65f6\u5148\u544a\u4e00\u6bb5\u843d\u3002\u82e5\u6709\u8865\u9057\uff0c\u4f1a\u53e6\u884c\u8bb0\u5f55\u3002<\/p>\n<p>\u5b8c\u6574\u53ca\u66f4\u52a0\u4e30\u5bcc\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u8bf7\u79fb\u6b65\u00a0<a href=\"https:\/\/github.com\/sprinfall\/boost-asio-study\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub<\/a>\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>2016-10-20 \u53d1\u5e03 \u57fa\u4e8e Asi<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}