{"id":118797,"date":"2026-10-28T05:18:48","date_gmt":"2026-10-27T21:18:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/118797.html"},"modified":"2026-10-28T05:18:48","modified_gmt":"2026-10-27T21:18:48","slug":"linux-%e7%bd%91%e7%bb%9c%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b%ef%bc%88%e4%ba%8c%ef%bc%89%e4%bb%8e-0-%e5%88%b0-1-%e5%ae%9e%e7%8e%b0-udp-%e5%9b%9e%e5%a3%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%8crec","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/118797.html","title":{"rendered":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E9%80%BB%E8%BE%91-toc\" style=\"margin-left:0px\">\u8bbe\u8ba1\u903b\u8f91<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E7%9B%AE%E7%9A%84-toc\" style=\"margin-left:0px\">\u8bbe\u8ba1\u76ee\u7684<\/p>\n<p id=\"%E4%BB%A3%E7%A0%81%E7%BB%93%E6%9E%84-toc\" style=\"margin-left:0px\">\u4ee3\u7801\u7ed3\u6784<\/p>\n<p id=\"EchoServer.hpp%C2%A0%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%A4%B4%E6%96%87%E4%BB%B6-toc\" style=\"margin-left:0px\">EchoServer.hpp\u00a0\u670d\u52a1\u7aef\u5934\u6587\u4ef6<\/p>\n<p id=\"1.%20%E5%A4%B4%E6%96%87%E4%BB%B6%E9%98%B2%E6%8A%A4%E4%B8%8E%E4%BE%9D%E8%B5%96%E5%BC%95%E5%85%A5-toc\" style=\"margin-left:40px\">1. \u5934\u6587\u4ef6\u9632\u62a4\u4e0e\u4f9d\u8d56\u5f15\u5165<\/p>\n<p id=\"2.%20%E7%A7%81%E6%9C%89%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F-toc\" style=\"margin-left:40px\">2. \u79c1\u6709\u6210\u5458\u53d8\u91cf<\/p>\n<p id=\"IP%20%E5%9C%B0%E5%9D%80%E5%92%8C%E7%AB%AF%E5%8F%A3%E5%8F%B7%E5%9C%A8%20UDP%20%E9%80%9A%E4%BF%A1%E4%B8%AD%E7%9A%84%E4%BD%9C%E7%94%A8-toc\" style=\"margin-left:80px\">IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u5728 UDP \u901a\u4fe1\u4e2d\u7684\u4f5c\u7528<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E6%94%B9%E5%8A%A8-toc\" style=\"margin-left:80px\">\u8bbe\u8ba1\u6539\u52a8<\/p>\n<p id=\"3.%20%E5%85%A8%E5%B1%80%E5%B8%B8%E9%87%8F%E4%B8%8E%E6%9E%9A%E4%B8%BE%E9%94%99%E8%AF%AF%E7%A0%81-toc\" style=\"margin-left:40px\">3. \u5168\u5c40\u5e38\u91cf\u4e0e\u679a\u4e3e\u9519\u8bef\u7801<\/p>\n<p id=\"4.%20%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%8E%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">4. \u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570<\/p>\n<p id=\"5.%20Init%20()%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">5. Init () \u670d\u52a1\u7aef\u521d\u59cb\u5316\u51fd\u6570<\/p>\n<p id=\"%E9%87%8D%E8%AF%86Socket-toc\" style=\"margin-left:120px\">\u91cd\u8bc6Socket<\/p>\n<p id=\"6.%C2%A0Start%20()%20%E4%B8%9A%E5%8A%A1%E5%BE%AA%E7%8E%AF%E5%90%AF%E5%8A%A8%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">6.\u00a0Start () \u4e1a\u52a1\u5faa\u73af\u542f\u52a8\u51fd\u6570<\/p>\n<p id=\"recvfrom%20%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8-toc\" style=\"margin-left:80px\">recvfrom \u7cfb\u7edf\u8c03\u7528<\/p>\n<p id=\"sendto%20%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8-toc\" style=\"margin-left:80px\">sendto \u7cfb\u7edf\u8c03\u7528<\/p>\n<p id=\"EchoServerMain.cc%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%85%A5%E5%8F%A3%E6%96%87%E4%BB%B6-toc\" style=\"margin-left:0px\">EchoServerMain.cc \u670d\u52a1\u7aef\u5165\u53e3\u6587\u4ef6<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E6%9C%8D%E5%8A%A1%E7%AB%AF%20%3A-toc\" style=\"margin-left:80px\">\u8fd0\u884c\u670d\u52a1\u7aef :<\/p>\n<p id=\"EchoClient.cc%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%96%87%E4%BB%B6%20%3A-toc\" style=\"margin-left:0px\">EchoClient.cc \u5ba2\u6237\u7aef\u6587\u4ef6 :<\/p>\n<p id=\"%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%8E%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%9A%84%E6%A0%B8%E5%BF%83%E5%BC%82%E5%90%8C-toc\" style=\"margin-left:40px\">\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u6838\u5fc3\u5f02\u540c<\/p>\n<p id=\"1.%20%E5%A4%B4%E6%96%87%E4%BB%B6%E4%B8%8E%E4%BD%BF%E7%94%A8%E6%8F%90%E7%A4%BA%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">1. \u5934\u6587\u4ef6\u4e0e\u4f7f\u7528\u63d0\u793a\u51fd\u6570<\/p>\n<p id=\"2.%20%E4%B8%BB%E5%87%BD%E6%95%B0%E5%85%A5%E5%8F%A3%20%26%20%E5%90%AF%E5%8A%A8%E5%8F%82%E6%95%B0-toc\" style=\"margin-left:40px\">2. \u4e3b\u51fd\u6570\u5165\u53e3 &amp; \u542f\u52a8\u53c2\u6570<\/p>\n<p id=\"3.%20%E5%88%9B%E5%BB%BA%20socket-toc\" style=\"margin-left:40px\">3. \u521b\u5efa socket<\/p>\n<p id=\"4.%20%E6%9E%84%E5%BB%BA%E6%9C%8D%E5%8A%A1%E7%AB%AF%20socket%20%E7%9B%AE%E6%A0%87%E5%9C%B0%E5%9D%80-toc\" style=\"margin-left:40px\">4. \u6784\u5efa\u670d\u52a1\u7aef socket \u76ee\u6807\u5730\u5740<\/p>\n<p id=\"5.%20%E5%BE%AA%E7%8E%AF%E4%BA%A4%E4%BA%92%E9%80%BB%E8%BE%91-toc\" style=\"margin-left:40px\">5. \u5faa\u73af\u4ea4\u4e92\u903b\u8f91<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%20%3A-toc\" style=\"margin-left:0px\">\u8fd0\u884c\u7ed3\u679c :<\/p>\n<p id=\"%E6%9C%AC%E5%9C%B0%E5%9B%9E%E7%8E%AF%E5%9C%B0%E5%9D%80%E9%80%9A%E4%BF%A1%20(127.0.0.1)-toc\" style=\"margin-left:40px\">\u672c\u5730\u56de\u73af\u5730\u5740\u901a\u4fe1 (127.0.0.1)<\/p>\n<p id=\"%E5%86%85%E7%BD%91%20IP%20%E9%80%9A%E4%BF%A1%20(10.0.16.7)-toc\" style=\"margin-left:40px\">\u5185\u7f51 IP \u901a\u4fe1 (10.0.16.7)<\/p>\n<p id=\"%E5%85%AC%E7%BD%91%20IP%20%E9%80%9A%E4%BF%A1%20(124.222.191.171)-toc\" style=\"margin-left:40px\">\u516c\u7f51 IP \u901a\u4fe1 (124.222.191.171)<\/p>\n<p id=\"%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%20%3A-toc\" style=\"margin-left:0px\">\u5b8c\u6574\u4ee3\u7801 :<\/p>\n<p id=\"%E6%A8%A1%E5%9E%8B%E5%9B%BE%3A-toc\" style=\"margin-left:0px\">\u6a21\u578b\u56fe:<\/p>\n<p id=\"%E2%80%8B%E7%BC%96%E8%BE%91-toc\" style=\"margin-left:0px\">\u603b\u7ed3:<\/p>\n<hr \/>\n<p>\u672c\u6587\u6211\u4eec\u5c06\u57fa\u4e8e C&#043;&#043; \u5b9e\u73b0\u4e00\u4e2a\u7ecf\u5178\u7684 UDP \u56de\u58f0\u670d\u52a1\u5668&#xff0c;\u901a\u8fc7\u6784\u5efa\u4e00\u5957\u5b8c\u6574\u7684\u5ba2\u6237\u7aef \/ \u670d\u52a1\u7aef(Server\/Client) \u901a\u4fe1\u6a21\u578b&#xff0c;\u628a\u7406\u8bba\u77e5\u8bc6\u8f6c\u5316\u4e3a\u53ef\u8fd0\u884c\u7684\u5de5\u7a0b\u4ee3\u7801\u3002<\/p>\n<h2 id=\"%E8%AE%BE%E8%AE%A1%E9%80%BB%E8%BE%91\">\u8bbe\u8ba1\u903b\u8f91<\/h2>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;UDP \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u901a\u4fe1&#xff0c;\u5c31\u50cf\u4e24\u4e2a\u4eba\u4e92\u76f8\u53d1\u77ed\u4fe1&#xff0c;\u5ba2\u6237\u7aef\u4ee3\u8868\u7528\u6237&#xff0c;\u670d\u52a1\u7aef\u4ee3\u8868\u5b98\u65b9\u670d\u52a1\u5668&#xff0c;\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u7aef\u53d1\u6d88\u606f&#xff0c;\u670d\u52a1\u7aef\u6536\u5230\u6d88\u606f\u540e\u518d\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u6211\u4eec\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u6574\u4e2a\u6d41\u7a0b&#xff0c;\u7528\u6700\u76f4\u767d\u7684\u8bdd\u8bb2\u5c31\u662f\u8fd9\u6837&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"982\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211841-6ae1153193c3a.png\" width=\"1358\" \/><\/p>\n<h2 id=\"%E8%AE%BE%E8%AE%A1%E7%9B%AE%E7%9A%84\">\u8bbe\u8ba1\u76ee\u7684<\/h2>\n<p>\u6211\u4eec\u9009\u62e9 UDP \u56de\u58f0\u670d\u52a1\u5668\u4f5c\u4e3a\u5165\u95e8\u5b9e\u6218\u6848\u4f8b&#xff0c;\u6838\u5fc3\u6709\u4e24\u70b9\u8bbe\u8ba1\u8003\u91cf&#xff1a;<\/p>\n<p>1. \u8d34\u5408 UDP \u534f\u8bae\u672c\u8d28&#xff1a;UDP \u662f\u65e0\u8fde\u63a5\u3001\u9762\u5411\u6570\u636e\u62a5\u7684\u4f20\u8f93\u534f\u8bae&#xff0c;\u65e0\u9700\u4e09\u6b21\u63e1\u624b\u5efa\u7acb\u8fde\u63a5&#xff0c;\u901a\u4fe1\u903b\u8f91\u7b80\u5355\u3002\u9002\u5408\u56de\u58f0\u670d\u52a1\u5668\u300c\u6536\u4ec0\u4e48\u3001\u56de\u4ec0\u4e48\u300d\u7684\u6838\u5fc3\u903b\u8f91&#xff0c;\u80fd\u5b8c\u5168\u805a\u7126 UDP \u539f\u751f\u901a\u4fe1\u6d41\u7a0b&#xff0c;\u907f\u5f00 TCP \u590d\u6742\u7684\u8fde\u63a5\u7ba1\u7406\u3001\u72b6\u6001\u7ef4\u62a4&#xff0c;\u8ba9\u6211\u4eec\u4e13\u6ce8\u7406\u89e3socket\u3001bind\u3001recvfrom\u3001sendto\u56db\u5927\u6838\u5fc3\u63a5\u53e3\u7684\u8bbe\u8ba1\u7528\u9014\u4e0e\u8c03\u7528\u65f6\u5e8f\u3002<\/p>\n<\/p>\n<p>2. \u6784\u5efa\u6807\u51c6\u5316 C\/S \u901a\u4fe1\u6a21\u578b&#xff1a;\u9879\u76ee\u62c6\u5206\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u53cc\u6a21\u5757&#xff0c;\u590d\u523b\u7f51\u7edc\u7f16\u7a0b\u6700\u57fa\u7840\u7684\u67b6\u6784\u8303\u5f0f&#xff0c;\u6e05\u6670\u533a\u5206\u670d\u52a1\u7aef\u88ab\u52a8\u76d1\u542c\u4e0e\u5ba2\u6237\u7aef\u4e3b\u52a8\u53d1\u8d77\u7684\u89d2\u8272\u5dee\u5f02&#xff0c;\u76f4\u89c2\u5448\u73b0\u65e0\u8fde\u63a5\u534f\u8bae\u4e0b\u4e24\u7aef\u7684\u4ea4\u4e92\u6d41\u7a0b&#xff0c;\u4e3a\u540e\u7eed TCP \u7f16\u7a0b\u3001\u9ad8\u5e76\u53d1\u7f51\u7edc\u6846\u67b6\u5b66\u4e60\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<h2 id=\"%E4%BB%A3%E7%A0%81%E7%BB%93%E6%9E%84\">\u4ee3\u7801\u7ed3\u6784<\/h2>\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u5c0f\u9879\u76ee\u662f\u7ecf\u5178\u7684 \u5ba2\u6237\u7aef &#8211; \u670d\u52a1\u7aef \u7ed3\u6784&#xff0c;\u6240\u4ee5\u6211\u4eec\u5c31\u9700\u8981\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5206\u522b\u521b\u5efa\u6587\u4ef6\u7f16\u5199\u4ee3\u7801&#xff0c;\u53c8\u56e0\u4e3a\u670d\u52a1\u7aef\u5f80\u5f80\u6bd4\u5ba2\u6237\u7aef\u66f4\u590d\u6742\u70b9&#xff0c;\u6240\u4ee5\u5728\u670d\u52a1\u7aef\u6211\u4eec\u5c31\u4f7f\u7528\u4e24\u4e2a\u6587\u4ef6 (EchoServer.hpp &#043; EchoServerMain.cc)&#xff0c;\u5728\u5ba2\u6237\u7aef\u4e2d\u6211\u4eec\u5c31\u53ea\u4f7f\u7528 (EchoClient.cc) \u8fd9\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"46\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211842-6ae115320d020.png\" width=\"510\" \/><\/p>\n<p>\u4e09\u4e2a\u6587\u4ef6\u5404\u81ea\u8d1f\u8d23\u4ec0\u4e48?<\/p>\n<p>EchoServer.hpp \u670d\u52a1\u7aef\u5934\u6587\u4ef6 :<\/p>\n<p>1. \u8fd9\u4e2a\u6587\u4ef6\u662f\u6574\u4e2a UDP \u670d\u52a1\u7aef\u7684 \u201c\u6838\u5fc3\u5b9a\u4e49\u4ed3\u5e93\u201d\u3002\u56e0\u4e3a\u6211\u4eec\u8981\u628a UDP \u670d\u52a1\u7aef\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b&#xff0c;\u4e3a\u4ec0\u4e48?\u00a0<\/p>\n<\/p>\n<p>2. \u5982\u679c\u6211\u4eec\u4e0d\u5199\u7c7b&#xff0c;\u628a\u6240\u6709\u4ee3\u7801\u90fd\u5806\u5728 main \u51fd\u6570\u91cc\u5168\u90e8\u6df7\u5728\u4e00\u8d77\u3002\u4ee5\u540e\u5982\u679c\u60f3\u6539\u7aef\u53e3\u3001\u6539\u65e5\u5fd7\u3001\u590d\u7528\u670d\u52a1\u7aef\u4ee3\u7801&#xff0c;\u5c31\u4f1a\u53d1\u73b0\u4ee3\u7801\u4e71\u6210\u4e00\u56e2\u3002\u800c\u5c01\u88c5\u6210 UdpServer \u7c7b\u4e4b\u540e&#xff0c;\u6574\u4e2a\u903b\u8f91\u53d8\u5f97\u975e\u5e38\u6e05\u6670&#xff1a;\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316&#xff0c;Init() \u8d1f\u8d23\u521b\u5efa&#xff0c;Start() \u8d1f\u8d23\u8dd1\u6574\u4e2a\u4ee3\u7801\u903b\u8f91&#xff0c;\u6790\u6784\u51fd\u6570\u8d1f\u8d23\u91ca\u653e\u8d44\u6e90&#xff0c;\u5bf9\u5916\u6765\u770b&#xff0c;\u4f7f\u7528\u8005\u53ea\u9700\u8981\u521b\u5efa\u5bf9\u8c61&#xff0c;\u8c03\u7528 Init ()&#xff0c;\u8c03\u7528 Start ()&#xff0c;\u4e0d\u7528\u5173\u5fc3\u5e95\u5c42 socket \u600e\u4e48\u521b\u5efa\u3001bind \u600e\u4e48\u5931\u8d25\u3001\u9519\u8bef\u600e\u4e48\u6253\u5370\u3002<\/p>\n<\/p>\n<p>3. \u8fd9\u5c31\u662f\u9762\u5411\u5bf9\u8c61\u6700\u6838\u5fc3\u7684\u597d\u5904&#xff1a;\u4f7f\u7528\u8005\u548c\u5b9e\u73b0\u8005\u89e3\u8026\u3002\u5728\u4ee5\u540e\u7684\u771f\u5b9e\u5f00\u53d1\u91cc\u597d\u5904\u66f4\u660e\u663e&#xff1a;\u4e00\u4e2a\u7c7b\u7ba1\u7406\u4e00\u4e2a\u670d\u52a1&#xff0c;\u5b9e\u73b0\u8f7b\u677e\u590d\u7528\u3002\u00a0<\/p>\n<p>EchoServerMain.cc \u670d\u52a1\u7aef\u5165\u53e3\u6587\u4ef6 :<\/p>\n<p>\u8fd9\u4e2a\u6587\u4ef6\u662f\u670d\u52a1\u7aef\u7684 \u201c\u542f\u52a8\u5165\u53e3\u201d&#xff0c;\u4e5f\u662f\u4e3b\u51fd\u6570\u6240\u5728\u6587\u4ef6\u3002\u5b83\u4e0d\u5199\u4efb\u4f55\u590d\u6742\u7684\u7f51\u7edc\u903b\u8f91&#xff0c;\u53ea\u505a\u4e09\u4ef6\u4e8b&#xff1a;(1). \u5f15\u5165\u5934\u6587\u4ef6 #include &#034;EchoServer.hpp&#034; ; (2). \u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u62ff\u5230\u7528\u6237\u4f20\u5165\u7684\u7aef\u53e3 ; (3). \u521b\u5efa UdpServer \u5bf9\u8c61&#xff0c;\u8c03\u7528 Init() \u521d\u59cb\u5316\u3001Start() \u542f\u52a8\u670d\u52a1\u3002\u7b80\u5355\u8bf4&#xff1a;\u5934\u6587\u4ef6\u8d1f\u8d23 \u201c\u9020\u96f6\u4ef6\u3001\u5b9a\u89c4\u5219\u201d&#xff0c;main \u6587\u4ef6\u8d1f\u8d23 \u201c\u7ec4\u88c5\u96f6\u4ef6\u3001\u542f\u52a8\u7a0b\u5e8f\u201d&#xff0c;\u8fd9\u5c31\u662f C&#043;&#043; \u6807\u51c6\u7684\u58f0\u660e\u4e0e\u5b9e\u73b0\u5206\u79bb\u5199\u6cd5\u3002<\/p>\n<p>EchoClient.cc \u5ba2\u6237\u7aef\u6587\u4ef6 :<\/p>\n<p>\u5ba2\u6237\u7aef\u903b\u8f91\u5c31\u76f8\u5bf9\u7b80\u5355&#xff0c;\u4e0d\u9700\u8981\u590d\u6742\u7684\u7c7b\u5c01\u88c5\u548c\u63a5\u53e3\u62c6\u5206&#xff0c;\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u7528\u4e00\u4e2a\u6587\u4ef6\u641e\u5b9a\u5168\u90e8\u529f\u80fd\u3002\u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u5ba2\u6237\u7aef\u7684\u4e3b\u51fd\u6570\u3001\u547d\u4ee4\u884c\u53c2\u6570\u6821\u9a8c\u3001socket \u521b\u5efa\u3001\u4e3b\u52a8\u53d1\u9001\u6570\u636e\u3001\u63a5\u6536\u670d\u52a1\u7aef\u56de\u663e\u7684\u5b8c\u6574\u6d41\u7a0b&#xff0c;\u7528\u6237\u8fd0\u884c\u540e\u76f4\u63a5\u5c31\u80fd\u548c\u670d\u52a1\u7aef\u901a\u4fe1\u3002<\/p>\n<p>Mkefile \u7f16\u8bd1\u6587\u4ef6 :<\/p>\n<h2 id=\"%E2%80%8B%E7%BC%96%E8%BE%91\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"384\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211842-6ae115321d122.png\" width=\"1518\" \/><\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u76f4\u63a5\u770b\u670d\u52a1\u7aef EchoServer.hpp \u7684\u4ee3\u7801 :<\/p>\n<h2 id=\"EchoServer.hpp%C2%A0%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%A4%B4%E6%96%87%E4%BB%B6\">EchoServer.hpp\u00a0\u670d\u52a1\u7aef\u5934\u6587\u4ef6<\/h2>\n<p>\u8fd9\u4e2a\u6587\u4ef6\u662f\u6574\u4e2a UDP \u670d\u52a1\u7aef\u7684\u6838\u5fc3\u5c01\u88c5&#xff0c;\u6211\u4eec\u5728\u8fd9\u91cc\u5b9a\u4e49 UdpServer \u7c7b\u3001\u9519\u8bef\u679a\u4e3e\u3001\u4f9d\u8d56\u5934\u6587\u4ef6&#xff0c;\u628a\u7f51\u7edc\u521d\u59cb\u5316\u3001\u7aef\u53e3\u7ed1\u5b9a\u3001\u5faa\u73af\u6536\u53d1\u7684\u903b\u8f91\u5168\u90e8\u5c01\u88c5\u6210\u7c7b\u7684\u6210\u5458\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u529f\u80fd\u548c\u5165\u53e3\u89e3\u8026\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2946\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211842-6ae1153251868.png\" width=\"1523\" \/><\/p>\n<h3 id=\"1.%20%E5%A4%B4%E6%96%87%E4%BB%B6%E9%98%B2%E6%8A%A4%E4%B8%8E%E4%BE%9D%E8%B5%96%E5%BC%95%E5%85%A5\">1. \u5934\u6587\u4ef6\u9632\u62a4\u4e0e\u4f9d\u8d56\u5f15\u5165<\/h3>\n<p>\u4f5c\u4e3a C&#043;&#043; \u5de5\u7a0b\u7684\u6807\u51c6\u89c4\u8303&#xff0c;\u6211\u4eec\u9996\u5148\u505a\u4e24\u4ef6\u4e8b&#xff1a;<\/p>\n<\/p>\n<p>1. \u4f7f\u7528 #ifndef \/ #define \/ #endif \u5934\u6587\u4ef6\u4fdd\u62a4&#xff0c;\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u91cd\u590d\u5305\u542b&#xff0c;\u907f\u514d\u7f16\u8bd1\u51b2\u7a81&#xff1b; 2. \u5f15\u5165\u6240\u6709\u4f9d\u8d56\u7684\u7cfb\u7edf\u5934\u6587\u4ef6\u3001\u65e5\u5fd7\u6a21\u5757\u3001\u547d\u540d\u7a7a\u95f4&#xff0c;\u56e0\u4e3a\u6211\u4eec\u5728\u6253\u5370\u65f6\u7528\u5230\u4e86\u524d\u9762\u5b66\u5230\u7684 Logger.hpp \u65e5\u5fd7\u7c7b\u7684\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u6253\u5370&#xff0c;\u6240\u4ee5\u6211\u4eec\u4e5f\u8981\u5305\u542b\u65e5\u5fd7\u7c7b\u5934\u6587\u4ef6\u3002<\/p>\n<h3 id=\"2.%20%E7%A7%81%E6%9C%89%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F\">2. \u79c1\u6709\u6210\u5458\u53d8\u91cf<\/h3>\n<p>\u5728\u8bb2\u7c7b\u7684\u529f\u80fd\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u660e\u786e\u8fd9\u4e2a\u7c7b\u5185\u90e8\u8981\u5b58\u4ec0\u4e48\u6570\u636e&#xff1a;<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"144\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211844-6ae115347fb35.png\" width=\"1078\" \/><\/p>\n<p>_sockfd \u672c\u8d28\u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7ed9\u6211\u4eec\u7684\u7f51\u7edc\u901a\u4fe1 \u201c\u64cd\u4f5c\u53e5\u67c4\u201d\u3002\u6211\u4eec\u540e\u9762\u521b\u5efa socket\u3001\u7ed1\u5b9a\u7aef\u53e3\u3001\u6536\u53d1\u6570\u636e\u3001\u5168\u9760\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u628a\u5b83\u8bbe\u4e3a\u79c1\u6709&#xff0c;\u907f\u514d\u5916\u90e8\u4ee3\u7801\u968f\u610f\u4fee\u6539\u3002<\/p>\n<\/p>\n<p>_port \u662f\u5b58\u50a8\u670d\u52a1\u7aef\u7684\u7aef\u53e3\u53f7\u3002UDP \u670d\u52a1\u7aef\u5fc5\u987b\u5148\u56fa\u5b9a\u7aef\u53e3&#xff0c;\u5ba2\u6237\u7aef\u624d\u80fd\u7cbe\u51c6\u627e\u5230\u670d\u52a1\u7aef\u3002\u79c1\u6709\u5316\u540e&#xff0c;\u7aef\u53e3\u53ea\u80fd\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316&#xff0c;\u8fd0\u884c\u4e2d\u65e0\u6cd5\u88ab\u4fee\u6539\u3002<\/p>\n<h4 id=\"IP%20%E5%9C%B0%E5%9D%80%E5%92%8C%E7%AB%AF%E5%8F%A3%E5%8F%B7%E5%9C%A8%20UDP%20%E9%80%9A%E4%BF%A1%E4%B8%AD%E7%9A%84%E4%BD%9C%E7%94%A8\">IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u5728 UDP \u901a\u4fe1\u4e2d\u7684\u4f5c\u7528<\/h4>\n<p>1. \u5728 UDP \u8fd9\u79cd\u65e0\u8fde\u63a5\u901a\u4fe1\u6a21\u578b\u91cc&#xff0c;IP &#043; \u7aef\u53e3\u53f7 &#061; \u552f\u4e00\u7684\u901a\u4fe1\u7aef\u70b9\u3002<\/p>\n<\/p>\n<p>2. <span style=\"color:#ff9900\">IP \u5730\u5740\u8d1f\u8d23\u5b9a\u4f4d\u4e3b\u673a<\/span>&#xff0c;\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u5c31\u662f\u5728\u4e92\u8054\u7f51\u7684\u832b\u832b\u4e3b\u673a\u91cc&#xff0c;\u7cbe\u51c6\u627e\u5230\u76ee\u6807\u670d\u52a1\u5668\u8fd9\u53f0\u673a\u5668\u672c\u8eab\u3002\u5ba2\u6237\u7aef\u901a\u8fc7 IP&#xff0c;\u5c31\u80fd\u786e\u5b9a\u6570\u636e\u8981\u53d1\u5230\u54ea\u4e00\u53f0\u670d\u52a1\u5668&#xff1b;\u670d\u52a1\u7aef\u901a\u8fc7 IP \u6536\u5230\u6570\u636e&#xff0c;\u7136\u540e\u8fdb\u884c\u5904\u7406\u3002\u6240\u4ee5 IP \u89e3\u51b3\u7684\u662f\u6570\u636e\u8be5\u53d1\u7ed9\u54ea\u53f0\u7535\u8111\u3002<\/p>\n<\/p>\n<p>3. <span style=\"color:#ff9900\">\u7aef\u53e3\u53f7port\u8d1f\u8d23\u5b9a\u4f4d\u8fdb\u7a0b<\/span>&#xff0c;\u56e0\u4e3a\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u53ef\u80fd\u540c\u65f6\u8dd1\u7740\u51e0\u5341\u4e0a\u767e\u4e2a\u7a0b\u5e8f&#xff0c;\u5b83\u4eec\u90fd\u7528\u540c\u4e00\u4e2a IP&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u6b63\u5f0f\u4f9d\u9760\u7aef\u53e3\u53f7\u624d\u80fd\u533a\u5206\u6570\u636e\u5230\u5e95\u8be5\u4ea4\u7ed9\u54ea\u4e2a\u8fdb\u7a0b\u3002\u6211\u4eec\u7684 UDP \u670d\u52a1\u7aef\u56fa\u5b9a\u76d1\u542c\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u6bd4\u5982 8080&#xff0c;\u5c31\u662f\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u6240\u6709\u53d1\u5230 8080 \u7aef\u53e3\u7684\u6570\u636e&#xff0c;\u5168\u90e8\u4ea4\u7ed9\u6211\u8fd9\u4e2a\u8fdb\u7a0b\u5904\u7406&#xff1b;\u5ba2\u6237\u7aef\u5fc5\u987b\u4e25\u683c\u5bf9\u5e94\u8fd9\u4e2a\u7aef\u53e3\u53f7&#xff0c;\u4e0d\u7136\u5c31\u7b97 IP \u5bf9\u4e86&#xff0c;\u7aef\u53e3\u4e0d\u5bf9&#xff0c;\u6570\u636e\u4e5f\u5230\u4e0d\u4e86\u6211\u4eec\u7684\u670d\u52a1\u7aef\u3002\u6240\u4ee5\u7aef\u53e3\u89e3\u51b3\u7684\u95ee\u9898\u662f\u6570\u636e\u5230\u4e86\u7535\u8111\u540e&#xff0c;\u8be5\u4ea4\u7ed9\u54ea\u4e2a\u7a0b\u5e8f\u3002<\/p>\n<h4 id=\"%E8%AE%BE%E8%AE%A1%E6%94%B9%E5%8A%A8\">\u8bbe\u8ba1\u6539\u52a8<\/h4>\n<p>\u4f46\u662f\u8fd9\u91cc\u8865\u5145\u4e00\u70b9\u6211\u5728\u5199\u4ee3\u7801\u65f6\u7684\u8bbe\u8ba1\u6539\u52a8&#xff1a;<\/p>\n<p>1. \u5728\u6700\u5f00\u59cb\u7684\u8bbe\u8ba1\u91cc&#xff0c;\u79c1\u6709\u6210\u5458\u4e2d\u6211\u4eec\u989d\u5916\u52a0\u8fc7\u4e00\u4e2a\u7528\u6765\u4fdd\u5b58 IP \u5730\u5740\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u672c\u610f\u662f\u60f3\u8ba9\u670d\u52a1\u7aef\u7ed1\u5b9a\u4e00\u4e2a\u56fa\u5b9a IP\u3002\u4f46\u5728\u5b9e\u9645\u6d4b\u8bd5\u4e2d\u53d1\u73b0\u5199\u6b7b\u67d0\u4e2a\u5177\u4f53 IP \u7684\u8fd9\u79cd\u5199\u6cd5\u9650\u5236\u592a\u5927&#xff0c;\u4e3a\u4ec0\u4e48\u5462?<\/p>\n<p>2. \u8fd9\u91cc\u6211\u4eec\u8981\u5148\u5398\u6e05\u4e00\u4e2a\u5bb9\u6613\u6df7\u6dc6\u7684\u5173\u952e\u70b9&#xff1a;IP \u5728\u5b8f\u89c2\u89c6\u89d2\u548c\u5fae\u89c2\u89c6\u89d2\u4e0b&#xff0c;\u5bf9\u5e94\u5bf9\u8c61\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5b8f\u89c2\u5c42\u9762&#xff0c;\u6211\u4eec\u8bf4 IP \u5bf9\u5e94\u4e00\u53f0\u4e3b\u673a\u3002\u5728\u6574\u4e2a\u4e92\u8054\u7f51\u73af\u5883\u4e2d&#xff0c;IP \u5730\u5740\u5c31\u662f\u4e00\u53f0\u670d\u52a1\u5668\u7684\u552f\u4e00\u6807\u8bc6&#xff0c;\u5ba2\u6237\u7aef\u901a\u8fc7\u76ee\u6807 IP&#xff0c;\u5c31\u80fd\u8de8\u8d8a\u7f51\u7edc&#xff0c;\u627e\u5230\u5bf9\u5e94\u7684\u90a3\u53f0\u4e3b\u673a&#xff0c;\u5b8c\u6210\u4e3b\u673a\u7ea7\u522b\u7684\u5bfb\u5740\u3002\u8fd9\u662f\u6211\u4eec\u65e5\u5e38\u4ea4\u6d41\u3001\u7f51\u7edc\u901a\u4fe1\u91cc\u6700\u5e38\u8bf4\u7684\u6982\u5ff5\u3002<\/p>\n<p>3. \u4f46\u843d\u5230\u6211\u4eec\u4ee3\u7801\u8fd9\u4e2a\u5fae\u89c2\u5c42\u9762( \u6bd4\u5982\u8bf4\u5728 bind() \u51fd\u6570\u7ed1\u5b9a\u64cd\u4f5c\u65f6)&#xff0c;IP \u5b9e\u9645\u5bf9\u5e94\u7684\u662f\u4e3b\u673a\u4e0a\u7684\u67d0\u4e00\u5f20\u7f51\u5361\u3002\u4e00\u53f0\u670d\u52a1\u5668\u4e3b\u673a\u53ef\u4ee5\u914d\u7f6e\u591a\u5f20\u7f51\u5361&#xff0c;<span style=\"color:#fe2c24\">\u6bcf\u5f20\u7f51\u5361\u90fd\u62e5\u6709\u72ec\u7acb\u7684 IP&#xff1a;\u672c\u673a\u56de\u73af\u7f51\u5361\u3001\u5185\u7f51\u7f51\u5361\u3001\u516c\u7f51\u7f51\u5361&#xff0c;\u5b83\u4eec\u90fd\u5c5e\u4e8e\u540c\u4e00\u53f0\u4e3b\u673a&#xff0c;\u5374\u662f\u4e09\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684\u7f51\u7edc\u5165\u53e3\u3002<\/span>\u5f53\u6211\u4eec\u5728\u4ee3\u7801\u91cc\u5199\u6b7b\u7ed1\u5b9a\u67d0\u4e00\u4e2a\u5177\u4f53 IP \u65f6&#xff0c;\u672c\u8d28\u662f\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u6211\u8fd9\u4e2a\u8fdb\u7a0b&#xff0c;\u53ea\u80fd\u76d1\u542c\u8fd9\u4e00\u5f20\u7f51\u5361\u4e0a\u6536\u5230\u7684\u6570\u636e&#xff0c;\u5176\u4ed6\u7f51\u5361\u7684\u6570\u636e&#xff0c;\u7cfb\u7edf\u4f1a\u76f4\u63a5\u4e22\u5f03&#xff0c;\u4e0d\u4f1a\u4ea4\u7ed9\u6211\u4eec\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>4. \u641e\u61c2\u4e86\u5b8f\u89c2 IP \u548c\u5fae\u89c2 IP \u7684\u533a\u522b&#xff0c;\u6211\u4eec\u518d\u5bf9\u5e94\u770b\u4e09\u79cd IP \u548c\u300c\u6211\u4eec\u7684\u7535\u8111\u3001\u4e91\u670d\u52a1\u5668\u3001Linux \u7cfb\u7edf\u300d\u7684\u5173\u7cfb&#xff0c;\u5c31\u7279\u522b\u597d\u7406\u89e3\u4e86&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"217\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211844-6ae115349dae4.png\" width=\"460\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"39\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211844-6ae11534c7bb1.png\" width=\"928\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0(1). \u7b2c\u4e00\u79cd\u662f\u672c\u673a\u56de\u73af IP : 127.0.0.1&#xff0c;\u5b83\u548c\u5916\u90e8\u7f51\u7edc\u6ca1\u5173\u7cfb&#xff0c;\u53ea\u5c5e\u4e8e\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u3002\u4e0d\u7ba1\u662fWindows \u7535\u8111&#xff0c;\u8fd8\u662f Linux \u4e91\u670d\u52a1\u5668&#xff0c;\u7cfb\u7edf\u90fd\u4f1a\u81ea\u5e26\u8fd9\u4e2a IP\u3002\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u7cfb\u7edf\u5185\u90e8\u81ea\u5df1\u548c\u81ea\u5df1\u901a\u4fe1&#xff0c;\u53ea\u80fd\u672c\u673a\u81ea\u6d4b&#xff0c;\u522b\u7684\u7535\u8111\u3001\u5916\u7f51\u90fd\u8bbf\u95ee\u4e0d\u4e86\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0(2). \u7b2c\u4e8c\u79cd\u662f\u5185\u7f51 IP : 10.0.16.7&#xff0c;\u5b83\u662f\u540c\u4e00\u5c40\u57df\u7f51\u91cc\u5404\u4e2a\u8bbe\u5907\u7684\u4e13\u7528\u5730\u5740\u3002\u6bd4\u5982\u8bf4\u5bb6\u7528\u7535\u8111\u8fde WiFi \u62ff\u5230\u7684\u5185\u7f51 IP&#xff0c;\u53ea\u80fd\u548c\u5bb6\u91cc\u5176\u4ed6\u8bbe\u5907\u4e92\u76f8\u8bbf\u95ee&#xff1b;\u4e91\u670d\u52a1\u5668\u7684\u5185\u7f51 IP&#xff0c;\u53ea\u80fd\u548c\u540c\u673a\u623f\u3001\u540c\u5185\u7f51\u7684\u5176\u4ed6\u670d\u52a1\u5668\u901a\u4fe1\u3002\u7b80\u5355\u8bf4&#xff1a;\u5185\u7f51 IP \u53ea\u5728 \u201c\u540c\u4e00\u4e2a\u5c40\u57df\u7f51\u201d \u91cc\u751f\u6548&#xff0c;\u51fa\u4e86\u8fd9\u4e2a\u5c0f\u5708\u5b50\u5c31\u7528\u4e0d\u4e86\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0(3). \u7b2c\u4e09\u79cd\u662f\u516c\u7f51 IP : 124.222.191.171&#xff0c;\u662f\u4e92\u8054\u7f51\u4e0a\u552f\u4e00\u7684\u5730\u5740\u3002\u4e91\u670d\u52a1\u5668\u516c\u7f51 IP&#xff0c;\u5c31\u662f\u5b83\u5728\u5168\u7f51\u7684\u8eab\u4efd\u6807\u8bc6\u3002\u5168\u4e16\u754c\u4efb\u4f55\u5730\u65b9\u7684\u7535\u8111\u3001\u624b\u673a&#xff0c;\u53ea\u8981\u8054\u7f51&#xff0c;\u90fd\u80fd\u901a\u8fc7\u8fd9\u4e2a IP \u627e\u5230\u4f60\u7684 Linux \u670d\u52a1\u5668\u3002\u5b83\u662f\u4e91\u670d\u52a1\u5668\u548c\u5916\u754c\u7f51\u7edc\u6c9f\u901a\u7684\u552f\u4e00\u901a\u9053\u3002<\/p>\n<p>5. \u660e\u767d\u4e86\u8fd9\u4e00\u70b9&#xff0c;\u6211\u4eec\u518d\u56de\u5934\u770b\u4ee3\u7801\u7684\u8bbe\u8ba1\u6539\u52a8&#xff0c;\u903b\u8f91\u5c31\u5b8c\u5168\u901a\u987a\u4e86&#xff1a;\u5982\u679c\u6211\u4eec\u5728\u4ee3\u7801\u91cc\u5199\u6b7b\u7ed1\u5b9a\u67d0\u4e00\u4e2a IP&#xff0c;\u5c31\u7b49\u4e8e\u7ed9\u81ea\u5df1\u7684\u670d\u52a1\u8bbe\u4e86\u9650\u5236&#xff1a;<span style=\"color:#fe2c24\">\u7ed1\u5b9a\u56de\u73af IP&#xff0c;\u5c31\u53ea\u80fd\u672c\u673a\u6d4b\u8bd5&#xff1b;\u7ed1\u5b9a\u5185\u7f51 IP&#xff0c;\u5c31\u53ea\u80fd\u5c40\u57df\u7f51\u8bbf\u95ee&#xff1b;\u7ed1\u5b9a\u516c\u7f51 IP&#xff0c;\u5c31\u53ea\u80fd\u5916\u7f51\u8fde\u63a5\u3002<\/span>\u4e0d\u7ba1\u7ed1\u5b9a\u54ea\u4e00\u4e2a&#xff0c;\u90fd\u4f1a\u635f\u5931\u53e6\u5916\u4e24\u79cd\u8bbf\u95ee\u65b9\u5f0f\u3002 \u6240\u4ee5\u6700\u7ec8\u6211\u4eec\u5220\u6389\u4e86 IP \u76f8\u5173\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u6539\u7528 <span style=\"color:#fe2c24\">INADDR_ANY<\/span>\u3002\u6539\u6210 INADDR_ANY \u4e4b\u540e&#xff0c;\u6211\u4eec\u7684\u5ba2\u6237\u7aef\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u6240\u5728\u7684\u7f51\u7edc\u73af\u5883&#xff0c;\u81ea\u7531\u9009\u62e9\u4f7f\u7528\u672c\u673a\u56de\u73af IP\u3001\u5185\u7f51 IP\u3001\u516c\u7f51 IP \u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u6765\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u4e09\u79cd\u65b9\u5f0f\u5168\u90e8\u90fd\u80fd\u6b63\u5e38\u901a\u4fe1&#xff0c;\u670d\u52a1\u7aef\u4e0d\u7528\u505a\u989d\u5916\u4fee\u6539\u3002<\/p>\n<p>6. \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u79c1\u6709\u6210\u5458\u91cc\u6211\u4eec\u53ea\u4fdd\u7559\u7aef\u53e3\u3001\u53bb\u6389 IP\u3002\u7aef\u53e3\u662f\u670d\u52a1\u5bf9\u5916\u56fa\u5b9a\u7684\u5165\u53e3\u6807\u8bc6&#xff0c;\u5fc5\u987b\u56fa\u5b9a&#xff1b;\u800c IP \u53ea\u662f\u7f51\u7edc\u5165\u53e3&#xff0c;\u670d\u52a1\u7aef\u672c\u6765\u5c31\u8be5\u63a5\u6536\u6240\u6709\u6765\u6e90\u7684\u8bf7\u6c42&#xff0c;\u4e0d\u9700\u8981\u9501\u6b7b\u67d0\u4e00\u5f20\u7f51\u5361\u3001\u67d0\u4e00\u4e2a IP\u3002<\/p>\n<h3 id=\"3.%20%E5%85%A8%E5%B1%80%E5%B8%B8%E9%87%8F%E4%B8%8E%E6%9E%9A%E4%B8%BE%E9%94%99%E8%AF%AF%E7%A0%81\">3. \u5168\u5c40\u5e38\u91cf\u4e0e\u679a\u4e3e\u9519\u8bef\u7801<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"135\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211844-6ae11534d9e5d.png\" width=\"432\" \/><\/p>\n<p>\u5728\u6b63\u5f0f\u5b9a\u4e49\u670d\u52a1\u7aef\u7c7b\u4e4b\u524d&#xff0c;\u6211\u4eec\u8fd8\u9700\u8981\u7edf\u4e00\u5168\u5c40\u5e38\u91cf\u4e0e\u9519\u8bef\u679a\u4e3e&#xff0c;\u6211\u4eec\u8bbe\u7f6e\u9ed8\u8ba4\u65e0\u6548\u6587\u4ef6\u63cf\u8ff0\u7b26default_fd &#061; -1\u3001\u9ed8\u8ba4\u76d1\u542c\u7aef\u53e3default_port &#061; 8888\u3002<\/p>\n<p>\u679a\u4e3e\u9519\u8bef\u7801\u5b9a\u4e49\u4e86\u53ef\u80fd\u51fa\u73b0\u7684\u5404\u7c7b\u5f02\u5e38\u72b6\u6001&#xff0c;\u5305\u542bSUCCESS\u6b63\u5e38\u72b6\u6001\u3001USAGE_ERR\u53c2\u6570\u9519\u8bef\u3001SOCKET_ERR\u5957\u63a5\u5b57\u521b\u5efa\u5931\u8d25\u3001BIND_ERR\u7aef\u53e3\u7ed1\u5b9a\u5931\u8d25\u3002<\/p>\n<h3 id=\"4.%20%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%8E%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0\">4. \u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"206\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae1153503d35.png\" width=\"546\" \/><\/p>\n<p>\u6784\u9020\u51fd\u6570 UdpServe \u8d1f\u8d23\u5bf9\u8c61\u521d\u59cb\u5316&#xff0c;\u63a5\u6536\u7528\u6237\u4f20\u5165\u7684\u7aef\u53e3&#xff0c;\u82e5\u7528\u6237\u672a\u6307\u5b9a\u7aef\u53e3&#xff0c;\u5219\u81ea\u52a8\u4f7f\u7528\u6211\u4eec\u5b9a\u4e49\u7684\u9ed8\u8ba4\u7aef\u53e3 8888&#xff0c;\u540c\u65f6\u5c06\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26\u521d\u59cb\u5316\u4e3adefault_fd &#061; -1&#xff0c;\u6807\u8bb0\u5957\u63a5\u5b57\u5c1a\u672a\u521b\u5efa\u3002<\/p>\n<p>\u6790\u6784\u51fd\u6570~UdpServer() \u8d1f\u8d23\u7a0b\u5e8f\u9000\u51fa\u65f6\u7684\u8d44\u6e90\u81ea\u52a8\u56de\u6536&#xff0c;\u6838\u5fc3\u6267\u884c close(_sockfd) \u64cd\u4f5c\u3002\u91ca\u653e\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5b8c\u6210\u64cd\u4f5c\u7cfb\u7edf\u8d44\u6e90\u7684\u5b89\u5168\u56de\u6536\u3002<\/p>\n<p>\u8fd9\u91cc\u91cd\u70b9\u89e3\u91ca\u4e00\u4e0b fd&#xff08;\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09; \u7684\u5e95\u5c42\u672c\u8d28&#xff1a;<span style=\"color:#ff9900\">\u5728 Linux \u64cd\u4f5c\u7cfb\u7edf\u91cc&#xff0c;\u4e00\u5207\u7686\u6587\u4ef6\u3002\u4e0d\u7ba1\u662f\u666e\u901a\u6587\u672c\u6587\u4ef6\u3001\u7ba1\u9053\u3001\u8bbe\u5907\u3001\u8fd8\u662f\u6211\u4eec\u8fd9\u91cc\u7684\u7f51\u7edc socket&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u7edf\u4e00\u628a\u5b83\u4eec\u5f53\u6210\u6587\u4ef6\u6765\u7ba1\u7406\u3002\u800c fd&#xff08;\u6587\u4ef6\u63cf\u8ff0\u7b26) \u672c\u8d28\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7684\u4e00\u4e2a\u6574\u6570\u7f16\u53f7\u3002<\/span>\u540e\u7eed\u6240\u6709\u5bf9\u7f51\u7edc\u7684\u64cd\u4f5c \u2014\u2014 \u521b\u5efa socket\u3001bind \u7ed1\u5b9a\u3001recvfrom \u6536\u6570\u636e\u3001sendto \u53d1\u6570\u636e\u3001close \u5173\u95ed&#xff0c;\u5168\u9760\u8fd9\u4e2a\u6574\u6570\u7f16\u53f7\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u8981\u64cd\u4f5c\u7684\u662f\u54ea\u4e00\u4e2a\u7f51\u7edc\u5bf9\u8c61\u3002fd &#061; -1 \u4ee3\u8868\u65e0\u6548\u3001\u672a\u521b\u5efa\u3001\u672a\u6253\u5f00&#xff1b;fd \u2265 0 \u4ee3\u8868\u64cd\u4f5c\u7cfb\u7edf\u5df2\u7ecf\u6210\u529f\u5206\u914d\u4e86\u4e00\u4e2a\u7f51\u7edc\u901a\u4fe1\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u521b\u5efa\u51fa\u6765\u7684 sockfd \u662f 3\u3002<\/p>\n<h3 id=\"5.%20Init%20()%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0\">5. Init () \u670d\u52a1\u7aef\u521d\u59cb\u5316\u51fd\u6570<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"365\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae115352710b.png\" width=\"628\" \/><\/p>\n<p>\u9996\u5148\u5728\u8fd9\u4e2a Init() \u521d\u59cb\u5316\u51fd\u6570\u4e2d\u5305\u62ec\u4e86\u4e09\u4e2a\u6b65\u9aa4 : 1.\u00a0\u521b\u5efa socket ; 2. \u586b\u5145\u7f51\u7edc\u5730\u5740\u4fe1\u606f ; 3.\u00a0bind \u7ed1\u5185\u6838 socket\u3002<\/p>\n<p>\u6211\u4eec\u5148\u770b\u7b2c\u4e00\u6b65\u521b\u5efa socket :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"231\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae115357e1a3.png\" width=\"1213\" \/>1. \u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u4e5f\u8bb2\u8fc7 socket() \u662f\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff0c;\u4f5c\u7528\u662f\u5411\u5185\u6838\u7533\u8bf7\u521b\u5efa\u4e00\u4e2a\u7f51\u7edc\u4e13\u7528\u7684 socket \u6587\u4ef6\u5bf9\u8c61&#xff0c;\u6210\u529f\u540e\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26 fd\u3002<\/p>\n<\/p>\n<p>2. \u7b2c\u4e00\u4e2a\u53c2\u6570 <span style=\"color:#fe2c24\">AF_INET \u6307\u5b9aIPv4 \u5730\u5740\u534f\u8bae\u65cf<\/span>&#xff0c;\u4ee3\u8868\u6211\u4eec\u4f7f\u7528\u7ecf\u5178\u7684 IPv4 \u7f51\u7edc\u901a\u4fe1&#xff1b;<\/p>\n<\/p>\n<p>3. \u7b2c\u4e8c\u4e2a\u53c2\u6570 <span style=\"color:#fe2c24\">SOCK_DGRAM \u6307\u5b9aUDP \u65e0\u8fde\u63a5\u6570\u636e\u62a5\u5957\u63a5\u5b57<\/span>&#xff0c;\u4ee3\u8868\u6211\u4eec\u91c7\u7528 UDP \u534f\u8bae\u901a\u4fe1&#xff0c;\u7279\u70b9\u662f\u65e0\u8fde\u63a5\u3001\u9762\u5411\u6570\u636e\u5305\u3001\u4e0d\u4fdd\u8bc1\u53ef\u9760\u4f20\u8f93&#xff1b;<\/p>\n<\/p>\n<p>4. \u7b2c\u4e09\u4e2a\u53c2\u6570 0 \u5bf9\u5e94\u534f\u8bae\u7f16\u53f7&#xff0c;\u5f53\u524d\u573a\u666f\u4e0b\u586b 0&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5339\u914d UDP \u534f\u8bae\u3002<\/p>\n<\/p>\n<p>5. \u8c03\u7528 socket \u51fd\u6570\u540e&#xff0c;\u5185\u6838\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa socket \u7f51\u7edc\u6587\u4ef6\u3001\u5206\u914d fd \u5e76\u8fd4\u56de\u3002\u6211\u4eec\u5224\u65ad\u8fd4\u56de\u503c\u662f\u5426\u5c0f\u4e8e 0&#xff1a;\u5c0f\u4e8e 0 \u4ee3\u8868\u521b\u5efa\u5931\u8d25&#xff0c;\u6253\u5370\u81f4\u547d\u65e5\u5fd7\u5e76\u9000\u51fa\u7a0b\u5e8f&#xff1b;\u5927\u4e8e\u7b49\u4e8e 0 \u4ee3\u8868\u521b\u5efa\u6210\u529f&#xff0c;\u62ff\u5230\u6709\u6548 fd&#xff0c;\u540e\u7eed\u6240\u6709\u7f51\u7edc\u64cd\u4f5c\u90fd\u5c06\u901a\u8fc7\u8fd9\u4e2a fd \u5b8c\u6210\u3002<\/p>\n<p>\u4e0b\u6765\u7b2c\u4e8c\u6b65\u5c31\u662f\u586b\u5145\u7f51\u7edc\u5730\u5740\u4fe1\u606f&#xff0c;\u51c6\u5907\u7ed1\u5b9a\u53c2\u6570:4<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"194\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae11535a38c8.png\" width=\"1245\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u586b\u5145\u7f51\u7edc\u4fe1\u606f&#xff1f;<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u7684 socket \u6587\u4ef6&#xff0c;\u6b64\u65f6\u53ea\u662f\u4e00\u4e2a \u201c\u7a7a\u767d\u7684\u7f51\u7edc\u5bf9\u8c61\u201d&#xff0c;\u6ca1\u6709\u548c\u4efb\u4f55 IP\u3001\u7aef\u53e3\u5173\u8054\u3002\u586b\u5145sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u5c31\u662f\u7ed9\u8fd9\u4e2a socket \u51c6\u5907\u597d\u8981\u7ed1\u5b9a\u7684\u5730\u5740\u914d\u7f6e&#xff0c;\u544a\u8bc9\u5185\u6838\u540e\u7eed\u8981\u76d1\u542c\u54ea\u4e2a\u7aef\u53e3\u3001\u54ea\u4e9b IP \u6765\u6e90\u7684\u8bf7\u6c42&#xff0c;\u4e3a\u7b2c\u4e09\u6b65 bind \u7ed1\u5b9a\u505a\u53c2\u6570\u51c6\u5907\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"24\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae11535c816c.png\" width=\"932\" \/> sockaddr_in \u662f Linux \u7cfb\u7edf\u4e13\u95e8\u4e3aIPv4 \u534f\u8bae\u8bbe\u8ba1\u7684\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u4e5f\u8bb2\u8fc7&#xff0c;\u4e13\u95e8\u7528\u6765\u5b58\u653e IP &#043; \u7aef\u53e3\u4fe1\u606f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"27\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae11535d97a4.png\" width=\"828\" \/> bzero \u662f\u7cfb\u7edf\u51fd\u6570&#xff0c;\u4f5c\u7528\u662f\u628a\u6574\u4e2a\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5168\u90e8\u6e05\u96f6\u3002C&#043;&#043; \u6808\u4e0a\u521b\u5efa\u7684\u53d8\u91cf\u4f1a\u5e26\u6709\u968f\u673a\u810f\u6570\u636e&#xff0c;\u4e0d\u6e05\u7a7a\u4f1a\u5bfc\u81f4\u540e\u7eed\u7ed1\u5b9a\u5f02\u5e38\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"32\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211845-6ae11535ebb79.png\" width=\"887\" \/> \u518d\u6b21\u6307\u5b9a\u534f\u8bae\u65cf\u4e3a IPv4&#xff0c;\u5fc5\u987b\u548c\u7b2c\u4e00\u6b65 socket \u521b\u5efa\u65f6\u7684AF_INET\u4fdd\u6301\u4e00\u81f4&#xff0c;\u534f\u8bae\u4e0d\u5339\u914d\u4f1a\u76f4\u63a5\u5bfc\u81f4\u7ed1\u5b9a\u5931\u8d25\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"25\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211846-6ae1153609c92.png\" width=\"797\" \/> htons() \u662f\u7f51\u7edc\u7f16\u7a0b\u6838\u5fc3\u51fd\u6570&#xff0c;\u5168\u79f0host to network short\u3002\u56e0\u4e3a CPU&#xff08;\u4e3b\u673a&#xff09;\u548c\u7f51\u7edc\u4f20\u8f93\u4f7f\u7528\u7684\u5b57\u8282\u5e8f\u76f8\u53cd&#xff1a;\u4e3b\u673a\u662f\u5c0f\u7aef\u5e8f&#xff0c;\u7f51\u7edc\u5f3a\u5236\u8981\u6c42\u5927\u7aef\u5e8f\u3002\u6211\u4eec\u5fc5\u987b\u901a\u8fc7htons&#xff0c;\u628a\u7528\u6237\u4f20\u5165\u7684\u4e3b\u673a\u5b57\u8282\u5e8f\u7aef\u53e3&#xff0c;\u8f6c\u6362\u6210\u7f51\u7edc\u80fd\u8bc6\u522b\u7684\u5b57\u8282\u5e8f&#xff0c;\u5426\u5219\u7aef\u53e3\u4f1a\u89e3\u6790\u9519\u8bef\u3001\u5ba2\u6237\u7aef\u65e0\u6cd5\u8fde\u63a5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"30\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211846-6ae115361b0ac.png\" width=\"912\" \/> \u8fd9\u5c31\u662f\u6211\u4eec\u524d\u9762\u53cd\u590d\u4f18\u5316\u7684\u6700\u4f73\u5b9e\u8df5\u3002INADDR_ANY\u672c\u8d28\u662f\u4e00\u4e2a\u5b8f&#xff0c;\u503c\u4e3a 0&#xff0c;\u4ee3\u8868\u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361\u3001\u6240\u6709 IP \u5730\u5740\u3002\u4e0d\u7ba1\u5ba2\u6237\u7aef\u901a\u8fc7 127.0.0.1 \u672c\u673a\u3001\u5185\u7f51 IP\u3001\u516c\u7f51 IP \u8bbf\u95ee&#xff0c;\u53ea\u8981\u7aef\u53e3\u5339\u914d&#xff0c;\u6570\u636e\u90fd\u4f1a\u8def\u7531\u5230\u8fd9\u4e2a socket&#xff0c;\u517c\u5bb9\u5404\u79cd\u8bbf\u95ee\u573a\u666f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"732\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211846-6ae115362c606.png\" width=\"2148\" \/><\/p>\n<p>\u8865\u5145 : \u8fd9\u91cc\u7684 local \u7ed3\u6784\u4f53&#xff0c;\u662f\u5b58\u653e\u5728\u7528\u6237\u6808\u5185\u5b58\u4e2d&#xff0c;\u53ea\u662f\u6211\u4eec\u4e34\u65f6\u51c6\u5907\u7684\u53c2\u6570&#xff0c;\u6b64\u65f6\u8fd8\u6ca1\u6709\u88ab\u5199\u5165\u5185\u6838&#xff0c;\u53ea\u6709\u7b2c\u4e09\u6b65 bind \u8c03\u7528\u540e&#xff0c;\u5730\u5740\u4fe1\u606f\u624d\u4f1a\u548c\u5185\u6838 socket \u6587\u4ef6\u7ed1\u5b9a\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\u7528\u00a0bind() \u7ed1\u5b9a&#xff0c;\u5c06\u5730\u5740\u4fe1\u606f\u6ce8\u518c\u5230\u5185\u6838 socket :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"234\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211846-6ae11536ec731.png\" width=\"963\" \/><\/p>\n<p>1. bind() \u662f\u7cfb\u7edf\u8c03\u7528&#xff0c;\u6838\u5fc3\u4f5c\u7528\u662f\u628a\u7b2c\u4e8c\u6b65\u51c6\u5907\u597d\u7684 IP &#043; \u7aef\u53e3\u5730\u5740\u4fe1\u606f&#xff0c;\u6ce8\u518c\u5230\u5185\u6838\u7684 socket \u6587\u4ef6\u5bf9\u8c61\u4e2d\u3002\u5b8c\u6210\u7ed1\u5b9a\u540e&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5efa\u7acb\u300c\u7aef\u53e3 \u2192 socket \u6587\u4ef6\u300d\u7684\u6620\u5c04\u5173\u7cfb&#xff0c;\u544a\u8bc9\u5185\u6838&#xff1a;\u6240\u6709\u53d1\u5f80\u8fd9\u4e2a\u7aef\u53e3\u3001\u5339\u914d\u5bf9\u5e94 IP \u89c4\u5219\u7684\u6570\u636e&#xff0c;\u5c31\u4ea4\u7ed9\u8fd9\u4e2a socket \u6587\u4ef6\u5904\u7406&#xff0c;\u8fd9\u6837\u5ba2\u6237\u7aef\u5c31\u80fd\u901a\u8fc7 IP &#043; \u7aef\u53e3\u627e\u5230\u6211\u4eec\u7684\u670d\u52a1\u7aef\u3002  2. \u7b2c\u4e00\u4e2a\u53c2\u6570 _sockfd \u5c31\u662f\u6211\u4eec\u7b2c\u4e00\u6b65\u521b\u5efa socket \u65f6\u62ff\u5230\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6307\u5b9a\u6211\u4eec\u8981\u7ed1\u5b9a\u54ea\u4e2a\u5185\u6838 socket \u5bf9\u8c61&#xff1b;\u7b2c\u4e8c\u4e2a\u53c2\u6570 (struct sockaddr *)&amp;local \u662f\u5730\u5740\u53c2\u6570\u3002\u56e0\u4e3a bind \u51fd\u6570\u7684\u53c2\u6570\u8bbe\u8ba1\u8981\u6c42\u4f20\u5165\u901a\u7528\u5730\u5740\u7ed3\u6784\u4f53 sockaddr&#xff0c;\u6240\u4ee5\u6211\u4eec\u9700\u8981\u628a IPv4 \u4e13\u7528\u7684 sockaddr_in \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002\u901a\u8fc7\u7236\u7c7b\u6307\u9488\u627e\u5230\u5b50\u7c7b\u5bf9\u8c61&#xff1b;\u7b2c\u4e09\u4e2a\u53c2\u6570 sizeof(local) \u662f\u5730\u5740\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p>3. bind \u51fd\u6570\u8fd4\u56de\u503c\u5c0f\u4e8e 0 \u4ee3\u8868\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u6700\u5e38\u89c1\u539f\u56e0\u662f\u7aef\u53e3\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528&#xff0c;\u6211\u4eec\u6253\u5370\u65e5\u5fd7\u5e76\u9000\u51fa&#xff1b;\u8fd4\u56de\u503c\u5927\u4e8e\u7b49\u4e8e 0 \u4ee3\u8868\u7ed1\u5b9a\u6210\u529f&#xff0c;\u6b64\u65f6 socket \u6b63\u5f0f\u548c IP &#043; \u7aef\u53e3\u5b8c\u6210\u5173\u8054&#xff0c;\u670d\u52a1\u7aef\u5177\u5907\u4e86\u63a5\u6536\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u80fd\u529b\u3002<\/p>\n<h5 id=\"%E9%87%8D%E8%AF%86Socket\">\u91cd\u8bc6Socket<\/h5>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5185\u5bb9&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4ece\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u7ef4\u5ea6&#xff0c;\u5bf9 Socket \u505a\u6700\u6e05\u6670\u7684\u603b\u7ed3&#xff1a;<\/p>\n<p>1. \u4ece\u7f51\u7edc\u901a\u4fe1\u5c42\u9762\u770b&#xff0c;Socket \u5c31\u662f IP &#043; \u7aef\u53e3\u53f7\u3002\u5b83\u662f\u4e00\u4e2a\u903b\u8f91\u610f\u4e49\u4e0a\u7684\u901a\u4fe1\u7aef\u70b9&#xff0c;\u7528\u6765\u552f\u4e00\u6807\u8bc6\u7f51\u7edc\u4e2d\u901a\u4fe1\u7684\u53cc\u65b9\u3002\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u4f9d\u9760\u8fd9\u4e2a\u5730\u5740\u4e92\u76f8\u5b9a\u4f4d\u3001\u5efa\u7acb\u5bf9\u8bdd&#xff0c;\u5b83\u662f\u62bd\u8c61\u7684\u901a\u4fe1\u8eab\u4efd&#xff0c;\u51b3\u5b9a\u4e86 \u201c\u8c01\u548c\u8c01\u5728\u5bf9\u8bdd\u201d\u3002<\/p>\n<\/p>\n<p>2. \u4ece Linux \u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u5c42\u9762\u770b&#xff0c;Socket \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7f51\u7edc\u6587\u4ef6\u5bf9\u8c61\u3002\u8fdb\u7a0b\u8c03\u7528 socket () \u7cfb\u7edf\u8c03\u7528&#xff0c;\u5185\u6838\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u7684\u6587\u4ef6&#xff0c;\u5e76\u5206\u914d\u552f\u4e00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 fd\u3002<span style=\"color:#fe2c24\">\u8fd9\u4e2a\u6587\u4ef6\u5bf9\u8c61\u91cc\u7ef4\u62a4\u7740\u7f51\u7edc\u72b6\u6001\u3001\u534f\u8bae\u4fe1\u606f\u3001\u4ee5\u53ca\u7528\u4e8e\u6536\u53d1\u6570\u636e\u7684\u5185\u6838\u7f13\u51b2\u533a\u3002\u6211\u4eec\u4ee3\u7801\u4e2d\u6240\u6709\u7684 bind\u3001sendto\u3001recvfrom \u64cd\u4f5c&#xff0c;\u672c\u8d28\u4e0a\u90fd\u662f\u62ff\u7740 fd&#xff0c;\u5bf9\u8fd9\u4e2a\u5185\u6838\u6587\u4ef6\u5bf9\u8c61\u8fdb\u884c\u8bfb\u5199\u63a7\u5236\u3002<\/span><\/p>\n<\/p>\n<p>\u7b80\u5355\u6765\u8bf4&#xff1a;IP &#043; \u7aef\u53e3\u662f\u5bf9\u5916\u7684\u901a\u4fe1\u8eab\u4efd&#xff0c;\u7528\u6765\u5bfb\u5740&#xff1b;Socket \u6587\u4ef6\u662f\u5bf9\u5185\u7684\u7cfb\u7edf\u8f7d\u4f53&#xff0c;\u7528\u6765\u6536\u53d1\u6570\u636e\u3002\u670d\u52a1\u7aef\u901a\u8fc7\u521b\u5efa\u5185\u6838\u4e2d\u7684 Socket \u6587\u4ef6&#xff0c;\u7ed1\u5b9a\u4e0a\u4e00\u4e2a\u516c\u5f00\u7684 IP &#043; \u7aef\u53e3 \u5730\u5740&#xff0c;\u5ba2\u6237\u7aef\u51ed\u501f\u8fd9\u4e2a\u5730\u5740\u627e\u5230\u5185\u6838\u4e2d\u7684 Socket \u5bf9\u8c61<span style=\"color:#fe2c24\">&#xff0c;\u53cc\u65b9\u901a\u8fc7 Socket \u6587\u4ef6\u81ea\u5e26\u7684\u5185\u6838\u7f13\u51b2\u533a\u5b8c\u6210\u6570\u636e\u4ea4\u6362&#xff0c;\u5b9e\u73b0\u8de8\u7f51\u7edc\u901a\u4fe1\u3002<\/span><\/p>\n<h3 id=\"6.%C2%A0Start%20()%20%E4%B8%9A%E5%8A%A1%E5%BE%AA%E7%8E%AF%E5%90%AF%E5%8A%A8%E5%87%BD%E6%95%B0\">6.\u00a0Start () \u4e1a\u52a1\u5faa\u73af\u542f\u52a8\u51fd\u6570<\/h3>\n<p>\u524d\u9762\u6211\u4eec\u901a\u8fc7 Init() \u5b8c\u6210\u4e86\u670d\u52a1\u7aef\u7684\u521d\u59cb\u5316&#xff0c;\u76f8\u5f53\u4e8e\u642d\u597d\u4e86\u67b6\u5b50\u3002\u800c Start() \u5c31\u662f\u670d\u52a1\u7aef\u771f\u6b63\u5f00\u59cb\u5e72\u6d3b\u3001\u6301\u7eed\u5bf9\u5916\u63d0\u4f9b\u56de\u58f0\u670d\u52a1\u7684\u6838\u5fc3\u4e1a\u52a1\u51fd\u6570\u3002\u6211\u4eec\u987a\u7740\u4ee3\u7801\u6267\u884c\u987a\u5e8f&#xff0c; \u628a Start() \u51fd\u6570\u62c6\u89e3\u6210 4 \u4e2a\u6838\u5fc3\u6b65\u9aa4 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"942\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211847-6ae11537151c4.png\" width=\"1373\" \/><\/p>\n<p>\u7b2c\u4e00\u6b65\u5f00\u542fwhile()\u5faa\u73af&#xff0c;\u6301\u7eed\u76d1\u542c\u5ba2\u6237\u7aef\u8bf7\u6c42 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"93\" src=\"2026-10-28ruo4krw2bi1.png\" width=\"846\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48\u7528 while(true) \u6b7b\u5faa\u73af&#xff1f;<\/p>\n<p>UDP \u670d\u52a1\u7aef\u7684\u6838\u5fc3\u7279\u6027\u662f\u88ab\u52a8\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5b83\u4e0d\u77e5\u9053\u5ba2\u6237\u7aef\u4ec0\u4e48\u65f6\u5019\u53d1\u6d88\u606f\u3001\u4f1a\u6709\u591a\u5c11\u4e2a\u5ba2\u6237\u7aef\u53d1\u6d88\u606f\u3002\u5982\u679c\u6ca1\u6709\u5faa\u73af&#xff0c;\u670d\u52a1\u7aef\u53ea\u4f1a\u63a5\u6536\u4e00\u6b21\u5ba2\u6237\u7aef\u6570\u636e&#xff0c;\u56de\u590d\u5b8c\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86&#xff1b;\u5f00\u542fwhile()\u6b7b\u5faa\u73af\u540e&#xff0c;\u670d\u52a1\u7aef\u5c31\u4f1a\u4e00\u76f4\u4fdd\u6301\u8fd0\u884c\u72b6\u6001&#xff0c;\u5faa\u73af\u963b\u585e\u7b49\u5f85&#xff0c;\u5b9e\u73b0\u6301\u4e45\u670d\u52a1\u3002<\/p>\n<p>char inbuffer[1024] \u662f\u6211\u4eec\u5728\u7528\u6237\u6001\u6808\u5185\u5b58\u5b9a\u4e49\u7684\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;<span style=\"color:#fe2c24\">\u7528\u6765\u4e34\u65f6\u5b58\u653e\u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u5230\u7684\u6570\u636e<\/span>\u30021024 \u662f\u81ea\u5b9a\u4e49\u7684\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;\u51b3\u5b9a\u4e86\u5355\u6b21\u6700\u591a\u80fd\u63a5\u6536\u591a\u5c11\u5b57\u8282\u7684\u6570\u636e\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053 socket() \u521b\u5efa\u597d\u4e4b\u540e\u5185\u6838\u4f1a\u81ea\u52a8\u4e3a\u8fd9\u4e2a socket \u7f51\u7edc\u6587\u4ef6\u5206\u914d\u4e24\u5757\u4e13\u5c5e\u7f13\u51b2\u533a&#xff1a;\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u548c\u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u7528\u6765\u6682\u5b58\u6570\u636e\u3002\u800c\u6211\u4eec\u4ee3\u7801\u91cc\u53c8\u5b9a\u4e49\u4e86\u00a0 inbuffer\u7528\u6237\u7f13\u51b2\u533a&#xff0c;\u8fd9\u4e24\u8005\u4e0d\u4f1a\u51b2\u7a81\u5417&#xff1f;<\/p>\n<\/p>\n<p>\u7b54\u6848\u662f\u4e0d\u51b2\u7a81&#xff0c;\u5b83\u4eec\u662f\u5206\u5de5\u660e\u786e\u3001\u63a5\u529b\u4f20\u9012\u7684\u5173\u7cfb\u3002\u6211\u4eec\u901a\u8fc7 socket \u521b\u5efa\u7f51\u7edc\u6587\u4ef6\u65f6&#xff0c;Linux \u5185\u6838\u4f1a\u81ea\u52a8\u4e3a\u8fd9\u4e2a socket \u5206\u914d\u4e24\u5757\u4e13\u5c5e\u7f13\u51b2\u533a&#xff1a;\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u548c\u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u7528\u6765\u6682\u5b58\u7f51\u7edc\u6570\u636e\u5305\u3002\u5ba2\u6237\u7aef\u901a\u8fc7 sendto \u53d1\u6765\u7684\u6570\u636e&#xff0c;\u4f1a\u5148\u7ecf\u8fc7\u7f51\u5361&#xff0c;\u76f4\u63a5\u8fdb\u5165\u670d\u52a1\u7aef socket \u5bf9\u5e94\u7684\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u6392\u961f\u7f13\u5b58\u3002\u5f53\u670d\u52a1\u7aef\u8c03\u7528 recvfrom \u63a5\u6536\u6570\u636e\u65f6&#xff0c;\u672c\u8d28\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u628a\u5185\u6838\u7f13\u51b2\u533a\u91cc\u6682\u5b58\u597d\u7684\u6570\u636e&#xff0c;\u62f7\u8d1d\u5230\u6211\u4eec\u5728\u7528\u6237\u6808\u4e0a\u81ea\u5b9a\u4e49\u7684 inbuffer \u7f13\u51b2\u533a\u4e2d&#xff0c;\u4e4b\u540e\u6211\u4eec\u7684\u4e1a\u52a1\u4ee3\u7801\u624d\u80fd\u5bf9\u6570\u636e\u8fdb\u884c\u8bfb\u53d6\u3001\u6253\u5370\u548c\u5904\u7406\u4e8c\u8005\u5404\u53f8\u5176\u804c&#xff0c;\u6570\u636e\u662f\u4ece\u5185\u6838\u7f13\u51b2\u533a\u5230\u7528\u6237\u7f13\u51b2\u533a\u5355\u5411\u62f7\u8d1d\u7684\u5173\u7cfb&#xff0c;\u4e0d\u5b58\u5728\u4efb\u4f55\u77db\u76fe\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65\u5b9a\u4e49\u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u8c03\u7528 recvfrom \u963b\u585e\u63a5\u6536\u6570\u636e :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"159\" src=\"2026-10-28vaxwlw3xghd.png\" width=\"1279\" \/><\/p>\n<p>\u8fd9\u662f Start() \u51fd\u6570\u7684\u6838\u5fc3&#xff0c;\u6211\u4eec\u5c06\u8fd9\u4e00\u6b65\u518d\u62c6\u6210\u4e24\u90e8&#xff1a;peer \u7ed3\u6784\u4f53\u7684\u4f5c\u7528\u3001recvfrom \u7cfb\u7edf\u8c03\u7528\u5168\u89e3\u6790\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49 struct sockaddr_in peer&#xff1f;<\/p>\n<p>\u6211\u4eec\u5728\u524d\u9762 Init() \u51fd\u6570\u91cc\u7684\u5b9a\u4e49\u7684<span style=\"color:#fe2c24\"> local \u7ed3\u6784\u4f53&#xff0c;\u5b58\u7684\u662f\u670d\u52a1\u7aef\u81ea\u5df1\u7684 IP &#043; \u7aef\u53e3<\/span>&#xff1b;\u800c\u8fd9\u91cc\u7684<span style=\"color:#fe2c24\"> peer \u7ed3\u6784\u4f53&#xff0c;\u5219\u662f\u7528\u6765\u5b58\u653e\u5ba2\u6237\u7aef\u7684 IP &#043; \u7aef\u53e3\u4fe1\u606f<\/span>\u3002\u56e0\u4e3a UDP \u662f\u65e0\u8fde\u63a5\u534f\u8bae&#xff0c;\u5ba2\u6237\u7aef\u6bcf\u6b21\u53d1\u6570\u636e\u90fd\u53ef\u80fd\u6765\u81ea\u4e0d\u540c\u7684\u5730\u5740&#xff1b;\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u901a\u8fc7 peer \u62ff\u5230\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u7aef\u70b9\u4fe1\u606f&#xff0c;\u624d\u80fd\u77e5\u9053 \u201c\u8c01\u5728\u7ed9\u6211\u53d1\u6d88\u606f\u201d&#xff0c;\u540e\u7eed\u624d\u80fd\u7cbe\u51c6\u628a\u56de\u58f0\u6570\u636e\u539f\u8def\u53d1\u56de\u53bb\u3002len &#061; sizeof(peer) \u63d0\u524d\u8ba1\u7b97\u597d\u7ed3\u6784\u4f53\u5927\u5c0f&#xff0c;\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u5185\u6838\u3002<\/p>\n<h4 id=\"recvfrom%20%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8\">recvfrom \u7cfb\u7edf\u8c03\u7528<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"32\" src=\"2026-10-28k4jm5jcsrvj.png\" width=\"1275\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"196\" src=\"2026-10-282kpiebjmhjn.png\" width=\"622\" \/> recvfrom \u662f UDP \u4e13\u7528\u7684<span style=\"color:#fe2c24\">\u63a5\u6536\u6570\u636e\u7cfb\u7edf\u8c03\u7528<\/span>&#xff0c;\u6838\u5fc3\u4f5c\u7528\u5c31\u662f<span style=\"color:#fe2c24\">\u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u6570\u636e&#xff0c;\u540c\u65f6\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u5730\u5740<\/span>&#xff0c;6 \u4e2a\u53c2\u6570\u9010\u4e2a\u62c6\u89e3&#xff1a;<\/p>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e00\u4e2a\u53c2\u6570 _sockfd \u662f\u6211\u4eec\u7b2c\u4e00\u6b65\u521b\u5efa\u7684 socket \u6587\u4ef6\u63cf\u8ff0\u7b26<\/span>&#xff0c;\u544a\u8bc9\u5185\u6838\u8981\u4ece\u8fd9\u4e2a\u7f51\u7edc\u6587\u4ef6\u00a0socket \u7684\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u8bfb\u53d6\u6570\u636e&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e8c\u4e2a\u53c2\u6570 inbuffer \u662f\u6211\u4eec\u5b9a\u4e49\u7684\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a<\/span>&#xff0c;\u6570\u636e\u4ece\u5185\u6838\u7f13\u51b2\u533a\u62f7\u8d1d\u51fa\u6765\u540e&#xff0c;\u6700\u7ec8\u5b58\u5230\u8fd9\u4e2a\u6570\u7ec4\u91cc&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e09\u4e2a\u53c2\u6570 sizeof(inbuffer) &#8211; 1 \u662f\u6700\u5927\u8bfb\u53d6\u5b57\u8282\u6570<\/span>\u3002\u51cf 1 \u662f\u5b89\u5168\u89c4\u8303&#xff0c;\u9884\u7559 1 \u4e2a\u5b57\u8282\u4f4d\u7f6e\u7ed9\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26 \\\\0&#xff0c;\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u56db\u4e2a\u53c2\u6570 0 \u662f\u8bfb\u53d6\u6807\u5fd7\u4f4d&#xff0c;\u586b 0 \u4ee3\u8868\u9ed8\u8ba4\u963b\u585e\u6a21\u5f0f<\/span>\u2014\u2014 \u5982\u679c\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u6ca1\u6709\u6570\u636e&#xff0c;\u7a0b\u5e8f\u4f1a\u5361\u5728\u8fd9\u4e00\u884c&#xff0c;\u4f11\u7720\u7b49\u5f85&#xff0c;\u76f4\u5230\u6709\u5ba2\u6237\u7aef\u53d1\u6570\u636e\u8fc7\u6765\u624d\u4f1a\u7ee7\u7eed\u6267\u884c&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e94\u4e2a\u53c2\u6570 (struct sockaddr *)&amp;peer \u662f\u5ba2\u6237\u7aef\u5730\u5740\u8f93\u51fa\u53c2\u6570\u3002<\/span>\u5185\u6838\u6536\u5230\u6570\u636e\u540e&#xff0c;\u4f1a\u81ea\u52a8\u628a\u6570\u636e\u5305\u91cc\u5ba2\u6237\u7aef\u7684 IP &#043; \u7aef\u53e3\u5730\u5740\u5199\u5165\u8fd9\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u65b9\u4fbf\u540e\u7eed\u628a\u6570\u636e\u539f\u8def\u53d1\u56de\u53bb&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u516d\u4e2a\u53c2\u6570 &amp;len \u662f\u5730\u5740\u957f\u5ea6\u8f93\u5165\u8f93\u51fa\u53c2\u6570<\/span>&#xff0c;\u4f20\u5165\u7ed3\u6784\u4f53\u5927\u5c0f&#xff0c;\u5185\u6838\u4f7f\u7528\u540e\u66f4\u65b0\u5b9e\u9645\u5199\u5165\u7684\u957f\u5ea6\u3002<\/li>\n<p>\u8fd4\u56de\u503c n \u63a5\u6536\u6210\u529f\u65f6 n \u8868\u793a\u662f\u5b9e\u9645\u8bfb\u53d6\u5230\u7684\u5ba2\u6237\u7aef\u6570\u636e\u5b57\u8282\u6570&#xff1b;n &lt; 0 \u8868\u793a\u5931\u8d25\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\u89e3\u6790\u5ba2\u6237\u7aef\u5730\u5740 &#043; \u5904\u7406\u4e1a\u52a1\u6570\u636e :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"340\" src=\"2026-10-281iahc1ldhrh.png\" width=\"1212\" \/><\/p>\n<p>\u9996\u5148\u6211\u4eec\u8981\u5c06\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u4e3b\u673a\u5b57\u8282\u5e8f&#xff0c;\u56e0\u4e3a\u5185\u6838\u4ece\u7f51\u7edc\u91cc\u62ff\u5230\u7684\u5ba2\u6237\u7aef IP\u3001\u7aef\u53e3&#xff0c;\u90fd\u662f\u7f51\u7edc\u5927\u7aef\u5e8f&#xff0c;CPU \u65e0\u6cd5\u76f4\u63a5\u8bc6\u522b&#xff0c;\u6240\u4ee5\u9700\u8981\u8f6c\u6362\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"28\" src=\"2026-10-28mk3ir0n05sd.png\" width=\"795\" \/>ntohs() \u51fd\u6570\u8868\u793a network to host short&#xff0c;\u628a\u5ba2\u6237\u7aef\u7aef\u53e3\u4ece\u7f51\u7edc\u5927\u7aef\u5e8f&#xff0c;\u8f6c\u6210\u4e3b\u673a\u5c0f\u7aef\u5e8f&#xff0c;\u65b9\u4fbf\u6211\u4eec\u6253\u5370\u663e\u793a&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"23\" src=\"2026-10-28vrx44bbwk3v.png\" width=\"945\" \/>inet_ntoa() \u8868\u793a\u628a 32 \u4f4d\u4e8c\u8fdb\u5236 IP \u5730\u5740&#xff0c;\u8f6c\u6362\u6210\u6211\u4eec\u80fd\u770b\u61c2\u7684\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32&#xff08;\u5982 127.0.0.1\u3001192.168.x.x)\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"29\" src=\"2026-10-28bvtmorhr31p.png\" width=\"935\" \/>\u8fd9\u884c\u4ee3\u7801\u628a\u5ba2\u6237\u7aef\u7684 IP \u548c\u7aef\u53e3\u53f7&#xff0c;\u62fc\u63a5\u6210\u4e00\u4e2a\u6211\u4eec\u80fd\u76f4\u63a5\u770b\u61c2\u7684\u5b57\u7b26\u4e32\u683c\u5f0f&#xff0c;\u65b9\u4fbf\u65e5\u5fd7\u6253\u5370\u548c\u8c03\u8bd5\u3002client_ip \u5c31\u662f\u524d\u9762\u6211\u4eec\u7528 inet_ntoa(peer.sin_addr) \u8f6c\u6362\u5f97\u5230\u7684\u5b57\u7b26\u4e32 IP&#xff0c;\u6bd4\u5982 &#034;127.0.0.1&#034;\u3002std::to_string(client_port) \u628a ntohs \u8f6c\u6362\u540e\u7684\u7aef\u53e3\u53f7\u6574\u6570&#xff0c;\u6bd4\u5982 54321&#xff0c;\u8f6c\u6362\u6210\u5b57\u7b26\u4e32 &#034;54321&#034;\u3002\u6309\u7167\u62fc\u63a5\u683c\u5f0f&#xff1a;\u7528 [IP:\u7aef\u53e3] \u7684\u5f62\u5f0f&#xff0c;\u6bd4\u5982 [127.0.0.1:54321]&#xff0c;\u518d\u52a0\u4e0a # \u4f5c\u4e3a\u5206\u9694\u7b26&#xff0c;\u548c\u540e\u9762\u7684\u6d88\u606f\u4f53\u533a\u5206\u5f00\u3002\u8fd9\u4e48\u505a\u7684\u597d\u5904\u5c31\u662f\u65e5\u5fd7\u6253\u5370\u7684\u65f6\u5019&#xff0c;\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u8fd9\u6761\u6d88\u606f\u6765\u81ea\u54ea\u4e2a\u5ba2\u6237\u7aef&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"45\" src=\"2026-10-28kfq5eixv0jl.png\" width=\"805\" \/>\u624b\u52a8\u8865\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\u3002\u56e0\u4e3a\u7f51\u7edc\u6570\u636e\u662f\u7eaf\u5b57\u8282\u6d41&#xff0c;\u6ca1\u6709\u7ed3\u675f\u6807\u8bb0&#xff0c;\u5fc5\u987b\u624b\u52a8\u52a0 \\\\0&#xff0c;\u5426\u5219\u6253\u5370\u5b57\u7b26\u4e32\u65f6\u4f1a\u51fa\u73b0\u4e71\u7801&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"96\" src=\"2026-10-28oeeu0blgm01.png\" width=\"768\" \/> \u7b2c\u4e00\u884c\u4ee3\u7801\u662f\u628a\u6211\u4eec\u524d\u9762\u62fc\u63a5\u597d\u7684 client_address&#xff08;\u4e5f\u5c31\u662f [\u5ba2\u6237\u7aefIP:\u7aef\u53e3]&#xff09;\u548c\u6536\u5230\u7684\u539f\u59cb\u6d88\u606f inbuffer&#xff0c;\u4e00\u8d77\u6253\u5370\u5230\u65e5\u5fd7\u91cc\u3002\u540e\u4e24\u884c\u4ee3\u7801\u5148\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u524d\u7f00\u5b57\u7b26\u4e32 &#034;server echo# &#034;&#xff1b;\u628a\u5ba2\u6237\u7aef\u53d1\u6765\u7684 inbuffer \u5185\u5bb9\u62fc\u63a5\u5728\u540e\u9762&#xff1b;\u6700\u7ec8\u751f\u6210\u7684 echo_string \u5c31\u662f&#xff1a;&#034;server echo# &#034;\u3002\u5b9e\u73b0\u6211\u4eec\u7684 \u201c\u56de\u58f0\u670d\u52a1\u5668\u201d \u529f\u80fd&#xff1a;\u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u7aef\u539f\u6837\u8fd4\u56de&#xff0c;\u5e76\u4e14\u52a0\u4e0a\u670d\u52a1\u7aef\u7684\u6807\u8bb0\u524d\u7f00\u3002<\/p>\n<p>\u6b65\u9aa4\u56db\u8c03\u7528 sendto \u539f\u8def\u56de\u590d\u6570\u636e\u7ed9\u5ba2\u6237\u7aef\u00a0 <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"39\" src=\"2026-10-28dzoqmlzmins.png\" width=\"1160\" \/><\/p>\n<h4 id=\"sendto%20%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8\">sendto \u7cfb\u7edf\u8c03\u7528<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"305\" src=\"2026-10-28xq2pfoqzom1.png\" width=\"948\" \/> sendto \u662f UDP \u4e13\u7528\u7684<span style=\"color:#fe2c24\">\u53d1\u9001\u6570\u636e\u7cfb\u7edf\u8c03\u7528&#xff0c;\u6838\u5fc3\u4f5c\u7528\u662f\u628a\u5904\u7406\u597d\u7684\u56de\u58f0\u6570\u636e&#xff0c;\u539f\u8def\u53d1\u56de\u7ed9\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef<\/span>&#xff0c;\u5b83\u4e5f\u662f 6 \u4e2a\u53c2\u6570&#xff0c;\u548c recvfrom() \u7684 6 \u4e2a\u53c2\u6570\u4e00\u4e00\u5bf9\u5e94&#xff0c;\u4f46\u662f\u903b\u8f91\u6070\u6070\u76f8\u53cd&#xff1a;<\/p>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e00\u4e2a\u53c2\u6570 _sockfd \u540c\u6837\u4f7f\u7528\u6211\u4eec\u7684 socket \u6587\u4ef6\u63cf\u8ff0\u7b26<\/span>&#xff0c;\u901a\u8fc7\u5b83\u64cd\u4f5c\u5185\u6838\u7f51\u7edc\u6587\u4ef6 socket \u7684\u53d1\u9001\u7f13\u51b2\u533a&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e8c\u4e2a\u53c2\u6570 const void *buf \u662f\u8981\u53d1\u9001\u7684\u6570\u636e\u7684\u8d77\u59cb\u5730\u5740<\/span>&#xff0c;\u8fd9\u91cc\u6211\u4eec\u586b echo_string.c_str() \u8868\u793a\u628a C&#043;&#043; string \u8f6c\u6210 C \u8bed\u8a00 const char* \u683c\u5f0f&#xff0c;\u56e0\u4e3a echo_string \u5df2\u7ecf &#043;&#061; \u4e86 inbuffer[]&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e09\u4e2a\u53c2\u6570 size_t len \u8868\u793a\u8981\u53d1\u9001\u7684\u6570\u636e\u5b9e\u9645\u5b57\u8282\u957f\u5ea6<\/span>&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u56db\u4e2a\u53c2\u6570 0 \u8868\u793a\u53d1\u9001\u6807\u5fd7\u4f4d&#xff0c;\u586b 0 \u4ee3\u8868\u9ed8\u8ba4\u6a21\u5f0f<\/span>&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u4e94\u4e2a\u53c2\u6570 (struct sockaddr *)&amp;peer \u662f\u76ee\u6807\u5ba2\u6237\u7aef\u5730\u5740<\/span>\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u524d\u9762\u89e3\u6790 peer \u7ed3\u6784\u4f53\u7684\u610f\u4e49 \u2014\u2014 \u544a\u8bc9\u5185\u6838&#xff0c;\u8fd9\u6761\u6570\u636e\u8981\u53d1\u7ed9\u54ea\u4e2a\u5ba2\u6237\u7aef\u7684 IP &#043; \u7aef\u53e3&#xff1b;<\/li>\n<li><span style=\"color:#ff9900\">\u7b2c\u516d\u4e2a\u53c2\u6570 len \u662f\u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784\u4f53\u7684\u957f\u5ea6<\/span>\u3002<\/li>\n<p><span style=\"color:#fe2c24\">recvfrom \u672c\u8d28\u662f&#xff1a;\u4ece socket \u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a \u2192 \u62f7\u8d1d\u6570\u636e\u5230\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff1b;<\/span><\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\">sendto \u672c\u8d28\u662f&#xff1a;\u4ece\u7528\u6237\u6001\u7f13\u51b2\u533a \u2192 \u62f7\u8d1d\u6570\u636e\u5230 socket \u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a \u2192 \u5185\u6838\u5f02\u6b65\u901a\u8fc7\u7f51\u5361\u53d1\u9001\u5230\u7f51\u7edc\u3002<\/span><\/p>\n<h2 id=\"EchoServerMain.cc%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%85%A5%E5%8F%A3%E6%96%87%E4%BB%B6\">EchoServerMain.cc \u670d\u52a1\u7aef\u5165\u53e3\u6587\u4ef6<\/h2>\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec UDP \u56de\u58f0\u670d\u52a1\u5668\u7684\u7a0b\u5e8f\u5165\u53e3\u6587\u4ef6&#xff0c;\u4e5f\u662f\u6574\u4e2a\u670d\u52a1\u7aef\u9879\u76ee\u7684\u5165\u53e3\u3002\u524d\u9762\u6211\u4eec\u62c6\u89e3\u4e86 UdpServer \u7c7b\u7684\u5934\u6587\u4ef6\u3001\u6210\u5458\u65b9\u6cd5\u3001\u5e95\u5c42\u901a\u4fe1\u903b\u8f91&#xff0c;\u800c\u8fd9\u4e2a\u5165\u53e3\u6587\u4ef6\u7684\u4f5c\u7528\u5c31\u662f<span style=\"color:#fe2c24\">\u6821\u9a8c\u542f\u52a8\u53c2\u6570\u3001\u521b\u5efa\u670d\u52a1\u7aef\u5bf9\u8c61\u3001\u8c03\u7528\u521d\u59cb\u5316\u4e0e\u4e1a\u52a1\u5faa\u73af&#xff0c;<\/span>\u4e00\u952e\u542f\u52a8\u6574\u4e2a UDP \u670d\u52a1\u3002\u6211\u4eec\u987a\u7740\u4ee3\u7801\u987a\u5e8f&#xff0c;\u5206 4 \u4e2a\u6b65\u9aa4\u9010\u884c\u62c6\u89e3&#xff0c;\u540c\u65f6\u8bb2\u6e05\u5b83\u548c\u5ba2\u6237\u7aef\u7684\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"792\" src=\"2026-10-28qfjrww1qsbc.png\" width=\"1292\" \/><\/p>\n<p>\u7b2c\u4e00\u6b65\u662f\u5de5\u5177\u51fd\u6570\u5b9a\u4e49 \u2014\u2014 \u7a0b\u5e8f\u4f7f\u7528\u63d0\u793a:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"144\" src=\"2026-10-28ttzppx0sxi0.png\" width=\"1134\" \/> Usage \u662f\u4e00\u4e2a\u9759\u6001\u5de5\u5177\u51fd\u6570&#xff0c;\u4e13\u95e8\u7528\u6765\u6253\u5370\u7a0b\u5e8f\u7684\u6b63\u786e\u542f\u52a8\u683c\u5f0f\u3002\u53c2\u6570 process \u7528\u6765\u63a5\u6536\u7a0b\u5e8f\u81ea\u8eab\u540d\u79f0&#xff08;\u6bd4\u5982 .\/server_udp)&#xff1b;<\/p>\n<\/p>\n<p>\u8f93\u51fa\u683c\u5f0f\u4e3a <span style=\"color:#fe2c24\">.\/server_udp &#043; local_port<\/span>&#xff0c;\u4e5f\u5c31\u662f\u544a\u8bc9\u4f7f\u7528\u8005&#xff1a;\u5728 .\/server_udp \u542f\u52a8\u670d\u52a1\u65f6&#xff0c;\u4e5f\u5fc5\u987b\u5728\u540e\u9762\u4f20\u5165\u4e00\u4e2a\u7aef\u53e3\u53f7\u53c2\u6570 port&#xff1b;\u5426\u5219\u53c2\u6570\u6570\u91cf\u5c31\u4e0d\u5bf9&#xff0c;\u4f1a\u81ea\u52a8\u89e6\u53d1\u63d0\u793a\u3002<\/p>\n<p>\u7b2c\u4e8c\u90e8\u5c31\u662f\u4e3b\u51fd\u6570\u7684\u5165\u53e3 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"222\" src=\"2026-10-28fb3u5kxntjn.png\" width=\"726\" \/> argc \u662f\u547d\u4ee4\u884c\u53c2\u6570\u4e2a\u6570&#xff0c;\u7a0b\u5e8f\u81ea\u8eab\u540d\u79f0\u7b97\u7b2c 1 \u4e2a\u53c2\u6570&#xff1b;argv[] \u662f\u547d\u4ee4\u884c\u53c2\u6570\u6570\u7ec4&#xff0c;argv[0] \u5b58\u7a0b\u5e8f\u540d&#xff0c;argv[1] \u5b58\u7528\u6237\u4f20\u5165\u7684\u7aef\u53e3\u53f7\u3002  \u6211\u4eec\u7684\u670d\u52a1\u7aef\u542f\u52a8\u540e&#xff0c;\u5fc5\u987b\u4f20\u5165 1 \u4e2a\u7aef\u53e3\u53c2\u6570&#xff0c;\u6240\u4ee5\u8981\u6c42 argc &#061;&#061; 2&#xff1b;\u5982\u679c\u53c2\u6570\u6570\u91cf\u4e0d\u5bf9\u5c31\u4f1a\u8c03\u7528 Usage \u6253\u5370\u4f7f\u7528\u63d0\u793a&#xff0c;\u76f4\u63a5\u4ee5 USAGE_ERR \u9519\u8bef\u7801\u9000\u51fa\u7a0b\u5e8f&#xff1b;\u8fd9\u4e00\u6b65\u662f\u7a0b\u5e8f\u542f\u52a8\u7684\u5b89\u5168\u6821\u9a8c&#xff0c;\u907f\u514d\u540e\u7eed\u56e0\u4e3a\u7aef\u53e3\u53c2\u6570\u7f3a\u5931\u5bfc\u81f4\u521d\u59cb\u5316\u5931\u8d25\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\u5c31\u662f\u73af\u5883\u914d\u7f6e &#043; \u7aef\u53e3\u53c2\u6570\u89e3\u6790 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"86\" src=\"2026-10-28wcjx1jk33fg.png\" width=\"1129\" \/> ENABLE_CONSOLE_LOG_STRATEGY() \u662f\u542f\u7528\u63a7\u5236\u53f0\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p>argv[1] \u662f\u7528\u6237\u547d\u4ee4\u884c\u4f20\u5165\u7684\u7aef\u53e3\u5b57\u7b26\u4e32&#xff08;\u6bd4\u5982 8080)&#xff1b;<span style=\"color:#fe2c24\">std::stoi(argv[1]) \u8868\u793a\u5c06\u5b57\u7b26\u4e32\u7aef\u53e3 argv[1]\u00a0\u8f6c\u4e3a\u6574\u6570&#xff0c;\u8d4b\u503c\u7ed9 uint16_t \u7c7b\u578b\u7684 server_port<\/span>&#xff1b;\u8fd9\u91cc\u7684\u7aef\u53e3&#xff0c;\u5c31\u662f\u670d\u52a1\u7aef\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u7684\u552f\u4e00\u6807\u8bc6&#xff0c;\u540e\u7eed\u5ba2\u6237\u7aef\u5fc5\u987b\u6307\u5b9a\u8fd9\u4e2a\u7aef\u53e3&#xff0c;\u624d\u80fd\u548c\u670d\u52a1\u7aef\u5efa\u7acb UDP \u901a\u4fe1\u3002<\/p>\n<p>\u6700\u540e\u4e00\u6b65\u521b\u5efa\u670d\u52a1\u7aef\u5bf9\u8c61 &#043; \u542f\u52a8\u670d\u52a1\u95ed\u73af:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"220\" src=\"2026-10-28mup2z50idr1.png\" width=\"1195\" \/> \u9996\u5148\u6211\u4eec\u521b\u5efa\u667a\u80fd\u6307\u9488\u5bf9\u8c61&#xff0c;\u91c7\u7528 C&#043;&#043;14 \u667a\u80fd\u6307\u9488\u7ba1\u7406 UdpServer \u5bf9\u8c61&#xff0c;\u81ea\u52a8\u7ba1\u7406\u5185\u5b58&#xff0c;\u7a0b\u5e8f\u9000\u51fa\u65f6\u81ea\u52a8\u6790\u6784&#xff0c;\u907f\u514d\u5185\u5b58\u6cc4\u6f0f&#xff1b;<span style=\"color:#fe2c24\">\u521b\u5efa\u5bf9\u8c61\u65f6&#xff0c;\u4f20\u5165\u6211\u4eec\u89e3\u6790\u597d\u7684 server_port \u7aef\u53e3\u53f7&#xff0c;\u7aef\u53e3\u4fe1\u606f\u76f4\u63a5\u4f20\u5165\u670d\u52a1\u7aef\u7c7b&#xff0c;\u548c\u524d\u9762\u5934\u6587\u4ef6\u7684\u6784\u9020\u51fd\u6570\u5f62\u6210\u95ed\u73af\u3002<\/span><\/p>\n<\/p>\n<p>\u968f\u540e\u8c03\u7528 Init () \u521d\u59cb\u5316&#xff0c;\u8c03\u7528\u6211\u4eec\u4e4b\u524d\u8be6\u7ec6\u62c6\u89e3\u7684\u521d\u59cb\u5316\u51fd\u6570&#xff1a;\u521b\u5efa socket \u5185\u6838\u6587\u4ef6\u3001\u7ed1\u5b9a INADDR_ANY \u5168\u7f51\u5361\u76d1\u542c\u3001\u5b8c\u6210\u64cd\u4f5c\u7cfb\u7edf\u7aef\u53e3\u6ce8\u518c&#xff1b;\u6267\u884c\u5b8c\u8fd9\u4e00\u6b65&#xff1a;\u670d\u52a1\u7aef\u5df2\u7ecf\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6ce8\u518c\u5b8c\u6210&#xff0c;\u7aef\u53e3\u5904\u4e8e\u76d1\u542c\u72b6\u6001&#xff0c;\u5ba2\u6237\u7aef\u6b64\u65f6\u5df2\u7ecf\u53ef\u4ee5\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p>\u6700\u540e\u8c03\u7528 Start () \u542f\u52a8\u4e1a\u52a1\u5faa\u73af&#xff0c;\u8c03\u7528\u6211\u4eec\u7684\u6838\u5fc3\u4e1a\u52a1\u51fd\u6570&#xff1a;\u5f00\u542f\u6b7b\u5faa\u73af&#xff0c;\u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u6570\u636e\u3001\u63a5\u6536\u89e3\u6790\u5ba2\u6237\u7aef\u5730\u5740\u3001\u5c01\u88c5\u56de\u58f0\u6570\u636e\u3001sendto \u539f\u8def\u56de\u590d&#xff1b;\u6267\u884c\u5b8c\u8fd9\u4e00\u6b65&#xff1a;\u670d\u52a1\u7aef\u8fdb\u5165\u670d\u52a1\u72b6\u6001&#xff0c;\u6301\u7eed\u5904\u7406\u6240\u6709\u5ba2\u6237\u7aef\u7684 UDP \u8bf7\u6c42&#xff0c;\u76f4\u5230\u7a0b\u5e8f\u624b\u52a8\u7ec8\u6b62\u3002<\/p>\n<h4 id=\"%E8%BF%90%E8%A1%8C%E6%9C%8D%E5%8A%A1%E7%AB%AF%20%3A\">\u8fd0\u884c\u670d\u52a1\u7aef :<\/h4>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62&#xff0c;\u6211\u4eec\u5bf9\u6574\u4e2a\u670d\u52a1\u7aef\u7684\u4ee3\u7801\u7f16\u5199\u5c31\u5b8c\u6210\u4e86&#xff0c;\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e0b\u670d\u52a1\u7aef\u7684\u7a0b\u5e8f :\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"198\" src=\"2026-10-281f1wzulibgj.png\" width=\"1211\" \/>\u5f53\u6211\u4eec\u6267\u884c\u547d\u4ee4 .\/server_udp 8080 \u540e&#xff0c;\u7ec8\u7aef\u5c31\u8f93\u51fa\u4e86\u4e24\u884c\u65e5\u5fd7 :\u00a0<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"51\" src=\"2026-10-28ytqzecjxig1.png\" width=\"992\" \/>\u7b2c\u4e00\u884c\u65e5\u5fd7\u5bf9\u5e94 socket() \u8c03\u7528\u6210\u529f&#xff0c;sockfd: 3 \u8bf4\u660e\u5185\u6838\u6210\u529f\u4e3a\u6211\u4eec\u521b\u5efa\u4e86 socket \u6587\u4ef6\u5bf9\u8c61&#xff0c;\u5e76\u5206\u914d\u4e86\u6587\u4ef6\u63cf\u8ff0\u7b26 3\u3002<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"45\" src=\"2026-10-28hiuxh4jhtlk.png\" width=\"1054\" \/>\u7b2c\u4e8c\u884c\u65e5\u5fd7\u5bf9\u5e94 bind() \u8c03\u7528\u6210\u529f&#xff0c;port: 8080 \u8bf4\u660e\u6211\u4eec\u7ed1\u5b9a\u7684 8080 \u7aef\u53e3\u6ce8\u518c\u6210\u529f\u3002\u7ed3\u5408\u6211\u4eec\u4f7f\u7528\u7684 INADDR_ANY&#xff0c;\u670d\u52a1\u7aef\u5df2\u7ecf\u5f00\u59cb\u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361\u4e0a\u3001\u76ee\u6807\u7aef\u53e3\u4e3a 8080 \u7684 UDP \u6570\u636e\u5305\u3002<\/p>\n<\/p>\n<p>\u8f93\u51fa\u5b8c\u8fd9\u4e24\u884c\u65e5\u5fd7\u540e&#xff0c;\u5149\u6807\u505c\u7559\u5728\u4e0b\u4e00\u884c&#xff0c;\u7a0b\u5e8f\u6ca1\u6709\u9000\u51fa&#xff0c;\u4e5f\u6ca1\u6709\u65b0\u65e5\u5fd7\u8f93\u51fa&#xff0c;\u8fd9\u6b63\u662f\u6211\u4eec\u671f\u671b\u7684\u72b6\u6001&#xff1a;\u670d\u52a1\u7aef\u5df2\u7ecf\u6210\u529f\u521b\u5efa socket\u3001\u7ed1\u5b9a\u7aef\u53e3&#xff0c;\u8fdb\u5165\u4e86 Start() \u51fd\u6570\u91cc\u7684 while(true) \u6b7b\u5faa\u73af\u3002\u6b64\u65f6 recvfrom() \u6b63\u5728\u5185\u6838\u4e2d\u963b\u585e\u7b49\u5f85&#xff0c;\u670d\u52a1\u7aef\u5df2\u7ecf\u51c6\u5907\u597d\u63a5\u6536\u4efb\u4f55\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"EchoClient.cc%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%96%87%E4%BB%B6%20%3A\">EchoClient.cc \u5ba2\u6237\u7aef\u6587\u4ef6 :<\/h2>\n<p>\u524d\u9762\u6211\u4eec\u5b8c\u6574\u62c6\u89e3\u4e86 UDP \u670d\u52a1\u7aef\u7684\u5168\u94fe\u8def\u903b\u8f91&#xff0c;\u670d\u52a1\u7aef\u662f\u88ab\u52a8\u76d1\u542c\u3001\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u89d2\u8272&#xff1b;\u800c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u6b63\u597d\u76f8\u53cd&#xff0c;\u662f\u4e3b\u52a8\u53d1\u8d77\u901a\u4fe1\u3001\u5b9a\u5411\u7ed9\u6307\u5b9a\u670d\u52a1\u7aef\u53d1\u6570\u636e\u7684\u89d2\u8272\u3002\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u770b\u5ba2\u6237\u7aef\u7684\u4ee3\u7801:<\/p>\n<h3 id=\"%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%8E%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%9A%84%E6%A0%B8%E5%BF%83%E5%BC%82%E5%90%8C\">\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u6838\u5fc3\u5f02\u540c<\/h3>\n<p>\u2705 \u76f8\u540c\u70b9 :\u00a0\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u4ee3\u7801\u6838\u5fc3\u5b8c\u5168\u4e00\u81f4&#xff0c;\u901a\u4fe1\u63a5\u53e3\u5b8c\u5168\u4e00\u81f4<\/p>\n<p>1. \u90fd\u9700\u8981<span style=\"color:#fe2c24\">\u901a\u8fc7 socket() \u521b\u5efa\u5185\u6838\u7f51\u7edc\u6587\u4ef6\u3001\u83b7\u53d6 fd \u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4f9d\u8d56 Linux \u5185\u6838\u7f13\u51b2\u533a\u5b8c\u6210\u6570\u636e\u6536\u53d1&#xff1b;<\/span><\/p>\n<\/p>\n<p>2. <span style=\"color:#fe2c24\">\u90fd\u4f7f\u7528sendto()\u53d1\u9001\u6570\u636e\u3001recvfrom()\u63a5\u6536\u6570\u636e<\/span>&#xff0c;\u9075\u5faa UDP \u65e0\u8fde\u63a5\u901a\u4fe1\u6807\u51c6\u3002<\/p>\n<p>\u274c \u6838\u5fc3\u4e0d\u540c\u70b9&#xff08;\u6700\u5173\u952e&#xff09;<\/p>\n<p>\u89d2\u8272\u5b9a\u4f4d\u4e0d\u540c :<\/p>\n<p>\u00a0 1. \u670d\u52a1\u7aef\u662f\u88ab\u52a8\u65b9&#xff0c;\u5fc5\u987bbind()\u7ed1\u5b9a\u56fa\u5b9a\u7aef\u53e3 &#043; INADDR_ANY\u5168\u7f51\u5361\u76d1\u542c&#xff0c;\u8ba9\u5ba2\u6237\u7aef\u80fd\u7cbe\u51c6\u627e\u5230\u81ea\u5df1&#xff1b;<\/p>\n<p>\u00a0 2. \u800c<span style=\"color:#fe2c24\">\u5ba2\u6237\u7aef\u662f\u4e3b\u52a8\u65b9&#xff0c;\u4e0d\u9700\u8981\u624b\u52a8 bind \u7ed1\u5b9a\u7aef\u53e3<\/span>&#xff0c;\u9996\u6b21\u8c03\u7528 sendto() \u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5206\u914d\u968f\u673a\u4e34\u65f6\u7aef\u53e3&#xff0c;\u7531 OS \u7ba1\u7406\u7aef\u53e3\u552f\u4e00\u6027&#xff0c;\u907f\u514d\u7aef\u53e3\u51b2\u7a81\u3002<\/p>\n<p>\u5730\u5740\u903b\u8f91\u4e0d\u540c :<\/p>\n<p>\u00a0 1. \u670d\u52a1\u7aef\u9700\u8981\u7ed1\u5b9a\u81ea\u5df1\u7684\u5730\u5740&#xff0c;\u7b49\u5f85\u6240\u6709\u5ba2\u6237\u7aef\u4e3b\u52a8\u627e\u8fc7\u6765&#xff1b;<\/p>\n<p>\u00a0 2. \u800c\u5ba2\u6237\u7aef\u9700\u8981\u9884\u5148\u914d\u7f6e\u670d\u52a1\u7aef\u7684 IP &#043; \u7aef\u53e3&#xff0c;\u4e3b\u52a8\u5b9a\u5411\u8bbf\u95ee\u6307\u5b9a\u670d\u52a1\u7aef\u3002<\/p>\n<p>\u5faa\u73af\u903b\u8f91\u4e0d\u540c :<\/p>\n<p>\u00a0 1. \u670d\u52a1\u7aef\u662f\u6b7b\u5faa\u73afwhile(true)\u963b\u585e\u76d1\u542c&#xff0c;\u6301\u7eed\u7b49\u5f85\u4efb\u610f\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff1b;<\/p>\n<p>\u00a0 2. \u5ba2\u6237\u7aef\u662f\u6b7b\u5faa\u73af\u7528\u4e8e\u6301\u7eed\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3001\u4e3b\u52a8\u53d1\u8d77\u5355\u6b21\u901a\u4fe1&#xff0c;\u6bcf\u6b21\u5faa\u73af\u5b8c\u6210\u4e00\u6b21 \u201c\u53d1\u9001 &#8211; \u63a5\u6536\u201d \u7684\u8bf7\u6c42\u5e94\u7b54\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u7684\u6267\u884c\u903b\u8f91\u53ef\u4ee5\u5206\u4e3a 5 \u4e2a\u6838\u5fc3\u6b65\u9aa4&#xff0c;\u4e0b\u9762\u6211\u4eec\u9010\u4e00\u4ecb\u7ecd&#xff1a;<\/p>\n<h3 id=\"1.%20%E5%A4%B4%E6%96%87%E4%BB%B6%E4%B8%8E%E4%BD%BF%E7%94%A8%E6%8F%90%E7%A4%BA%E5%87%BD%E6%95%B0\">1. \u5934\u6587\u4ef6\u4e0e\u4f7f\u7528\u63d0\u793a\u51fd\u6570<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"403\" src=\"2026-10-2855dhfjzx1jj.png\" width=\"1044\" \/><\/p>\n<p>1. \u5934\u6587\u4ef6\u548c\u670d\u52a1\u7aef\u5b8c\u5168\u4e00\u81f4&#xff0c;\u5305\u542b\u7f51\u7edc\u7f16\u7a0b\u5fc5\u5907\u7684<span style=\"color:#fe2c24\"> socket.h\u3001inet.h\u3001in.h<\/span>&#xff0c;\u63d0\u4f9b socket \u521b\u5efa\u3001IP \/ \u7aef\u53e3\u8f6c\u6362\u3001\u5730\u5740\u7ed3\u6784\u4f53\u5b9a\u4e49\u7684\u7cfb\u7edf\u63a5\u53e3&#xff1b;<\/p>\n<\/p>\n<p>2. Usage \u5de5\u5177\u51fd\u6570&#xff1a;\u6253\u5370\u5ba2\u6237\u7aef\u6b63\u786e\u542f\u52a8\u683c\u5f0f\u3002\u5ba2\u6237\u7aef\u542f\u52a8\u5fc5\u987b\u4f20\u5165\u4e24\u4e2a\u53c2\u6570<span style=\"color:#494949\">&#xff1a;<\/span><span style=\"color:#fe2c24\">\u670d\u52a1\u7aef IP\u3001\u670d\u52a1\u7aef\u7aef\u53e3<\/span>&#xff0c;\u4f8b\u5982 <span style=\"color:#ff9900\">.\/client_udp 127.0.0.1 8080<\/span>\u3002\u53c2\u6570\u7f3a\u5931\u4f1a\u76f4\u63a5\u63d0\u793a\u7528\u6cd5\u5e76\u9000\u51fa&#xff0c;\u548c\u670d\u52a1\u7aef\u53c2\u6570\u6821\u9a8c\u903b\u8f91\u5bf9\u5e94&#xff0c;\u4f46\u6821\u9a8c\u7684\u53c2\u6570\u542b\u4e49\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n<h3 id=\"2.%20%E4%B8%BB%E5%87%BD%E6%95%B0%E5%85%A5%E5%8F%A3%20%26%20%E5%90%AF%E5%8A%A8%E5%8F%82%E6%95%B0\">2. \u4e3b\u51fd\u6570\u5165\u53e3 &amp; \u542f\u52a8\u53c2\u6570<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"290\" src=\"2026-10-28euyj35baoio.png\" width=\"999\" \/><\/p>\n<li>argv[0] \u8868\u793a\u5ba2\u6237\u7aef\u7a0b\u5e8f\u540d&#xff1b;<\/li>\n<li>argv[1] \u8868\u793a\u670d\u52a1\u7aef IP&#xff08;\u5982127.0.0.1\u3001\u516c\u7f51 IP)&#xff1b;<\/li>\n<li>argv[2] \u8868\u793a\u670d\u52a1\u7aef\u7aef\u53e3&#xff08;\u5fc5\u987b\u548c\u670d\u52a1\u7aef bind \u7ed1\u5b9a\u7684\u7aef\u53e3\u5b8c\u5168\u4e00\u81f4&#xff0c;\u5982 8080)&#xff1b;<\/li>\n<p>1. \u5f53 argc !&#061; 3 \u5c31\u4f1a\u76f4\u63a5\u9000\u51fa&#xff0c;\u56e0\u4e3a\u5ba2\u6237\u7aef\u5fc5\u987b\u660e\u786e\u77e5\u9053\u670d\u52a1\u7aef\u7684 IP \u548c\u7aef\u53e3&#xff0c;\u8fd9\u662f\u4e3b\u52a8\u901a\u4fe1\u7684\u524d\u63d0&#xff1b;<\/p>\n<\/p>\n<p>2. \u4e0b\u6765\u5c31\u5c06\u547d\u4ee4\u884c\u4f20\u5165\u7684\u5b57\u7b26\u4e32 IP\u3001\u7aef\u53e3&#xff0c;\u8f6c\u4e3a\u7a0b\u5e8f\u53ef\u7528\u7684 std::string \u548c uint16_t \u7c7b\u578b&#xff0c;\u4e3a\u540e\u7eed\u505a\u51c6\u5907\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5ba2\u6237\u7aef\u4f20\u5165\u7684\u662f\u670d\u52a1\u7aef\u7684 IP \u548c\u670d\u52a1\u7aef\u7684\u7aef\u53e3\u53f7&#xff1f;<\/p>\n<\/p>\n<p>\u56e0\u4e3a\u5ba2\u6237\u7aef\u662f\u4e3b\u52a8\u627e\u4eba\u7684\u4e00\u65b9&#xff0c;\u5b83\u5fc5\u987b\u77e5\u9053\u5bf9\u65b9\u5728\u54ea&#xff0c;\u4e0d\u7136\u4e0d\u77e5\u9053\u6570\u636e\u8be5\u53d1\u7ed9\u8c01\u3002\u5ba2\u6237\u7aef\u542f\u52a8\u4f20\u5165\u670d\u52a1\u7aef IP \u548c\u7aef\u53e3&#xff0c;\u672c\u8d28\u662f\u4e3a\u4e86\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u6307\u660e\u901a\u4fe1\u76ee\u6807\u3002UDP \u662f\u65e0\u8fde\u63a5\u901a\u4fe1&#xff0c;\u5ba2\u6237\u7aef\u6bcf\u6b21\u53d1\u9001\u6570\u636e\u5305\u90fd\u5fc5\u987b\u660e\u786e\u6307\u5b9a\u63a5\u6536\u65b9\u5730\u5740&#xff0c;\u56e0\u6b64\u5fc5\u987b\u9884\u5148\u77e5\u9053\u670d\u52a1\u7aef\u56fa\u5b9a\u7684\u76d1\u542c IP \u4e0e\u7aef\u53e3&#xff1b;\u800c\u5ba2\u6237\u7aef\u81ea\u8eab\u7684\u5730\u5740\u7531\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u968f\u673a\u4e34\u65f6\u7aef\u53e3&#xff0c;\u65e0\u9700\u624b\u52a8\u914d\u7f6e\u3002<\/p>\n<h3 id=\"3.%20%E5%88%9B%E5%BB%BA%20socket\">3. \u521b\u5efa socket<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"212\" src=\"2026-10-28tnbm5ckxmy5.png\" width=\"900\" \/><\/p>\n<p>1. \u5ba2\u6237\u7aef\u521b\u5efa socket \u903b\u8f91\u548c\u670d\u52a1\u7aef\u4e00\u81f4&#xff1a;AF_INET \u8868\u793a IPv4 \u534f\u8bae&#xff1b;SOCK_DGRAM \u8868\u793a UDP \u65e0\u8fde\u63a5\u6570\u636e\u62a5&#xff1b;\u6838\u5fc3\u4f5c\u7528\u5c31\u662f\u5ba2\u6237\u7aef\u540c\u6837\u5411 Linux \u5185\u6838\u7533\u8bf7\u521b\u5efa\u4e00\u4e2a socket \u7f51\u7edc\u6587\u4ef6&#xff0c;\u62ff\u5230\u552f\u4e00 fd\u3002\u540e\u7eed\u6240\u6709\u7684\u64cd\u4f5c&#xff0c;\u90fd\u901a\u8fc7\u8fd9\u4e2a fd \u64cd\u4f5c\u5ba2\u6237\u7aef socket \u7684\u5185\u6838\u6536\u53d1\u7f13\u51b2\u533a\u3002<\/p>\n<\/p>\n<p>2. \u9700\u8981\u6ce8\u610f\u7684\u662f : \u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u7528\u7684\u4e0d\u662f\u540c\u4e00\u4e2a\u00a0socket&#xff0c;\u4e8c\u8005\u5b8c\u5168\u72ec\u7acb\u3002\u670d\u52a1\u7aef\u8c03\u7528 socket () \u521b\u5efa\u4e00\u4e2a\u5185\u6838\u7f51\u7edc\u6587\u4ef6&#xff0c;\u7ed1\u5b9a\u56fa\u5b9a\u7aef\u53e3\u88ab\u52a8\u76d1\u542c&#xff1b;\u5ba2\u6237\u7aef\u540c\u6837\u8c03\u7528 socket () \u521b\u5efa\u53e6\u4e00\u4e2a\u5168\u65b0\u7684\u5185\u6838\u7f51\u7edc\u6587\u4ef6&#xff0c;\u7531\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u968f\u673a\u7aef\u53e3\u4e3b\u52a8\u901a\u4fe1\u3002\u4e8c\u8005\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u4e24\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684\u6587\u4ef6\u5bf9\u8c61&#xff0c;\u53ea\u662f<span style=\"color:#fe2c24\">\u4f9d\u9760\u300cIP &#043; \u7aef\u53e3\u300d\u7684\u5bfb\u5740\u89c4\u5219\u901a\u8fc7\u7f51\u7edc\u4ea4\u6362\u6570\u636e&#xff0c;\u4e0d\u5b58\u5728\u4efb\u4f55\u5171\u7528\u5173\u7cfb\u3002<\/span><\/p>\n<p>3. \u5e76\u4e14\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5173\u952e\u533a\u522b&#xff1a;<span style=\"color:#fe2c24\">\u5ba2\u6237\u7aef\u521b\u5efa socket \u540e&#xff0c;\u4e0d\u6267\u884c bind \u7ed1\u5b9a\u3002<\/span><span style=\"color:#494949\">\u4e3a\u4ec0\u4e48?<\/span><\/p>\n<p>\u56e0\u4e3a bind \u7684\u672c\u8d28\u76f8\u5f53\u4e8e\u4e3b\u52a8\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u6211\u8981\u5360\u7528\u8fd9\u4e2a IP &#043; \u7aef\u53e3&#xff0c;\u522b\u4eba\u90fd\u4e0d\u80fd\u7528\u3002\u6240\u4ee5\u670d\u52a1\u7aef\u5fc5\u987b bind&#xff1a;\u56e0\u4e3a\u5b83\u8981\u5f53\u56fa\u5b9a\u670d\u52a1\u70b9&#xff0c;\u5fc5\u987b\u63d0\u524d\u5360\u597d\u4e00\u4e2a\u56fa\u5b9a\u7aef\u53e3&#xff0c;\u8ba9\u6240\u6709\u4eba\u90fd\u80fd\u627e\u5230\u5b83&#xff1b;\u800c\u5ba2\u6237\u7aef\u5219\u4e0d\u9700\u8981\u7528 bind&#xff0c;\u56e0\u4e3a\u5ba2\u6237\u7aef\u53ea\u662f\u4e34\u65f6\u8bbf\u95ee\u8005&#xff0c;\u4e0d\u9700\u8981\u56fa\u5b9a\u8eab\u4efd&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5728\u5e95\u5c42\u968f\u673a\u6311\u4e00\u4e2a\u968f\u673a\u7684\u7a7a\u95f2\u7aef\u53e3&#xff1b;\u81ea\u52a8\u628a\u5ba2\u6237\u7aef\u672c\u673a IP &#043; \u8fd9\u4e2a\u968f\u673a\u7aef\u53e3\u7ed1\u5b9a\u7ed9\u5ba2\u6237\u7aef\u7684 socket&#xff1b;\u540e\u7eed\u6240\u6709\u6536\u53d1\u6570\u636e&#xff0c;\u90fd\u7528\u8fd9\u4e2a\u81ea\u52a8\u5206\u914d\u7684\u4e34\u65f6\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p>\u8fd9\u5c31\u76f8\u5f53\u4e8e\u6211\u4eec\u5237\u6296\u97f3&#xff0c;\u6296\u97f3\u670d\u52a1\u5668&#xff08;\u670d\u52a1\u7aef&#xff09;\u5fc5\u987b bind\u3002\u56e0\u4e3a\u6296\u97f3\u8981\u7ed9\u5168\u56fd\u51e0\u4ebf\u7528\u6237\u63d0\u4f9b\u670d\u52a1&#xff0c;\u5b83\u5fc5\u987b\u56fa\u5b9a\u4e00\u4e2a\u4e0d\u53d8 IP &#043; \u7aef\u53e3\u3002\u7528\u6765\u63a5\u6536\u6240\u6709\u7528\u6237\u7684\u89c6\u9891\u8bf7\u6c42\u3002\u800c\u6211\u4eec\u7684\u624b\u673a(\u5ba2\u6237\u7aef)\u4e0d\u7528 bind\u3002\u5f53\u6211\u4eec\u5237\u6296\u97f3\u65f6&#xff0c;\u53ea\u9700\u8981\u6253\u5f00 APP&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u7ed9\u624b\u673a\u5206\u914d\u4e00\u4e2a\u4e34\u65f6\u7aef\u53e3&#xff0c;\u7528\u5b8c\u5c31\u56de\u6536\u3002<\/p>\n<h3 id=\"4.%20%E6%9E%84%E5%BB%BA%E6%9C%8D%E5%8A%A1%E7%AB%AF%20socket%20%E7%9B%AE%E6%A0%87%E5%9C%B0%E5%9D%80\">4. \u6784\u5efa\u670d\u52a1\u7aef socket \u76ee\u6807\u5730\u5740<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"158\" src=\"2026-10-28ogyh4f522qk.png\" width=\"1258\" \/> struct sockaddr_in server \u7528\u6765<span style=\"color:#fe2c24\">\u5b58\u653e\u670d\u52a1\u7aef\u7684 IP &#043; \u7aef\u53e3<\/span>&#xff0c;\u4f5c\u4e3a\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u7684\u76ee\u6807\u5730\u5740&#xff1b;\u670d\u52a1\u7aef\u6784\u5efa\u7684\u662f\u81ea\u5df1\u7684\u76d1\u542c\u5730\u5740&#xff1b;\u5ba2\u6237\u7aef\u6784\u5efa\u7684\u662f\u5bf9\u65b9\u7684\u76ee\u6807\u5730\u5740&#xff1b;<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"21\" src=\"2026-10-284clweheifgr.png\" width=\"475\" \/> \u6e05\u7a7a\u5730\u5740\u7ed3\u6784\u4f53\u810f\u6570\u636e&#xff0c;\u548c\u670d\u52a1\u7aef\u89c4\u8303\u4e00\u81f4&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"21\" src=\"2026-10-28oj2hvlhko5u.png\" width=\"675\" \/> \u4fdd\u6301 IPv4 \u534f\u8bae\u4e00\u81f4&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"17\" src=\"2026-10-28vqyjbrwaa5g.png\" width=\"601\" \/> \u7aef\u53e3\u4e3b\u673a\u5e8f\u8f6c\u7f51\u7edc\u5e8f&#xff0c;\u5fc5\u987b\u548c\u670d\u52a1\u7aef\u7aef\u53e3\u5b8c\u5168\u4e00\u81f4&#xff1b;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"29\" src=\"2026-10-28yz21aktljhn.png\" width=\"729\" \/> \u5c06\u5341\u8fdb\u5236\u7684 IP&#xff08;\u5982 127.0.0.1)&#xff0c;\u8f6c\u4e3a 32 \u4f4d\u7f51\u7edc\u4e8c\u8fdb\u5236 IP&#xff0c;\u7528\u4e8e\u7f51\u7edc\u4f20\u8f93\u5bfb\u5740&#xff1b;<\/p>\n<\/p>\n<p>\u5ba2\u6237\u7aef\u540e\u7eed\u6bcf\u6b21 sendto \u53d1\u9001\u6570\u636e&#xff0c;\u90fd\u4f1a\u6307\u5b9a\u8fd9\u4e2a server \u7ed3\u6784\u4f53&#xff0c;\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u8fd9\u6761 UDP \u6570\u636e\u5305&#xff0c;\u8981\u53d1\u5f80\u8fd9\u4e2a IP &#043; \u7aef\u53e3\u7684\u670d\u52a1\u7aef\u3002<\/p>\n<h3 id=\"5.%20%E5%BE%AA%E7%8E%AF%E4%BA%A4%E4%BA%92%E9%80%BB%E8%BE%91\">5. \u5faa\u73af\u4ea4\u4e92\u903b\u8f91<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"762\" src=\"2026-10-28yswzduivwc5.png\" width=\"1347\" \/><\/p>\n<p>(1). \u83b7\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"186\" src=\"2026-10-28q3yrwf3nhk4.png\" width=\"1161\" \/> \u6bcf\u6b21\u5faa\u73af\u4e3b\u52a8\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32&#xff0c;\u4f5c\u4e3a\u8981\u53d1\u9001\u7ed9\u670d\u52a1\u7aef\u7684\u6570\u636e\u3002<\/p>\n<p>(2). sendto \u4e3b\u52a8\u53d1\u9001\u6570\u636e<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"64\" src=\"2026-10-281jsi0ynnv45.png\" width=\"1440\" \/><\/p>\n<ul>\n<li>sockfd \u662f\u5ba2\u6237\u7aef\u81ea\u5df1\u7684 socket fd&#xff1b;<\/li>\n<li>message.c_str() \u662f\u8981\u53d1\u9001\u7684\u7528\u6237\u6570\u636e&#xff1b;<\/li>\n<li>(struct sockaddr*)&amp;server \u662f\u76ee\u6807\u670d\u52a1\u7aef\u5730\u5740&#xff08;\u6211\u4eec\u524d\u9762\u63d0\u524d\u6784\u5efa\u597d\u7684 IP &#043; \u7aef\u53e3&#xff09;&#xff1b;<\/li>\n<\/ul>\n<p>\u5ba2\u6237\u7aef\u6ca1\u6709\u624b\u52a8\u8c03\u7528 bind()&#xff0c;<span style=\"color:#fe2c24\">\u9996\u6b21\u8c03\u7528 sendto() \u65f6&#xff0c;Linux \u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u5ba2\u6237\u7aef socket \u5206\u914d\u4e00\u4e2a\u968f\u673a\u4e34\u65f6\u7aef\u53e3&#xff1b;<\/span>\u5b8c\u6210\u5ba2\u6237\u7aef fd\u3001\u968f\u673a\u7aef\u53e3\u3001\u672c\u673a IP \u7684\u5185\u6838\u7ed1\u5b9a&#xff1b;\u540e\u7eed\u6240\u6709\u6536\u53d1&#xff0c;\u90fd\u7528\u8fd9\u4e2a\u81ea\u52a8\u5206\u914d\u7684\u968f\u673a\u7aef\u53e3\u3002\u670d\u52a1\u7aef\u5fc5\u987b\u624b\u52a8 bind \u56fa\u5b9a\u7aef\u53e3&#xff0c;\u5ba2\u6237\u7aef\u4e0d\u9700\u8981&#xff0c;\u8fd9\u662f UDP \u5ba2\u6237\u7aef\u5f00\u53d1\u7684\u6838\u5fc3\u89c4\u8303\u3002<\/p>\n<p>(3). recvfrom \u63a5\u6536\u670d\u52a1\u7aef\u56de\u58f0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"203\" src=\"2026-10-28nw3hjnxrvtn.png\" width=\"1268\" \/> \u8fd9\u91cc\u7684<span style=\"color:#fe2c24\"> temp \u7ed3\u6784\u4f53&#xff0c;\u7528\u6765\u63a5\u6536\u670d\u52a1\u7aef\u7684 IP &#043; \u7aef\u53e3&#xff1b;<\/span>\u5ba2\u6237\u7aef\u7684 recvfrom \u662f\u975e\u963b\u585e\u7b49\u5f85&#xff1a;\u53ea\u6709\u53d1\u9001\u5b8c\u6570\u636e\u540e&#xff0c;\u624d\u4f1a\u7b49\u5f85\u670d\u52a1\u7aef\u7684\u56de\u58f0&#xff1b;\u670d\u52a1\u7aef\u7684recvfrom\u662f\u6c38\u4e45\u963b\u585e\u7b49\u5f85&#xff0c;\u968f\u65f6\u63a5\u6536\u6240\u6709\u5ba2\u6237\u7aef\u6570\u636e&#xff1b;\u6570\u636e\u63a5\u6536\u903b\u8f91\u548c\u670d\u52a1\u7aef\u5b8c\u5168\u4e00\u81f4&#xff1a;\u4ece\u5ba2\u6237\u7aef socket \u7684\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u62f7\u8d1d\u6570\u636e\u5230\u7528\u6237\u6001inbuffer\u3002<\/p>\n<p>(4). \u6253\u5370\u56de\u58f0\u7ed3\u679c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"139\" src=\"2026-10-28v5rb2qnqibd.png\" width=\"828\" \/> \u5c06\u670d\u52a1\u7aef\u8fd4\u56de\u7684\u56de\u58f0\u6570\u636e\u6253\u5370\u5230\u7ec8\u7aef&#xff0c;\u5b8c\u6210\u4e00\u6b21 \u201c\u7528\u6237\u8f93\u5165\u2192\u5ba2\u6237\u7aef\u53d1\u9001\u2192\u670d\u52a1\u7aef\u56de\u58f0\u2192\u5ba2\u6237\u7aef\u8f93\u51fa\u201d \u7684\u5b8c\u6574\u4ea4\u4e92\u95ed\u73af\u3002<\/p>\n<h2 id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%20%3A\">\u8fd0\u884c\u7ed3\u679c :<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u76f4\u63a5\u5c55\u793a\u8fd0\u884c\u7ed3\u679c&#xff0c;\u5c55\u793a UDP \u56de\u58f0\u670d\u52a1\u5668\u5728\u4e09\u79cd\u4e0d\u540c\u8bbf\u95ee\u573a\u666f\u4e0b\u7684\u5b9e\u9645\u8868\u73b0<\/p>\n<h3 id=\"%E6%9C%AC%E5%9C%B0%E5%9B%9E%E7%8E%AF%E5%9C%B0%E5%9D%80%E9%80%9A%E4%BF%A1%20(127.0.0.1)\">\u672c\u5730\u56de\u73af\u5730\u5740\u901a\u4fe1 (127.0.0.1)<\/h3>\n<p>\u8fd0\u884c .\/client_udp 127.0.0.1 8080 \u547d\u4ee4\u540e&#xff0c;\u5728\u5ba2\u6237\u7aef&#xff1a;\u8f93\u5165 &#034;\u4e2d\u5348\u597d&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"96\" src=\"2026-10-28fy1kbrw0vbk.png\" width=\"1077\" \/><\/p>\n<p>\u56de\u8f66\u540e\u7acb\u523b\u6536\u5230\u670d\u52a1\u7aef\u56de\u590d &#034;server echo# \u4e2d\u5348\u597d&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"75\" src=\"2026-10-2802bfvfjo1gs.png\" width=\"1152\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"144\" src=\"2026-10-285shhvjdf0ks.png\" width=\"1227\" \/><\/p>\n<p>\u670d\u52a1\u7aef\u65e5\u5fd7\u6253\u5370\u51fa\u5ba2\u6237\u7aef\u6765\u6e90\u4e3a [127.0.0.1:43677]\u3002\u540c\u65f6\u5c06 &#034;\u4e2d\u5348\u597d&#034; \u8fd4\u56de\u6253\u5370&#xff0c;\u6ce8\u610f\u8fd9\u91cc\u7684\u7aef\u53e3\u53f7 43677 \u662f\u5ba2\u6237\u7aef\u81ea\u52a8\u5206\u914d\u7684\u4e34\u65f6\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\">\u8fd9\u662f\u672c\u673a\u5185\u90e8\u901a\u4fe1\u3002\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u90fd\u5728\u540c\u4e00\u53f0\u4e3b\u673a\u4e0a\u3002\u800c 127.0.0.1 \u662f\u56de\u73af\u5730\u5740&#xff0c;\u6570\u636e\u4e0d\u7ecf\u8fc7\u771f\u5b9e\u7f51\u5361&#xff0c;\u76f4\u63a5\u5728\u5185\u6838\u91cc\u7ed5\u4e00\u5708\u5c31\u56de\u5230\u4e86\u5ba2\u6237\u7aef\u3002<\/span><\/p>\n<h3 id=\"%E5%86%85%E7%BD%91%20IP%20%E9%80%9A%E4%BF%A1%20(10.0.16.7)\">\u5185\u7f51 IP \u901a\u4fe1 (10.0.16.7)<\/h3>\n<p>\u8fd0\u884c .\/client_udp 10.0.16.7 8080 \u540e\u5728\u5ba2\u6237\u7aef&#xff1a;\u8f93\u5165 &#034;\u5403\u4e86\u5417&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"69\" src=\"2026-10-28sv4pg2hh4tr.png\" width=\"963\" \/><\/p>\n<p>\u56de\u8f66\u540e\u7acb\u523b\u6536\u5230\u670d\u52a1\u7aef\u56de\u590d &#034;server echo# \u5403\u4e86\u5417&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"72\" src=\"2026-10-280gmdyh0ai42.png\" width=\"1099\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"138\" src=\"2026-10-283w1ljnoyokb.png\" width=\"1203\" \/><\/p>\n<p>\u670d\u52a1\u7aef\u65e5\u5fd7\u6253\u5370\u51fa\u5ba2\u6237\u7aef\u6765\u6e90\u4e3a [10.0.16.7:33657]\u3002\u8fd9\u91cc\u7684\u00a0IP \u53d8\u6210\u4e86\u5185\u7f51 IP\u3002<\/p>\n<\/p>\n<p>\u8fd9\u662f\u540c\u4e00\u5c40\u57df\u7f51\u5185\u7684\u673a\u5668\u901a\u4fe1\u3002\u5ba2\u6237\u7aef\u660e\u786e\u6307\u5b9a\u4e86\u670d\u52a1\u7aef\u7684\u5185\u7f51 IP\u3002\u6570\u636e\u7ecf\u8fc7\u4e86\u771f\u5b9e\u7684\u7f51\u5361\u534f\u8bae\u6808\u3002\u5ba2\u6237\u7aef\u62ff\u5230\u7684\u6e90 IP \u662f\u5b83\u53d1\u51fa\u6570\u636e\u5305\u65f6\u7684\u672c\u673a\u7f51\u5361 IP\u3002\u8fd9\u8bc1\u660e\u6211\u4eec\u7684\u670d\u52a1\u7aef\u4ee3\u7801\u4e0d\u662f\u6b7b\u7ed1\u67d0\u4e00\u4e2a IP&#xff0c;\u800c\u662f\u76d1\u542c\u4e86\u6240\u6709\u7f51\u5361&#xff08;\u5f52\u529f\u4e8e INADDR_ANY&#xff09;&#xff0c;\u6240\u4ee5\u65e0\u8bba\u5ba2\u6237\u7aef\u4ece\u54ea\u4e2a\u7f51\u5361&#xff08;\u5185\u7f51\u3001\u5916\u7f51&#xff09;\u53d1\u6765\u6570\u636e&#xff0c;\u670d\u52a1\u7aef\u90fd\u80fd\u6536\u5230\u3002<\/p>\n<h3 id=\"%E5%85%AC%E7%BD%91%20IP%20%E9%80%9A%E4%BF%A1%20(124.222.191.171)\">\u516c\u7f51 IP \u901a\u4fe1 (124.222.191.171)<\/h3>\n<p>\u8fd0\u884c .\/client_udp 124.222.191.171 8080\u00a0\u540e\u5ba2\u6237\u7aef\u8f93\u5165 &#034;\u4eca\u5929\u8fc7\u5f97\u600e\u4e48\u6837&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"59\" src=\"2026-10-28yx34214irws.png\" width=\"1091\" \/><\/p>\n<p>\u56de\u8f66\u540e\u7acb\u523b\u6536\u5230\u670d\u52a1\u7aef\u56de\u590d &#034;server echo# \u4eca\u5929\u8fc7\u5f97\u600e\u4e48\u6837&#034; :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"79\" src=\"2026-10-28wxcgap1w1rv.png\" width=\"1025\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"136\" src=\"2026-10-28tpfhtt4dbpa.png\" width=\"1260\" \/><\/p>\n<p>\u670d\u52a1\u7aef\u65e5\u5fd7\u6253\u5370\u51fa\u5ba2\u6237\u7aef\u6765\u6e90\u4e3a [124.222.191.171:44555]\u3002\u6e90 IP \u53d8\u6210\u4e86\u516c\u7f51 IP\u3002<\/p>\n<\/p>\n<p>\u8fd9\u662f\u8de8\u7f51\u7edc\u3001\u8de8\u516c\u7f51\u7684\u901a\u4fe1\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u516c\u7f51 IP \u627e\u5230\u4e86\u670d\u52a1\u7aef\u3002<span style=\"color:#fe2c24\">\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u5185\u6838\u81ea\u52a8\u5206\u914d\u4e00\u4e2a\u4e34\u65f6\u516c\u7f51\u7aef\u53e3&#xff08;\u5982 44555&#xff09;<\/span>\u3002\u6570\u636e\u901a\u8fc7\u7f51\u7edc\u8def\u7531&#xff0c;\u5230\u8fbe\u4e86\u62e5\u6709\u8be5\u516c\u7f51 IP \u7684\u673a\u5668\u3002\u670d\u52a1\u7aef\u6536\u5230\u6570\u636e&#xff0c;\u5e76\u901a\u8fc7\u6570\u636e\u5305\u91cc\u7684\u516c\u7f51 IP &#043; \u4e34\u65f6\u7aef\u53e3&#xff0c;\u786e\u8ba4\u4e86\u5ba2\u6237\u7aef\u8eab\u4efd&#xff0c;\u4ece\u800c\u80fd\u51c6\u786e\u56de\u4fe1\u3002\u8fd9\u4e5f\u662f\u6700\u63a5\u8fd1\u771f\u5b9e\u751f\u4ea7\u73af\u5883\u7684\u6548\u679c\u3002\u670d\u52a1\u7aef\u4ee3\u7801\u652f\u6301\u516c\u7f51\u8bbf\u95ee&#xff0c;\u53ea\u8981\u516c\u7f51 IP \u6620\u5c04\u6b63\u786e&#xff0c;\u670d\u52a1\u7aef\u5c31\u80fd\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n<h2 id=\"%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%20%3A\">\u5b8c\u6574\u4ee3\u7801 :<\/h2>\n<p>EchoServer.hpp<\/p>\n<p>#ifndef __ECHOSERVER_HPP<br \/>\n#define __ECHOSERVER_HPP<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;strings.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;Logger.hpp&#034;<\/p>\n<p>using namespace NS_LOG_MODULE;<br \/>\nconst static int default_fd &#061; -1;<br \/>\nconst static int default_port &#061; 8888;<br \/>\nenum<br \/>\n{<br \/>\n    SUCCESS &#061; 0,<br \/>\n    USAGE_ERR,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR,<br \/>\n};<\/p>\n<p>class UdpServer<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/ UdpServer(const std::string &amp;ip, uint16_t port &#061; default_port)<br \/>\n    UdpServer(uint16_t port &#061; default_port)<br \/>\n        : _port(port),<br \/>\n          _sockfd(default_fd)<br \/>\n    {<br \/>\n    }<br \/>\n    ~UdpServer()<br \/>\n    {<br \/>\n        close(_sockfd);<br \/>\n    }<br \/>\n    void Init()<br \/>\n    {<br \/>\n        \/\/ \u7b2c\u4e00\u6b65: \u521b\u5efasocket, \u672c\u8d28: \u6253\u5f00\u7f51\u5361 &#8212; \u7cfb\u7edf\u7279\u6027<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;create socket error&#034;;<br \/>\n            exit(SOCKET_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;create socket success, sockfd: &#034; &lt;&lt; _sockfd;<\/p>\n<p>        \/\/ \u7b2c\u4e8c\u6b65: \u586b\u5145\u7f51\u7edc\u4fe1\u606f, \u6709\u6ca1\u6709IP\u548c\u7aef\u53e3\u4fe1\u606f\u8bbe\u7f6e\u5230\u5185\u6838\u4e2d&#xff1f;&#xff1f;\u8bbe\u7f6e\u5230\u4f60\u521a\u521a\u6253\u5f00\u7684\u7f51\u7edcsocket\u5bf9\u5e94\u7684\u6587\u4ef6\u5185\u90e8&#xff1f;<br \/>\n        struct sockaddr_in local; \/\/ struct sockaddr_in \u6570\u636e\u7c7b\u578b&#xff0c;local \u7528\u6237\u6808\u4e0a\u7684&#xff01;&#xff01;&#xff01;&#xff0c;\u5e76\u6ca1\u6709\u8bbe\u7f6e\u5230\u5185\u6838<br \/>\n        bzero(&amp;local, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_port);                  \/\/ h-&gt;n<\/p>\n<p>        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u6700\u4f73\u5b9e\u8df5&#xff1a;\u4efb\u610fIP\u5730\u5740bind<\/p>\n<p>        \/\/ \u7b2c\u4e09\u6b65&#xff1a;bind socket \u4fe1\u606f<br \/>\n        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 socket error&#034;;<br \/>\n            exit(BIND_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;bind socket success&#034;&lt;&lt; &#034;, port: &#034; &lt;&lt; _port;<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        \/\/ \u4f20\u9012\u7684\u662f\u5b57\u7b26\u4e32,echo server<br \/>\n        char inbuffer[1024];<br \/>\n        while (true)<br \/>\n        {<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<br \/>\n            \/\/ 1. \u7528\u6237\u53d1\u6765\u7684\u6570\u636e<br \/>\n            \/\/ 2. \u7528\u6237\u7684socket\u4fe1\u606f<br \/>\n            ssize_t n &#061; recvfrom(_sockfd, inbuffer, sizeof(inbuffer) &#8211; 1, 0, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                \/\/ 1. \u7f51\u7edc\u5b57\u8282\u5e8f \u2192 \u4e3b\u673a\u5b57\u8282\u5e8f&#xff0c;\u89e3\u6790\u5ba2\u6237\u7aefIP\u548c\u7aef\u53e3<br \/>\n                uint16_t client_port &#061; ntohs(peer.sin_port);<br \/>\n                std::string client_ip &#061; inet_ntoa(peer.sin_addr); \/\/ 4\u5b57\u8282IP-&gt; ntoh -&gt; \u5b57\u7b26\u4e32<br \/>\n                std::string client_address &#061; &#034;[&#034; &#043; client_ip &#043; &#034;:&#034; &#043; std::to_string(client_port) &#043; &#034;]# &#034;;<br \/>\n                \/\/ 2. \u7ed9\u63a5\u6536\u6570\u636e\u52a0\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26<br \/>\n                inbuffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n                \/\/ 3. \u65e5\u5fd7\u6253\u5370 &#043; \u5c01\u88c5\u670d\u52a1\u7aef\u8981\u539f\u8def\u8fd4\u56de\u7684\u6570\u636e<br \/>\n                LOG(LogLevel::DEBUG) &lt;&lt; client_address &lt;&lt; inbuffer;<br \/>\n                std::string echo_string &#061; &#034;server echo# &#034;;<br \/>\n                echo_string &#043;&#061; inbuffer;<br \/>\n                \/\/ h to n<br \/>\n                sendto(_sockfd, echo_string.c_str(), echo_string.size(), 0, (struct sockaddr *)&amp;peer, len);<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                LOG(LogLevel::ERROR) &lt;&lt; &#034;recvfrom error&#034;;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _sockfd;<br \/>\n    uint16_t _port;  \/\/ \u7528\u6237\u8bbe\u7f6e\u597d\u7684&#xff0c;server port\u5fc5\u987b\u662f\u56fa\u5b9a\u7684&#xff01;<br \/>\n    \/\/ std::string _ip; \/\/ &#034;192.168.2.2&#034;<br \/>\n};<\/p>\n<p>#endif <\/p>\n<p>EchoServerMain.cc<\/p>\n<p>#include &#034;EchoServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>static void Usage(const std::string &amp;process)<br \/>\n{<br \/>\n    std::cerr &lt;&lt; &#034;Usage:\\\\n\\\\t&#034;;<br \/>\n    std::cerr &lt;&lt; process &lt;&lt; &#034; local_port&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/server_udp port<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 2)<br \/>\n    {<br \/>\n        Usage(argv[0]);<br \/>\n        exit(USAGE_ERR);<br \/>\n    }<br \/>\n    ENABLE_CONSOLE_LOG_STRATEGY();<\/p>\n<p>    uint16_t server_port &#061; std::stoi(argv[1]);<\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(server_port);<br \/>\n    usvr-&gt;Init();<br \/>\n    usvr-&gt;Start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>EchoClient.cc<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdlib&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>static void Usage(const std::string &amp;proc)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;Usage:\\\\n\\\\t&#034;;<br \/>\n    std::cout &lt;&lt; proc &lt;&lt; &#034; server_ip server_port&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ \u6211\u600e\u4e48\u77e5\u9053server\u5bf9\u65b9\u7684IP\u548c\u7aef\u53e3\u554a&#xff0c; \u7c7b\u4f3cIP&#043;Port \u662f\u88ab\u5185\u7f6e\u5230client\u7684&#xff01;&#xff01;&#xff01;<br \/>\n\/\/ .\/client_udp server_ip server_port<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 3)<br \/>\n    {<br \/>\n        Usage(argv[0]);<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    std::string server_ip &#061; argv[1];<br \/>\n    uint16_t server_port &#061; std::stoi(argv[2]);<\/p>\n<p>    \/\/ 1. \u521b\u5efasocket<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;socket error&#034; &lt;&lt; std::endl;<br \/>\n        exit(2);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u6784\u5efaserver\u7aefsocket\u4fe1\u606f<br \/>\n    \/\/ client \u9700\u8981\u6709\u81ea\u5df1\u7684IP\u548cPort\u4fe1\u606f\u5417? \u9700\u8981\u7684&#xff01;<br \/>\n    \/\/ \u9700\u8981\u663e\u793a\u7684bind\u81ea\u5df1\u7684ip\u548c\u7aef\u53e3\u5417&#xff1f;\u4e0d\u8981\u663e\u793abind&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/ 1. \u4e3a\u4ec0\u4e48\u4e0d\u8ba9client\u663e\u793abind&#xff1f;client bind port \u51fa\u73b0\u51b2\u7a81&#xff01;client port\u53ea\u9700\u8981\u5177\u6709\u552f\u4e00\u6027\u5373\u53ef&#xff0c;\u5177\u4f53\u662f\u51e0&#xff0c;\u4e0d\u91cd\u8981\u3002<br \/>\n    \/\/ 2. \u5982\u4f55\u8bbe\u7f6e\u81ea\u5df1\u7684IP\u548c\u7aef\u53e3\u5462&#xff1f;client \u4e00\u822c\u4f1a\u91c7\u7528\u968f\u673a\u7aef\u53e3\u7684\u65b9\u5f0f&#xff01;\u7531OS\u81ea\u4e3b\u9009\u62e9&#xff01;<br \/>\n    \/\/ udp client \u9996\u6b21\u53d1\u9001\u6570\u636e\u7684\u65f6\u5019&#xff0c;OS\u5e95\u5c42\u4f1a\u9690\u5f0f\u81ea\u52a8\u5e2e\u4f60\u8fdb\u884c\u83b7\u53d6\u968f\u673a\u7aef\u53e3&#xff0c;\u7136\u540ebind &#043; Port &#043; IP<br \/>\n    struct sockaddr_in server;<br \/>\n    memset(&amp;server, 0, sizeof(0));<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_port &#061; htons(server_port);<br \/>\n    server.sin_addr.s_addr &#061; inet_addr(server_ip.c_str());<\/p>\n<p>    while(true)<br \/>\n    {<br \/>\n        std::string message;<br \/>\n        \/\/ 1. \u83b7\u53d6\u7528\u6237\u8f93\u5165<br \/>\n        std::cout &lt;&lt; &#034;Please Enter# &#034;;<br \/>\n        std::getline(std::cin, message);<\/p>\n<p>        \/\/ 2. clinet \u53d1\u9001\u6570\u636e\u7ed9 server&#xff0c;\u9996\u6b21\u53d1\u9001\u5373\u81ea\u52a8bind<br \/>\n        ssize_t n &#061; sendto(sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&amp;server, sizeof(server));<br \/>\n        if(n &gt; 0)<br \/>\n        {<br \/>\n            \/\/ recvfrom<br \/>\n            char inbuffer[1024] &#061; {0};<br \/>\n            struct sockaddr_in temp;<br \/>\n            socklen_t len &#061; sizeof(temp);<br \/>\n            ssize_t m &#061; recvfrom(sockfd, inbuffer, sizeof(inbuffer)-1, 0, (struct sockaddr*)&amp;temp, &amp;len);<br \/>\n            if(m &gt; 0)<br \/>\n            {<br \/>\n                inbuffer[m] &#061; 0;<br \/>\n                std::cout &lt;&lt; inbuffer &lt;&lt; std::endl;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h2 id=\"%E6%A8%A1%E5%9E%8B%E5%9B%BE%3A\">\u6a21\u578b\u56fe:<\/h2>\n<h2 id=\"%E2%80%8B%E7%BC%96%E8%BE%91\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"4206\" src=\"2026-10-28smo5t0wnon1.png\" width=\"5310\" \/><\/h2>\n<h2 id=\"%E2%80%8B%E7%BC%96%E8%BE%91\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"820\" src=\"2026-10-28inir2xm3mbo.png\" width=\"1470\" \/><\/h2>\n<p>\u7ed3\u6784\u4f53\u533a\u522b:<\/p>\n<h2 id=\"%E2%80%8B%E7%BC%96%E8%BE%91\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"675\" src=\"2026-10-28mnrs4s1tjfz.png\" width=\"1164\" \/><\/h2>\n<h2 id=\"%E6%80%BB%E7%BB%93%3A\" style=\"background-color:transparent\">\u603b\u7ed3:<\/h2>\n<p>1. \u670d\u52a1\u7aef\u4e4b\u6240\u4ee5\u5fc5\u987b\u7ed1\u5b9a 8080 \u7aef\u53e3&#xff0c;\u662f\u56e0\u4e3a\u670d\u52a1\u7aef\u4f5c\u4e3a\u88ab\u8bbf\u95ee\u65b9&#xff0c;\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u56fa\u5b9a\u7684\u7f51\u7edc\u5165\u53e3&#xff0c;\u8fd9\u6837\u5ba2\u6237\u7aef\u624d\u80fd\u627e\u5230\u5b83\u3002<span style=\"color:#fe2c24\">\u56e0\u6b64\u5ba2\u6237\u7aef\u5728\u8bbf\u95ee\u65f6&#xff0c;\u5fc5\u987b\u6307\u5b9a 8080 \u7aef\u53e3&#xff0c;\u5c31\u662f\u4e3a\u4e86\u901a\u8fc7\u8fd9\u4e2a\u7aef\u53e3\u7cbe\u51c6\u5b9a\u4f4d\u5230\u670d\u52a1\u7aef\u7a0b\u5e8f\u3002 \u00a0<\/span> 2. \u800c\u5ba2\u6237\u7aef\u5728\u7b2c\u4e00\u6b21\u53d1\u9001\u6570\u636e\u4e4b\u524d&#xff0c;\u5e76\u6ca1\u6709\u81ea\u5df1\u7684\u7aef\u53e3\u3002\u5f53\u5ba2\u6237\u7aef\u7b2c\u4e00\u6b21\u8c03\u7528 sendto\u00a0\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u5ba2\u6237\u7aef\u5206\u914d\u4e00\u4e2a\u968f\u673a\u7684\u4e34\u65f6\u7aef\u53e3\u3002\u8fd9\u4e2a\u4e34\u65f6\u7aef\u53e3\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u8bb0\u5f55\u5728\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u4e2d&#xff0c;\u5e76\u968f\u7740\u6570\u636e\u5305\u4e00\u8d77\u53d1\u9001\u5230\u670d\u52a1\u7aef\u3002 \u00a0 3. \u670d\u52a1\u7aef\u6536\u5230\u5ba2\u6237\u7aef\u6570\u636e\u540e&#xff0c;\u5c31\u80fd\u4ece\u6570\u636e\u5305\u4e2d\u89e3\u6790\u51fa\u5ba2\u6237\u7aef\u7684 IP &#043; \u4e34\u65f6\u7aef\u53e3&#xff0c;\u5e76\u5728\u56de\u590d\u65f6\u628a\u8fd9\u4e2a\u5730\u5740\u586b\u8fdb\u00a0sendto\u00a0\u7684\u76ee\u6807\u5730\u5740\u91cc\u3002\u8fd9\u6837\u670d\u52a1\u7aef\u5c31\u80fd\u51c6\u786e\u628a\u6570\u636e\u56de\u53d1\u7ed9\u5ba2\u6237\u7aef&#xff0c;\u5b9e\u73b0\u53cc\u65b9\u7684\u53cc\u5411\u901a\u4fe1\u3002 \u00a0 4. \u800c IP \u7684\u4f5c\u7528&#xff0c;\u5c31\u662f\u533a\u5206\u4e0d\u540c\u7684\u7f51\u7edc\u8def\u5f84\u3002\u6bd4\u5982\u901a\u8fc7\u672c\u5730\u56de\u73af \u00a0127.0.0.1\u00a0\u3001\u5185\u7f51 IP\u3001\u6216\u8005\u516c\u7f51 IP \u8fdb\u884c\u8bbf\u95ee\u3002\u4e0d\u7ba1\u662f\u54ea\u4e00\u79cd IP&#xff0c;\u6700\u7ec8\u90fd\u662f\u4e3a\u4e86\u627e\u5230\u90a3\u53f0\u8fd0\u884c\u7740\u670d\u52a1\u7aef\u7a0b\u5e8f\u7684\u673a\u5668&#xff0c;\u518d\u901a\u8fc7 8080 \u7aef\u53e3\u627e\u5230\u5177\u4f53\u7684\u670d\u52a1\u7aef\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p>5. \u6211\u4eec\u73b0\u5728\u770b\u5230\u7684\u5168\u662f&#xff1a;socket\u3001sendto\u3001recvfrom\u3001\u5185\u6838\u7f13\u51b2\u533a\u3001\u7ed3\u6784\u4f53\u62f7\u8d1d&#xff0c;\u5168\u662f\u672c\u673a\u5185\u90e8\u7684\u7cfb\u7edf\u8c03\u7528\u903b\u8f91&#xff0c;\u770b\u4e0d\u5230\u7f51\u7ebf\u3001\u7f51\u5361\u3001\u8def\u7531\u3001IP \u7f51\u7edc&#xff0c;\u6240\u4ee5\u6211\u4eec\u4f1a\u89c9\u5f97\u201c\u8fd9\u4e0d\u5c31\u662f\u4e24\u4e2a\u7a0b\u5e8f\u901a\u8fc7\u5185\u6838\u7f13\u51b2\u533a\u4e92\u76f8\u62f7\u8d1d\u6570\u636e\u5417&#xff1f;\u548c\u7f51\u7edc\u6709\u5565\u5173\u7cfb&#xff1f;<\/p>\n<p>\u00a0 \u00a0 \u00a0\u00a0\u2705 Socket \u4e0d\u662f \u201c\u5185\u5b58\u7f13\u51b2\u533a\u201d&#xff0c;Socket \u662f\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u4f60\u5c01\u88c5\u597d\u7684\u3001\u64cd\u4f5c\u7f51\u5361\u6536\u53d1\u7f51\u7edc\u6570\u636e\u5305\u7684\u7edf\u4e00\u63a5\u53e3\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u2705 \u6211\u4eec\u8c03\u7528 sendto&#xff0c;\u672c\u8d28\u4e0d\u662f \u201c\u62f7\u8d1d\u5230\u5185\u6838\u7f13\u51b2\u533a\u201d&#xff0c;\u800c\u662f&#xff1a;\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#xff0c;\u628a\u8fd9\u6bb5\u6570\u636e&#xff0c;\u5c01\u88c5\u6210\u6807\u51c6 IP\/UDP \u6570\u636e\u5305&#xff0c;\u901a\u8fc7\u7f51\u5361\u53d1\u51fa\u53bb&#xff0c;\u9001\u5230\u6307\u5b9a IP \u7684\u53e6\u4e00\u53f0\u673a\u5668\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u2705 \u6211\u4eec\u770b\u4e0d\u5230\u7f51\u7ebf\u3001\u770b\u4e0d\u5230\u7f51\u5361&#xff0c;\u662f\u56e0\u4e3a Linux \u5185\u6838\u628a\u7f51\u5361\u9a71\u52a8\u3001\u7f51\u7edc\u534f\u8bae\u3001\u8def\u7531\u8f6c\u53d1\u5168\u90e8\u5c01\u88c5\u5728\u4e86 socket\u3001sendto\u3001recvfrom \u8fd9\u51e0\u4e2a\u7cfb\u7edf\u8c03\u7528\u91cc&#xff0c;\u8c03\u7528\u7684\u4e00\u77ac\u95f4&#xff0c;\u7f51\u7edc\u5c31\u5df2\u7ecf\u53c2\u4e0e\u5de5\u4f5c\u4e86\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u2705 \u771f\u5b9e\u60c5\u51b5\u662f\u6211\u4eec\u5ba2\u6237\u7aef\u8c03\u7528 sendto(&#034;\u4e2d\u5348\u597d&#034;, \u516c\u7f51IP 124.222.191.171, \u7aef\u53e3\u53f7 8080)&#xff0c;Linux \u5e95\u5c42\u4f1a\u5c01\u88c5 UDP \u6570\u636e\u5305 \u2192 \u901a\u8fc7\u7f51\u5361 \u2192 \u7ecf\u8fc7\u8def\u7531\u5668 \u2192 \u8d70\u771f\u5b9e\u7684\u516c\u7f51 \u2192 \u6570\u636e\u5305\u7a7f\u8fc7\u7f51\u7ebf\u5230\u8fbe \u670d\u52a1\u7aef Linux \u4e3b\u673a\u7684\u7269\u7406\u7f51\u5361 \u2192 \u89e3\u6790 IP &#043; \u7aef\u53e3 \u2192 \u5339\u914d 8080 \u7aef\u53e3\u7684\u670d\u52a1\u7aef Socket\u00a0\u2192 \u6570\u636e\u8fdb\u5165\u670d\u52a1\u7aef Socket \u5185\u6838\u7f13\u51b2\u533a \u2192 recvfrom \u8bfb\u5230\u6570\u636e\u670d\u52a1\u7aef sendto \u56de\u53d1 \u2192 \u53cd\u5411\u8d70\u4e00\u904d\u771f\u5b9e\u7f51\u7edc \u2192 \u56de\u5230 \u5ba2\u6237\u7aef Linux \u4e3b\u673a\u7684\u7269\u7406\u7f51\u5361 \u2192 \u5ba2\u6237\u7aef\u5185\u6838\u89e3\u6790\u5e76\u5339\u914d\u5ba2\u6237\u7aef Socket \u2192 \u6570\u636e\u8fdb\u5165\u5ba2\u6237\u7aef Socket \u5185\u6838\u7f13\u51b2\u533a \u2192 recvfrom \u8bfb\u5230\u54cd\u5e94\u6570\u636e<\/p>\n<p id=\"%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9A%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%87%86%E5%A4%87%E5%8F%91%E9%80%81\">\u8c22\u8c22\u5927\u5bb6\u7684\u89c2\u770b!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/p>\n","protected":false},"author":2,"featured_media":118777,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,193,270,78,44],"topic":[],"class_list":["post-118797","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-udp","tag-270","tag-78","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \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\/118797.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/118797.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-27T21:18:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211841-6ae1153193c3a.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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118797.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/118797.html\",\"name\":\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-27T21:18:48+00:00\",\"dateModified\":\"2026-10-27T21:18:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/118797.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/118797.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118797.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto\"}]},{\"@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":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \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\/118797.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/118797.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-27T21:18:48+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261027211841-6ae1153193c3a.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/118797.html","url":"https:\/\/www.wsisp.com\/helps\/118797.html","name":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-27T21:18:48+00:00","dateModified":"2026-10-27T21:18:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/118797.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/118797.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/118797.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e8c\uff09\u4ece 0 \u5230 1 \u5b9e\u73b0 UDP \u56de\u58f0\u670d\u52a1\u5668\uff0crecvfrom\uff0csendto"}]},{"@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\/118797","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=118797"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/118797\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/118777"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=118797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=118797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=118797"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=118797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}