{"id":338,"date":"2025-09-18T00:45:45","date_gmt":"2025-09-17T16:45:45","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=338"},"modified":"2025-11-24T16:36:43","modified_gmt":"2025-11-24T08:36:43","slug":"%e5%9f%ba%e4%ba%8e%e5%a4%9a%e4%ba%ba%e8%81%8a%e5%a4%a9%e5%ae%a4%e5%ad%a6%e4%b9%a0udpsocket%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=338","title":{"rendered":"\u57fa\u4e8e\u591a\u4eba\u804a\u5929\u5ba4\u5b66\u4e60udpsocket\u7f16\u7a0b"},"content":{"rendered":"\n<p>\u4ee3\u7801\u4ed3\u5e93\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/ThreadPool\">https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/ThreadPool<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"309\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-25.png\" alt=\"\" class=\"wp-image-339\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-25.png 620w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-25-300x150.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/figure>\n\n\n\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u5e26\u9886\u5927\u5bb6\u5b66\u4e60\u4e86socket\u7f16\u7a0b\u57fa\u7840\u4eca\u5929\u5c31\u5e26\u9886\u5927\u5bb6\u5b9e\u6218\u6f14\u7ec3\u4e00\u4e0b\uff0c\u591c\u4e5f\u6df1\u4e86\uff0c\u535a\u4e3b\u7801\u5b8c\u4e5f\u8981\u53bb\u4f11\u606f\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u8bbe\u8ba1\u601d\u8def<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7f51\u7edc\u901a\u4fe1\u5c42\u8bbe\u8ba1<\/h3>\n\n\n\n<p><strong>UDP\u534f\u8bae\u9009\u62e9\u7406\u7531\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\uff1a\u804a\u5929\u7cfb\u7edf\u5bf9\u5ef6\u8fdf\u654f\u611f\uff0cUDP\u65e0\u8fde\u63a5\u7279\u6027\u66f4\u9002\u5408<\/li>\n\n\n\n<li>\u5e7f\u64ad\u529f\u80fd\u5929\u7136\u652f\u6301\uff1aUDP\u5929\u7136\u652f\u6301\u4e00\u5bf9\u591a\u901a\u4fe1\u6a21\u5f0f<\/li>\n\n\n\n<li>\u8d44\u6e90\u6d88\u8017\u4f4e\uff1a\u65e0\u9700\u7ef4\u62a4TCP\u8fde\u63a5\u72b6\u6001\uff0c\u8282\u7701\u670d\u52a1\u5668\u8d44\u6e90<\/li>\n<\/ul>\n\n\n\n<p>\u5173\u952e\u8bbe\u8ba1\u8981\u70b9\uff1a<\/p>\n\n\n\n<p>\u5355\u7ebf\u7a0b\u4e8b\u4ef6\u5faa\u73af\uff1a\u4e3b\u7ebf\u7a0b\u4e13\u95e8\u8d1f\u8d23\u6d88\u606f\u63a5\u6536\uff0c\u907f\u514dIO\u963b\u585e<\/p>\n\n\n\n<p>\u5f02\u6b65\u5904\u7406\u673a\u5236\uff1a\u6536\u5230\u6d88\u606f\u540e\u7acb\u5373\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u5904\u7406\uff0c\u4e0d\u963b\u585e\u63a5\u6536\u5faa\u73af<\/p>\n\n\n\n<p>\u5730\u5740\u4fe1\u606f\u5c01\u88c5\uff1a\u5c06sockaddr_in\u7ed3\u6784\u5c01\u88c5\u4e3a\u53cb\u597d\u7684InetAddr\u5bf9\u8c61<\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6d88\u606f\u8def\u7531\u67b6\u6784<\/h3>\n\n\n\n<p><strong>\u8def\u7531\u8868\u7ba1\u7406\u7b56\u7565\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u52a8\u53d1\u73b0\u673a\u5236\uff1a\u9996\u6b21\u53d1\u9001\u6d88\u606f\u7684\u7528\u6237\u81ea\u52a8\u52a0\u5165\u5728\u7ebf\u5217\u8868<\/li>\n\n\n\n<li>\u5fc3\u8df3\u68c0\u6d4b\uff1a\u901a\u8fc7\u6d88\u606f\u6d3b\u8dc3\u5ea6\u7ef4\u6301\u7528\u6237\u5728\u7ebf\u72b6\u6001<\/li>\n\n\n\n<li>\u4f18\u96c5\u9000\u51fa\uff1a\u652f\u6301&#8221;QUIT&#8221;\u547d\u4ee4\u7684\u89c4\u8303\u9000\u51fa\u5904\u7406<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5e76\u53d1\u5904\u7406\u6a21\u578b<\/h3>\n\n\n\n<p><strong>\u7ebf\u7a0b\u6c60\u5de5\u4f5c\u6a21\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u56fa\u5b9a\u7ebf\u7a0b\u6570<\/strong>\uff1a\u9884\u5148\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u5de5\u4f5c\u7ebf\u7a0b<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u961f\u5217<\/strong>\uff1a\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u961f\u5217\u7f13\u51b2\u5f85\u5904\u7406\u6d88\u606f<\/li>\n\n\n\n<li><strong>\u6761\u4ef6\u53d8\u91cf\u540c\u6b65<\/strong>\uff1a\u5de5\u4f5c\u7ebf\u7a0b\u5728\u65e0\u4efb\u52a1\u65f6\u81ea\u52a8\u4f11\u7720<\/li>\n<\/ul>\n\n\n\n<p><strong>\u8d44\u6e90\u7ba1\u7406\u7b56\u7565\uff1a<\/strong><\/p>\n\n\n\n<p>\u8fde\u63a5\u65e0\u72b6\u6001\uff1aUDP\u7279\u6027\u4f7f\u5f97\u65e0\u9700\u7ef4\u62a4\u8fde\u63a5\u72b6\u6001<\/p>\n\n\n\n<p>\u5185\u5b58\u6c60\u4f18\u5316\uff1a\u53ef\u8003\u8651\u4f7f\u7528\u5bf9\u8c61\u6c60\u7ba1\u7406\u9891\u7e41\u521b\u5efa\u7684\u5bf9\u8c61<\/p>\n\n\n\n<p>\u6d41\u91cf\u63a7\u5236\uff1a\u901a\u8fc7\u961f\u5217\u957f\u5ea6\u5b9e\u73b0\u7b80\u5355\u7684\u80cc\u538b\u63a7\u5236<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u6838\u5fc3\u4ee3\u7801\uff0c\u5b8c\u6574\u4ee3\u7801\u8bf7\u53c2\u8003\u4ee3\u7801\u4ed3\u5e93<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u67b6\u6784\uff1a\u4e09\u5c42\u8bbe\u8ba1<\/strong><\/p>\n\n\n\n<p>1. \u7f51\u7edc\u5c42 &#8211; \u7cbe\u7b80\u7684UDP\u670d\u52a1<\/p>\n\n\n\n<p>\/\/ UDP\u670d\u52a1\u5668\u6838\u5fc3\u5faa\u73af<br>void runServer() {<br>while (running) {<br>char buffer[1024];<br>sockaddr_in clientAddr;<br>socklen_t addrLen = sizeof(clientAddr);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u63a5\u6536\u6d88\u606f\n    int received = recvfrom(sockfd, buffer, sizeof(buffer), 0,\n                           (sockaddr*)&clientAddr, &addrLen);\n\n    if (received > 0) {\n        buffer[received] = '\\0';\n        \/\/ \u5c06\u6d88\u606f\u4ea4\u7ed9\u5904\u7406\u7ebf\u7a0b\n        threadPool.enqueue([this, clientAddr, msg = std::string(buffer)] {\n            processMessage(clientAddr, msg);\n        });\n    }\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>2. \u4e1a\u52a1\u5c42 &#8211; \u667a\u80fd\u8def\u7531<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6d88\u606f\u5904\u7406\u6838\u5fc3\nvoid processMessage(sockaddr_in clientAddr, const std::string& msg) {\n    \/\/ \u81ea\u52a8\u6ce8\u518c\u65b0\u7528\u6237\n    if (!userExists(clientAddr)) {\n        addUser(clientAddr);\n    }\n    \n    \/\/ \u5e7f\u64ad\u6d88\u606f\n    std::string formattedMsg = formatMessage(clientAddr, msg);\n    broadcast(formattedMsg);\n    \n    \/\/ \u5904\u7406\u9000\u51fa\u547d\u4ee4\n    if (msg == \"QUIT\") {\n        removeUser(clientAddr);\n    }\n}<\/code><\/pre>\n\n\n\n<p>3. \u5e76\u53d1\u5c42 &#8211; \u9ad8\u6548\u7ebf\u7a0b\u6c60<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7ebf\u7a0b\u6c60\u5de5\u4f5c\u7ebf\u7a0b\nvoid workerThread() {\n    while (running) {\n        std::function<void()> task;\n        \n        {\n            std::unique_lock<std::mutex> lock(queueMutex);\n            \/\/ \u7b49\u5f85\u4efb\u52a1\n            condition.wait(lock, [this] { \n                return !taskQueue.empty() || !running; \n            });\n            \n            if (!running) break;\n            \n            task = std::move(taskQueue.front());\n            taskQueue.pop();\n        }\n        \n        \/\/ \u6267\u884c\u4efb\u52a1\n        task();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u4e5f\u4e0d\u65e9\u4e86\uff0c\u5982\u679c\u6709\u9519\u8bef\uff0c\u6b22\u8fce\u5728\u4f5c\u8005\u8bc4\u8bba\u533a\u7559\u8a00\uff0c\u665a\u5b89\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u4ed3\u5e93\uff1ahttps:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/m&#8230;<\/p>\n","protected":false},"author":1,"featured_media":339,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[5,7],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-7"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":837,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions\/837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/339"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}