{"id":30856,"date":"2025-04-21T04:46:32","date_gmt":"2025-04-20T20:46:32","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/30856.html"},"modified":"2025-04-21T04:46:32","modified_gmt":"2025-04-20T20:46:32","slug":"net-6-udp_chat_server%e5%9f%ba%e4%ba%8eudp%e7%9a%84%e7%ae%80%e5%8d%95%e8%81%8a%e5%a4%a9%e5%ae%a4%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%9a%84%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e4%b8%9a%e5%8a%a1%e7%9b%b8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/30856.html","title":{"rendered":"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:40px\">\n<p id=\"avdsI-toc\" style=\"margin-left:40px\">1. \u7f51\u7edc\u804a\u5929\u5ba4\u7684\u610f\u4e49<\/p>\n<p id=\"HYwII-toc\" style=\"margin-left:40px\">2. \u7f51\u7edc\u804a\u5929\u5ba4\u4e86\u89e3<\/p>\n<p id=\"WMGvr-toc\" style=\"margin-left:80px\">2.1. \u7f51\u7edc\u804a\u5929\u5ba4\u6a21\u5757\u5206\u6790<\/p>\n<p id=\"WofBo-toc\" style=\"margin-left:80px\">2.2. \u76ee\u6807<\/p>\n<p id=\"yPu4f-toc\" style=\"margin-left:40px\">3. \u57fa\u672c\u6846\u67b6<\/p>\n<p id=\"m791J-toc\" style=\"margin-left:80px\">3.1. \u6587\u4ef6\u57fa\u672c\u6846\u67b6<\/p>\n<p id=\"ffhwW-toc\" style=\"margin-left:80px\">3.2. \u8bbe\u8ba1\u56de\u8c03\u51fd\u6570\u89e3\u8026<\/p>\n<p id=\"HaUBX-toc\" style=\"margin-left:40px\">4. Route.hpp \u6a21\u5757(\u6d88\u606f\u8f6c\u53d1)<\/p>\n<p id=\"gGRGV-toc\" style=\"margin-left:80px\">4.1. \u5934\u6587\u4ef6\u5305\u542b<\/p>\n<p id=\"zvzrT-toc\" style=\"margin-left:80px\">4.2. \u57fa\u672c\u7c7b\u6846\u67b6<\/p>\n<p id=\"HT4x2-toc\" style=\"margin-left:80px\">4.3. Route::Forward() \u8f6c\u53d1<\/p>\n<p id=\"M4WWe-toc\" style=\"margin-left:120px\">4.3.1. \u51fd\u6570\u5934\u8bbe\u8ba1<\/p>\n<p id=\"Ov9i9-toc\" style=\"margin-left:120px\">4.3.2. \u7ef4\u62a4\u7528\u6237\u5217\u8868<\/p>\n<p id=\"auT4v-toc\" style=\"margin-left:120px\">4.3.3. \u5ba2\u6237\u9000\u51fa\u804a\u5929<\/p>\n<p id=\"K1h26-toc\" style=\"margin-left:120px\">4.3.4. \u6d88\u606f\u8f6c\u53d1 -&gt; \u7ebf\u7a0b\u6c60<\/p>\n<p id=\"irvtf-toc\" style=\"margin-left:160px\">4.3.4.1. \u5f15\u5165\u7ebf\u7a0b\u6c60\u4ee3\u7801<\/p>\n<p id=\"nx4vG-toc\" style=\"margin-left:160px\">4.3.4.2. \u8f6c\u53d1\u4efb\u52a1<\/p>\n<p id=\"OcCMG-toc\" style=\"margin-left:80px\">4.4. \u8f6c\u53d1\u6a21\u5757\u4e0e\u670d\u52a1\u5668\u6a21\u5757\u5173\u8054<\/p>\n<p id=\"vwQgr-toc\" style=\"margin-left:80px\">4.5. \u6d4b\u8bd5: \u591a\u4e2a\u5ba2\u6237\u7aef\u4e0e\u4e00\u4e2a\u670d\u52a1\u7aef<\/p>\n<p id=\"bV9JF-toc\" style=\"margin-left:40px\">5. \u5ba2\u6237\u7aef\u8bfb\u5199\u5e76\u884c<\/p>\n<p id=\"c1NWj-toc\" style=\"margin-left:80px\">5.1. \u5ba2\u6237\u7aef\u521d\u59cb\u5316<\/p>\n<p id=\"baKAN-toc\" style=\"margin-left:80px\">5.2. \u53d1\u6d88\u606f\u5ba2\u6237\u7aef\u4e0e\u6536\u6d88\u606f\u5ba2\u6237\u7aef\u89e3\u8026\u5408<\/p>\n<p id=\"dVhRz-toc\" style=\"margin-left:120px\">5.2.1. \u6536\u6d88\u606f<\/p>\n<p id=\"FAdfP-toc\" style=\"margin-left:120px\">5.2.2. \u53d1\u6d88\u606f<\/p>\n<p id=\"nwsgF-toc\" style=\"margin-left:40px\">6. \u53c2\u8003\u4ee3\u7801<\/p>\n<p id=\"bXzCB-toc\" style=\"margin-left:80px\">6.1. \u6838\u5fc3\u4ee3\u7801<\/p>\n<p id=\"rNHuw-toc\" style=\"margin-left:80px\">6.2. \u5176\u4ed6\u4ee3\u7801<\/p>\n<hr id=\"hr-toc\" \/>\n<h3 id=\"avdsI\">1. \u7f51\u7edc\u804a\u5929\u5ba4\u7684\u610f\u4e49<\/h3>\n<p id=\"u255f833d\">\u4e3a\u4ec0\u4e48\u8981\u5199\u4e00\u4e2a\u7f51\u7edc\u804a\u5929\u5ba4\u5462? \u524d\u9762\u90fd\u5199\u8fc7\u5b57\u5178\u90a3\u4e2a\u4f8b\u5b50\u4e86.<\/p>\n<p id=\"u8453a060\">\u610f\u4e49\u5728\u4e8e: \u7f51\u7edc\u804a\u5929\u5ba4\u7684\u610f\u4e49\u5728\u4e8e\u591a\u8fdb\u7a0b, \u5f15\u5165\u4f7f\u7528\u7ebf\u7a0b\u6c60.<\/p>\n<h3 id=\"HYwII\">2. \u7f51\u7edc\u804a\u5929\u5ba4\u4e86\u89e3<\/h3>\n<h4 id=\"WMGvr\">2.1. \u7f51\u7edc\u804a\u5929\u5ba4\u6a21\u5757\u5206\u6790<\/h4>\n<p id=\"u9d329610\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"457\" id=\"u273fb63e\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d23219d5.png\" width=\"1116\" \/><\/p>\n<ul>\n<li id=\"u02e795d2\">udpserver<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li id=\"u01100a31\">\u529f\u80fd: \u8bfb\u53d6\u6570\u636e<\/li>\n<li id=\"uf27d035a\">\u8bfb\u5230\u4e24\u4e2a\u4e1c\u897f: \u6570\u636e &#043; clientinfo(ip\/port&#8230;)<\/li>\n<li id=\"ua1f1a8b8\">\u5728\u7ebf\u7528\u6237\u5217\u8868, \u7ef4\u62a4&#034;\u8c01\u5728\u7ebf&#034;\u7684\u542b\u4e49(ip &#043; port).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li id=\"u8a7e2697\">\u8def\u7531\u4e0e\u8f6c\u53d1\u6a21\u5757<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li id=\"ude3d458b\">\u529f\u80fd: \u8f6c\u53d1\u6d88\u606f(\u6839\u636e\u7528\u6237\u5217\u8868), \u8f6c\u53d1\u7ed9\u5bf9\u5e94\u7684 client.<\/li>\n<li id=\"u68d70ca4\">\u5b9e\u73b0\u65b9\u5f0f: \u7ebf\u7a0b\u6c60\u7684\u65b9\u5f0f\u8fdb\u884c\u8f6c\u53d1.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 id=\"WofBo\">2.2. \u76ee\u6807<\/h4>\n<p id=\"ufc8006ba\">\u8981\u6c42\u4e0e\u5b57\u5178\u670d\u52a1\u4e00\u6837, \u8981\u6c42\u5404\u4e2a\u6a21\u5757\u89e3\u8026\u5408.<\/p>\n<p id=\"u96802eac\">\u4e1a\u52a1\u903b\u8f91 \u4e0e IO \u903b\u8f91\u89e3\u8026.<\/p>\n<h3 id=\"yPu4f\">3. \u57fa\u672c\u6846\u67b6<\/h3>\n<h4 id=\"m791J\">3.1. \u6587\u4ef6\u57fa\u672c\u6846\u67b6<\/h4>\n<p id=\"u1fdb51d5\">\u540c\u6837\u6211\u4eec\u628a\u4e4b\u524d\u5199\u7684 echo_server \u76f4\u63a5 CV \u5230\u6211\u4eec\u7684 chat_server \u5373\u53ef.<\/p>\n<p id=\"u7095dca2\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"312\" id=\"ud6fbe1e9\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d236d0e7.png\" width=\"277\" \/><\/p>\n<h4 id=\"ffhwW\">3.2. \u8bbe\u8ba1\u56de\u8c03\u51fd\u6570\u89e3\u8026<\/h4>\n<p id=\"ue5f74a4e\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"61\" id=\"u5a9ef07f\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d2399a2b.png\" width=\"829\" \/><\/p>\n<p id=\"ue002a615\">\u89e3\u91ca\u4e00\u4e0b\u5404\u4e2a\u53c2\u6570\u7684\u542b\u4e49:<\/p>\n<ul>\n<li id=\"ufd1895ee\">\u8fd4\u56de\u503c\u662f void, \u56e0\u4e3a\u662f\u53d1\u9001\u6d88\u606f\u561b, \u53ef\u4ee5\u4e0d\u7528\u8fd4\u56de\u503c.<\/li>\n<li id=\"u07acf3bb\">\u53c2\u6570 1: int, \u8fd9\u4e2a\u662f\u5957\u63a5\u5b57<\/li>\n<li id=\"uf0a3c5f5\">\u53c2\u6570 2: \u53d1\u9001\u7684\u6d88\u606f<\/li>\n<li id=\"ud5f9392d\">\u53c2\u6570 3: \u8c01\u53d1\u7684\u6d88\u606f<\/li>\n<\/ul>\n<p id=\"u025b31ab\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"190\" id=\"u9afc8610\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d23c0fd1.png\" width=\"702\" \/><\/p>\n<p id=\"ub36a6a00\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"214\" id=\"u173eb35d\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d23e8be1.png\" width=\"807\" \/><\/p>\n<p id=\"u02943b97\">\n<p id=\"u8fd2f353\">\u89e3\u8026\u5408: \u628a\u670d\u52a1\u53d1\u6d88\u606f\u7ed9\u5230\u8f6c\u53d1\u6a21\u5757, \u6240\u4ee5\u4e0b\u9762\u8fd9\u90e8\u5206\u662f\u4e0d\u8981\u4e86.<\/p>\n<p id=\"u88771663\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"468\" id=\"u0c2f7a13\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204628-68055d242760f.png\" width=\"1005\" \/><\/p>\n<p id=\"ub7c79744\">\u53d8\u6210:<\/p>\n<p id=\"uf2cd7f28\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"199\" id=\"uac28f33d\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204628-68055d2465a4f.png\" width=\"895\" \/><\/p>\n<h3 id=\"HaUBX\">4. Route.hpp \u6a21\u5757(\u6d88\u606f\u8f6c\u53d1)<\/h3>\n<h4 id=\"gGRGV\">4.1. \u5934\u6587\u4ef6\u5305\u542b<\/h4>\n<p id=\"u2906e218\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"141\" id=\"u302ce9a1\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204628-68055d2494ce7.png\" width=\"375\" \/><\/p>\n<h4 id=\"zvzrT\">4.2. \u57fa\u672c\u7c7b\u6846\u67b6<\/h4>\n<p id=\"ue769b82b\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"273\" id=\"udcbefc0c\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204628-68055d24bc2d3.png\" width=\"520\" \/><\/p>\n<h4 id=\"HT4x2\">4.3. Route::Forward() \u8f6c\u53d1<\/h4>\n<h5 id=\"M4WWe\">4.3.1. \u51fd\u6570\u5934\u8bbe\u8ba1<\/h5>\n<p id=\"u0e4c879b\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"216\" id=\"u69252eb5\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204628-68055d24e4e30.png\" width=\"763\" \/><\/p>\n<ul>\n<li id=\"u75b7672a\">int sockfd: \u4ece\u54ea\u4e2a\u5957\u63a5\u5b57\u53d1?<\/li>\n<li id=\"u40cdbd84\">message: \u53d1\u9001\u7684\u6d88\u606f\u662f\u4ec0\u4e48?<\/li>\n<li id=\"uab603d4c\">who: \u8c01\u6765\u53d1?<\/li>\n<\/ul>\n<h5 id=\"Ov9i9\">4.3.2. \u7ef4\u62a4\u7528\u6237\u5217\u8868<\/h5>\n<p id=\"ue5a3fe48\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"123\" id=\"ue7477411\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204629-68055d251ab6e.png\" width=\"928\" \/><\/p>\n<p id=\"ue5a3fe48\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"276\" id=\"u4390ffd8\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204629-68055d2546321.png\" width=\"811\" \/><\/p>\n<p id=\"ub884f442\">\n<p id=\"udce25174\">\u540c\u65f6, \u8fd8\u9700\u8981\u6ce8\u610f\u5728 InetAddr \u5f53\u4e2d\u53bb\u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26.<\/p>\n<p id=\"ub7bba1e6\">\u5982\u679c\u53ea\u4fdd\u8bc1 ip \u7684\u552f\u4e00\u6027, \u6240\u4ee5\u4e00\u4e2a\u4e3b\u673a\u4e0b\u7684\u4e00\u4e2a ip \u53ea\u80fd\u542f\u52a8\u4e00\u4e2a\u5ba2\u6237\u7aef, \u6240\u4ee5\u4e0d\u592a\u597d(\u6b63\u5e38\u6765\u8bb2\u662f ip \u552f\u4e00\u7684), \u6211\u4eec\u8fd9\u91cc\u4fdd\u8bc1\u7aef\u53e3\u552f\u4e00\u5373\u53ef.<\/p>\n<p id=\"u6dad506a\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"306\" id=\"u79bb1151\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204629-68055d25798a1.png\" width=\"826\" \/><\/p>\n<h5 id=\"auT4v\">4.3.3. \u5ba2\u6237\u9000\u51fa\u804a\u5929<\/h5>\n<p id=\"u5de6c73f\">\u6211\u4eec\u7ea6\u5b9a: \u5047\u8bbe message &#061;&#061; &#034;QUIT&#034;, \u6211\u4eec\u79fb\u9664 online_user \u4e2d\u7684\u5ba2\u6237. \u5e76\u4e14\u628a\u8fd9\u4e2a\u6d88\u606f\u540c\u6837\u505a\u8f6c\u53d1\u5904\u7406(\u544a\u77e5\u6240\u6709\u4eba\u6709\u4e2a\u4eba\u8d70\u4e86~).<\/p>\n<p id=\"u1c6fc6a0\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"345\" id=\"u2f7ddbfb\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204629-68055d25adb86.png\" width=\"832\" \/><\/p>\n<p id=\"ua2423ba3\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"301\" id=\"u5703288a\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204629-68055d25e4e60.png\" width=\"745\" \/><\/p>\n<p id=\"u2eaf1780\">\u6ce8\u610f: \u627e\u5230\u4e86\u4e00\u5b9a\u8981\u7acb\u523b break, \u8fd9\u4e2a\u5730\u65b9\u5b58\u5728\u8fed\u4ee3\u5668\u5931\u6548\u95ee\u9898.<\/p>\n<h5 id=\"K1h26\">4.3.4. \u6d88\u606f\u8f6c\u53d1 -&gt; \u7ebf\u7a0b\u6c60<\/h5>\n<p id=\"u5a6cbaf1\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"339\" id=\"u3ee3832d\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204630-68055d261c2d4.png\" width=\"801\" \/><\/p>\n<p id=\"uab0ce01e\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"202\" id=\"udacb3ccf\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204630-68055d2655293.png\" width=\"969\" \/><\/p>\n<p id=\"u462cfe70\">\u5b57\u8282\u5e8f\u5217\u95ee\u9898\u53ef\u4ee5\u7528 sockaddr \u76f4\u63a5\u89e3\u51b3:<\/p>\n<p id=\"u40d5ebae\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"358\" id=\"fbKd2\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204630-68055d2684e1c.png\" width=\"577\" \/><\/p>\n<p id=\"ub0db6491\">\n<p id=\"u1afa10c2\">\u4e0b\u9762\u5b9e\u9645\u4e0a\u8fd8\u6709\u4fe9\u6d3b:<\/p>\n<li id=\"u9e4e8d59\">\u5f15\u5165\u7ebf\u7a0b\u6c60<\/li>\n<li id=\"ue82c9742\">\u4e1a\u52a1 \u4e0e \u670d\u52a1\u5668\u4e4b\u95f4\u5173\u8054\u8d77\u6765.<\/li>\n<h6 id=\"irvtf\">4.3.4.1. \u5f15\u5165\u7ebf\u7a0b\u6c60\u4ee3\u7801<\/h6>\n<p id=\"u23888b39\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"484\" id=\"u8ed4aaa8\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204630-68055d26af830.png\" width=\"304\" \/><\/p>\n<p id=\"uaa12f845\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"433\" id=\"uf84a4eb7\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204630-68055d26daeaa.png\" width=\"724\" \/><\/p>\n<h6 id=\"nx4vG\">4.3.4.2. \u8f6c\u53d1\u4efb\u52a1<\/h6>\n<p id=\"ua442631f\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"559\" id=\"ua1771ddb\" src=\"2025-04-21d33v1ins0va.png\" width=\"1342\" \/><\/p>\n<p id=\"u64a377c4\">\u5565\u610f\u601d\u5462? \u4f60\u8fd9\u6837\u76f4\u63a5\u8c03\u7528, \u662f\u4e3b\u7ebf\u7a0b\u505a\u7684, \u6548\u7387\u4e0d\u9ad8, \u56e0\u6b64\u6211\u4eec\u8003\u8651\u628a\u8fd9\u4e2a ForwardHelper \u4efb\u52a1\u5916\u5305\u51fa\u53bb, \u8ba9\u7ebf\u7a0b\u6c60\u7684\u7ebf\u7a0b\u6765\u8fdb\u884c\u5904\u7406, \u800c\u6211\u4eec\u7684\u4e3b\u7ebf\u7a0b\u5219\u6709\u7a7a\u95f2\u53bb\u5904\u7406\u81ea\u5df1\u7684\u4e8b\u60c5.<\/p>\n<p id=\"ud660ef53\">\n<p id=\"u94bd6053\">\u5305\u88c5\u4efb\u52a1:<\/p>\n<p id=\"u6859eeca\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"162\" id=\"uabe01f86\" src=\"2025-04-21l412wikapov.png\" width=\"639\" \/><\/p>\n<p id=\"u7ccee96b\">\u6784\u5efa\u4efb\u52a1:<\/p>\n<p id=\"u13004e43\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"279\" id=\"ufb0ef48d\" src=\"2025-04-21fo0f2hjzp3w.png\" width=\"861\" \/><\/p>\n<p id=\"u7f70a6e8\">\u83b7\u53d6\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u5e76\u5165\u7ebf\u7a0b\u6c60\u4efb\u52a1\u961f\u5217:<\/p>\n<p id=\"ucbb8cc55\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"108\" id=\"u10591d6c\" src=\"2025-04-210umf4d20qem.png\" width=\"726\" \/><\/p>\n<h4 id=\"OcCMG\">4.4. \u8f6c\u53d1\u6a21\u5757\u4e0e\u670d\u52a1\u5668\u6a21\u5757\u5173\u8054<\/h4>\n<p id=\"u3fee46f3\">\u56e0\u4e3a\u7ebf\u7a0b\u6c60\u4e2d\u7684 func_t \u7c7b\u578b\u5df2\u7ecf\u5360\u7528\u4e86, \u56e0\u6b64\u6211\u4eec\u7ed9 UdpServer.hpp \u4e2d\u7684\u5305\u88c5\u5668\u6539\u4e00\u4e0b\u540d\u5b57.<\/p>\n<p id=\"u236a10e3\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"126\" id=\"u065c5da1\" src=\"2025-04-21fnoozzmzrsb.png\" width=\"940\" \/><\/p>\n<p id=\"u420d8c8c\">\u670d\u52a1\u7aef\u5bf9\u8c61:<\/p>\n<p id=\"u6e325493\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"207\" id=\"u6f56f96d\" src=\"2025-04-21ohiurv51vwb.png\" width=\"774\" \/><\/p>\n<p id=\"u1a132246\">\u7ed1\u5b9a\u8f6c\u53d1\u6a21\u5757, \u5e76\u4e14\u5173\u8054\u5230\u670d\u52a1\u5668:<\/p>\n<p id=\"u470b58ee\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"298\" id=\"ud05a71d2\" src=\"2025-04-21jclwphsptff.png\" width=\"1077\" \/><\/p>\n<p id=\"ud0c18541\">\u5e26\u4e0a\u7ebf\u7a0b\u5e93\u8fdb\u884c\u7f16\u8bd1:<\/p>\n<p id=\"ud3a59f71\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"177\" id=\"u85b1bc4d\" src=\"2025-04-212r5txh11xbk.png\" width=\"615\" \/><\/p>\n<h4 id=\"vwQgr\">4.5. \u6d4b\u8bd5: \u591a\u4e2a\u5ba2\u6237\u7aef\u4e0e\u4e00\u4e2a\u670d\u52a1\u7aef<\/h4>\n<p id=\"u5edc2a63\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"741\" id=\"u3fa1d038\" src=\"2025-04-21dq32li4wlyd.png\" width=\"1294\" \/><\/p>\n<p id=\"ua6ce5faa\">\u7528 ps -aL\u53ef\u4ee5\u67e5\u8be2\u5f53\u524d\u7684\u4e00\u4e2a\u7ebf\u7a0b\u542f\u52a8\u60c5\u51b5.<\/p>\n<p id=\"u862c21e4\">\n<p id=\"u9ac34455\">\u4e0b\u9762\u662f\u4e00\u4e9b\u65b9\u4fbf\u9a8c\u8bc1\u7684\u8c03\u8bd5\u6d88\u606f:<\/p>\n<p id=\"uba6a2f51\">\u76f4\u63a5\u770b\u6e90\u7801\u5427, \u6539\u7684\u6bd4\u8f83\u591a~<\/p>\n<p id=\"u0db54f33\">\n<p id=\"u459ac310\">\u4f46\u662f\u53d1\u73b0\u4e86\u4e00\u4e2a\u5927\u95ee\u9898: \u5c31\u662f\u591a\u4e2a\u5ba2\u6237\u7aef\u5728\u8ddf\u670d\u52a1\u5668\u53bb\u4ea4\u4e92\u7684\u65f6\u5019, \u53ea\u8981\u4f60\u5ba2\u6237\u7aef\u4e0d\u8bf4\u8bdd, \u4f60\u5c31\u6536\u4e0d\u5230\u6d88\u606f, \u867d\u7136\u8bf4\u670d\u52a1\u5668\u628a\u6d88\u606f\u8f6c\u53d1\u4e86, \u8fd9\u5c31\u76f8\u5f53\u9006\u5929!<\/p>\n<p id=\"u095fba30\">\u56e0\u4e3a\u5ba2\u6237\u7aef\u963b\u585e\u5728\u53d1\u6d88\u606f\u5f53\u4e2d. \u4f60\u60f3\u63a5\u53d7\u6d88\u606f\u5c31\u5fc5\u987b\u53d1\u6d88\u606f, \u56e0\u4e3a\u662f\u963b\u585e\u72b6\u6001\u561b, \u8fd9\u5c31\u5f88\u5751\u7239. \u6240\u4ee5\u8bf4, \u6211\u4eec\u53ef\u4ee5\u7ed9\u6bcf\u4e2a\u5ba2\u6237\u7aef\u641e\u4e24\u4e2a\u7ebf\u7a0b, \u4e00\u4e2a\u8bfb\u4e00\u4e2a\u53d1\u6d88\u606f, \u4e24\u4e2a\u5e76\u884c\u8dd1\u8fd9\u6837\u7684\u8bdd\u53ef\u80fd\u4f1a\u6bd4\u8f83\u597d.<\/p>\n<p id=\"u28200826\">\n<h3 id=\"bV9JF\">5. \u5ba2\u6237\u7aef\u8bfb\u5199\u5e76\u884c<\/h3>\n<h4 id=\"c1NWj\">5.1. \u5ba2\u6237\u7aef\u521d\u59cb\u5316<\/h4>\n<p id=\"udb8cb94c\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"495\" id=\"uac673ad8\" src=\"2025-04-2124vpeda0jle.png\" width=\"913\" \/><\/p>\n<h4 id=\"baKAN\">5.2. \u53d1\u6d88\u606f\u5ba2\u6237\u7aef\u4e0e\u6536\u6d88\u606f\u5ba2\u6237\u7aef\u89e3\u8026\u5408<\/h4>\n<p id=\"ud74206b5\">\u5ba2\u6237\u7aef\u52a0\u5165\u7ebf\u7a0b\u6a21\u5757:<\/p>\n<p id=\"u483be291\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"136\" id=\"u9065eb6e\" src=\"2025-04-21erny0sw4gk3.png\" width=\"688\" \/><\/p>\n<p id=\"u05f3ed69\">\u521b\u5efa\u8bfb\u5199\u7ebf\u7a0b\u5e76\u4e14\u542f\u52a8:<\/p>\n<p id=\"u062a041c\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"226\" id=\"u2922eba8\" src=\"2025-04-21qnityzq5pfv.png\" width=\"634\" \/><\/p>\n<p id=\"u6e1c4b44\">\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b\u5373\u53ef:<\/p>\n<p id=\"ub2349a48\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"271\" id=\"udea55fc1\" src=\"2025-04-21fgqcgvirbjp.png\" width=\"625\" \/><\/p>\n<p id=\"ub63b478c\">\u6211\u4eec\u6536\u6d88\u606f\u7684\u7ebf\u7a0b\u9700\u8981\u7528\u5230\u5957\u63a5\u5b57, \u56e0\u6b64\u7ed9\u4ed6\u4f20\u8fc7\u53bb\u5373\u53ef, \u56e0\u4e3a\u6211\u4eec\u7684 thread \u81ea\u5df1\u5c01\u88c5\u7684\u7ebf\u7a0b\u5e93\u53ea\u8981\u6c42\u4e00\u4e2a\u53c2\u6570, \u6240\u4ee5\u6211\u4eec bind \u7ed9\u4ed6\u8fc7\u53bb\u5373\u53ef.<\/p>\n<p id=\"ubb84972a\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"393\" id=\"u03b33353\" src=\"2025-04-213nnww1o25ir.png\" width=\"897\" \/><\/p>\n<p id=\"ue2da7b4a\">\u5230\u4e86\u53d1\u6d88\u606f\u5462? \u53d1\u6d88\u606f\u7684\u7ebf\u7a0b\u9700\u8981\u5957\u63a5\u5b57, \u8fd8\u9700\u8981\u77e5\u9053\u670d\u52a1\u5668\u7684 ip &#043; port. \u8fd9\u4e2a\u5730\u65b9\u8001\u5e08\u53c8\u628a\u547d\u4ee4\u884c\u53c2\u6570\u653e\u5230 main \u91cc\u4e86.<\/p>\n<p id=\"ubceb736f\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"432\" id=\"u69779310\" src=\"2025-04-21pvd2avjh5uu.png\" width=\"1104\" \/><\/p>\n<h5 id=\"dVhRz\">5.2.1. \u6536\u6d88\u606f<\/h5>\n<p id=\"u8e078011\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"691\" id=\"u289d1f4b\" src=\"2025-04-21xzeglj1tfcd.png\" width=\"1440\" \/><\/p>\n<p id=\"u9eee459b\">\u4e0a\u9762 code \u7528\u7684\u662f cerr, \u662f\u65b9\u4fbf\u7528\u6765\u91cd\u5b9a\u5411\u6253\u5370\u5230\u4e24\u4e2a\u4e0d\u540c\u7ec8\u7aef\u770b\u6548\u679c\u7684.<\/p>\n<h5 id=\"FAdfP\">5.2.2. \u53d1\u6d88\u606f<\/h5>\n<p id=\"u2afc4e75\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"661\" id=\"ua38ff57f\" src=\"2025-04-21bgrj3qrtgri.png\" width=\"1494\" \/><\/p>\n<p id=\"ub0619f6f\">\n<p id=\"ue142a39b\">\u5b9a\u4f4d\u4e00\u4e0b\u4e0d\u540c\u7684\u7ec8\u7aef\u53f7:<\/p>\n<p id=\"ufb01ff0c\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"382\" id=\"u9a3df9da\" src=\"2025-04-21ekj0swt3zxe.png\" width=\"979\" \/><\/p>\n<p id=\"uc81637df\">\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u6807\u51c6\u9519\u8bef\u5168\u90e8\u91cd\u5b9a\u5411\u5230 0 \u53f7\u7ec8\u7aef.<\/p>\n<p id=\"u64f5c67d\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"168\" id=\"ue53b50d3\" src=\"2025-04-213byxm3iowcn.png\" width=\"1078\" \/><\/p>\n<h3 id=\"nwsgF\">6. \u53c2\u8003\u4ee3\u7801<\/h3>\n<h4 id=\"bXzCB\">6.1. \u6838\u5fc3\u4ee3\u7801<\/h4>\n<p id=\"uec55e933\">ip &#043; port -&gt; \u6807\u8bc6\u5ba2\u6237\u7aef\u7684\u552f\u4e00\u6027.<\/p>\n<p>#include &#034;UdpServer.hpp&#034;<br \/>\n#include &#034;Route.hpp&#034;<\/p>\n<p>#include &lt;memory&gt;<\/p>\n<p>\/\/ .\/udp_server local-port<br \/>\n\/\/ .\/udp_server 8888<br \/>\nint 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; local-port&#034; &lt;&lt; std::endl;<br \/>\n        exit(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<\/p>\n<p>    EnableScreen();<\/p>\n<p>    Route messageRoute; \/\/ \u5b9e\u4f8b\u5316\u8f6c\u53d1\u6a21\u5757<\/p>\n<p>    service_t message_route &#061; std::bind(&amp;Route::Forward,\\\\<br \/>\n        &amp;messageRoute, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);<br \/>\n    \/\/ \u628a\u8f6c\u53d1\u6a21\u5757\u4e2d\u7684\u8f6c\u53d1\u51fd\u6570\u7ed9\u5230\u670d\u52a1\u5668\u7528\u6765\u7ed1\u5b9a\u5230\u4e00\u8d77. <\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(message_route, port); \/\/C&#043;&#043;14\u7684\u6807\u51c6<br \/>\n    usvr-&gt;InitServer();<br \/>\n    usvr-&gt;Start();<br \/>\n    return 0;<br \/>\n}<br \/>\n#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<\/p>\n<p>#include &#034;nocopy.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n\/*<br \/>\n* \u670d\u52a1\u5668\u7528\u6765\u8bfb\u53d6\u6570\u636e<br \/>\n* \u8bfb\u53d6\u7684\u6570\u636e\u6709\u4e24\u4e2a\u4e1c\u897f<br \/>\n* &#8211; \u7528\u6237\u53d1\u9001\u7684\u6570\u636e\/\u4fe1\u606f -&gt; \u4ea4\u7ed9 [\u8def\u7531\u8f6c\u53d1\u6a21\u5757] \u8fdb\u884c\u5904\u7406.<br \/>\n* &#8211; \u7528\u6237\u672c\u8eab\u7684\u4fe1\u606f -&gt; ip &#043; port \u6784\u6210\u4e00\u4e2a \u5728\u7ebf\u7528\u6237\u5217\u8868.<br \/>\n* [\u8def\u7531\u8f6c\u53d1\u6a21\u5757] \u6839\u636e \u5728\u7ebf\u7528\u6237\u5217\u8868 \u6765\u8f6c\u53d1\u7ed9 \u6240\u6709\u4eba \u6d88\u606f.<br \/>\n* \u7ebf\u7a0b\u6c60\u5e2e\u52a9\u8def\u7531\u8f6c\u53d1\u6a21\u5757\u8fdb\u884c\u8f6c\u53d1. <\/p>\n<p>\u76ee\u6807: \u670d\u52a1\u5668, \u8f6c\u53d1\u6a21\u5757 \u4ee5\u53ca \u7ebf\u7a0b\u6c60 \u5168\u90e8\u89e3\u8026\u5408!<br \/>\n*\/<br \/>\nusing namespace log_ns;<\/p>\n<p>static const int gsockfd &#061; -1;<br \/>\nstatic const uint16_t glocalport &#061; 8888;<\/p>\n<p>enum<br \/>\n{<br \/>\n    SOCKET_ERROR &#061; 1,<br \/>\n    BIND_ERROR<br \/>\n};<\/p>\n<p>using service_t &#061; std::function&lt;void(int, const std::string &amp;message, InetAddr &amp;who)&gt;;<br \/>\n\/\/ \u8f6c\u53d1\u6a21\u5757\u7684\u51fd\u6570\u6307\u9488. -&gt; \u5e2e\u52a9\u6211\u4eec\u670d\u52a1\u5668\u6765\u8f6c\u53d1\u6d88\u606f\u7684.<br \/>\n\/\/ \u53c2\u65701: \u4ece\u54ea\u4e2a\u5957\u63a5\u5b57\u53d1\u6d88\u606f?<br \/>\n\/\/ \u53c2\u65702: message -&gt; \u8f6c\u53d1\u7684\u6d88\u606f\u5185\u5bb9<br \/>\n\/\/ \u53c2\u65703: \u8c01\u53d1\u7684\u8fd9\u4e2a\u6d88\u606f -&gt; \u56e0\u4e3a\u9700\u8981\u7528\u5230\u7528\u6237\u7684ip &#043; port\u6765\u6807\u8bc6\u7528\u6237\u7684\u552f\u4e00\u6027. <\/p>\n<p>\/\/ UdpServer user(&#034;192.1.1.1&#034;, 8899);<br \/>\n\/\/ \u4e00\u822c\u670d\u52a1\u5668\u4e3b\u8981\u662f\u7528\u6765\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u8bfb\u53d6\u548c\u5199\u5165\u7684\u3002IO\u7684<br \/>\n\/\/ \u670d\u52a1\u5668IO\u903b\u8f91 \u548c \u4e1a\u52a1\u903b\u8f91 \u89e3\u8026<br \/>\nclass UdpServer : public nocopy<br \/>\n{<br \/>\npublic:<br \/>\n    UdpServer(service_t func, uint16_t localport &#061; glocalport)<br \/>\n        : _func(func),<br \/>\n          _sockfd(gsockfd),<br \/>\n          _localport(localport),<br \/>\n          _isrunning(false)<br \/>\n    {<br \/>\n    }<br \/>\n    void InitServer()<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efasocket\u6587\u4ef6<br \/>\n        _sockfd &#061; ::socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n        if (_sockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(FATAL, &#034;socket error\\\\n&#034;);<br \/>\n            exit(SOCKET_ERROR);<br \/>\n        }<br \/>\n        LOG(DEBUG, &#034;socket create success, _sockfd: %d\\\\n&#034;, _sockfd); \/\/ 3<\/p>\n<p>        \/\/ 2. bind<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_localport);<br \/>\n        \/\/ local.sin_addr.s_addr &#061; inet_addr(_localip.c_str()); \/\/ 1. \u9700\u89814\u5b57\u8282IP 2. \u9700\u8981\u7f51\u7edc\u5e8f\u5217\u7684IP &#8212; \u6682\u65f6<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u670d\u52a1\u5668\u7aef&#xff0c;\u8fdb\u884c\u4efb\u610fIP\u5730\u5740\u7ed1\u5b9a<\/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(FATAL, &#034;bind error\\\\n&#034;);<br \/>\n            exit(BIND_ERROR);<br \/>\n        }<br \/>\n        LOG(DEBUG, &#034;socket bind success\\\\n&#034;);<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        _isrunning &#061; true;<br \/>\n        char message[1024];<br \/>\n        while (_isrunning)<br \/>\n        {<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<br \/>\n            ssize_t n &#061; recvfrom(_sockfd, message, sizeof(message) &#8211; 1, 0, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                InetAddr addr(peer);<br \/>\n                message[n] &#061; 0; \/\/ \u6536\u5230\u6d88\u606f\u7684\u7f13\u51b2\u533a.<br \/>\n                LOG(DEBUG, &#034;[%s]# %s\\\\n&#034;, addr.AddrStr().c_str(), message); \/\/ \u63d0\u793a\u4e00\u4e0b\u662f\u5426\u63a5\u53d7\u6d88\u606f\u6210\u529f. <\/p>\n<p>                _func(_sockfd, message, addr); \/\/ \u628a\u8f6c\u53d1\u4efb\u52a1\u7ed9\u5230\u8f6c\u53d1\u6a21\u5757\u8fdb\u884c\u5904\u7406.<br \/>\n                LOG(DEBUG, &#034;return udpserver\\\\n&#034;); \/\/ \u63d0\u793a\u56de\u8c03\u5b8c\u6210!<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                std::cout &lt;&lt; &#034;recvfrom ,  error&#034; &lt;&lt; std::endl;<br \/>\n            }<br \/>\n        }<br \/>\n        _isrunning &#061; false;<br \/>\n    }<br \/>\n    ~UdpServer()<br \/>\n    {<br \/>\n        if (_sockfd &gt; gsockfd)<br \/>\n            ::close(_sockfd);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _sockfd; \/\/ \u8bfb\u5199\u90fd\u7528\u540c\u4e00\u4e2asockfd&#xff0c; \u53cd\u5e94\u8bf4\u660e&#xff1a;UDP\u662f \u5168\u53cc\u5de5 \u901a\u4fe1\u7684&#xff01;<br \/>\n    uint16_t _localport;<br \/>\n    \/\/ std::string _localip; \/\/ TODO:\u540e\u9762\u4e13\u95e8\u8981\u5904\u7406\u4e00\u4e0b\u8fd9\u4e2aIP<br \/>\n    bool _isrunning;<\/p>\n<p>    service_t _func; \/\/ \u8be5\u51fd\u6570\u7528\u6765\u670d\u52a1\u5668\u56de\u8c03\u8f6c\u53d1\u6a21\u5757, \u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6307\u9488.<br \/>\n};<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &#034;Thread.hpp&#034;<\/p>\n<p>\/*<br \/>\n&#8211; \u5ba2\u6237\u7aef\u8981\u4e00\u8fb9\u8bfb\u4e00\u8fb9\u5199 -&gt; \u5ba2\u6237\u7aef\u8981\u591a\u7ebf\u7a0b\u5316!<br \/>\n&#8211; \u7ebf\u7a0b1: \u8bfb\u6570\u636e \u4ece\u670d\u52a1\u7aef\u90a3\u8fb9\u968f\u65f6\u63a5\u53d7\u6d88\u606f, \u5e76\u7acb\u523b\u663e\u793a\u5230\u7ec8\u7aef\u4e0a.<br \/>\n&#8211; \u7ebf\u7a0b2: \u53d1\u6d88\u606f, \u4ece\u952e\u76d8\u4e0a\u83b7\u53d6\u7528\u6237\u7684\u6d88\u606f, \u663e\u793a\u5230\u7ec8\u7aef\u4e0a, \u5e76\u7acb\u523b\u53d1\u9001\u7ed9\u670d\u52a1\u5668.<br \/>\n*\/<br \/>\n\/\/ .\/udpclient 127.0.0.1 8888 2&gt;\/dev\/pts\/0 -&gt; \u628a.\/udpclient\u8fdb\u7a0b\u7684\u6807\u51c6\u9519\u8bef\u4fe1\u606f\u90fd\u91cd\u5b9a\u5411\u52300\u53f7\u865a\u62df\u7ec8\u7aef\u4e0b, \u65b9\u4fbf\u89c2\u5bdf\u73b0\u8c61.<br \/>\nusing namespace ThreadMoudle;<\/p>\n<p>int InitClient()<br \/>\n{<br \/>\n    int sockfd &#061; ::socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n    if (sockfd &lt; 0)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;create socket error&#034; &lt;&lt; std::endl;<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    return sockfd;<br \/>\n}<\/p>\n<p>\/\/ \u8bfb\u7ebf\u7a0b: \u9700\u8981\u77e5\u9053\u4ece\u54ea\u4e2asocket\u8bfb,<br \/>\nvoid RecvMessage(int sockfd, const std::string &amp;name)<br \/>\n{<br \/>\n    \/\/ \u63a5\u53d7\u6d88\u606f\u662f\u4e0d\u65ad\u53bb\u5faa\u73af\u63a5\u53d7\u7684.<br \/>\n    while (true)<br \/>\n    {<br \/>\n        struct sockaddr_in peer;<br \/>\n        socklen_t len &#061; sizeof(peer);<br \/>\n        char buffer[1024];<br \/>\n        int n &#061; recvfrom(sockfd, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            buffer[n] &#061; 0;<br \/>\n            std::cerr &lt;&lt; buffer &lt;&lt; std::endl; \/\/ \u8bfb\u53d6\u6210\u529f, \u54b1\u4eec\u6253\u5370\u51fa\u6765.<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            std::cerr &lt;&lt; &#034;recvfrom error&#034; &lt;&lt; std::endl; \/\/ \u8bfb\u53d6\u5931\u8d25, \u54b1\u4eec\u63d0\u793a\u4e00\u4e0b.<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>void SendMessage(int sockfd, std::string serverip, uint16_t serverport, const std::string &amp;name)<br \/>\n{<br \/>\n    \/\/ \u586b\u5199\u76ee\u6807\u7ed3\u6784\u4f53<br \/>\n    struct sockaddr_in server;<br \/>\n    memset(&amp;server, 0, sizeof(server));<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_port &#061; htons(serverport);<br \/>\n    server.sin_addr.s_addr &#061; inet_addr(serverip.c_str());<\/p>\n<p>    \/\/ \u51c6\u5907\u8981\u53d1\u9001\u7684\u5185\u5bb9\u7684\u524d\u7f00.<br \/>\n    std::string cli_profix &#061; name &#043; &#034;# &#034;; \/\/ sender-thread# \u4f60\u597d<br \/>\n    while (true)<br \/>\n    {<br \/>\n        std::string line; \/\/ \u7f13\u51b2\u533a<br \/>\n        std::cout &lt;&lt; cli_profix; \/\/ \u6253\u5370\u53d1\u9001\u524d\u7f00<br \/>\n        std::getline(std::cin, line); \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6d88\u606f<br \/>\n        int n &#061; sendto(sockfd, line.c_str(), line.size(), 0, (struct sockaddr *)&amp;server, sizeof(server)); \/\/ \u628a\u6d88\u606f\u53d1\u7ed9server.<br \/>\n        if (n &lt;&#061; 0)<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    \/\/ \u68c0\u67e5\u53c2\u6570<br \/>\n    if (argc !&#061; 3)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; server-ip server-port&#034; &lt;&lt; std::endl;<br \/>\n        exit(0);<br \/>\n    }<\/p>\n<p>    std::string serverip &#061; argv[1];<br \/>\n    uint16_t serverport &#061; std::stoi(argv[2]);<br \/>\n    int sockfd &#061; InitClient();<\/p>\n<p>    \/\/ \u6536\u6d88\u606f\u7684\u7ebf\u7a0b -&gt; \u5e76\u628a\u6536\u6d88\u606f\u7684\u51fd\u6570\u7ed9\u5230\u8be5\u7ebf\u7a0b<br \/>\n    Thread recver(&#034;recver-thread&#034;, std::bind(&amp;RecvMessage, sockfd, std::placeholders::_1));<br \/>\n    \/\/ string \u7528\u6765\u8ba9Thread\u6807\u8bc6\u7ebf\u7a0b\u540d, func(string), \u662f\u4e3a\u4e86\u65b9\u4fbf\u5728func\u91cc\u6253\u5370\u7ebf\u7a0b\u540d.<br \/>\n    \/\/ \u53d1\u6d88\u606f\u7684\u7ebf\u7a0b -&gt; \u5e76\u628a\u53d1\u6d88\u606f\u7684\u51fd\u6570\u7ed9\u5230\u8be5\u7ebf\u7a0b<br \/>\n    Thread sender(&#034;sender-thread&#034;, std::bind(&amp;SendMessage, sockfd, serverip, serverport, std::placeholders::_1));<\/p>\n<p>    \/\/ \u4e24\u4e2a\u7ebf\u7a0b\u542f\u52a8\u8d77\u6765<br \/>\n    recver.Start();<br \/>\n    sender.Start();<\/p>\n<p>    \/\/ \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b<br \/>\n    recver.Join();<br \/>\n    sender.Join();<\/p>\n<p>    ::close(sockfd); \/\/ \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    return 0;<br \/>\n}<br \/>\n#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;ThreadPool.hpp&#034;<br \/>\n#include &#034;LockGuard.hpp&#034;<\/p>\n<p>\/\/ class user<br \/>\n\/\/ {};<\/p>\n<p>using task_t &#061; std::function&lt;void()&gt;;<\/p>\n<p>class Route<br \/>\n{<br \/>\npublic:<br \/>\n    Route()<br \/>\n    {<br \/>\n        pthread_mutex_init(&amp;_mutex, nullptr);<br \/>\n    }<br \/>\n    \/\/ \u68c0\u67e5\u7528\u6237\u662f\u5426\u5728\u5728\u7ebf\u7528\u6237\u5217\u8868\u5f53\u4e2d &#043; \u5bf9\u5e94\u7684\u884c\u4e3a.<br \/>\n    \/\/ \u4e0d\u5728 -&gt; \u6dfb\u52a0\u7528\u6237.<br \/>\n    \/\/ \u5728 -&gt; \u4ec0\u4e48\u90fd\u4e0d\u505a.<br \/>\n    void CheckOnlineUser(InetAddr &amp;who)<br \/>\n    {<br \/>\n        LockGuard lockguard(&amp;_mutex);<br \/>\n        for (auto &amp;user : _online_user)<br \/>\n        {<br \/>\n            if (user &#061;&#061; who) \/\/ \u5982\u679c\u5b58\u5728, \u5c31\u4ec0\u4e48\u90fd\u4e0d\u505a. -&gt; \u91cd\u8f7dInetAddr\u7684\u6bd4\u8f83bool operator&#061;&#061;().<br \/>\n            {<br \/>\n                LOG(DEBUG, &#034;%s is exists\\\\n&#034;, who.AddrStr().c_str()); \/\/ \u5b58\u5728\u7684\u7528\u6237\u6d88\u606f\u63d0\u793a.<br \/>\n                return; \/\/ \u76f4\u63a5\u9000\u51fa<br \/>\n            }<br \/>\n        }<br \/>\n        LOG(DEBUG, &#034;%s is not exists, add it\\\\n&#034;, who.AddrStr().c_str()); \/\/ \u4e0d\u5b58\u5728\u7684\u7528\u6237\u4e5f\u63d0\u793a\u4e00\u4e0b.<br \/>\n        _online_user.push_back(who); \/\/ \u4e0d\u5728, \u6211\u4eec\u5c31\u6dfb\u52a0\u8fdb\u53bb.<br \/>\n    }<br \/>\n    \/\/ for test<\/p>\n<p>    \/\/ \u5728\u7528\u6237\u5217\u8868\u5f53\u4e2d\u79fb\u9664\u7528\u6237.<br \/>\n    void Offline(InetAddr &amp;who)<br \/>\n    {<br \/>\n        LockGuard lockguard(&amp;_mutex); \/\/ \u52a0\u9501.<br \/>\n        auto iter &#061; _online_user.begin();<br \/>\n        for (; iter !&#061; _online_user.end(); iter&#043;&#043;)<br \/>\n        {<br \/>\n            if (*iter &#061;&#061; who)<br \/>\n            {<br \/>\n                LOG(DEBUG, &#034;%s is offline\\\\n&#034;, who.AddrStr().c_str()); \/\/ \u65e5\u5fd7: \u63d0\u793a\u67d0\u7528\u6237\u79bb\u5f00.<br \/>\n                _online_user.erase(iter);<br \/>\n                break; \/\/ \u8fed\u4ee3\u5668\u5931\u6548, \u76f4\u63a5break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void ForwardHelper(int sockfd, const std::string message, InetAddr who)<br \/>\n    {<br \/>\n        \/\/ \u4ece\u54ea\u8f6c\u53d1, \u54ea\u4e2a\u5957\u63a5\u5b57? -&gt; sockfd<br \/>\n        \/\/ \u8f6c\u53d1\u5565\u6d88\u606f? -&gt; message<br \/>\n        \/\/ \u8c01\u53d1\u7684? -&gt; who<br \/>\n        LockGuard lockguard(&amp;_mutex); \/\/ \u52a0\u9501.<br \/>\n        std::string send_message &#061; &#034;[&#034; &#043; who.AddrStr() &#043; &#034;]# &#034; &#043; message; \/\/ \u51c6\u5907\u8981\u8f6c\u53d1\u7684\u6d88\u606f<br \/>\n        for (auto &amp;user : _online_user)<br \/>\n        {<br \/>\n            struct sockaddr_in peer &#061; user.Addr(); \/\/ Addr(): \u8fd4\u56de\u7684\u662f\u7f51\u7edc\u5e8f\u5217\u7684\u5957\u63a5\u5b57\u4fe1\u606f.<br \/>\n            LOG(DEBUG, &#034;Forward message to %s, message is %s\\\\n&#034;, user.AddrStr().c_str(), send_message.c_str()); \/\/ debug: \u5373\u5c06\u8f6c\u53d1\u7684\u6d88\u606f\u4e5f\u63d0\u793a\u4e00\u4e0b.<br \/>\n            ::sendto(sockfd, send_message.c_str(), send_message.size(), 0, (struct sockaddr *)&amp;peer, sizeof(peer));<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6d88\u606f\u8f6c\u53d1<br \/>\n    void Forward(int sockfd, const std::string &amp;message, InetAddr &amp;who)<br \/>\n    {<br \/>\n        \/\/ 1. \u8be5\u7528\u6237\u662f\u5426\u5728 \u5728\u7ebf\u7528\u6237\u5217\u8868\u4e2d\u5462&#xff1f;\u5982\u679c\u5728&#xff0c;\u4ec0\u4e48\u90fd\u4e0d\u505a&#xff0c;\u5982\u679c\u4e0d\u5728&#xff0c;\u81ea\u52a8\u6dfb\u52a0\u5230_online_user<br \/>\n        CheckOnlineUser(who);<\/p>\n<p>        \/\/ 1.1 \u7528\u6237\u53ef\u9009\u62e9\u9000\u51fa: message &#061;&#061; &#034;QUIT&#034; &#034;Q&#034;<br \/>\n        if (message &#061;&#061; &#034;QUIT&#034; || message &#061;&#061; &#034;Q&#034;)<br \/>\n        {<br \/>\n            Offline(who);<br \/>\n        }<\/p>\n<p>        \/\/ 2. who \u4e00\u5b9a\u5728_online_user\u5217\u8868\u91cc\u9762<br \/>\n        \/\/ ForwardHelper(sockfd, message); \/\/ \u8fd9\u6837\u662f\u8def\u7531\u6a21\u5757\/\u670d\u52a1\u5668\u8fdb\u884c\u6d88\u606f\u8f6c\u53d1, \u6211\u4eec\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5c31\u7b97\u628a\u8fd9\u4e2a\u8f6c\u53d1\u4efb\u52a1\u7ed9\u5230\u7ebf\u7a0b.<br \/>\n        task_t t &#061; std::bind(&amp;Route::ForwardHelper, this, sockfd, message, who); \/\/ \u5305\u88c5\u4e00\u4e0b\u8f6c\u53d1\u51fd\u6570. \u56e0\u4e3a\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u53ea\u63a5\u53d7void()\u7c7b\u578b\u7684\u884c\u4e3a.<br \/>\n        ThreadPool&lt;task_t&gt;::GetInstance()-&gt;Equeue(t); \/\/ \u542f\u52a8\u7ebf\u7a0b\u6c60, \u83b7\u53d6\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5355\u4f8b\u5bf9\u8c61 -&gt; \u6d3e\u53d1\u4efb\u52a1\u7ed9\u4ed6\u7684\u5b50\u7ebf\u7a0b.<br \/>\n    }<br \/>\n    ~Route()<br \/>\n    {<br \/>\n        pthread_mutex_destroy(&amp;_mutex);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    std::vector&lt;InetAddr&gt; _online_user; \/\/ \u7ef4\u62a4\u5728\u7ebf\u7528\u6237\u5217\u8868.<br \/>\n    pthread_mutex_t _mutex; \/\/ \u7528\u6765\u4fdd\u62a4\u516c\u5171\u8d44\u6e90.<br \/>\n};<br \/>\n#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>class InetAddr<br \/>\n{<br \/>\nprivate:<br \/>\n    void ToHost(const struct sockaddr_in &amp;addr)<br \/>\n    {<br \/>\n        _port &#061; ntohs(addr.sin_port);<br \/>\n        \/\/ _ip &#061; inet_ntoa(addr.sin_addr);<br \/>\n        char ip_buf[32];<br \/>\n        \/\/ inet_p to n<br \/>\n        \/\/ p: process<br \/>\n        \/\/ n: net<br \/>\n        \/\/ inet_pton(int af, const char *src, void *dst);<br \/>\n        \/\/ inet_pton(AF_INET, ip.c_str(), &amp;addr.sin_addr.s_addr);<br \/>\n        ::inet_ntop(AF_INET, &amp;addr.sin_addr, ip_buf, sizeof(ip_buf));<br \/>\n        _ip &#061; ip_buf;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    InetAddr(const struct sockaddr_in &amp;addr):_addr(addr)<br \/>\n    {<br \/>\n        ToHost(addr);<br \/>\n    }<\/p>\n<p>    \/\/ ip &#043; port \u552f\u4e00\u6027:<br \/>\n    \/\/ \u4e00\u822c\u6765\u8bf4ip\u552f\u4e00\u5c31\u7b97\u662f\u552f\u4e00\u7684.<br \/>\n    \/\/ \u8fd9\u91cc\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5, \u628aport\u4e5f\u641e\u6210\u533a\u5206\u552f\u4e00\u6027\u7684.<br \/>\n    bool operator &#061;&#061; (const InetAddr &amp;addr)<br \/>\n    {<br \/>\n        return (this-&gt;_ip &#061;&#061; addr._ip &amp;&amp; this-&gt;_port &#061;&#061; addr._port);<br \/>\n    }<br \/>\n    std::string Ip()<br \/>\n    {<br \/>\n        return _ip;<br \/>\n    }<br \/>\n    uint16_t Port()<br \/>\n    {<br \/>\n        return _port;<br \/>\n    }<br \/>\n    struct sockaddr_in Addr()<br \/>\n    {<br \/>\n        return _addr;<br \/>\n    }<br \/>\n    \/\/ \u7528\u6765\u65b9\u4fbf\u8c03\u8bd5, \u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32, \u65b9\u4fbf\u6253\u5370.<br \/>\n    std::string AddrStr()<br \/>\n    {<br \/>\n        return _ip &#043; &#034;:&#034; &#043; std::to_string(_port);<br \/>\n    }<br \/>\n    ~InetAddr()<br \/>\n    {<br \/>\n    }<\/p>\n<p>private:<br \/>\n    std::string _ip;<br \/>\n    uint16_t _port;<br \/>\n    struct sockaddr_in _addr;<br \/>\n}; <\/p>\n<h4 id=\"rNHuw\">6.2. \u5176\u4ed6\u4ee3\u7801<\/h4>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>namespace ThreadMoudle<br \/>\n{<br \/>\n    \/\/ \u7ebf\u7a0b\u8981\u6267\u884c\u7684\u65b9\u6cd5&#xff0c;\u540e\u9762\u6211\u4eec\u968f\u65f6\u8c03\u6574<br \/>\n    \/\/ typedef void (*func_t)(ThreadData *td); \/\/ \u51fd\u6570\u6307\u9488\u7c7b\u578b<\/p>\n<p>    \/\/ typedef std::function&lt;void()&gt; func_t;<br \/>\n    using func_t &#061; std::function&lt;void(const std::string&amp;)&gt;;<\/p>\n<p>    class Thread<br \/>\n    {<br \/>\n    public:<br \/>\n        void Excute()<br \/>\n        {<br \/>\n            _isrunning &#061; true;<br \/>\n            _func(_name);<br \/>\n            _isrunning &#061; false;<br \/>\n        }<br \/>\n    public:<br \/>\n        Thread(const std::string &amp;name, func_t func):_name(name), _func(func)<br \/>\n        {<br \/>\n        }<br \/>\n        static void *ThreadRoutine(void *args) \/\/ \u65b0\u7ebf\u7a0b\u90fd\u4f1a\u6267\u884c\u8be5\u65b9\u6cd5&#xff01;<br \/>\n        {<br \/>\n            Thread *self &#061; static_cast&lt;Thread*&gt;(args); \/\/ \u83b7\u5f97\u4e86\u5f53\u524d\u5bf9\u8c61<br \/>\n            self-&gt;Excute();<br \/>\n            return nullptr;<br \/>\n        }<br \/>\n        bool Start()<br \/>\n        {<br \/>\n            int n &#061; ::pthread_create(&amp;_tid, nullptr, ThreadRoutine, this);<br \/>\n            if(n !&#061; 0) return false;<br \/>\n            return true;<br \/>\n        }<br \/>\n        std::string Status()<br \/>\n        {<br \/>\n            if(_isrunning) return &#034;running&#034;;<br \/>\n            else return &#034;sleep&#034;;<br \/>\n        }<br \/>\n        void Stop()<br \/>\n        {<br \/>\n            if(_isrunning)<br \/>\n            {<br \/>\n                ::pthread_cancel(_tid);<br \/>\n                _isrunning &#061; false;<br \/>\n            }<br \/>\n        }<br \/>\n        void Join()<br \/>\n        {<br \/>\n            ::pthread_join(_tid, nullptr);<br \/>\n        }<br \/>\n        std::string Name()<br \/>\n        {<br \/>\n            return _name;<br \/>\n        }<br \/>\n        ~Thread()<br \/>\n        {<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::string _name;<br \/>\n        pthread_t _tid;<br \/>\n        bool _isrunning;<br \/>\n        func_t _func; \/\/ \u7ebf\u7a0b\u8981\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    };<br \/>\n} \/\/ namespace ThreadModle<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &#034;Thread.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;LockGuard.hpp&#034;<\/p>\n<p>using namespace ThreadMoudle;<br \/>\nusing namespace log_ns;<\/p>\n<p>static const int gdefaultnum &#061; 5;<\/p>\n<p>void test()<br \/>\n{<br \/>\n    while (true)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;hello world&#034; &lt;&lt; std::endl;<br \/>\n        sleep(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>template &lt;typename T&gt;<br \/>\nclass ThreadPool<br \/>\n{<br \/>\nprivate:<br \/>\n    void LockQueue()<br \/>\n    {<br \/>\n        pthread_mutex_lock(&amp;_mutex);<br \/>\n    }<br \/>\n    void UnlockQueue()<br \/>\n    {<br \/>\n        pthread_mutex_unlock(&amp;_mutex);<br \/>\n    }<br \/>\n    void Wakeup()<br \/>\n    {<br \/>\n        pthread_cond_signal(&amp;_cond);<br \/>\n    }<br \/>\n    void WakeupAll()<br \/>\n    {<br \/>\n        pthread_cond_broadcast(&amp;_cond);<br \/>\n    }<br \/>\n    void Sleep()<br \/>\n    {<br \/>\n        pthread_cond_wait(&amp;_cond, &amp;_mutex);<br \/>\n    }<br \/>\n    bool IsEmpty()<br \/>\n    {<br \/>\n        return _task_queue.empty();<br \/>\n    }<br \/>\n    void HandlerTask(const std::string &amp;name) \/\/ this<br \/>\n    {<br \/>\n        while (true)<br \/>\n        {<br \/>\n            \/\/ \u53d6\u4efb\u52a1<br \/>\n            LockQueue();<br \/>\n            while (IsEmpty() &amp;&amp; _isrunning)<br \/>\n            {<br \/>\n                _sleep_thread_num&#043;&#043;;<br \/>\n                LOG(INFO, &#034;%s thread sleep begin!\\\\n&#034;, name.c_str());<br \/>\n                Sleep();<br \/>\n                LOG(INFO, &#034;%s thread wakeup!\\\\n&#034;, name.c_str());<br \/>\n                _sleep_thread_num&#8211;;<br \/>\n            }<br \/>\n            \/\/ \u5224\u5b9a\u4e00\u79cd\u60c5\u51b5<br \/>\n            if (IsEmpty() &amp;&amp; !_isrunning)<br \/>\n            {<br \/>\n                UnlockQueue();<br \/>\n                LOG(INFO, &#034;%s thread quit\\\\n&#034;, name.c_str());<br \/>\n                break;<br \/>\n            }<\/p>\n<p>            \/\/ \u6709\u4efb\u52a1<br \/>\n            T t &#061; _task_queue.front();<br \/>\n            _task_queue.pop();<br \/>\n            UnlockQueue();<\/p>\n<p>            \/\/ \u5904\u7406\u4efb\u52a1<br \/>\n            t(); \/\/ \u5904\u7406\u4efb\u52a1&#xff0c;\u6b64\u5904\u4e0d\u7528\/\u4e0d\u80fd\u5728\u4e34\u754c\u533a\u4e2d\u5904\u7406<br \/>\n            \/\/ std::cout &lt;&lt; name &lt;&lt; &#034;: &#034; &lt;&lt; t.result() &lt;&lt; std::endl;<br \/>\n            \/\/ LOG(DEBUG, &#034;hander task done, task is : %s\\\\n&#034;, t.result().c_str());<br \/>\n        }<br \/>\n    }<br \/>\n    void Init()<br \/>\n    {<br \/>\n        func_t func &#061; std::bind(&amp;ThreadPool::HandlerTask, this, std::placeholders::_1);<br \/>\n        for (int i &#061; 0; i &lt; _thread_num; i&#043;&#043;)<br \/>\n        {<br \/>\n            std::string threadname &#061; &#034;thread-&#034; &#043; std::to_string(i &#043; 1);<br \/>\n            _threads.emplace_back(threadname, func);<br \/>\n            LOG(DEBUG, &#034;construct thread %s done, init success\\\\n&#034;, threadname.c_str());<br \/>\n        }<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        _isrunning &#061; true;<br \/>\n        for (auto &amp;thread : _threads)<br \/>\n        {<br \/>\n            LOG(DEBUG, &#034;start thread %s done.\\\\n&#034;, thread.Name().c_str());<br \/>\n            thread.Start();<br \/>\n        }<br \/>\n    }<br \/>\n    ThreadPool(int thread_num &#061; gdefaultnum)<br \/>\n        : _thread_num(thread_num), _isrunning(false), _sleep_thread_num(0)<br \/>\n    {<br \/>\n        pthread_mutex_init(&amp;_mutex, nullptr);<br \/>\n        pthread_cond_init(&amp;_cond, nullptr);<br \/>\n    }<br \/>\n    ThreadPool(const ThreadPool&lt;T&gt; &amp;) &#061; delete;<br \/>\n    void operator&#061;(const ThreadPool&lt;T&gt; &amp;) &#061; delete;<\/p>\n<p>public:<br \/>\n    void Stop()<br \/>\n    {<br \/>\n        LockQueue();<br \/>\n        _isrunning &#061; false;<br \/>\n        WakeupAll();<br \/>\n        UnlockQueue();<br \/>\n        LOG(INFO, &#034;Thread Pool Stop Success!\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u5982\u679c\u662f\u591a\u7ebf\u7a0b\u83b7\u53d6\u5355\u4f8b\u5462&#xff1f;<br \/>\n    static ThreadPool&lt;T&gt; *GetInstance()<br \/>\n    {<br \/>\n        if (_tp &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            LockGuard lockguard(&amp;_sig_mutex);<br \/>\n            if (_tp &#061;&#061; nullptr)<br \/>\n            {<br \/>\n                LOG(INFO, &#034;create threadpool\\\\n&#034;);<br \/>\n                \/\/ thread-1 thread-2 thread-3&#8230;.<br \/>\n                _tp &#061; new ThreadPool&lt;T&gt;();<br \/>\n                _tp-&gt;Init();<br \/>\n                _tp-&gt;Start();<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                LOG(INFO, &#034;get threadpool\\\\n&#034;);<br \/>\n            }<br \/>\n        }<br \/>\n        return _tp;<br \/>\n    }<\/p>\n<p>    void Equeue(const T &amp;in)<br \/>\n    {<br \/>\n        LockQueue();<br \/>\n        if (_isrunning)<br \/>\n        {<br \/>\n            _task_queue.push(in);<br \/>\n            if (_sleep_thread_num &gt; 0)<br \/>\n                Wakeup();<br \/>\n        }<br \/>\n        UnlockQueue();<br \/>\n    }<br \/>\n    ~ThreadPool()<br \/>\n    {<br \/>\n        pthread_mutex_destroy(&amp;_mutex);<br \/>\n        pthread_cond_destroy(&amp;_cond);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _thread_num;<br \/>\n    std::vector&lt;Thread&gt; _threads;<br \/>\n    std::queue&lt;T&gt; _task_queue;<br \/>\n    bool _isrunning;<\/p>\n<p>    int _sleep_thread_num;<\/p>\n<p>    pthread_mutex_t _mutex;<br \/>\n    pthread_cond_t _cond;<\/p>\n<p>    \/\/ \u5355\u4f8b\u6a21\u5f0f<br \/>\n    \/\/ volatile static ThreadPool&lt;T&gt; *_tp;<br \/>\n    static ThreadPool&lt;T&gt; *_tp;<br \/>\n    static pthread_mutex_t _sig_mutex;<br \/>\n};<\/p>\n<p>template &lt;typename T&gt;<br \/>\nThreadPool&lt;T&gt; *ThreadPool&lt;T&gt;::_tp &#061; nullptr;<br \/>\ntemplate &lt;typename T&gt;<br \/>\npthread_mutex_t ThreadPool&lt;T&gt;::_sig_mutex &#061; PTHREAD_MUTEX_INITIALIZER;<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>class LockGuard<br \/>\n{<br \/>\npublic:<br \/>\n    LockGuard(pthread_mutex_t *mutex):_mutex(mutex)<br \/>\n    {<br \/>\n        pthread_mutex_lock(_mutex);<br \/>\n    }<br \/>\n    ~LockGuard()<br \/>\n    {<br \/>\n        pthread_mutex_unlock(_mutex);<br \/>\n    }<br \/>\nprivate:<br \/>\n    pthread_mutex_t *_mutex;<br \/>\n};<br \/>\n#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;ctime&gt;<br \/>\n#include &lt;cstdarg&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &#034;LockGuard.hpp&#034;<\/p>\n<p>namespace log_ns<br \/>\n{<\/p>\n<p>    enum<br \/>\n    {<br \/>\n        DEBUG &#061; 1,<br \/>\n        INFO,<br \/>\n        WARNING,<br \/>\n        ERROR,<br \/>\n        FATAL<br \/>\n    };<\/p>\n<p>    std::string LevelToString(int level)<br \/>\n    {<br \/>\n        switch (level)<br \/>\n        {<br \/>\n        case DEBUG:<br \/>\n            return &#034;DEBUG&#034;;<br \/>\n        case INFO:<br \/>\n            return &#034;INFO&#034;;<br \/>\n        case WARNING:<br \/>\n            return &#034;WARNING&#034;;<br \/>\n        case ERROR:<br \/>\n            return &#034;ERROR&#034;;<br \/>\n        case FATAL:<br \/>\n            return &#034;FATAL&#034;;<br \/>\n        default:<br \/>\n            return &#034;UNKNOWN&#034;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    std::string GetCurrTime()<br \/>\n    {<br \/>\n        time_t now &#061; time(nullptr);<br \/>\n        struct tm *curr_time &#061; localtime(&amp;now);<br \/>\n        char buffer[128];<br \/>\n        snprintf(buffer, sizeof(buffer), &#034;%d-%02d-%02d %02d:%02d:%02d&#034;,<br \/>\n                 curr_time-&gt;tm_year &#043; 1900,<br \/>\n                 curr_time-&gt;tm_mon &#043; 1,<br \/>\n                 curr_time-&gt;tm_mday,<br \/>\n                 curr_time-&gt;tm_hour,<br \/>\n                 curr_time-&gt;tm_min,<br \/>\n                 curr_time-&gt;tm_sec);<br \/>\n        return buffer;<br \/>\n    }<\/p>\n<p>    class logmessage<br \/>\n    {<br \/>\n    public:<br \/>\n        std::string _level;<br \/>\n        pid_t _id;<br \/>\n        std::string _filename;<br \/>\n        int _filenumber;<br \/>\n        std::string _curr_time;<br \/>\n        std::string _message_info;<br \/>\n    };<\/p>\n<p>#define SCREEN_TYPE 1<br \/>\n#define FILE_TYPE 2<\/p>\n<p>    const std::string glogfile &#061; &#034;.\/log.txt&#034;;<br \/>\n    pthread_mutex_t glock &#061; PTHREAD_MUTEX_INITIALIZER;<\/p>\n<p>    \/\/ log.logMessage(&#034;&#034;, 12, INFO, &#034;this is a %d message ,%f, %s hellwrodl&#034;, x, , , );<br \/>\n    class Log<br \/>\n    {<br \/>\n    public:<br \/>\n        Log(const std::string &amp;logfile &#061; glogfile) : _logfile(logfile), _type(SCREEN_TYPE)<br \/>\n        {<br \/>\n        }<br \/>\n        void Enable(int type)<br \/>\n        {<br \/>\n            _type &#061; type;<br \/>\n        }<br \/>\n        void FlushLogToScreen(const logmessage &amp;lg)<br \/>\n        {<br \/>\n            printf(&#034;[%s][%d][%s][%d][%s] %s&#034;,<br \/>\n                   lg._level.c_str(),<br \/>\n                   lg._id,<br \/>\n                   lg._filename.c_str(),<br \/>\n                   lg._filenumber,<br \/>\n                   lg._curr_time.c_str(),<br \/>\n                   lg._message_info.c_str());<br \/>\n        }<br \/>\n        void FlushLogToFile(const logmessage &amp;lg)<br \/>\n        {<br \/>\n            std::ofstream out(_logfile, std::ios::app);<br \/>\n            if (!out.is_open())<br \/>\n                return;<br \/>\n            char logtxt[2048];<br \/>\n            snprintf(logtxt, sizeof(logtxt), &#034;[%s][%d][%s][%d][%s] %s&#034;,<br \/>\n                     lg._level.c_str(),<br \/>\n                     lg._id,<br \/>\n                     lg._filename.c_str(),<br \/>\n                     lg._filenumber,<br \/>\n                     lg._curr_time.c_str(),<br \/>\n                     lg._message_info.c_str());<br \/>\n            out.write(logtxt, strlen(logtxt));<br \/>\n            out.close();<br \/>\n        }<br \/>\n        void FlushLog(const logmessage &amp;lg)<br \/>\n        {<br \/>\n            \/\/ \u52a0\u8fc7\u6ee4\u903b\u8f91 &#8212; TODO<\/p>\n<p>            LockGuard lockguard(&amp;glock);<br \/>\n            switch (_type)<br \/>\n            {<br \/>\n            case SCREEN_TYPE:<br \/>\n                FlushLogToScreen(lg);<br \/>\n                break;<br \/>\n            case FILE_TYPE:<br \/>\n                FlushLogToFile(lg);<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n        void logMessage(std::string filename, int filenumber, int level, const char *format, &#8230;)<br \/>\n        {<br \/>\n            logmessage lg;<\/p>\n<p>            lg._level &#061; LevelToString(level);<br \/>\n            lg._id &#061; getpid();<br \/>\n            lg._filename &#061; filename;<br \/>\n            lg._filenumber &#061; filenumber;<br \/>\n            lg._curr_time &#061; GetCurrTime();<\/p>\n<p>            va_list ap;<br \/>\n            va_start(ap, format);<br \/>\n            char log_info[1024];<br \/>\n            vsnprintf(log_info, sizeof(log_info), format, ap);<br \/>\n            va_end(ap);<br \/>\n            lg._message_info &#061; log_info;<\/p>\n<p>            \/\/ \u6253\u5370\u51fa\u6765\u65e5\u5fd7<br \/>\n            FlushLog(lg);<br \/>\n        }<br \/>\n        ~Log()<br \/>\n        {<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _type;<br \/>\n        std::string _logfile;<br \/>\n    };<\/p>\n<p>    Log lg;<\/p>\n<p>#define LOG(Level, Format, &#8230;)                                        \\\\<br \/>\n    do                                                                 \\\\<br \/>\n    {                                                                  \\\\<br \/>\n        lg.logMessage(__FILE__, __LINE__, Level, Format, ##__VA_ARGS__); \\\\<br \/>\n    } while (0)<br \/>\n#define EnableScreen()          \\\\<br \/>\n    do                          \\\\<br \/>\n    {                           \\\\<br \/>\n        lg.Enable(SCREEN_TYPE); \\\\<br \/>\n    } while (0)<br \/>\n#define EnableFILE()          \\\\<br \/>\n    do                        \\\\<br \/>\n    {                         \\\\<br \/>\n        lg.Enable(FILE_TYPE); \\\\<br \/>\n    } while (0)<br \/>\n};<br \/>\n.PHONY:all<br \/>\nall:udpserver udpclient<\/p>\n<p>udpserver:UdpServerMain.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;14 -lpthread \/\/ \u7ebf\u7a0b\u5e93<br \/>\nudpclient:UdpClientMain.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;14 -lpthread \/\/ \u7ebf\u7a0b\u5e93<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -rf udpserver udpclient<br \/>\n#pragma once<\/p>\n<p>class nocopy<br \/>\n{<br \/>\npublic:<br \/>\n    nocopy(){}<br \/>\n    ~nocopy(){}<br \/>\n    nocopy(const nocopy&amp;) &#061; delete;<br \/>\n    const nocopy&amp; operator&#061;(const nocopy&amp;) &#061; delete;<br \/>\n};<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb826\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf13\u6b21\u3002[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)<\/p>\n","protected":false},"author":2,"featured_media":30836,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,230,43],"topic":[],"class_list":["post-30856","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-php","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \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\/30856.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb826\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf13\u6b21\u3002[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/30856.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-20T20:46:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d23219d5.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\/30856.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/30856.html\",\"name\":\"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-20T20:46:32+00:00\",\"dateModified\":\"2025-04-20T20:46:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/30856.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/30856.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/30856.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)\"}]},{\"@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":"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \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\/30856.html","og_locale":"zh_CN","og_type":"article","og_title":"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb826\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf13\u6b21\u3002[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)","og_url":"https:\/\/www.wsisp.com\/helps\/30856.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-20T20:46:32+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250420204627-68055d23219d5.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\/30856.html","url":"https:\/\/www.wsisp.com\/helps\/30856.html","name":"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-20T20:46:32+00:00","dateModified":"2025-04-20T20:46:32+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/30856.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/30856.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/30856.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[net 6] udp_chat_server\u57fa\u4e8eudp\u7684\u7b80\u5355\u804a\u5929\u5ba4(\u591a\u7ebf\u7a0b\u7684\u670d\u52a1\u5668\u4e0e\u4e1a\u52a1\u76f8\u5206\u79bb)"}]},{"@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\/30856","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=30856"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/30856\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/30836"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=30856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=30856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=30856"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=30856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}