{"id":105268,"date":"2026-09-15T07:40:52","date_gmt":"2026-09-14T23:40:52","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/105268.html"},"modified":"2026-09-15T07:40:52","modified_gmt":"2026-09-14T23:40:52","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e5%8a%a0%e9%a4%90%ef%bc%88%e7%af%87%e5%9b%9b%ef%bc%89%e3%80%91%e6%89%8b%e6%8a%8a%e6%89%8b%e5%86%99%e4%b8%80%e4%b8%aa-udp-%e8%8b%b1%e8%af%91%e6%b1%89%e8%af%8d%e5%85%b8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/105268.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218"},"content":{"rendered":"<p id=\"\u624b\u628a\u624b\u5199\u4e00\u4e2a-udp-\u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684-c++-\u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\">\u73af\u5883&#xff1a;Ubuntu &#043; g&#043;&#043;&#xff08;C&#043;&#043;17&#xff09; \u4f60\u5c06\u770b\u5230&#xff1a;\u4e00\u4e2a\u80fd\u8dd1\u7684&#034;\u7f51\u7edc\u7ffb\u8bd1\u8f6f\u4ef6&#034;\u662f\u600e\u4e48\u4ece 9 \u4e2a\u6587\u4ef6\u91cc\u957f\u51fa\u6765\u7684 <\/p>\n<p>\u9002\u5408\u8c01&#xff1a;\u5b66\u8fc7 C&#043;&#043; \u57fa\u7840\u8bed\u6cd5&#xff0c;\u4f46\u6ca1\u5199\u8fc7\u7f51\u7edc\u7a0b\u5e8f\u7684\u4f60<\/p>\n<hr \/>\n<p id=\"\u5148\u770b\u770b\u8fd9\u4e1c\u897f\u5230\u5e95\u662f\u4e2a\u5565\">\u5148\u770b\u770b\u8fd9\u4e1c\u897f\u5230\u5e95\u662f\u4e2a\u5565<\/p>\n<p>\u60f3\u8c61\u4e00\u4e2a\u573a\u666f&#xff1a;\u4f60\u5728\u7535\u8111\u4e0a\u8f93\u5165\u4e00\u4e2a\u82f1\u6587\u5355\u8bcd&#xff0c;\u56de\u8f66&#xff0c;\u5c4f\u5e55\u4e0a\u8e66\u51fa\u4e2d\u6587\u610f\u601d\u2014\u2014\u53ea\u4e0d\u8fc7\u5e2e\u4f60\u7ffb\u8bd1\u7684\u90a3\u672c&#034;\u8bcd\u5178&#034;\u4e0d\u5728\u4f60\u672c\u673a&#xff0c;\u800c\u5728\u7f51\u7edc\u53e6\u4e00\u5934\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u91cc\u3002<\/p>\n<p>\u8dd1\u8d77\u6765\u662f\u8fd9\u6837\u7684&#xff0c;\u5f00\u4e24\u4e2a\u7ec8\u7aef&#xff1a;<\/p>\n<p># \u7ec8\u7aef\u4e00&#xff1a;\u670d\u52a1\u5668&#xff08;\u8bcd\u5178\u5728\u5b83\u624b\u91cc&#xff09;<br \/>\n.\/udpserver 8080<\/p>\n<p># \u7ec8\u7aef\u4e8c&#xff1a;\u5ba2\u6237\u7aef&#xff08;\u4f60\u5728\u8fd9\u91cc\u6572\u5355\u8bcd&#xff09;<br \/>\n.\/udpclient 127.0.0.1 8080<br \/>\nPlease Enter# apple<br \/>\n\u82f9\u679c<br \/>\nPlease Enter# love<br \/>\n\u7231<br \/>\nPlease Enter# computer<br \/>\nNone<\/p>\n<p>\u8bcd\u5178\u91cc\u6709\u7684\u8bcd&#xff08;apple\u3001love\u2026\u2026&#xff09;\u8fd4\u56de\u4e2d\u6587&#xff0c;\u6ca1\u6536\u5f55\u7684\u8bcd\u8fd4\u56de None\u3002\u4e0e\u6b64\u540c\u65f6&#xff0c;\u670d\u52a1\u5668\u90a3\u4e00\u5934\u8fd8\u4f1a\u8bb0\u4e0b&#034;\u8c01\u3001\u5728\u4ec0\u4e48\u65f6\u5019\u3001\u67e5\u4e86\u4ec0\u4e48\u8bcd&#034;\u3002<\/p>\n<p>\u8fd9\u4e2a\u5c0f\u9879\u76ee\u9ebb\u96c0\u867d\u5c0f&#xff0c;\u4e94\u810f\u4ff1\u5168&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7f51\u7edc\u901a\u4fe1\u3001\u4e1a\u52a1\u903b\u8f91\u3001\u65e5\u5fd7\u7cfb\u7edf\u3001\u591a\u7ebf\u7a0b\u9501\u3001\u5de5\u7a0b\u6784\u5efa<\/span><\/span>\u5168\u90fd\u78b0\u5230\u4e86\u3002\u4e0b\u9762\u54b1\u4eec\u6309\u529f\u80fd\u6a21\u5757\u62c6\u5f00\u63c9\u788e\u4e86\u804a\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e00\u3001\u5148\u770b\u5168\u666f\uff1a9-\u4e2a\u6587\u4ef6\u662f\u600e\u4e48\u5206\u5de5\u7684\">\u4e00\u3001\u5148\u770b\u5168\u666f&#xff1a;9 \u4e2a\u6587\u4ef6\u662f\u600e\u4e48\u5206\u5de5\u7684<\/h3>\n<p>\u628a\u6574\u4e2a\u9879\u76ee\u60f3\u6210\u4e00\u5bb6&#034;\u7ffb\u8bd1\u5c0f\u5e97&#034;&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<p>\u6587\u4ef6<\/p>\n<p>\u89d2\u8272<\/p>\n<p>\u6bd4\u55bb<\/p>\n<\/tr>\n<tr>\n<td>\n<p>UdpClient.cc<\/p>\n<\/td>\n<td>\n<p>\u5ba2\u6237\u7aef<\/p>\n<\/td>\n<td>\n<p>\u987e\u5ba2&#xff0c;\u62ff\u7740\u5c0f\u7eb8\u6761\u6765\u95ee\u5355\u8bcd<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UdpServer.hpp<\/p>\n<\/td>\n<td>\n<p>\u670d\u52a1\u5668\u6838\u5fc3<\/p>\n<\/td>\n<td>\n<p>\u524d\u53f0\u63a5\u5f85\u5458&#xff0c;\u53ea\u7ba1\u6536\u53d1\u7eb8\u6761&#xff0c;\u4e0d\u78b0\u7ffb\u8bd1\u5185\u5bb9<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UdpServer.cc<\/p>\n<\/td>\n<td>\n<p>\u670d\u52a1\u5668\u5165\u53e3<\/p>\n<\/td>\n<td>\n<p>\u5e97\u957f&#xff0c;\u5f00\u5e97\u524d\u628a&#034;\u8bcd\u5178&#034;\u548c&#034;\u524d\u53f0&#034;\u7ec4\u88c5\u5230\u4e00\u8d77<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Dict.hpp<\/p>\n<\/td>\n<td>\n<p>\u8bcd\u5178\u4e1a\u52a1<\/p>\n<\/td>\n<td>\n<p>\u540e\u53a8\u7ffb\u8bd1\u5b98&#xff0c;\u771f\u6b63\u8d1f\u8d23\u82f1\u8bd1\u6c49<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>dictionary.txt<\/p>\n<\/td>\n<td>\n<p>\u8bcd\u5178\u6570\u636e<\/p>\n<\/td>\n<td>\n<p>\u7ffb\u8bd1\u5b98\u624b\u8fb9\u90a3\u672c\u8bcd\u6c47\u624b\u518c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>InetAddr.hpp<\/p>\n<\/td>\n<td>\n<p>\u5730\u5740\u5c01\u88c5<\/p>\n<\/td>\n<td>\n<p>\u628a\u5bc4\u4ef6\u4eba\u5730\u5740\u6574\u7406\u6210\u597d\u770b\u7684\u540d\u7247<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Log.hpp<\/p>\n<\/td>\n<td>\n<p>\u65e5\u5fd7\u7cfb\u7edf<\/p>\n<\/td>\n<td>\n<p>\u5e97\u91cc\u7684\u76d1\u63a7\u6444\u50cf\u5934&#xff0c;\u4ec0\u4e48\u65f6\u95f4\u53d1\u751f\u4e86\u4ec0\u4e48\u90fd\u8bb0\u4e0b\u6765<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Mutex.hpp<\/p>\n<\/td>\n<td>\n<p>\u4e92\u65a5\u9501<\/p>\n<\/td>\n<td>\n<p>\u6444\u50cf\u5934\u524d\u7684\u6392\u961f\u680f\u6746&#xff0c;\u4fdd\u8bc1\u4e24\u4e2a\u4eba\u7684\u8bb0\u5f55\u4e0d\u7cca\u5728\u4e00\u8d77<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Makefile<\/p>\n<\/td>\n<td>\n<p>\u6784\u5efa\u811a\u672c<\/p>\n<\/td>\n<td>\n<p>\u4e00\u952e\u6253\u5305\u673a&#xff0c;\u6572\u4e2a make \u5168\u641e\u5b9a<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6570\u636e\u6d41\u52a8\u7684\u8def\u7ebf&#xff0c;\u8bb0\u4f4f\u8fd9\u4e00\u6761\u5c31\u591f\u4e86&#xff1a;<\/p>\n<p>\u952e\u76d8\u8f93\u5165 \u2192 \u5ba2\u6237\u7aef sendto \u2192 \u7f51\u7edc \u2192 \u670d\u52a1\u5668 recvfrom<br \/>\n        \u2192 \u56de\u8c03\u51fd\u6570\u67e5\u8bcd\u5178 \u2192 sendto \u56de\u5bc4 \u2192 \u5ba2\u6237\u7aef recvfrom \u2192 \u5c4f\u5e55\u663e\u793a<\/p>\n<hr \/>\n<h3 id=\"\u4e8c\u3001\u7f51\u7edc\u901a\u4fe1\u6a21\u5757\uff1a\u8ba9\u4e24\u53f0\u673a\u5668\u8bf4\u4e0a\u8bdd\">\u4e8c\u3001\u7f51\u7edc\u901a\u4fe1\u6a21\u5757&#xff1a;\u8ba9\u4e24\u53f0\u673a\u5668\u8bf4\u4e0a\u8bdd<\/h3>\n<h4 id=\"2.1-\u670d\u52a1\u5668\u7684\u4e00\u751f\uff1asocket-\u2192-bind-\u2192-recvfrom-\u2192-sendto\">2.1 \u670d\u52a1\u5668\u7684\u4e00\u751f&#xff1a;socket \u2192 bind \u2192 recvfrom \u2192 sendto<\/h4>\n<p>UDP \u670d\u52a1\u5668\u7684\u6838\u5fc3\u5168\u5728 UdpServer.hpp \u91cc\u3002\u5b83\u7684\u751f\u547d\u5468\u671f\u7b80\u5355\u5f97\u79bb\u8c31&#xff0c;\u5c31\u5e72\u56db\u4ef6\u4e8b&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5f00\u5f20&#xff08;socket&#xff09;\u3001\u79df\u95e8\u9762&#xff08;bind&#xff09;\u3001\u7b49\u5ba2\u4eba&#xff08;recvfrom&#xff09;\u3001\u56de\u8bdd&#xff08;sendto&#xff09;\u3002<\/span><\/span><\/p>\n<hr \/>\n<p>\u5148\u770b\u6784\u9020\u51fd\u6570\u548c\u7c7b\u578b\u5b9a\u4e49&#xff1a;<\/p>\n<p>using func_t &#061; std::function&lt;std::string(const std::string&amp;, InetAddr&amp;)&gt;;<\/p>\n<p>class UdpServer<br \/>\n{<br \/>\npublic:<br \/>\n    UdpServer(uint16_t port, func_t func)<br \/>\n        : _sockfd(defaultfd),<br \/>\n          _port(port),<br \/>\n          _isrunning(false),<br \/>\n          _func(func)<br \/>\n    {<br \/>\n    }<\/p>\n<p>\u6ce8\u610f\u8fd9\u4e2a func_t&#xff0c;\u5b83\u662f\u6574\u4e2a\u8bbe\u8ba1\u91cc\u6700\u5999\u7684\u4e00\u7b14\u3002UdpServer \u6536\u5230\u6d88\u606f\u540e<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u81ea\u5df1\u4e0d\u5904\u7406<\/span><\/span>&#xff0c;\u800c\u662f\u628a\u5b57\u7b26\u4e32\u4e22\u7ed9\u4e00\u4e2a\u5916\u90e8\u4f20\u8fdb\u6765\u7684\u51fd\u6570\u3002\u4eca\u5929\u8fd9\u4e2a\u51fd\u6570\u662f&#034;\u67e5\u8bcd\u5178&#034;&#xff0c;\u660e\u5929\u4f60\u6362\u6210&#034;\u8ba1\u7b97\u5668&#034;&#034;\u804a\u5929\u5ba4\u673a\u5668\u4eba&#034;&#xff0c;\u670d\u52a1\u5668\u4ee3\u7801\u4e00\u884c\u90fd\u4e0d\u7528\u6539\u3002\u8fd9\u5c31\u662f\u56de\u8c03&#xff0c;\u4e0b\u9762\u8fd8\u4f1a\u7ec6\u8bb2\u3002<\/p>\n<p>1. \u4ec0\u4e48\u662f\u56de\u8c03\u51fd\u6570&#xff1f;&#xff08;\u751f\u6d3b\u6bd4\u55bb&#xff09;<\/p>\n<p>\u5047\u8bbe\u4f60\u8981\u53bb\u4e00\u5bb6\u9910\u5385\u5403\u996d&#xff1a;<\/p>\n<li>\n<p>\u540c\u6b65\u8c03\u7528&#xff08;\u666e\u901a\u51fd\u6570&#xff09;&#xff1a;\u4f60\u70b9\u5b8c\u83dc&#xff0c;\u5c31\u50bb\u7ad9\u5728\u53a8\u623f\u95e8\u53e3&#xff0c;\u4e00\u76f4\u7b49\u5230\u53a8\u5e08\u628a\u83dc\u505a\u597d\u9012\u7ed9\u4f60&#xff0c;\u4f60\u624d\u8d70\u3002\u8fd9\u671f\u95f4\u4f60\u4ec0\u4e48\u90fd\u5e72\u4e0d\u4e86\u3002<\/p>\n<\/li>\n<li>\n<p>\u56de\u8c03\u51fd\u6570&#xff1a;\u4f60\u70b9\u5b8c\u83dc&#xff0c;\u544a\u8bc9\u670d\u52a1\u5458&#xff1a;\u201c\u8fd9\u662f\u6211\u7684\u624b\u673a\u53f7&#xff08;\u6ce8\u518c\u56de\u8c03&#xff09;&#xff0c;\u83dc\u505a\u597d\u4e86\u6253\u7ed9\u6211&#xff08;\u89e6\u53d1\u56de\u8c03&#xff09;&#xff0c;\u987a\u4fbf\u544a\u8bc9\u6211\u83dc\u540d&#xff08;\u53c2\u6570&#xff09;\u3002\u201d \u7136\u540e\u4f60\u5c31\u56de\u5ea7\u4f4d\u73a9\u624b\u673a\u4e86&#xff08;\u4e3b\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&#xff09;\u3002\u7b49\u53a8\u623f\u505a\u597d\u4e86&#xff0c;\u670d\u52a1\u5458\u901a\u8fc7\u4f60\u7684\u624b\u673a\u53f7\u627e\u5230\u4f60&#xff08;\u6267\u884c\u56de\u8c03&#xff09;&#xff0c;\u628a\u83dc\u7aef\u7ed9\u4f60\u3002<\/p>\n<\/li>\n<p>\u5728\u8fd9\u4e2a\u6bd4\u55bb\u91cc&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f60\u7684\u624b\u673a\u53f7\u00a0&#061; \u51fd\u6570\u6307\u9488\/\u56de\u8c03\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p>\u53a8\u5e08\/\u670d\u52a1\u5458\u00a0&#061; \u8c03\u7528\u8005<\/p>\n<\/li>\n<li>\n<p>\u83dc\u540d\u00a0&#061; \u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570<\/p>\n<\/li>\n<\/ul>\n<p>\u5728 C&#043;&#043; \u91cc&#xff0c;\u56de\u8c03\u51fd\u6570\u5c31\u662f&#xff1a;\u628a\u4e00\u4e2a\u51fd\u6570&#xff08;\u6216\u53ef\u8c03\u7528\u5bf9\u8c61&#xff09;\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570&#xff0c;\u5f53\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6&#xff08;\u6bd4\u5982\u6536\u5230\u7f51\u7edc\u6570\u636e&#xff09;\u65f6&#xff0c;\u7531\u90a3\u4e2a\u51fd\u6570\u6765\u8c03\u7528\u4f60\u4f20\u8fdb\u53bb\u7684\u51fd\u6570\u3002<\/p>\n<hr \/>\n<p>2. \u9010\u8bcd\u62c6\u89e3\u56fe\u4e2d\u7684\u8fd9\u884c\u4ee3\u7801<\/p>\n<p>using func_t &#061; std::function&lt;std::string(const std::string&amp;, InetAddr&amp;)&gt;;<\/p>\n<p>\u8fd9\u884c\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\u522b\u540d&#xff0c;\u540d\u5b57\u53eb\u00a0func_t\u3002\u4ee5\u540e\u4f60\u60f3\u58f0\u660e\u8fd9\u79cd\u7c7b\u578b\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u76f4\u63a5\u5199\u00a0func_t my_callback;\u00a0\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u7b49\u53f7\u53f3\u8fb9&#xff1a;<\/p>\n<ul>\n<li>\n<p>std::function&#xff1a;\u8fd9\u662f C&#043;&#043;11 \u5f15\u5165\u7684\u201c\u4e07\u80fd\u51fd\u6570\u5bb9\u5668\u201d\u3002\u5b83\u53ef\u4ee5\u88c5\u4e0b\u666e\u901a\u51fd\u6570\u3001Lambda \u8868\u8fbe\u5f0f\u3001\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3001\u4eff\u51fd\u6570\u7b49\u4efb\u4f55\u201c\u957f\u5f97\u50cf\u51fd\u6570\u201d\u7684\u4e1c\u897f\u3002\u5b83\u6bd4\u4f20\u7edf\u7684 C \u8bed\u8a00\u51fd\u6570\u6307\u9488\u5f3a\u5927\u5f97\u591a\u3002<\/p>\n<\/li>\n<li>\n<p>&lt;std::string(&#8230;)&gt;&#xff1a;\u5c16\u62ec\u53f7\u91cc\u63cf\u8ff0\u7684\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u7b7e\u540d&#xff08;Signature&#xff09;\u3002<\/p>\n<ul>\n<li>\n<p>\u7b2c\u4e00\u4e2a\u00a0std::string&#xff1a;\u8868\u793a\u88ab\u5305\u88c5\u7684\u51fd\u6570\u8fd4\u56de\u503c\u662f\u00a0std::string\u3002<\/p>\n<\/li>\n<li>\n<p>(const std::string&amp;, InetAddr&amp;)&#xff1a;\u8868\u793a\u88ab\u5305\u88c5\u7684\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u662f\u00a0const std::string&amp;&#xff08;\u53ea\u8bfb\u7684\u5b57\u7b26\u4e32&#xff09;&#xff0c;\u7b2c\u4e8c\u4e2a\u662f\u00a0InetAddr&amp;&#xff08;\u7f51\u7edc\u5730\u5740\u5bf9\u8c61\u7684\u5f15\u7528&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u603b\u7ed3\u8fd9\u884c\u4ee3\u7801\u7684\u610f\u601d&#xff1a;<br \/>\nfunc_t\u00a0\u4ee3\u8868\u4e00\u79cd\u51fd\u6570\u7c7b\u578b&#xff0c;\u8fd9\u79cd\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u7f51\u7edc\u5730\u5740&#xff0c;\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<hr \/>\n<p>3. \u8fd9\u4e2a\u7c7b\u578b\u7684\u56de\u8c03\u5728\u9879\u76ee\u4e2d\u662f\u600e\u4e48\u7528\u7684&#xff1f;<\/p>\n<p>\u7ed3\u5408\u4f60\u6b63\u5728\u505a\u7684\u201cUDP\u82f1\u8bd1\u6c49\u7f51\u7edc\u8bcd\u5178\u201d\u9879\u76ee&#xff0c;\u8fd9\u4e2a\u00a0func_t\u00a0\u7b80\u76f4\u5c31\u662f\u4e3a\u5b83\u91cf\u8eab\u5b9a\u505a\u7684\u3002<\/p>\n<p>\u573a\u666f\u8bbe\u5b9a&#xff1a;<br \/>\n\u670d\u52a1\u5668\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u4e00\u4e2a\u82f1\u6587\u5355\u8bcd&#xff08;std::string&#xff09;&#xff0c;\u4ee5\u53ca\u5ba2\u6237\u7aef\u7684\u5730\u5740&#xff08;InetAddr&#xff09;\u3002\u670d\u52a1\u5668\u9700\u8981\u8c03\u7528\u4e1a\u52a1\u903b\u8f91&#xff0c;\u628a\u8fd9\u4e2a\u8bcd\u7ffb\u8bd1\u6210\u4e2d\u6587&#xff08;\u8fd4\u56de\u00a0std::string&#xff09;&#xff0c;\u7136\u540e\u518d\u628a\u8fd9\u4e2a\u4e2d\u6587\u53d1\u56de\u53bb\u3002<\/p>\n<p>\u5178\u578b\u7684\u5b9e\u73b0\u6d41\u7a0b&#xff1a;<\/p>\n<p>1. \u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91&#xff08;\u5728\u00a0Dict.hpp\u00a0\u4e2d&#xff09;&#xff1a;<\/p>\n<p>class Dict {<br \/>\npublic:<br \/>\n    \/\/ \u8fd9\u5c31\u662f\u4e00\u4e2a\u7b26\u5408 func_t \u7b7e\u540d\u7684\u51fd\u6570&#xff01;<br \/>\n    std::string Translate(const std::string&amp; word, InetAddr&amp; client_addr) {<br \/>\n        \/\/ \u67e5\u8bcd\u5178&#xff0c;\u8fd4\u56de\u7ffb\u8bd1\u7ed3\u679c<br \/>\n        return &#034;\u82f9\u679c&#034;;<br \/>\n    }<br \/>\n};<\/p>\n<hr \/>\n<p>2. \u6ce8\u518c\u56de\u8c03&#xff08;\u5728\u00a0UdpServer\u00a0\u4e2d&#xff09;&#xff1a;<br \/>\n\u4f60\u7684\u00a0UdpServer\u00a0\u7c7b\u91cc\u4f1a\u6709\u4e00\u4e2a\u00a0func_t _callback;\u00a0\u6210\u5458\u53d8\u91cf\u3002<br \/>\n\u5728\u00a0main\u00a0\u51fd\u6570\u91cc&#xff0c;\u4f60\u628a\u00a0Dict\u00a0\u5bf9\u8c61\u7684\u00a0Translate\u00a0\u65b9\u6cd5\u201c\u6ce8\u518c\u201d\u7ed9\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>Dict my_dict(&#034;.\/dictionary.txt&#034;);<br \/>\nUdpServer server(ip, port);<br \/>\n\/\/ \u6ce8\u518c\u56de\u8c03&#xff1a;\u4ee5\u540e\u6536\u5230\u6570\u636e&#xff0c;\u5c31\u8c03\u7528 my_dict \u7684 Translate \u65b9\u6cd5<br \/>\nserver.RegisterCallback(std::bind(&amp;Dict::Translate, &amp;my_dict, std::placeholders::_1, std::placeholders::_2));<\/p>\n<p>3. \u89e6\u53d1\u56de\u8c03&#xff08;\u5728\u00a0UdpServer::Start()\u00a0\u5faa\u73af\u4e2d&#xff09;&#xff1a;<br \/>\n\u5f53\u00a0recvfrom\u00a0\u6536\u5230\u6570\u636e\u540e&#xff0c;\u670d\u52a1\u5668\u4e0d\u9700\u8981\u77e5\u9053\u5177\u4f53\u600e\u4e48\u7ffb\u8bd1&#xff08;\u90a3\u662f\u4e1a\u52a1\u5c42\u7684\u4e8b&#xff09;\u3002\u5b83\u53ea\u9700\u8981\u8c03\u7528\u90a3\u4e2a\u56de\u8c03\u5373\u53ef&#xff1a;<\/p>\n<p>\/\/ \u6536\u5230\u6d88\u606f\u540e<br \/>\nstd::string result &#061; _callback(buffer, peer_addr); \/\/ \u89e6\u53d1\u56de\u8c03&#xff0c;\u6267\u884c\u7ffb\u8bd1<br \/>\nsendto(&#8230;, result, &#8230;); \/\/ \u628a\u7ffb\u8bd1\u7ed3\u679c\u53d1\u56de\u53bb<\/p>\n<hr \/>\n<p>4.\u4e3a\u4ec0\u4e48\u8981\u7528\u00a0std::function\u00a0\u800c\u4e0d\u662f C \u8bed\u8a00\u7684\u51fd\u6570\u6307\u9488&#xff1f;<\/p>\n<p>\u5728 C \u8bed\u8a00\u91cc&#xff0c;\u56de\u8c03\u901a\u5e38\u5199\u6210\u8fd9\u6837&#xff1a;<\/p>\n<p>typedef void (*func_ptr)(int);<\/p>\n<p>\u5b83\u53ea\u80fd\u6307\u5411\u666e\u901a\u7684\u5168\u5c40\u51fd\u6570&#xff0c;\u65e0\u6cd5\u6307\u5411\u7c7b\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4e5f\u65e0\u6cd5\u6355\u83b7\u5c40\u90e8\u53d8\u91cf&#xff08;\u6bd4\u5982 Lambda \u91cc\u7684\u00a0[]&#xff09;\u3002<\/p>\n<p>\u800c\u00a0std::function\u00a0\u662f C&#043;&#043; \u7684\u73b0\u4ee3\u5229\u5668&#xff0c;\u5b83\u53ef\u4ee5\u7edf\u4e00\u88c5\u4e0b&#xff1a;<\/p>\n<li>\n<p>\u666e\u901a\u51fd\u6570<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570&#xff08;\u914d\u5408\u00a0std::bind\u00a0\u6216 Lambda&#xff09;<\/p>\n<\/li>\n<li>\n<p>Lambda \u8868\u8fbe\u5f0f&#xff08;\u6781\u5176\u65b9\u4fbf&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4eff\u51fd\u6570&#xff08;\u91cd\u8f7d\u4e86\u00a0operator()\u00a0\u7684\u7c7b&#xff09;<\/p>\n<\/li>\n<p>\u8fd9\u884c\u4ee3\u7801\u7684\u5999\u5904\u5728\u4e8e&#xff1a;\u00a0\u5b83\u628a\u201c\u4e1a\u52a1\u903b\u8f91&#xff08;\u7ffb\u8bd1&#xff09;\u201d\u548c\u201c\u7f51\u7edc\u6846\u67b6&#xff08;\u6536\u53d1\u6570\u636e&#xff09;\u201d\u5f7b\u5e95\u89e3\u8026\u4e86\u3002\u670d\u52a1\u5668\u53ea\u7ba1\u6536\u53d1&#xff0c;\u5177\u4f53\u600e\u4e48\u7ffb\u8bd1&#xff0c;\u7531\u4f60\u6ce8\u518c\u8fdb\u53bb\u7684\u56de\u8c03\u51fd\u6570\u8bf4\u4e86\u7b97\u3002\u8fd9\u5c31\u53eb<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u9762\u5411\u63a5\u53e3\u7f16\u7a0b<\/span><\/span>\u3002<\/p>\n<hr \/>\n<p>\u5f00\u5f20&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57<\/p>\n<p>void Init()<br \/>\n{<br \/>\n    _sockfd &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n    if (_sockfd &lt; 0)<br \/>\n    {<br \/>\n        LOG(LogLevel::FATAL) &lt;&lt; &#034;socket error!&#034;;<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    LOG(LogLevel::INFO) &lt;&lt; &#034;socket success, sockfd : &#034; &lt;&lt; _sockfd;<\/p>\n<p>socket \u5c31\u50cf\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u53f0\u5bf9\u8bb2\u673a&#xff1a;<\/p>\n<ul>\n<li>\n<p>AF_INET \u8868\u793a\u7528 IPv4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>SOCK_DGRAM \u8868\u793a\u8981&#034;\u6570\u636e\u62a5&#034;\u7c7b\u578b\u7684\u5bf9\u8bb2\u673a\u2014\u2014\u8fd9\u5c31\u662f UDP&#xff08;\u60f3\u8981 TCP \u5c31\u586b SOCK_STREAM&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570\u53eb\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6210\u529f\u65f6\u4e00\u822c\u4ece 3 \u5f00\u59cb&#xff08;0\u30011\u30012 \u88ab\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u3001\u6807\u51c6\u9519\u8bef\u63d0\u524d\u5360\u4e86&#xff09;&#xff0c;\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<p>\u79df\u95e8\u9762&#xff1a;\u586b\u5730\u5740 &#043; bind<\/p>\n<p>    struct sockaddr_in local;<br \/>\n    bzero(&amp;local, sizeof(local));<br \/>\n    local.sin_family &#061; AF_INET;<br \/>\n    local.sin_port &#061; htons(_port);<br \/>\n    local.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>    int n &#061; bind(_sockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n    if (n &lt; 0)<br \/>\n    {<br \/>\n        LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n        exit(2);<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">struct sockaddr_in<\/span><span style=\"background-color:#ffd900\"> \u5c31\u662f\u670d\u52a1\u5668\u7684&#034;\u6536\u4ef6\u5730\u5740&#034;&#xff0c;\u8981\u586b\u4e09\u6837\u4e1c\u897f\u3002\u8fd9\u91cc\u6bcf\u4e2a\u7ec6\u8282\u90fd\u6709\u8bb2\u7a76&#xff1a;<\/span><\/span><\/p>\n<p>bzero <span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5148\u6e05\u96f6<\/span><\/span>\u3002 \u7ed3\u6784\u4f53\u91cc\u6709\u4e9b\u5b57\u6bb5\u6211\u4eec\u6ca1\u586b&#xff0c;\u4e0d\u6e05\u96f6\u7684\u8bdd\u91cc\u9762\u662f\u968f\u673a\u5783\u573e\u503c&#xff0c;\u53ef\u80fd\u57cb\u96f7\u3002\u8fd9\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u808c\u8089\u8bb0\u5fc6\u3002<\/p>\n<p>htons(_port)&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e3a\u4ec0\u4e48\u7aef\u53e3\u53f7\u8fd8\u8981\u8f6c\u6362&#xff1f;<\/span><\/span> \u56e0\u4e3a\u4e0d\u540c CPU \u5b58\u6570\u5b57\u7684\u5b57\u8282\u987a\u5e8f\u4e0d\u4e00\u6837&#xff08;\u6709\u7684\u5927\u7aef\u3001\u6709\u7684\u5c0f\u7aef&#xff09;&#xff0c;\u7f51\u7edc\u4e16\u754c\u7ea6\u5b9a\u5927\u5bb6\u7edf\u4e00\u7528\u5927\u7aef\u4f20\u8f93\u3002htons \u5c31\u662f&#034;host to network short&#034;&#xff0c;\u628a\u672c\u673a\u683c\u5f0f\u7ffb\u8bd1\u6210\u7f51\u7edc\u901a\u7528\u683c\u5f0f\u3002\u4e0d\u8f6c\u7684\u8bdd&#xff0c;\u4f60\u4f20 8080&#xff0c;\u5bf9\u65b9\u770b\u5230\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u83ab\u540d\u5176\u5999\u7684\u7aef\u53e3\u3002<\/p>\n<p>INADDR_ANY&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u672c\u5e97\u5730\u5740\u5199&#034;\u54ea\u513f\u90fd\u884c&#034;<\/span><\/span>\u3002 \u8fd9\u662f\u65b0\u624b\u6700\u5bb9\u6613\u61f5\u7684\u5730\u65b9&#xff0c;\u591a\u8bf4\u4e24\u53e5\u3002<\/p>\n<hr \/>\n<p>\u4e00\u53f0\u670d\u52a1\u5668\u5f80\u5f80\u6709\u591a\u5757\u7f51\u5361&#xff08;127.0.0.1 \u56de\u73af\u7f51\u5361\u300110.0.0.2 \u5c40\u57df\u7f51\u7f51\u5361\u2026\u2026&#xff09;\u3002\u5982\u679c bind \u65f6\u5199\u6b7b\u67d0\u4e2a IP&#xff0c;\u5c31\u7b49\u4e8e\u544a\u8bc9\u90ae\u5c40&#034;\u53ea\u6536\u9001\u5230\u8fd9\u4e2a\u95e8\u7684\u4fe1&#034;&#xff0c;\u5ba2\u6237\u7aef\u6362\u4e2a IP \u8bbf\u95ee\u5c31\u6536\u4e0d\u5230\u4e86\u3002\u586b INADDR_ANY&#xff08;\u503c\u5c31\u662f 0.0.0.0&#xff09;\u7684\u610f\u601d\u662f&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u672c\u673a\u6240\u6709\u7f51\u5361\u4e0a\u7684\u6570\u636e\u6211\u90fd\u6536<\/span><\/span>\u3002\u7528 ss -uln \u80fd\u770b\u5230\u5b83\u663e\u793a\u4e3a&#xff1a;<\/p>\n<p>UNCONN 0  0  0.0.0.0:8080  0.0.0.0:*<\/p>\n<p>0.0.0.0 \u5c31\u662f&#034;\u4e0d\u6311\u7f51\u5361&#034;\u7684\u610f\u601d\u3002\u987a\u5e26\u4e00\u63d0&#xff0c;\u5b83\u7684\u503c\u662f 0&#xff0c;0 \u600e\u4e48\u8f6c\u5b57\u8282\u5e8f\u8fd8\u662f 0&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u4e0d\u7528\u518d\u5305 htonl\u3002<\/p>\n<hr \/>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e3a\u4ec0\u4e48\u670d\u52a1\u5668\u5fc5\u987b bind&#xff0c;\u5ba2\u6237\u7aef\u5374\u4e0d\u7528&#xff1f;<\/span><\/span><\/p>\n<p>\u56e0\u4e3a\u670d\u52a1\u5668\u7684\u95e8\u724c\u53f7\u5fc5\u987b\u56fa\u5b9a\u4e14\u4f17\u6240\u5468\u77e5&#xff0c;\u5ba2\u6237\u7aef\u624d\u77e5\u9053\u53bb\u54ea\u91cc\u627e\u5b83\u2014\u2014\u6ca1\u4eba\u4f1a\u4e09\u5929\u4e24\u5934\u6362\u5730\u5740\u5f00\u5e97\u3002\u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u662f\u51e0\u53f7\u65e0\u6240\u8c13&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u7b2c\u4e00\u6b21\u53d1\u6d88\u606f\u65f6\u5e2e\u5b83\u968f\u673a\u6311\u4e00\u4e2a\u7a7a\u95f2\u7684\u5c31\u884c&#xff0c;\u8fd8\u80fd\u907f\u514d\u4f60\u540c\u65f6\u5f00\u4e24\u4e2a\u5ba2\u6237\u7aef\u65f6\u7aef\u53e3\u6253\u67b6\u3002<\/p>\n<hr \/>\n<p>\u8425\u4e1a&#xff1a;\u6536\u53d1\u5faa\u73af<\/p>\n<p>void Start()<br \/>\n{<br \/>\n    _isrunning &#061; true;<br \/>\n    while (_isrunning)<br \/>\n    {<br \/>\n        char buffer[1024];<br \/>\n        struct sockaddr_in peer;<br \/>\n        socklen_t len &#061; sizeof(peer);<\/p>\n<p>        ssize_t s &#061; recvfrom(_sockfd, buffer, sizeof(buffer) &#8211; 1, 0,<br \/>\n                             (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n        if (s &gt; 0)<br \/>\n        {<br \/>\n            InetAddr client(peer);<br \/>\n            buffer[s] &#061; 0;<br \/>\n            std::string result &#061; _func(buffer, client);<\/p>\n<p>            sendto(_sockfd, result.c_str(), result.size(), 0,<br \/>\n                   (struct sockaddr*)&amp;peer, len);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>recvfrom \u8fd9\u4e2a\u51fd\u6570\u5f88\u8d2a\u5fc3&#xff0c;\u4e00\u6b21\u5e72\u4e24\u4ef6\u4e8b&#xff1a;\u628a\u6570\u636e\u6536\u8fdb buffer&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u540c\u65f6\u628a\u53d1\u9001\u65b9\u7684\u5b8c\u6574\u5730\u5740\u585e\u8fdb <\/span><span style=\"background-color:#ffd900\">peer<\/span><\/span>\u3002\u8fd9\u592a\u91cd\u8981\u4e86\u2014\u2014\u670d\u52a1\u5668\u6536\u5230\u4fe1\u7684\u540c\u65f6\u4e5f\u62ff\u5230\u4e86&#034;\u5bc4\u4ef6\u4eba\u5730\u5740&#034;&#xff0c;\u5f85\u4f1a\u513f\u56de\u4fe1\u7528\u7684\u5c31\u662f\u5b83\u3002UDP \u670d\u52a1\u5668\u56e0\u6b64\u4e0d\u7528\u7ef4\u62a4\u4efb\u4f55\u5ba2\u6237\u7aef\u540d\u5355&#xff0c;\u6765\u4e00\u4e2a\u4eba\u670d\u52a1\u4e00\u4e2a\u4eba&#xff0c;\u4e00\u4e07\u4e2a\u5ba2\u6237\u7aef\u4e5f\u4e0d\u6015\u3002<\/p>\n<p>\u6536\u5230\u6570\u636e\u540e\u505a\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<li>\n<p>InetAddr client(peer)&#xff1a;\u628a\u539f\u59cb\u5730\u5740\u5305\u88c5\u6210\u4e00\u4e2a\u597d\u7528\u7684\u5bf9\u8c61&#xff08;2.3 \u8282\u8bb2&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>_func(buffer, client)&#xff1a;\u628a\u6d88\u606f\u4ea4\u7ed9\u56de\u8c03\u51fd\u6570\u5904\u7406&#xff0c;\u62ff\u56de\u7ed3\u679c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>sendto(&#8230;)&#xff1a;\u628a\u7ed3\u679c\u7167\u7740 peer \u5730\u5740\u5bc4\u56de\u53bb\u3002<\/p>\n<\/li>\n<hr \/>\n<h4 id=\"2.2-\u4e24\u4e2a\u5fc5\u987b\u77e5\u9053\u7684\u5751\uff1a\u62a5\u6587\u8fb9\u754c\u548c\u7ed3\u5c3e\u7684-\\\\0\">2.2 \u4e24\u4e2a\u5fc5\u987b\u77e5\u9053\u7684\u5751&#xff1a;\u62a5\u6587\u8fb9\u754c\u548c\u7ed3\u5c3e\u7684 \\\\0<\/h4>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">UDP \u4f1a\u4e0d\u4f1a&#034;\u7c98\u5305&#034;&#xff1f;<\/span><span style=\"background-color:#ffd900\"> <\/span><\/span>\u8fd9\u662f\u9762\u8bd5\u9ad8\u9891\u9898&#xff0c;\u5728\u8fd9\u4e2a\u9879\u76ee\u91cc\u4f60\u53ef\u4ee5\u4eb2\u624b\u4f53\u4f1a\u3002TCP \u50cf\u4e00\u6761\u6cb3&#xff0c;\u6570\u636e\u662f\u6c34&#xff0c;\u591a\u6b21\u53d1\u9001\u53ef\u80fd\u8fde\u6210\u4e00\u7247&#xff0c;\u63a5\u6536\u65b9\u5206\u4e0d\u6e05\u54ea\u6bb5\u662f\u54ea\u53e5\u2014\u2014\u90a3\u624d\u53eb\u7c98\u5305\u3002UDP \u4e0d\u4e00\u6837&#xff0c;\u5b83\u50cf\u5bc4\u5feb\u9012&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4f60 sendto \u4e00\u6b21\u5c31\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5305\u88f9&#xff0c;recvfrom \u4e00\u6b21\u6536\u4e00\u4e2a\u5305\u88f9<\/span><\/span>&#xff0c;\u62a5\u6587\u8fb9\u754c\u662f\u64cd\u4f5c\u7cfb\u7edf\u5e2e\u4f60\u5b88\u4f4f\u7684\u3002\u5ba2\u6237\u7aef\u53d1\u4e00\u884c\u3001\u670d\u52a1\u5668\u6536\u4e00\u884c&#xff0c;\u5929\u7136\u4e0d\u4f1a\u7c98\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u90a3\u4e2d\u6587\u4e71\u7801\u3001\u5c3e\u90e8\u810f\u5b57\u7b26\u662f\u600e\u4e48\u56de\u4e8b&#xff1f;<\/span><span style=\"background-color:#ffd900\"> <\/span><\/span>\u770b\u8fd9\u884c&#xff1a;<\/p>\n<p>buffer[s] &#061; 0;<\/p>\n<p>UDP \u5305\u88f9\u91cc\u88c5\u7684\u662f\u7eaf\u6570\u636e&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e0d\u4f1a\u81ea\u5e26 C \u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684 <\/span><span style=\"background-color:#ffd900\">&#039;\\\\0&#039;<\/span><\/span>\u3002recvfrom \u8fd4\u56de\u7684 s \u662f\u8fd9\u6b21\u6536\u5230\u7684\u5b57\u8282\u6570&#xff0c;\u6bd4\u5982 5&#xff0c;\u90a3 buffer \u91cc\u53ea\u6709\u524d 5 \u4e2a\u5b57\u8282\u662f\u6709\u6548\u7684&#xff0c;\u540e\u9762\u5168\u662f\u4e0a\u4e00\u6b21\u7684\u6b8b\u7559\u6216\u8005\u5783\u573e\u503c\u3002\u76f4\u63a5\u5f53\u5b57\u7b26\u4e32\u6253\u5370&#xff0c;\u5c31\u4f1a\u8bfb\u51fa \u82f9\u679c\u70eb\u70eb\u70eb\u70eb \u8fd9\u79cd\u9b3c\u4e1c\u897f\u3002\u624b\u52a8\u5728\u6709\u6548\u6570\u636e\u672b\u5c3e\u8865\u4e00\u4e2a 0&#xff0c;\u5b57\u7b26\u4e32\u624d\u77e5\u9053\u81ea\u5df1\u5728\u54ea\u513f\u7ed3\u675f\u3002<\/p>\n<p>\u81f3\u4e8e\u4e2d\u6587\u80fd\u6b63\u5e38\u663e\u793a&#xff0c;\u662f\u56e0\u4e3a\u5ba2\u6237\u7aef\u3001\u670d\u52a1\u5668\u3001\u7ec8\u7aef\u90fd\u7edf\u4e00\u7528 UTF-8&#xff0c;\u4e2d\u6587\u6309\u5b57\u8282\u539f\u6837\u642c\u8fd0\u3001\u539f\u6837\u663e\u793a&#xff0c;\u7a0b\u5e8f\u6839\u672c\u4e0d\u9700\u8981&#034;\u8ba4\u8bc6&#034;\u4e2d\u6587\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u957f\u5ea6\u4e5f\u8981\u7559\u4e2a\u5fc3\u773c<\/span><span style=\"background-color:#ffd900\">&#xff1a;<\/span><\/span>\u6536\u5305\u957f\u5ea6\u4f20\u7684\u662f sizeof(buffer) &#8211; 1&#xff08;1023&#xff09;&#xff0c;\u6545\u610f\u7559\u4e00\u4e2a\u5b57\u8282\u653e &#039;\\\\0&#039;\u3002\u5982\u679c\u5ba2\u6237\u7aef\u53d1\u6765 2000 \u5b57\u8282&#xff0c;UDP \u4f1a\u76f4\u63a5\u628a\u88c5\u4e0d\u4e0b\u7684\u90e8\u5206\u4e22\u6389&#xff08;\u622a\u65ad&#xff09;&#xff0c;\u8fd9\u662f UDP &#034;\u5c3d\u529b\u800c\u4e3a\u3001\u4e0d\u8d1f\u8d23&#034;\u7684\u672c\u6027\u3002<\/p>\n<hr \/>\n<h4 id=\"2.3-inetaddr\uff1a\u7ed9\u88f8\u5730\u5740\u5957\u4e00\u4ef6\u8212\u670d\u7684\u5916\u5957\">2.3 InetAddr&#xff1a;\u7ed9\u88f8\u5730\u5740\u5957\u4e00\u4ef6\u8212\u670d\u7684\u5916\u5957<\/h4>\n<p>recvfrom \u7ed9\u7684 sockaddr_in \u662f\u7ed9\u673a\u5668\u770b\u7684&#xff1a;\u7aef\u53e3\u662f\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u6574\u6570&#xff0c;IP \u662f 4 \u5b57\u8282\u4e8c\u8fdb\u5236\u3002\u4eba\u60f3\u770b\u7684\u662f 127.0.0.1 \u548c 58050\u3002InetAddr.hpp \u5c31\u5e72\u8fd9\u4e00\u4ef6\u4e8b&#xff1a;<\/p>\n<p>class InetAddr<br \/>\n{<br \/>\npublic:<br \/>\n    InetAddr(struct sockaddr_in &amp;addr) : _addr(addr)<br \/>\n    {<br \/>\n        _port &#061; ntohs(_addr.sin_port);              \/\/ \u7f51\u7edc\u5e8f \u2192 \u4e3b\u673a\u5e8f<br \/>\n        _ip &#061; inet_ntoa(_addr.sin_addr);            \/\/ 4\u5b57\u8282IP \u2192 &#034;127.0.0.1&#034;<br \/>\n    }<br \/>\n    uint16_t Port() { return _port; }<br \/>\n    std::string Ip() { return _ip; }<\/p>\n<p>private:<br \/>\n    struct sockaddr_in _addr;<br \/>\n    std::string _ip;<br \/>\n    uint16_t _port;<br \/>\n};<\/p>\n<p>\u4e24\u4e2a\u8f6c\u6362\u51fd\u6570\u6b63\u597d\u548c\u53d1\u9001\u65f6\u76f8\u53cd&#xff1a;<\/p>\n<ul>\n<li>\n<p>ntohs&#xff1a;network to host short&#xff0c;\u7aef\u53e3\u53f7\u7ffb\u8bd1\u56de\u672c\u673a\u683c\u5f0f&#xff1b;<\/p>\n<\/li>\n<li>\n<p>inet_ntoa&#xff1a;\u628a 4 \u5b57\u8282\u7f51\u7edc\u5730\u5740\u8f6c\u6210\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32&#xff08;n \u662f network&#xff0c;a \u662f ASCII&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6ce8\u610f\u6210\u5458 _addr \u662f<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u6309\u503c\u62f7\u8d1d<\/span><\/span>\u7684&#xff0c;\u4e0d\u662f\u5f15\u7528\u3002\u6784\u9020\u65f6\u5c31\u628a\u8fd9\u4efd\u5730\u5740\u5feb\u7167\u5b58\u4e0b\u6765&#xff0c;\u4e4b\u540e\u5c31\u7b97\u5916\u9762\u7684 peer \u88ab\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u8986\u76d6\u4e86&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u91cc\u7559\u7684\u8fd8\u662f\u5f53\u65f6\u90a3\u4f4d\u5ba2\u4eba\u7684\u5730\u5740&#xff0c;\u5f88\u7a33\u59a5\u3002<\/p>\n<hr \/>\n<h4 id=\"2.4-\u5e97\u957f\u767b\u573a\uff1audpserver.cc-\u600e\u4e48\u628a\u96f6\u4ef6\u62fc\u8d77\u6765\">2.4 \u5e97\u957f\u767b\u573a&#xff1a;UdpServer.cc \u600e\u4e48\u628a\u96f6\u4ef6\u62fc\u8d77\u6765<\/h4>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 2)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; port&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<br \/>\n    Enable_Console_Log_Strategy();<\/p>\n<p>    Dict dict;<br \/>\n    if (!dict.LoadDict())<br \/>\n    {<br \/>\n        LOG(LogLevel::FATAL) &lt;&lt; &#034;\u5b57\u5178\u52a0\u8f7d\u5931\u8d25&#xff0c;\u670d\u52a1\u5668\u9000\u51fa&#034;;<br \/>\n        return 2;<br \/>\n    }<\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(<br \/>\n        port,<br \/>\n        [&amp;dict](const std::string &amp;word, InetAddr &amp;cli) -&gt; std::string {<br \/>\n            return dict.Translate(word, cli);<br \/>\n        });<br \/>\n    usvr-&gt;Init();<br \/>\n    usvr-&gt;Start();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u5e97\u957f\u5f00\u5e97\u5206\u4e09\u6b65&#xff1a;<span style=\"color:#fe2c24\">\u628a\u8bcd\u6c47\u624b\u518c\u7ffb\u597d&#xff08;\u52a0\u8f7d\u8bcd\u5178&#xff09;\u2192 \u96c7\u597d\u524d\u53f0&#xff08;\u521b\u5efa\u670d\u52a1\u5668&#xff0c;\u5e76\u544a\u8bc9\u5b83&#034;\u6536\u5230\u5355\u8bcd\u4ea4\u7ed9\u8bcd\u5178\u7ffb\u8bd1&#034;&#xff09;\u2192 \u5f00\u95e8\u8425\u4e1a\u3002<\/span><\/p>\n<p>\u91cd\u70b9\u770b\u8fd9\u4e2a lambda\u3002UdpServer \u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4e00\u4e2a&#034;\u5403\u5b57\u7b26\u4e32\u3001\u5410\u5b57\u7b26\u4e32&#034;\u7684\u51fd\u6570&#xff0c;\u8fd9\u91cc\u7528\u4e00\u4e2a\u533f\u540d\u51fd\u6570&#xff08;lambda&#xff09;\u5c31\u5730\u5199\u597d&#xff1a;<\/p>\n<p>[&amp;dict](const std::string &amp;word, InetAddr &amp;cli) -&gt; std::string {<br \/>\n    return dict.Translate(word, cli);<br \/>\n}<\/p>\n<ul>\n<li>\n<p>[&amp;dict] \u8868\u793a\u6309\u5f15\u7528\u6355\u83b7\u5916\u9762\u7684\u8bcd\u5178\u5bf9\u8c61&#xff0c;lambda \u5185\u90e8\u5c31\u80fd\u76f4\u63a5\u7528\u5b83&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668\u6bcf\u6536\u5230\u4e00\u4e2a\u5355\u8bcd&#xff0c;\u5c31\u56de\u8c03\u4e00\u6b21\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7ed5\u8fd9\u4e00\u5c42&#xff0c;\u4e0d\u76f4\u63a5\u5728\u670d\u52a1\u5668\u91cc #include &#034;Dict.hpp&#034; \u7136\u540e\u67e5\u8bcd\u5178&#xff1f;\u56e0\u4e3a\u90a3\u6837\u7f51\u7edc\u4ee3\u7801\u548c\u4e1a\u52a1\u4ee3\u7801\u5c31\u710a\u6b7b\u4e86&#xff0c;\u4ee5\u540e\u60f3\u628a&#034;\u7ffb\u8bd1&#034;\u6362\u6210&#034;\u8ba1\u7b97\u5e73\u65b9&#034;&#xff0c;\u5f97\u52a8\u670d\u52a1\u5668\u7684\u4e94\u810f\u5e99\u3002\u73b0\u5728\u7684\u5206\u5de5\u662f&#xff1a;\u670d\u52a1\u5668\u53ea\u61c2\u5feb\u9012&#xff0c;\u8bcd\u5178\u53ea\u61c2\u7ffb\u8bd1&#xff0c;\u4e2d\u95f4\u7528\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u4e00\u6837\u7684\u4e1c\u897f\u8fde\u63a5&#xff0c;\u5404\u81ea\u80fd\u72ec\u7acb\u751f\u957f\u3002\u8fd9\u5c31\u662f\u89e3\u8026\u3002<\/p>\n<hr \/>\n<h4 id=\"2.5-\u5ba2\u6237\u7aef\uff1a\u4e00\u4e2a\u7b49\u56de\u4fe1\u7684\u987e\u5ba2\">2.5 \u5ba2\u6237\u7aef&#xff1a;\u4e00\u4e2a\u7b49\u56de\u4fe1\u7684\u987e\u5ba2<\/h4>\n<p>UdpClient.cc \u7684\u6838\u5fc3\u5faa\u73af&#xff1a;<\/p>\n<p>while(true)<br \/>\n{<br \/>\n    std::string input;<br \/>\n    std::cout &lt;&lt; &#034;Please Enter# &#034; &lt;&lt; std::flush;<br \/>\n    if (!std::getline(std::cin, input))   \/\/ Ctrl&#043;D&#xff1a;\u8f93\u5165\u7ed3\u675f<br \/>\n        break;<br \/>\n    if (input.empty())                   \/\/ \u7a7a\u884c\u4e0d\u53d1<br \/>\n        continue;<\/p>\n<p>    int n &#061; sendto(sockfd, input.c_str(), input.size(), 0,<br \/>\n                   (struct sockaddr*)&amp;server, sizeof(server));<br \/>\n    (void)n;<\/p>\n<p>    char buffer[1024];<br \/>\n    struct sockaddr_in peer;<br \/>\n    socklen_t len &#061; sizeof(peer);<br \/>\n    int m &#061; recvfrom(sockfd, buffer, sizeof(buffer)-1, 0,<br \/>\n                     (struct sockaddr*)&amp;peer, &amp;len);<br \/>\n    if(m &gt; 0)<br \/>\n    {<br \/>\n        buffer[m] &#061; 0;<br \/>\n        std::cout &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5ba2\u6237\u7aef\u540c\u6837\u8981 socket \u7533\u8bf7\u5bf9\u8bb2\u673a&#xff0c;\u4f46\u53ea\u586b\u670d\u52a1\u5668\u5730\u5740&#xff0c;\u4e0d bind&#xff1a;<\/p>\n<p>struct sockaddr_in server;<br \/>\nmemset(&amp;server, 0, sizeof(server));<br \/>\nserver.sin_family &#061; AF_INET;<br \/>\nserver.sin_port &#061; htons(server_port);<br \/>\nserver.sin_addr.s_addr &#061; inet_addr(server_ip.c_str());<\/p>\n<p>inet_addr \u628a &#034;127.0.0.1&#034; \u76f4\u63a5\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f\u7684 4 \u5b57\u8282\u6574\u6570&#xff0c;\u6240\u4ee5\u4e0d\u7528\u518d htonl\u3002\u4e4b\u540e\u7b2c\u4e00\u6b21 sendto \u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u7ed9\u5ba2\u6237\u7aef\u5206\u914d IP \u548c\u968f\u673a\u7aef\u53e3\u2014\u2014\u8fd9\u5c31\u662f\u524d\u9762\u8bf4\u7684&#034;\u987e\u5ba2\u4e0d\u9700\u8981\u56fa\u5b9a\u644a\u4f4d&#034;\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e24\u884c\u662f\u8e29\u5751\u540e\u957f\u51fa\u6765\u7684&#034;\u62a4\u680f&#034;&#xff0c;\u503c\u5f97\u4e13\u95e8\u8bb2&#xff1a;<\/p>\n<p>if (!std::getline(&#8230;)) break;&#xff1a;\u5f53\u4f60\u6309 Ctrl&#043;D&#xff08;\u8f93\u5165\u6d41\u7ed3\u675f&#xff09;&#xff0c;getline \u4f1a\u5931\u8d25\u3002\u5982\u679c\u4e0d\u68c0\u67e5&#xff0c;\u5faa\u73af\u91cc input \u4e00\u76f4\u662f\u7a7a\u7684&#xff0c;\u7a0b\u5e8f\u5c31\u4f1a\u539f\u5730\u75af\u72c2\u7a7a\u8f6c\u3001\u5237\u5c4f&#xff0c;\u5b9e\u6d4b\u80fd\u77ac\u95f4\u5237\u51fa\u51e0\u5341 MB \u7684\u8f93\u51fa\u3002<\/p>\n<p>if (input.empty()) continue;&#xff1a;\u76f4\u63a5\u6309\u56de\u8f66\u4f1a\u4ea7\u751f\u7a7a\u5b57\u7b26\u4e32\u3002\u66f4\u9690\u853d\u7684\u5751\u662f\u2014\u2014\u7a7a\u5b57\u7b26\u4e32\u4e5f\u4f1a\u89e6\u53d1 sendto&#xff0c;\u53d1\u51fa\u4e00\u4e2a 0 \u5b57\u8282\u7684 UDP \u5305\u88f9&#xff1b;\u670d\u52a1\u5668\u90a3\u8fb9 recvfrom \u8fd4\u56de 0&#xff0c;\u4ee3\u7801\u5224\u65ad if (s &gt; 0) \u4e0d\u6210\u7acb&#xff0c;\u4e8e\u662f\u65e2\u4e0d\u5904\u7406\u4e5f\u4e0d\u56de\u590d&#xff0c;\u5ba2\u6237\u7aef\u5c31\u4f1a\u6c38\u8fdc\u5835\u5728 recvfrom \u7b49\u4e00\u5c01\u6c38\u8fdc\u4e0d\u4f1a\u6765\u7684\u56de\u4fe1\u3002\u6240\u4ee5\u7a7a\u884c\u5e72\u8106\u4e0d\u53d1\u3002<\/p>\n<p>\u53e6\u5916 std::cout &lt;&lt; &#8230; &lt;&lt; std::flush \u7528 flush \u800c\u4e0d\u662f endl&#xff1a;\u53ea\u9700\u8981\u8ba9\u63d0\u793a\u7b26\u7acb\u523b\u8e66\u51fa\u6765&#xff0c;\u5149\u6807\u505c\u5728 # \u540e\u9762\u7b49\u4f60\u6253\u5b57&#xff0c;\u4e0d\u9700\u8981\u6362\u884c\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e09\u3001\u8bcd\u5178\u4e1a\u52a1\u6a21\u5757\uff1a\u7ffb\u8bd1\u529f\u80fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\">\u4e09\u3001\u8bcd\u5178\u4e1a\u52a1\u6a21\u5757&#xff1a;\u7ffb\u8bd1\u529f\u80fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684<\/h3>\n<h4 id=\"3.1-\u8bcd\u5e93\u957f\u4ec0\u4e48\u6837\uff1adictionary.txt\">3.1 \u8bcd\u5e93\u957f\u4ec0\u4e48\u6837&#xff1a;dictionary.txt<\/h4>\n<p>apple: \u82f9\u679c<br \/>\nbanana: \u9999\u8549<br \/>\ncat: \u732b<br \/>\ndog: \u72d7<br \/>\nbook: \u4e66<br \/>\npen: \u7b14<br \/>\nhappy: \u5feb\u4e50\u7684<br \/>\nsad: \u60b2\u4f24\u7684<br \/>\nrun: \u8dd1<br \/>\njump: \u8df3<br \/>\nteacher: \u8001\u5e08<br \/>\nstudent: \u5b66\u751f<br \/>\ncar: \u6c7d\u8f66<br \/>\nbus: \u516c\u4ea4\u8f66<br \/>\nlove: \u7231<br \/>\nhate: \u6068<br \/>\nhello: \u4f60\u597d<br \/>\ngoodbye: \u518d\u89c1<br \/>\nsummer: \u590f\u5929<br \/>\nwinter: \u51ac\u5929<\/p>\n<p>20 \u4e2a\u8bcd\u6761&#xff0c;\u6bcf\u884c\u4e00\u6761&#xff0c;\u683c\u5f0f\u7b80\u5355\u7c97\u66b4&#xff1a;\u82f1\u6587 &#043; \u5192\u53f7\u7a7a\u683c &#043; \u4e2d\u6587\u3002\u6ce8\u610f\u5206\u9694\u7b26\u662f &#034;: &#034;&#xff08;\u5192\u53f7\u5e26\u4e00\u4e2a\u7a7a\u683c&#xff09;&#xff0c;\u89e3\u6790\u65f6\u8981\u6309\u8fd9\u4e24\u4e2a\u5b57\u7b26\u5207\u3002<\/p>\n<hr \/>\n<h4 id=\"3.2-dict.hpp\uff1a\u52a0\u8f7d\u548c\u67e5\u8be2\">3.2 Dict.hpp&#xff1a;\u52a0\u8f7d\u548c\u67e5\u8be2<\/h4>\n<p>\u6570\u636e\u7ed3\u6784\u9009\u578b\u51e0\u4e4e\u4e0d\u7528\u60f3&#xff1a;\u82f1\u6587\u5230\u4e2d\u6587\u7684\u4e00\u4e00\u5bf9\u5e94&#xff0c;unordered_map \u5929\u9009\u4e4b\u5b50&#xff0c;\u67e5\u8bcd\u5e73\u5747 O(1)&#xff1a;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>class Dict<br \/>\n{<br \/>\npublic:<br \/>\n    Dict(const std::string &amp;path &#061; defaultdict) : _dict_path(path) {}<\/p>\n<p>    bool LoadDict()<br \/>\n    {<br \/>\n        std::ifstream in(_dict_path);<br \/>\n        if (!in.is_open())<br \/>\n        {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;\u6253\u5f00\u5b57\u5178: &#034; &lt;&lt; _dict_path &lt;&lt; &#034; \u9519\u8bef&#034;;<br \/>\n            return false;<br \/>\n        }<br \/>\n        std::string line;<br \/>\n        while (std::getline(in, line))<br \/>\n        {<br \/>\n            auto pos &#061; line.find(sep);<br \/>\n            if (pos &#061;&#061; std::string::npos)<br \/>\n            {<br \/>\n                LOG(LogLevel::WARNING) &lt;&lt; &#034;\u89e3\u6790: &#034; &lt;&lt; line &lt;&lt; &#034; \u5931\u8d25&#034;;<br \/>\n                continue;<br \/>\n            }<br \/>\n            std::string english &#061; line.substr(0, pos);<br \/>\n            std::string chinese &#061; line.substr(pos &#043; sep.size());<br \/>\n            if (english.empty() || chinese.empty())<br \/>\n            {<br \/>\n                LOG(LogLevel::WARNING) &lt;&lt; &#034;\u6ca1\u6709\u6709\u6548\u5185\u5bb9: &#034; &lt;&lt; line;<br \/>\n                continue;<br \/>\n            }<br \/>\n            _dict.insert(std::make_pair(english, chinese));<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;\u52a0\u8f7d: &#034; &lt;&lt; line;<br \/>\n        }<br \/>\n        in.close();<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>\u9010\u884c\u8bfb\u6587\u4ef6&#xff0c;\u7136\u540e\u4e00\u884c\u91cc\u505a&#034;\u5207\u86cb\u7cd5&#034;&#xff1a;<\/p>\n<ul>\n<li>\n<p>line.find(&#034;: &#034;) \u627e\u5230\u5206\u9694\u7b26\u7684\u4f4d\u7f6e&#xff1b;\u627e\u4e0d\u5230\u8fd4\u56de npos&#xff08;\u53ef\u4ee5\u7406\u89e3\u6210&#034;\u65e0\u5c3d\u5934&#034;\u7684\u54e8\u5175\u503c&#xff09;&#xff0c;\u8fd9\u884c\u662f\u574f\u6570\u636e&#xff0c;\u8bb0\u4e00\u6761 WARNING \u8df3\u8fc7&#xff0c;\u4e0d\u8ba9\u4e00\u9897\u8001\u9f20\u5c4e\u641e\u5d29\u6574\u4e2a\u542f\u52a8\u8fc7\u7a0b&#xff1b;<\/p>\n<\/li>\n<li>\n<p>substr(0, pos) \u53d6\u5192\u53f7\u524d\u534a\u6bb5\u82f1\u6587&#xff1b;<\/p>\n<\/li>\n<li>\n<p>substr(pos &#043; sep.size()) \u53d6\u5192\u53f7\u7a7a\u683c\u4e4b\u540e\u7684\u5168\u90e8\u5185\u5bb9\u5f53\u4e2d\u6587\u2014\u2014\u6240\u4ee5\u4e2d\u6587\u91ca\u4e49\u91cc\u5c31\u7b97\u5e26\u7a7a\u683c\u4e5f\u4e0d\u6015&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u4e24\u534a\u4efb\u610f\u4e00\u4e2a\u662f\u7a7a\u7684\u4e5f\u8df3\u8fc7\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u79cd&#034;\u52a0\u8f7d\u65f6\u5bb9\u9519&#034;\u7684\u610f\u8bc6\u5f88\u91cd\u8981&#xff1a;\u6587\u4ef6\u662f\u53ef\u80fd\u88ab\u624b\u6539\u574f\u7684&#xff0c;\u7a0b\u5e8f\u4e0d\u80fd\u56e0\u4e3a\u67d0\u4e00\u884c\u683c\u5f0f\u9519\u4e86\u5c31\u8d4c\u6c14\u4e0d\u5e72\u3002<\/p>\n<p>\u67e5\u8be2\u90e8\u5206&#xff1a;<\/p>\n<p>    std::string Translate(const std::string &amp;word, InetAddr &amp;client)<br \/>\n    {<br \/>\n        auto iter &#061; _dict.find(word);<br \/>\n        if (iter &#061;&#061; _dict.end())<br \/>\n        {<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;\u8fdb\u5165\u5230\u4e86\u7ffb\u8bd1\u6a21\u5757, [&#034;<br \/>\n                &lt;&lt; client.Ip() &lt;&lt; &#034; : &#034; &lt;&lt; client.Port() &lt;&lt; &#034;]# &#034;<br \/>\n                &lt;&lt; word &lt;&lt; &#034;-&gt;None&#034;;<br \/>\n            return &#034;None&#034;;<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;\u8fdb\u5165\u5230\u4e86\u7ffb\u8bd1\u6a21\u5757, [&#034;<br \/>\n            &lt;&lt; client.Ip() &lt;&lt; &#034; : &#034; &lt;&lt; client.Port() &lt;&lt; &#034;]# &#034;<br \/>\n            &lt;&lt; word &lt;&lt; &#034;-&gt;&#034; &lt;&lt; iter-&gt;second;<br \/>\n        return iter-&gt;second;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    std::string _dict_path;<br \/>\n    std::unordered_map&lt;std::string, std::string&gt; _dict;<br \/>\n};<\/p>\n<p>find \u6ca1\u67e5\u5230\u65f6\u8fd4\u56de end()&#xff0c;\u6211\u4eec\u4e0d\u629b\u5f02\u5e38\u3001\u4e0d\u5d29\u6e83&#xff0c;\u53cb\u597d\u5730\u8fd4\u56de\u5b57\u7b26\u4e32 &#034;None&#034;&#xff0c;\u5ba2\u6237\u7aef\u7167\u539f\u6837\u663e\u793a\u5c31\u884c\u3002\u540c\u65f6\u5229\u7528\u4f20\u8fdb\u6765\u7684 client \u5730\u5740\u8bb0\u4e00\u6761\u65e5\u5fd7\u2014\u2014<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e1a\u52a1\u5c42\u5c45\u7136\u77e5\u9053\u662f\u54ea\u4e2a\u5ba2\u4eba\u5728\u67e5\u8bcd<\/span><\/span>&#xff0c;\u8fd9\u5c31\u662f\u56de\u8c03\u7b7e\u540d\u91cc\u5e26 InetAddr&amp; \u7684\u4ef7\u503c\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u5de5\u7a0b\u7ec6\u8282&#xff1a;LoadDict() \u7684\u8fd4\u56de\u503c\u5728 main \u91cc\u662f\u88ab\u8ba4\u771f\u68c0\u67e5\u7684&#xff0c;\u8bcd\u5178\u6587\u4ef6\u4e22\u4e86\u5c31 FATAL \u9000\u51fa\u3002\u5426\u5219\u670d\u52a1\u5668&#034;\u6210\u529f\u542f\u52a8&#034;\u5374\u5bf9\u6240\u6709\u5355\u8bcd\u90fd\u56de None&#xff0c;\u6392\u67e5\u8d77\u6765\u80fd\u8ba9\u4f60\u6000\u7591\u4eba\u751f\u3002<\/p>\n<hr \/>\n<h3 id=\"\u56db\u3001\u65e5\u5fd7\u6a21\u5757\uff1a\u8ba9\u7a0b\u5e8f\u5b66\u4f1a&quot;\u5199\u65e5\u8bb0&quot;\">\u56db\u3001\u65e5\u5fd7\u6a21\u5757&#xff1a;\u8ba9\u7a0b\u5e8f\u5b66\u4f1a&#034;\u5199\u65e5\u8bb0&#034;<\/h3>\n<p>\u670d\u52a1\u5668\u4e00\u8dd1\u5c31\u662f\u6b7b\u5faa\u73af&#xff0c;\u51fa\u4e86\u95ee\u9898\u9760\u4ec0\u4e48\u8fd8\u539f\u73b0\u573a&#xff1f;\u9760\u65e5\u5fd7\u3002\u8fd9\u4e2a\u9879\u76ee\u6ca1\u6709\u7528\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93&#xff0c;\u81ea\u5df1\u624b\u5199\u4e86\u4e00\u4e2a\u8fd8\u633a\u50cf\u6837\u7684\u65e5\u5fd7\u7cfb\u7edf\u3002<\/p>\n<hr \/>\n<h4 id=\"4.1-\u7b56\u7565\u6a21\u5f0f\uff1a\u65e5\u5fd7\u60f3\u6253\u54ea\u513f\u5c31\u6253\u54ea\u513f\">4.1 \u7b56\u7565\u6a21\u5f0f&#xff1a;\u65e5\u5fd7\u60f3\u6253\u54ea\u513f\u5c31\u6253\u54ea\u513f<\/h4>\n<p>\u5148\u770b\u9876\u5c42\u8bbe\u8ba1\u3002\u65e5\u5fd7\u7684\u53bb\u5411\u53ef\u80fd\u662f\u5c4f\u5e55&#xff0c;\u4e5f\u53ef\u80fd\u662f\u6587\u4ef6&#xff0c;\u660e\u5929\u4e5f\u8bb8\u60f3\u53d1\u5230\u7f51\u7edc\u3002Log.hpp \u7528\u7b56\u7565\u6a21\u5f0f\u5904\u7406\u8fd9\u79cd\u53d8\u5316&#xff1a;\u5b9a\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u5f53&#034;\u63a5\u53e3&#034;&#xff0c;\u518d\u6d3e\u751f\u51fa\u5177\u4f53\u5b9e\u73b0&#xff1a;<\/p>\n<p>class LogStrategy<br \/>\n{<br \/>\npublic:<br \/>\n    ~LogStrategy() &#061; default;<br \/>\n    virtual void SyncLog(const std::string &amp;message) &#061; 0;<br \/>\n};<\/p>\n<p>\u5c4f\u5e55\u7b56\u7565\u7684\u6838\u5fc3\u5c31\u4e24\u884c&#xff08;\u52a0\u9501 &#043; \u8f93\u51fa&#xff09;&#xff1a;<\/p>\n<p>void SyncLog(const std::string &amp;message) override<br \/>\n{<br \/>\n    LockGuard lockguard(_mutex);<br \/>\n    std::cout &lt;&lt; message &lt;&lt; gsep &lt;&lt; std::flush;<br \/>\n}<\/p>\n<p>\u6587\u4ef6\u7b56\u7565\u5219\u8d1f\u8d23&#034;\u786e\u4fdd\u76ee\u5f55\u5b58\u5728 &#043; \u8ffd\u52a0\u6253\u5f00\u6587\u4ef6 &#043; \u5199\u5165&#034;&#xff1a;<\/p>\n<p>if (std::filesystem::exists(_path)) return;<br \/>\nstd::filesystem::create_directories(_path);   \/\/ C&#043;&#043;17&#xff0c;\u76ee\u5f55\u4e0d\u5b58\u5728\u5c31\u9012\u5f52\u5efa<br \/>\n&#8230;<br \/>\nstd::ofstream out(filename, std::ios::app);   \/\/ app &#061; \u8ffd\u52a0&#xff0c;\u4e0d\u8986\u76d6\u5386\u53f2<br \/>\nout &lt;&lt; message &lt;&lt; gsep;<\/p>\n<p>\u7ba1\u7406\u65e5\u5fd7\u7684 Logger \u624b\u91cc\u6525\u7740\u4e00\u4e2a\u53ef\u4ee5\u968f\u65f6\u66ff\u6362\u7684\u6307\u9488&#xff1a;<\/p>\n<p>std::unique_ptr&lt;LogStrategy&gt; _fflush_strategy;<\/p>\n<p>\u60f3\u6362\u8f93\u51fa\u53bb\u5411&#xff1f;\u8ba9\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u53e6\u4e00\u4e2a\u7b56\u7565\u5bf9\u8c61\u5c31\u884c&#xff0c;\u8c03\u7528\u65b9\u5b8c\u5168\u65e0\u611f\u3002\u8fd9\u5c31\u50cf\u5e97\u91cc\u7684\u6444\u50cf\u5934&#xff0c;\u4eca\u5929\u63a5\u76d1\u89c6\u5668\u3001\u660e\u5929\u63a5\u5f55\u50cf\u673a&#xff0c;\u6444\u50cf\u5934\u672c\u8eab\u4e0d\u7528\u6362\u3002<\/p>\n<p>\u4e00\u6761\u5b8c\u6574\u65e5\u5fd7\u957f\u8fd9\u6837&#xff1a;<\/p>\n<p>[2026-09-13 17:27:41] [DEBUG] [952447] [Dict.hpp] [62] &#8211; apple-&gt;\u82f9\u679c<\/p>\n<p>\u65f6\u95f4\u3001\u7b49\u7ea7\u3001\u8fdb\u7a0b\u53f7\u3001\u6e90\u6587\u4ef6\u3001\u884c\u53f7\u3001\u6b63\u6587&#xff0c;\u516d\u8981\u7d20\u9f50\u5168\u3002\u65f6\u95f4\u6233\u7528 localtime_r&#xff08;\u7ebf\u7a0b\u5b89\u5168\u7248\u672c&#xff0c;_r \u5c31\u662f reentrant \u53ef\u91cd\u5165\u7684\u610f\u601d&#xff09;&#xff0c;\u8fd8\u6709\u4e24\u4e2a\u7ecf\u5178\u51b7\u77e5\u8bc6&#xff1a;tm_year \u8981\u52a0 1900\u3001tm_mon \u4ece 0 \u5f00\u59cb\u6240\u4ee5\u8981\u52a0 1\u3002<\/p>\n<hr \/>\n<h4 id=\"4.2-\u6d41\u5f0f\u65e5\u5fd7\u7684\u9b54\u6cd5\uff1a\u4e3a\u4ec0\u4e48\u80fd-log(info)-&lt;&lt;-&quot;x&quot;-&lt;&lt;-3\uff1f\">4.2 \u6d41\u5f0f\u65e5\u5fd7\u7684\u9b54\u6cd5&#xff1a;\u4e3a\u4ec0\u4e48\u80fd LOG(INFO) &lt;&lt; &#034;x&#034; &lt;&lt; 3&#xff1f;<\/h4>\n<p>\u8fd9\u662f\u6574\u4e2a\u65e5\u5fd7\u7cfb\u7edf\u6700\u7cbe\u5de7\u7684\u5730\u65b9\u3002\u7528\u6cd5\u5927\u5bb6\u90fd\u719f&#xff1a;<\/p>\n<p>LOG(LogLevel::INFO) &lt;&lt; &#034;bind success, sockfd : &#034; &lt;&lt; _sockfd;<\/p>\n<p>\u53ef\u8fd9\u80cc\u540e\u662f\u600e\u4e48\u5de5\u4f5c\u7684&#xff1f;\u62c6\u5f00\u770b\u5b8f&#xff1a;<\/p>\n<p>#define LOG(level) logger(level, __FILE__, __LINE__)<\/p>\n<p>__FILE__ \u548c __LINE__ \u662f\u7f16\u8bd1\u5668\u5185\u5efa\u5b8f&#xff0c;\u81ea\u52a8\u53d8\u6210\u672c\u6587\u4ef6\u540d\u548c\u5f53\u524d\u884c\u53f7&#xff0c;\u6240\u4ee5\u6bcf\u6761\u65e5\u5fd7\u90fd\u80fd\u7cbe\u786e\u5b9a\u4f4d\u3002logger(&#8230;) \u8fd4\u56de\u4e00\u4e2a<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e34\u65f6 <\/span><span style=\"background-color:#ffd900\">LogMessage<\/span><span style=\"background-color:#ffd900\"> \u5bf9\u8c61<\/span><\/span>&#xff0c;\u5b83\u5728\u6784\u9020\u65f6\u5148\u62fc\u597d\u65e5\u5fd7\u7684\u5de6\u534a\u8fb9&#xff08;\u65f6\u95f4\u3001\u7b49\u7ea7\u90a3\u4e9b\u56fa\u5b9a\u4fe1\u606f&#xff09;&#xff1a;<\/p>\n<p>LogMessage(LogLevel &amp;level, std::string &amp;src_name, int line_number, Logger &amp;logger)<br \/>\n{<br \/>\n    std::stringstream ss;<br \/>\n    ss &lt;&lt; &#034;[&#034; &lt;&lt; _curr_time &lt;&lt; &#034;] &#034;<br \/>\n       &lt;&lt; &#034;[&#034; &lt;&lt; Level2Str(_level) &lt;&lt; &#034;] &#034;<br \/>\n       &lt;&lt; &#034;[&#034; &lt;&lt; _pid &lt;&lt; &#034;] &#034;<br \/>\n       &lt;&lt; &#034;[&#034; &lt;&lt; _src_name &lt;&lt; &#034;] &#034;<br \/>\n       &lt;&lt; &#034;[&#034; &lt;&lt; _line_number &lt;&lt; &#034;] &#8211; &#034;;<br \/>\n    _loginfo &#061; ss.str();<br \/>\n}<\/p>\n<p>\u7136\u540e\u9760\u6a21\u677f operator&lt;&lt; \u628a\u53f3\u8fb9\u7684\u5185\u5bb9\u4e00\u5757\u5757\u6512\u8fdb _loginfo&#xff1a;<\/p>\n<p>template &lt;typename T&gt;<br \/>\nLogMessage &amp;operator&lt;&lt;(const T &amp;info)<br \/>\n{<br \/>\n    std::stringstream ss;<br \/>\n    ss &lt;&lt; info;<br \/>\n    _loginfo &#043;&#061; ss.str();<br \/>\n    return *this;                 \/\/ \u8fd4\u56de\u81ea\u5df1&#xff0c;\u624d\u80fd\u94fe\u5f0f &lt;&lt; a &lt;&lt; b &lt;&lt; c<br \/>\n}<\/p>\n<p>\u6700\u5999\u7684\u662f\u8f93\u51fa\u65f6\u673a\u2014\u2014<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5728\u6790\u6784\u51fd\u6570\u91cc<\/span><span style=\"background-color:#ffd900\">&#xff1a;<\/span><\/span><\/p>\n<p>~LogMessage()<br \/>\n{<br \/>\n    if (_logger._fflush_strategy)<br \/>\n        _logger._fflush_strategy-&gt;SyncLog(_loginfo);<br \/>\n}<\/p>\n<p>\u4e34\u65f6\u5bf9\u8c61\u5728\u6574\u6761\u8bed\u53e5\u7ed3\u675f\u65f6\u6790\u6784&#xff0c;&#034;\u556a&#034;\u4e00\u4e0b\u628a\u6512\u597d\u7684\u5b8c\u6574\u65e5\u5fd7\u4ea4\u51fa\u53bb\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u6210&#xff1a;\u6bcf\u6b21\u5199\u65e5\u5fd7\u90fd\u662f\u9886\u4e86\u4e00\u5f20\u4fbf\u5229\u8d34&#xff0c;\u968f\u624b\u5f80\u4e0a\u8bb0\u5185\u5bb9&#xff0c;\u4e0b\u73ed&#xff08;\u6790\u6784&#xff09;\u65f6\u81ea\u52a8\u628a\u4fbf\u5229\u8d34\u8d34\u5230\u516c\u544a\u680f\u4e0a&#xff0c;\u5168\u7a0b\u4e0d\u7528\u4f60\u624b\u52a8\u558a&#034;\u63d0\u4ea4&#034;\u3002<\/p>\n<hr \/>\n<h4 id=\"4.3-mutex.hpp\uff1araii-\u98ce\u683c\u7684\u81ea\u52a8\u9501\">4.3 Mutex.hpp&#xff1a;RAII \u98ce\u683c\u7684\u81ea\u52a8\u9501<\/h4>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e3a\u4ec0\u4e48\u65e5\u5fd7\u9700\u8981\u9501&#xff1f;<\/span><\/span>\u5982\u679c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6253\u65e5\u5fd7&#xff0c;\u5b83\u4eec\u7684\u5b57\u7b26\u53ef\u80fd\u4f60\u4e00\u4e2a\u6211\u4e00\u4e2a\u5730\u4ea4\u53c9\u5199&#xff0c;\u5c4f\u5e55\u4e0a\u5c31\u4e71\u6210\u7ca5\u4e86\u3002\u6240\u4ee5&#034;\u5199&#034;\u8fd9\u4e2a\u52a8\u4f5c\u5fc5\u987b\u6392\u961f\u3002<\/p>\n<p>\u9879\u76ee\u6ca1\u6709\u76f4\u63a5\u88f8\u7528 pthread \u51fd\u6570&#xff0c;\u800c\u662f\u5148\u5305\u4e00\u5c42 Mutex&#xff1a;<\/p>\n<p>class Mutex<br \/>\n{<br \/>\npublic:<br \/>\n    Mutex()  { pthread_mutex_init(&amp;_mutex, nullptr); }<br \/>\n    void Lock()   { pthread_mutex_lock(&amp;_mutex); }<br \/>\n    void Unlock() { pthread_mutex_unlock(&amp;_mutex); }<br \/>\n    ~Mutex() { pthread_mutex_destroy(&amp;_mutex); }<br \/>\nprivate:<br \/>\n    pthread_mutex_t _mutex;<br \/>\n};<\/p>\n<p>\u518d\u914d\u4e00\u4e2a LockGuard&#xff1a;<\/p>\n<p>class LockGuard<br \/>\n{<br \/>\npublic:<br \/>\n    LockGuard(Mutex &amp;mutex) : _mutex(mutex) { _mutex.Lock(); }<br \/>\n    ~LockGuard() { _mutex.Unlock(); }<br \/>\nprivate:<br \/>\n    Mutex &amp;_mutex;<br \/>\n};<\/p>\n<p>\u8fd9\u5c31\u662f <span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">RAII<\/span><\/span>&#xff08;\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316&#xff09;&#xff1a;\u8fdb\u95e8\u65f6\u6784\u9020\u5bf9\u8c61\u81ea\u52a8\u4e0a\u9501&#xff0c;\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u5bf9\u8c61\u6790\u6784\u81ea\u52a8\u89e3\u9501\u3002\u54ea\u6015\u4e2d\u95f4 return \u751a\u81f3\u629b\u5f02\u5e38&#xff0c;\u9501\u90fd\u4e0d\u4f1a\u5fd8\u89e3\u2014\u2014\u7f16\u8bd1\u5668\u5e2e\u4f60\u515c\u5e95\u3002\u6210\u5458\u662f\u5f15\u7528 Mutex&amp;&#xff0c;\u8868\u793a\u5b83\u53ea\u662f&#034;\u501f\u7528&#034;\u5916\u9762\u90a3\u628a\u9501&#xff0c;\u81ea\u5df1\u4e0d\u62e5\u6709\u9501\u3002<\/p>\n<hr \/>\n<h4 id=\"4.4-\u4e00\u4e2a\u85cf\u5f97\u5f88\u6df1\u7684\u5751\uff1a\u65e5\u5fd7\u4e3a\u4ec0\u4e48\u5199\u4e0d\u8fdb\u6587\u4ef6\uff1f\">4.4 \u4e00\u4e2a\u85cf\u5f97\u5f88\u6df1\u7684\u5751&#xff1a;\u65e5\u5fd7\u4e3a\u4ec0\u4e48\u5199\u4e0d\u8fdb\u6587\u4ef6&#xff1f;<\/h4>\n<p>\u8fd9\u4e2a\u9879\u76ee\u91cc\u65e5\u5fd7\u8f93\u51fa\u7528\u7684\u662f&#xff1a;<\/p>\n<p>std::cout &lt;&lt; message &lt;&lt; gsep &lt;&lt; std::flush;<\/p>\n<p>gsep \u662f &#034;\\\\r\\\\n&#034;\u3002\u65b0\u624b\u5bb9\u6613\u5ffd\u7565\u6700\u540e\u90a3\u4e2a std::flush\u3002C&#043;&#043; \u7684\u8f93\u51fa\u662f\u5e26\u7f13\u51b2\u7684&#xff1a;\u5728\u7ec8\u7aef\u91cc\u8dd1\u901a\u5e38\u662f&#034;\u884c\u7f13\u51b2&#034;&#xff0c;\u9047\u5230\u6362\u884c\u8fd8\u80fd\u5237\u51fa\u6765&#xff1b;\u53ef\u4e00\u65e6\u4f60\u628a\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6&#xff08;.\/udpserver 8080 &gt; log.txt&#xff09;&#xff0c;\u5c31\u53d8\u6210&#034;\u5168\u7f13\u51b2&#034;&#xff0c;\u5185\u5bb9\u4f1a\u5728\u7f13\u51b2\u533a\u91cc\u6512\u7740\u3002\u5b9e\u6d4b\u4e2d\u4e0d\u52a0 flush \u65f6&#xff0c;\u670d\u52a1\u8fdb\u7a0b\u4e00\u76f4\u8dd1\u3001\u65e5\u5fd7\u6587\u4ef6\u5374\u59cb\u7ec8\u662f 0 \u5b57\u8282&#xff0c;\u8fde\u8fdb\u7a0b\u88ab\u6740\u6389\u65e5\u5fd7\u90fd\u6ca1\u843d\u5730\u3002<\/p>\n<p>std::flush \u7684\u4f5c\u7528\u5c31\u662f\u624b\u52a8\u6309\u4e0b&#034;\u7acb\u523b\u9001\u51fa&#034;\u3002\u8981\u4e48 flush&#xff0c;\u8981\u4e48\u7528\u81ea\u5e26\u5237\u65b0\u529f\u80fd\u7684 std::endl&#xff08;\u5b83\u7b49\u4e8e\u6362\u884c &#043; flush&#xff0c;\u4f46\u9891\u7e41 flush \u6709\u6027\u80fd\u5f00\u9500&#xff0c;\u6b63\u5f0f\u9879\u76ee\u8981\u6743\u8861&#xff09;\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e94\u3001\u6784\u5efa\u6a21\u5757\uff1amakefile-\u8fd9\u4e2a\u5c0f\u7ba1\u5bb6\">\u4e94\u3001\u6784\u5efa\u6a21\u5757&#xff1a;Makefile \u8fd9\u4e2a\u5c0f\u7ba1\u5bb6<\/h3>\n<p>.PHONY:all<br \/>\nall:udpclient udpserver<\/p>\n<p>udpclient:UdpClient.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17 -pthread<br \/>\nudpserver:UdpServer.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17 -pthread<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -f udpclient udpserver<\/p>\n<p>\u51e0\u4e2a\u5c0f\u767d\u77e5\u8bc6\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Makefile \u91cc\u7f29\u8fdb\u5fc5\u987b\u662f Tab&#xff0c;\u4e0d\u80fd\u662f\u7a7a\u683c<\/span><\/span>&#xff0c;\u8fd9\u662f\u65b0\u624b\u7b2c\u4e00\u5929\u5fc5\u8e29\u7684\u5751&#xff1b;<\/p>\n<\/li>\n<li>\n<p>$&#064; \u4ee3\u8868\u5192\u53f7\u5de6\u8fb9\u7684\u76ee\u6807\u540d&#xff0c;$^ \u4ee3\u8868\u53f3\u8fb9\u6240\u6709\u4f9d\u8d56&#xff0c;\u4e24\u6761\u89c4\u5219\u5c55\u5f00\u540e\u5c31\u662f\u5b8c\u6574\u7684 g&#043;&#043; \u547d\u4ee4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>-std&#061;c&#043;&#043;17 \u4e0d\u80fd\u7701&#xff0c;\u4ee3\u7801\u7528\u4e86 std::filesystem&#xff1b;<\/p>\n<\/li>\n<li>\n<p>-pthread \u544a\u8bc9\u7f16\u8bd1\u5668\u542f\u7528 pthread \u652f\u6301&#xff08;\u4e92\u65a5\u9501\u8981\u7528&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>.PHONY \u58f0\u660e all\u3001clean \u662f&#034;\u4f2a\u76ee\u6807&#034;&#xff0c;\u610f\u601d\u662f\u5b83\u4eec\u4e0d\u662f\u771f\u5b9e\u6587\u4ef6\u540d&#xff0c;\u54ea\u6015\u76ee\u5f55\u91cc\u6070\u597d\u6709\u4e2a\u53eb clean \u7684\u6587\u4ef6&#xff0c;make clean \u4e5f\u7167\u5e38\u6267\u884c\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"\u516d\u3001\u8e29\u5751\u5c0f\u7ed3\uff1a\u8fd9\u4e9b\u96f7\u6211\u4eec\u90fd\u66ff\u4f60\u8d9f\u8fc7\u4e86\">\u516d\u3001\u8e29\u5751\u5c0f\u7ed3&#xff1a;\u8fd9\u4e9b\u96f7\u6211\u4eec\u90fd\u66ff\u4f60\u8d9f\u8fc7\u4e86<\/h3>\n<p>\u628a\u5f00\u53d1\u4e2d\u771f\u5b9e\u9047\u5230\u7684\u95ee\u9898\u6c47\u603b\u6210\u4e00\u5f20\u6025\u6551\u8868&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<p>\u73b0\u8c61<\/p>\n<p>\u6839\u56e0<\/p>\n<p>\u89e3\u6cd5<\/p>\n<\/tr>\n<tr>\n<td>\n<p>\u670d\u52a1\u5668\u62a5 bind error<\/p>\n<\/td>\n<td>\n<p>\u7aef\u53e3\u88ab\u4e0a\u6b21\u6ca1\u5173\u7684\u8fdb\u7a0b\u5360\u7740<\/p>\n<\/td>\n<td>\n<p>ss -ulnp | grep \u7aef\u53e3 \u627e\u5230 PID&#xff0c;kill \u6389<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>bash: .\/udpserver: No such file<\/p>\n<\/td>\n<td>\n<p>\u8fd8\u6ca1\u7f16\u8bd1&#xff0c;\u6216\u521a make clean \u8fc7<\/p>\n<\/td>\n<td>\n<p>\u5148 make<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6572 make \u53cd\u800c\u5220\u6587\u4ef6<\/p>\n<\/td>\n<td>\n<p>Makefile \u7b2c\u4e00\u4e2a\u76ee\u6807\u662f clean<\/p>\n<\/td>\n<td>\n<p>\u628a all \u653e\u5230\u7b2c\u4e00\u884c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5ba2\u6237\u7aef\u6309\u56de\u8f66\u540e\u6c38\u4e45\u5361\u6b7b<\/p>\n<\/td>\n<td>\n<p>\u53d1\u51fa 0 \u5b57\u8282\u5305&#xff0c;\u670d\u52a1\u5668\u4e0d\u56de\u590d<\/p>\n<\/td>\n<td>\n<p>\u7a7a\u884c continue \u8df3\u8fc7<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Ctrl&#043;D \u540e\u75af\u72c2\u5237\u5c4f<\/p>\n<\/td>\n<td>\n<p>\u6ca1\u68c0\u67e5 getline \u8fd4\u56de\u503c<\/p>\n<\/td>\n<td>\n<p>\u5931\u8d25\u65f6 break<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5355\u8bcd\u540e\u9762\u8ddf\u4e71\u7801<\/p>\n<\/td>\n<td>\n<p>UDP \u6570\u636e\u4e0d\u5e26 &#039;\\\\0&#039;<\/p>\n<\/td>\n<td>\n<p>buffer[s] &#061; 0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u91cd\u5b9a\u5411\u540e\u65e5\u5fd7\u6587\u4ef6\u662f\u7a7a\u7684<\/p>\n<\/td>\n<td>\n<p>\u8f93\u51fa\u7f13\u51b2\u6ca1\u5237<\/p>\n<\/td>\n<td>\n<p>&lt;&lt; std::flush<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6539\u4e86 hpp \u8fd0\u884c\u6ca1\u53d8\u5316<\/p>\n<\/td>\n<td>\n<p>make \u4e0d\u8ffd\u8e2a\u5934\u6587\u4ef6\u4f9d\u8d56<\/p>\n<\/td>\n<td>\n<p>make clean &amp;&amp; make<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u7528 127.0.0.1 \u80fd\u901a\u3001\u6362 IP \u4e0d\u901a<\/p>\n<\/td>\n<td>\n<p>bind \u5199\u6b7b\u4e86\u5355\u4e2a IP<\/p>\n<\/td>\n<td>\n<p>\u7528 INADDR_ANY<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"\u4e03\u3001\u77e5\u8bc6\u56fe\u8c31\uff1a\u628a\u8fd9\u4e00\u8bfe\u88c5\u8fdb\u8111\u5b50\">\u4e03\u3001\u77e5\u8bc6\u56fe\u8c31&#xff1a;\u628a\u8fd9\u4e00\u8bfe\u88c5\u8fdb\u8111\u5b50<\/h3>\n<p>\u6700\u540e\u7528\u4e00\u5f20\u56fe\u6536\u4e2a\u5c3e\u3002\u522b\u770b\u4ee3\u7801\u4e0d\u5c11&#xff0c;\u9aa8\u67b6\u5176\u5b9e\u975e\u5e38\u6e05\u6670&#xff1a;<\/p>\n<p>                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502            UdpServer.cc&#xff08;\u5e97\u957f&#xff09;       \u2502<br \/>\n                    \u2502  \u89e3\u6790\u7aef\u53e3 \u2192 \u52a0\u8f7d\u8bcd\u5178 \u2192 \u7ec4\u88c5\u670d\u52a1\u5668 \u2192 \u542f\u52a8 \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                                    \u2502 \u6301\u6709<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502          UdpServer.hpp&#xff08;\u524d\u53f0&#xff09;        \u2502<br \/>\n                    \u2502  socket \u2192 bind \u2192 recvfrom \u2192 sendto  \u2502<br \/>\n                    \u2502  \u81ea\u5df1\u4e0d\u61c2\u4e1a\u52a1&#xff0c;\u53ea\u8d1f\u8d23\u6536\u53d1\u548c\u8c03\u7528\u56de\u8c03     \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n            std::function \u56de\u8c03\u2502                 \u2502 \u4f7f\u7528<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502   Dict.hpp     \u2502  \u2502  InetAddr.hpp  \u2502<br \/>\n                    \u2502 unordered_map  \u2502  \u2502 ntohs\/inet_ntoa\u2502<br \/>\n                    \u2502  \u82f1\u8bd1\u6c49\u4e1a\u52a1     \u2502  \u2502 \u5c01\u88c5\u5ba2\u6237\u7aef\u5730\u5740   \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                            \u2502 \u8bfb\u53d6<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502 dictionary.txt \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>        \u6a2a\u5207\u5173\u6ce8\u70b9&#xff08;\u8c01\u90fd\u80fd\u7528&#xff09;&#xff1a;<br \/>\n        \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u5185\u90e8\u52a0\u9501   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n        \u2502   Log.hpp    \u2502 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25b6 \u2502  Mutex.hpp   \u2502<br \/>\n        \u2502 \u7b56\u7565\u6a21\u5f0f&#043;\u6d41\u5f0f\u65e5\u5fd7\u2502           \u2502 RAII LockGuard\u2502<br \/>\n        \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518            \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>        \u5de5\u7a0b\u5316&#xff1a;Makefile&#xff08;all \/ clean&#xff0c;g&#043;&#043; -std&#061;c&#043;&#043;17 -pthread&#xff09;<br \/>\n        \u5bf9\u7aef&#xff1a;UdpClient.cc&#xff08;socket \u4e0d bind&#xff0c;sendto \u540e recvfrom \u7b49\u56de\u4fe1&#xff09;<\/p>\n<p>\u9700\u8981\u5728\u8111\u5b50\u91cc\u5efa\u7acb\u7684\u51e0\u6761\u4e3b\u7ebf&#xff1a;<\/p>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7f51\u7edc\u56db\u677f\u65a7<\/span><\/span>&#xff1a;UDP \u670d\u52a1\u5668\u5c31\u662f socket \u2192 bind \u2192 \u5faa\u73af(recvfrom\/sendto)&#xff1b;UDP \u4fdd\u7559\u62a5\u6587\u8fb9\u754c&#xff08;\u65e0\u7c98\u5305&#xff09;\u3001\u4e0d\u53ef\u9760\u3001\u4e0d\u8fde\u63a5&#xff0c;\u9002\u5408\u8fd9\u79cd\u4e00\u95ee\u4e00\u7b54\u7684\u5c0f\u573a\u666f\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5730\u5740\u4e09\u8981\u7d20<\/span><\/span>&#xff1a;sockaddr_in \u586b family\u3001port&#xff08;htons&#xff09;\u3001addr&#xff08;INADDR_ANY \u6216 inet_addr&#xff09;&#xff1b;\u670d\u52a1\u5668\u56fa\u5b9a bind&#xff0c;\u5ba2\u6237\u7aef\u8ba9 OS \u81ea\u52a8\u5206\u914d\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u6570\u636e\u5b89\u5168\u610f\u8bc6<\/span><\/span>&#xff1a;\u6536\u5230\u7684\u6570\u636e\u624b\u52a8\u8865 &#039;\\\\0&#039;&#xff0c;\u6536\u5305\u957f\u5ea6\u7559\u4e00\u683c&#xff1b;\u7a7a\u8f93\u5165\u3001EOF \u90fd\u8981\u9632\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u8bbe\u8ba1\u601d\u60f3<\/span><span style=\"background-color:#ffd900\">&#xff1a;<\/span><\/span>\u56de\u8c03\/std::function \u8ba9\u7f51\u7edc\u6846\u67b6\u4e0e\u4e1a\u52a1\u89e3\u8026&#xff1b;\u7b56\u7565\u6a21\u5f0f\u8ba9\u65e5\u5fd7\u8f93\u51fa\u65b9\u5f0f\u53ef\u66ff\u6362&#xff1b;RAII \u8ba9\u8d44\u6e90&#xff08;\u9501&#xff09;\u81ea\u52a8\u91ca\u653e\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5de5\u7a0b\u7d20\u517b<\/span><\/span>&#xff1a;\u65e5\u5fd7\u516d\u8981\u7d20 &#043; flush&#xff1b;Makefile \u9ed8\u8ba4\u76ee\u6807\u3001Tab \u7f29\u8fdb\u3001\u5934\u6587\u4ef6\u4f9d\u8d56&#xff1b;\u7aef\u53e3\u5360\u7528\u600e\u4e48\u67e5\u600e\u4e48\u6740\u3002<\/p>\n<\/li>\n<hr \/>\n<p>\u7b49\u8fd9\u4e9b\u5728\u4f60\u8111\u5b50\u91cc\u4e32\u6210\u7ebf\u4e4b\u540e&#xff0c;\u4e0b\u4e00\u6b65\u53ef\u4ee5\u73a9\u7684\u5347\u7ea7\u4e5f\u5f88\u660e\u786e&#xff1a;\u7ed9\u5ba2\u6237\u7aef\u52a0\u63a5\u6536\u8d85\u65f6&#xff08;setsockopt(SO_RCVTIMEO)&#xff09;\u9632\u6b62\u670d\u52a1\u5668\u4e0d\u56de\u590d\u65f6\u50bb\u7b49&#xff1b;\u7ed9\u8bcd\u5178\u52a0&#034;\u589e\u5220\u8bcd\u6761&#034;\u7f51\u7edc\u547d\u4ee4&#xff1b;\u628a\u5355\u7ebf\u7a0b\u5faa\u73af\u5347\u7ea7\u6210\u591a\u7ebf\u7a0b&#xff1b;\u751a\u81f3\u628a UDP \u6362\u6210 TCP \u5bf9\u6bd4\u7740\u5b66\u3002\u7f51\u7edc\u7f16\u7a0b\u8fd9\u6247\u95e8&#xff0c;\u4f60\u5df2\u7ecf\u8fc8\u8fdb\u6765\u4e86\u3002<\/p>\n<hr \/>\n<h2>\u516b.\u6700\u7ec8\u7684\u5b8c\u6574\u4ee3\u7801<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"480\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234042-6aa885faeaea7.png\" width=\"1255\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2196\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234043-6aa885fb3fd1a.png\" width=\"1031\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"884\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234044-6aa885fc5d775.png\" width=\"1023\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"6708\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234044-6aa885fc9939f.png\" width=\"1068\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1849\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234046-6aa885fed27d0.png\" width=\"933\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234047-6aa885ff8b3b6.png\" width=\"1134\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1508\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234048-6aa88600b8b4c.png\" width=\"1439\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3007\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234049-6aa8860155baf.png\" width=\"1075\" \/><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883&#xff1a;Ubuntu  g&#xff08;C17&#xff09; \u4f60\u5c06\u770b\u5230&#xff1a;\u4e00\u4e2a\u80fd\u8dd1\u7684\\&#8221;\u7f51\u7edc\u7ffb\u8bd1\u8f6f\u4ef6\\&#8221;\u662f\u600e\u4e48\u4ece 9 \u4e2a\u6587\u4ef6\u91cc\u957f\u51fa\u6765\u7684 \u9002\u5408\u8c01&#xff1a;\u5b66\u8fc7 C \u57fa\u7840\u8bed\u6cd5&#xff0c;\u4f46\u6ca1\u5199\u8fc7\u7f51\u7edc\u7a0b\u5e8f\u7684\u4f60\u5148\u770b\u770b\u8fd9\u4e1c\u897f\u5230\u5e95\u662f\u4e2a\u5565\u60f3\u8c61\u4e00\u4e2a\u573a\u666f&#xff1a;\u4f60\u5728\u7535\u8111\u4e0a\u8f93\u5165\u4e00\u4e2a\u82f1\u6587\u5355\u8bcd&#xff0c;\u56de\u8f66&#xff0c;\u5c4f\u5e55\u4e0a\u8e66\u51fa\u4e2d\u6587\u610f\u601d\u2014\u2014\u53ea\u4e0d\u8fc7\u5e2e\u4f60\u7ffb\u8bd1\u7684\u90a3\u672c\\&#8221;\u8bcd\u5178\\&#8221;\u4e0d\u5728\u4f60\u672c\u673a&#xff0c;\u800c\u5728\u7f51\u7edc\u53e6\u4e00\u5934\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u91cc\u3002\u8dd1\u8d77\u6765\u662f\u8fd9\u6837\u7684&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":105260,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11653,11958,55,193,190,3063],"topic":[],"class_list":["post-105268","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11653","tag-11958","tag-c","tag-udp","tag-190","tag-3063"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \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\/105268.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u73af\u5883&#xff1a;Ubuntu g&#xff08;C17&#xff09; \u4f60\u5c06\u770b\u5230&#xff1a;\u4e00\u4e2a\u80fd\u8dd1\u7684&quot;\u7f51\u7edc\u7ffb\u8bd1\u8f6f\u4ef6&quot;\u662f\u600e\u4e48\u4ece 9 \u4e2a\u6587\u4ef6\u91cc\u957f\u51fa\u6765\u7684 \u9002\u5408\u8c01&#xff1a;\u5b66\u8fc7 C \u57fa\u7840\u8bed\u6cd5&#xff0c;\u4f46\u6ca1\u5199\u8fc7\u7f51\u7edc\u7a0b\u5e8f\u7684\u4f60\u5148\u770b\u770b\u8fd9\u4e1c\u897f\u5230\u5e95\u662f\u4e2a\u5565\u60f3\u8c61\u4e00\u4e2a\u573a\u666f&#xff1a;\u4f60\u5728\u7535\u8111\u4e0a\u8f93\u5165\u4e00\u4e2a\u82f1\u6587\u5355\u8bcd&#xff0c;\u56de\u8f66&#xff0c;\u5c4f\u5e55\u4e0a\u8e66\u51fa\u4e2d\u6587\u610f\u601d\u2014\u2014\u53ea\u4e0d\u8fc7\u5e2e\u4f60\u7ffb\u8bd1\u7684\u90a3\u672c&quot;\u8bcd\u5178&quot;\u4e0d\u5728\u4f60\u672c\u673a&#xff0c;\u800c\u5728\u7f51\u7edc\u53e6\u4e00\u5934\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u91cc\u3002\u8dd1\u8d77\u6765\u662f\u8fd9\u6837\u7684&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/105268.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-14T23:40:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234042-6aa885faeaea7.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105268.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/105268.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-14T23:40:52+00:00\",\"dateModified\":\"2026-09-14T23:40:52+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/105268.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/105268.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105268.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\"}]},{\"@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":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \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\/105268.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u73af\u5883&#xff1a;Ubuntu g&#xff08;C17&#xff09; \u4f60\u5c06\u770b\u5230&#xff1a;\u4e00\u4e2a\u80fd\u8dd1\u7684\"\u7f51\u7edc\u7ffb\u8bd1\u8f6f\u4ef6\"\u662f\u600e\u4e48\u4ece 9 \u4e2a\u6587\u4ef6\u91cc\u957f\u51fa\u6765\u7684 \u9002\u5408\u8c01&#xff1a;\u5b66\u8fc7 C \u57fa\u7840\u8bed\u6cd5&#xff0c;\u4f46\u6ca1\u5199\u8fc7\u7f51\u7edc\u7a0b\u5e8f\u7684\u4f60\u5148\u770b\u770b\u8fd9\u4e1c\u897f\u5230\u5e95\u662f\u4e2a\u5565\u60f3\u8c61\u4e00\u4e2a\u573a\u666f&#xff1a;\u4f60\u5728\u7535\u8111\u4e0a\u8f93\u5165\u4e00\u4e2a\u82f1\u6587\u5355\u8bcd&#xff0c;\u56de\u8f66&#xff0c;\u5c4f\u5e55\u4e0a\u8e66\u51fa\u4e2d\u6587\u610f\u601d\u2014\u2014\u53ea\u4e0d\u8fc7\u5e2e\u4f60\u7ffb\u8bd1\u7684\u90a3\u672c\"\u8bcd\u5178\"\u4e0d\u5728\u4f60\u672c\u673a&#xff0c;\u800c\u5728\u7f51\u7edc\u53e6\u4e00\u5934\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u91cc\u3002\u8dd1\u8d77\u6765\u662f\u8fd9\u6837\u7684&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/105268.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-14T23:40:52+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260914234042-6aa885faeaea7.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/105268.html","url":"https:\/\/www.wsisp.com\/helps\/105268.html","name":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-14T23:40:52+00:00","dateModified":"2026-09-14T23:40:52+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/105268.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/105268.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/105268.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u7bc7\u56db\uff09\u3011\u624b\u628a\u624b\u5199\u4e00\u4e2a UDP \u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\uff1a\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2\u7684 C++ \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218"}]},{"@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\/105268","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=105268"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/105268\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/105260"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=105268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=105268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=105268"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=105268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}