{"id":116821,"date":"2026-10-22T05:56:41","date_gmt":"2026-10-21T21:56:41","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/116821.html"},"modified":"2026-10-22T05:56:41","modified_gmt":"2026-10-21T21:56:41","slug":"%e4%bb%8e%e9%9b%b6%e5%85%a5%e9%97%a8%e8%ae%a1%e7%ae%97%e6%9c%ba%e7%bd%91%e7%bb%9c%e7%b3%bb%e5%88%97%ef%bc%88%e4%ba%94%ef%bc%89%ef%bc%9audp%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/116821.html","title":{"rendered":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1"},"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>\u672c\u6587\u4eceUDP Socket\u7684\u57fa\u7840\u51fa\u53d1&#xff0c;\u4e00\u8def\u5f80\u4e0b\u8d70\u3002<\/p>\n<p>\u5148\u628asocket\u3001sockaddr_in\u8fd9\u4e9b\u6700\u57fa\u7840\u7684\u6982\u5ff5\u8bb2\u6e05\u695a&#xff0c;\u518d\u628a\u5b57\u8282\u5e8f\u3001recvfrom\/sendto\u8fd9\u5bf9\u6536\u53d1\u642d\u6863\u9010\u4e00\u62c6\u5f00\u3002\u63a5\u7740\u770b\u670d\u52a1\u7aef\u600e\u4e48bind&#xff0c;\u5ba2\u6237\u7aef\u4e3a\u4ec0\u4e48\u662f\u9690\u5f0fbind&#xff0c;INADDR_ANY\u53c8\u5230\u5e95\u610f\u5473\u7740\u4ec0\u4e48\u3002\u6700\u540e&#xff0c;\u628aIP\u5730\u5740\u8f6c\u6362\u548c\u7ebf\u7a0b\u5b89\u5168\u7684API\u4e00\u5e76\u6536\u5c3e\u3002<\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81UDP%20%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E4%B8%8E%E6%A0%B8%E5%BF%83%20API%20%E8%AF%A6%E8%A7%A3-toc\" style=\"margin-left:80px\">\u4e00\u3001UDP\u5957\u63a5\u5b57\u7f16\u7a0b\u57fa\u7840&#xff1a;\u4ece\u521b\u5efa\u5957\u63a5\u5b57\u5230\u63a5\u6536\u6570\u636e<\/p>\n<p id=\"1.1%20%E5%A5%97%E6%8E%A5%E5%AD%97%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%B8%B8%E7%94%A8%E5%A4%B4%E6%96%87%E4%BB%B6-toc\" style=\"margin-left:120px\">1.1 Socket\u7f16\u7a0b\u57fa\u7840\u4e0e\u5e38\u7528\u5934\u6587\u4ef6<\/p>\n<p id=\"1.2%20%E5%88%9B%E5%BB%BA%E5%A5%97%E6%8E%A5%E5%AD%97%EF%BC%9Asocket%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:120px\">1.2 \u521b\u5efa\u5957\u63a5\u5b57&#xff1a;socket\u51fd\u6570\u8be6\u89e3<\/p>\n<p id=\"1.2.1%20%E5%8F%82%E6%95%B0%E8%A7%A3%E6%9E%90%E4%B8%8E%E5%B8%B8%E6%95%B0%E5%AE%9A%E4%B9%89-toc\" style=\"margin-left:160px\">1.2.1 \u53c2\u6570\u89e3\u6790\u4e0e\u5e38\u7528\u5e38\u91cf<\/p>\n<p id=\"1.2.2%20%E8%BF%94%E5%9B%9E%E5%80%BC%E4%B8%8E%E2%80%9C%E7%BD%91%E7%BB%9C%E7%9A%86%E6%96%87%E4%BB%B6%E2%80%9D%E5%93%B2%E5%AD%A6-toc\" style=\"margin-left:160px\">1.2.2 \u8fd4\u56de\u503c\u4e0eLinux\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u7684\u8bbe\u8ba1\u601d\u60f3<\/p>\n<p id=\"1.3%20%E5%9C%B0%E5%9D%80%E7%BB%93%E6%9E%84%E4%BD%93%E8%AF%A6%E8%A7%A3%EF%BC%9Asockaddr_in-toc\" style=\"margin-left:120px\">1.3 IPv4\u5730\u5740\u7ed3\u6784&#xff1a;sockaddr_in\u8be6\u89e3<\/p>\n<p id=\"1.3.1%20sockaddr_in%20%E7%BB%93%E6%9E%84%E4%BD%93%E6%88%90%E5%91%98%E5%AE%9A%E4%B9%89-toc\" style=\"margin-left:160px\">1.3.1 sockaddr_in\u7684\u7ed3\u6784\u6210\u5458\u4e0e\u542b\u4e49<\/p>\n<p id=\"1.4%20IP%20%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2%E4%B8%8E%E5%AD%97%E8%8A%82%E5%BA%8F%E5%A4%84%E7%90%86-toc\" style=\"margin-left:120px\">1.4 IP\u5730\u5740\u8f6c\u6362\u4e0e\u5b57\u8282\u5e8f\u5904\u7406<\/p>\n<p id=\"1.4.1%20%E7%82%B9%E5%88%86%E5%8D%81%E8%BF%9B%E5%88%B6%E5%9C%B0%E5%9D%80%E4%B8%8E%E7%BD%91%E7%BB%9C%E5%AD%97%E8%8A%82%E5%BA%8F%E7%9A%84%E8%BD%AC%E6%8D%A2-toc\" style=\"margin-left:120px\">1.4.1 \u70b9\u5206\u5341\u8fdb\u5236\u5730\u5740\u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u8f6c\u6362<\/p>\n<p id=\"1.5%20%E6%95%B0%E6%8D%AE%E6%8E%A5%E6%94%B6%E4%B8%8E%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86%EF%BC%9Arecvfrom%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:120px\">1.5 \u63a5\u6536\u6570\u636e&#xff1a;recvfrom\u51fd\u6570\u8be6\u89e3<\/p>\n<p id=\"1.5.1%20recvfrom%20%E5%8F%82%E6%95%B0%E4%B8%8E%E8%BF%94%E5%9B%9E%E5%80%BC-toc\" style=\"margin-left:160px\">1.5.1 \u53c2\u6570\u542b\u4e49\u4e0e\u8fd4\u56de\u503c<\/p>\n<p id=\"%E4%BA%8C%E3%80%81UDP%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8E%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%A0%B8%E5%BF%83%E9%80%9A%E4%BF%A1%E6%B5%81%E7%A8%8B%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:80px\">\u4e8c\u3001UDP\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff1a;\u4ece\u521d\u59cb\u5316\u5230\u6570\u636e\u6536\u53d1<\/p>\n<p id=\"2.1%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%A5%97%E6%8E%A5%E5%AD%97%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%8E%E5%9C%B0%E5%9D%80%E7%BB%91%E5%AE%9A-toc\" style=\"margin-left:120px\">2.1 \u670d\u52a1\u5668\u521d\u59cb\u5316&#xff1a;\u5730\u5740\u914d\u7f6e\u4e0e\u7aef\u53e3\u7ed1\u5b9a<\/p>\n<p id=\"2.1.1%20%E7%BB%93%E6%9E%84%E4%BD%93%E6%B8%85%E9%9B%B6%EF%BC%9Abzero%20%E4%B8%8E%20explicit_bzero-toc\" style=\"margin-left:160px\">2.1.1 \u7ed3\u6784\u4f53\u6e05\u96f6&#xff1a;bzero\u4e0eexplicit_bzero<\/p>\n<p id=\"2.1.2%20%E5%A1%AB%E5%85%85%20sockaddr_in%20%E4%B8%8E%E7%BD%91%E7%BB%9C%E5%AD%97%E8%8A%82%E5%BA%8F%E8%BD%AC%E6%8D%A2-toc\" style=\"margin-left:160px\">2.1.2 \u586b\u5145sockaddr_in\u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u6362<\/p>\n<p id=\"2.1.3%20%E7%BB%91%E5%AE%9A%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%EF%BC%9Abind%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:160px\">2.1.3 \u7ed1\u5b9a\u672c\u5730\u5730\u5740&#xff1a;bind\u7cfb\u7edf\u8c03\u7528<\/p>\n<p id=\"2.2%20UDP%20%E5%85%A8%E5%8F%8C%E5%B7%A5%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%95%B0%E6%8D%AE%E6%94%B6%E5%8F%91%E6%9C%BA%E5%88%B6-toc\" style=\"margin-left:120px\">2.2 UDP\u6570\u636e\u6536\u53d1\u673a\u5236&#xff1a;\u4ece\u63a5\u6536\u5230\u54cd\u5e94<\/p>\n<p id=\"2.2.1%20%E6%95%B0%E6%8D%AE%E6%8E%A5%E6%94%B6%E6%9C%BA%E5%88%B6%EF%BC%9Arecvfrom%20%E4%B8%8E%E9%98%BB%E5%A1%9E%20IO-toc\" style=\"margin-left:160px\">2.2.1 \u6570\u636e\u63a5\u6536&#xff1a;recvfrom\u4e0e\u963b\u585eI\/O<\/p>\n<p id=\"2.2.2%20%E6%95%B0%E6%8D%AE%E5%8F%91%E9%80%81%E6%9C%BA%E5%88%B6%EF%BC%9Asendto%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:160px\">2.2.2 \u6570\u636e\u53d1\u9001&#xff1a;sendto\u51fd\u6570<\/p>\n<p id=\"2.3%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%AE%BF%E9%97%AE%E6%9C%BA%E5%88%B6%E4%B8%8E%E7%AB%AF%E5%8F%A3%E5%A4%84%E7%90%86-toc\" style=\"margin-left:120px\">2.3 \u5ba2\u6237\u7aef\u901a\u4fe1&#xff1a;\u670d\u52a1\u5668\u5730\u5740\u4e0e\u7aef\u53e3\u5206\u914d<\/p>\n<p id=\"2.3.1%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%9C%B0%E5%9D%80%E7%9A%84%E2%80%9C%E7%A1%AC%E7%BC%96%E7%A0%81%E2%80%9D%E4%B8%8E%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-toc\" style=\"margin-left:160px\">2.3.1 \u670d\u52a1\u5668\u5730\u5740\u7684\u786c\u7f16\u7801\u4e0e\u914d\u7f6e\u6587\u4ef6\u7ba1\u7406<\/p>\n<p id=\"2.3.2%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84%E9%9A%90%E5%BC%8F%20Bind-toc\" style=\"margin-left:160px\">2.3.2 \u5ba2\u6237\u7aef\u7684\u9690\u5f0f\u7ed1\u5b9a&#xff1a;\u7aef\u53e3\u7531\u8c01\u5206\u914d<\/p>\n<p id=\"%E4%B8%89%E3%80%81Client%20%E7%AB%AF%E7%9A%84%20Bind%20%E7%AD%96%E7%95%A5%E4%B8%8E%20Server%20%E7%AB%AF%E7%9A%84%20Bind%20%E5%BF%85%E8%A6%81%E6%80%A7%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90-toc\" style=\"margin-left:80px\">\u4e09\u3001\u6df1\u5165\u7406\u89e3bind&#xff1a;\u5ba2\u6237\u7aef\u7b56\u7565\u4e0e\u670d\u52a1\u7aef\u9700\u6c42<\/p>\n<p id=\"3.1%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%A6%81%E4%B8%8D%E8%A6%81%20bind%EF%BC%9F-toc\" style=\"margin-left:120px\">3.1 UDP\u5ba2\u6237\u7aef\u662f\u5426\u9700\u8981\u663e\u5f0f\u8c03\u7528bind<\/p>\n<p id=\"3.2%20%E4%B8%BA%E4%BB%80%E4%B9%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BF%85%E9%A1%BB%E6%98%BE%E5%BC%8F%20bind%EF%BC%9F-toc\" style=\"margin-left:120px\">3.2 \u4e3a\u4ec0\u4e48UDP\u670d\u52a1\u7aef\u901a\u5e38\u9700\u8981\u663e\u5f0f\u7ed1\u5b9a\u7aef\u53e3<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%20IP%20%E7%BB%91%E5%AE%9A%E9%99%B7%E9%98%B1%E3%80%81INADDR_ANY%20%E5%8E%9F%E7%90%86%E4%B8%8E%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E6%BC%94%E8%BF%9B-toc\" style=\"margin-left:80px\">\u56db\u3001UDP\u670d\u52a1\u5668\u8fdb\u9636&#xff1a;\u5730\u5740\u7ed1\u5b9a\u3001\u4e1a\u52a1\u89e3\u8026\u4e0e\u8f6c\u6362\u63a5\u53e3<\/p>\n<p id=\"4.1%20%E6%9C%8D%E5%8A%A1%E5%99%A8%20IP%20%E7%BB%91%E5%AE%9A%E7%9A%84%E2%80%9C%E5%9D%91%E2%80%9D%E4%B8%8E%20INADDR_ANY%20%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:120px\">4.1 \u670d\u52a1\u5668\u5730\u5740\u7ed1\u5b9a\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u4e0eINADDR_ANY\u539f\u7406<\/p>\n<p id=\"4.1.1%20%E5%B8%B8%E8%A7%81%E7%9A%84%E7%BB%91%E5%AE%9A%E8%AF%AF%E5%8C%BA-toc\" style=\"margin-left:160px\">4.1.1 \u5e38\u89c1\u7684\u5730\u5740\u7ed1\u5b9a\u8bef\u533a<\/p>\n<p id=\"4.1.2%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%EF%BC%9A%E4%BD%BF%E7%94%A8%20INADDR_ANY%EF%BC%88%E4%BB%BB%E6%84%8F%E5%9C%B0%E5%9D%80%E7%BB%91%E5%AE%9A%EF%BC%89-toc\" style=\"margin-left:160px\">4.1.2 \u4f7f\u7528INADDR_ANY&#xff1a;\u76d1\u542c\u672c\u673a\u7684\u591a\u4e2aIPv4\u5730\u5740<\/p>\n<p id=\"4.2%20%E8%BD%AF%E4%BB%B6%E5%88%86%E5%B1%82%E8%AE%BE%E8%AE%A1%EF%BC%9AUDP%20%E6%9C%8D%E5%8A%A1%E4%B8%8E%E4%B8%9A%E5%8A%A1%E9%80%BB%E8%BE%91%E8%A7%A3%E8%80%A6-toc\" style=\"margin-left:120px\">4.2 \u8f6f\u4ef6\u5206\u5c42\u8bbe\u8ba1&#xff1a;\u5b9e\u73b0UDP\u901a\u4fe1\u4e0e\u4e1a\u52a1\u903b\u8f91\u89e3\u8026<\/p>\n<p id=\"4.2.1%20%E4%BD%BF%E7%94%A8%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%E5%A4%84%E7%90%86%E4%B8%9A%E5%8A%A1%E9%80%BB%E8%BE%91-toc\" style=\"margin-left:160px\">4.2.1 \u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u4e1a\u52a1\u903b\u8f91<\/p>\n<p id=\"4.2.2%20%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%E6%A8%A1%E5%BC%8F%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:160px\">4.2.2 \u56de\u8c03\u51fd\u6570\u6a21\u5f0f\u5b9e\u73b0<\/p>\n<p id=\"4.3%20%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E7%9A%84%E6%BC%94%E8%BF%9B%E4%B8%8E%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E6%80%A7%E5%88%86%E6%9E%90-toc\" style=\"margin-left:120px\">4.3 IP\u5730\u5740\u8f6c\u6362\u51fd\u6570\u7684\u6f14\u8fdb\u4e0e\u7ebf\u7a0b\u5b89\u5168\u6027<\/p>\n<p id=\"4.3.1%20%E6%97%A7%E7%89%88%20API%20%E5%8F%8A%E5%85%B6%E7%BC%BA%E9%99%B7-toc\" style=\"margin-left:160px\">4.3.1 \u65e7\u7248\u5730\u5740\u8f6c\u6362API\u53ca\u5176\u5c40\u9650<\/p>\n<p id=\"4.3.2%20%E7%8E%B0%E4%BB%A3%E5%8C%96%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%20API%EF%BC%9Ainet_pton%20%E4%B8%8E%20inet_ntop-toc\" style=\"margin-left:160px\">4.3.2 \u73b0\u4ee3\u5730\u5740\u8f6c\u6362API&#xff1a;inet_pton\u4e0einet_ntop<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E9%99%84%E4%B8%8A%E6%BA%90%E7%A0%81-toc\" style=\"margin-left:80px\">\u4e94\u3001\u5b8c\u6574\u6e90\u7801\u4e0e\u8fd0\u884c\u6d4b\u8bd5<\/p>\n<p id=\"5.1%20InetAddr.hpp%EF%BC%88IP%20%E5%9C%B0%E5%9D%80%E5%B0%81%E8%A3%85%E7%B1%BB%EF%BC%89-toc\" style=\"margin-left:120px\">5.1 InetAddr.hpp&#xff1a;IP\u5730\u5740\u5c01\u88c5\u7c7b<\/p>\n<p id=\"5.2%20UdpServer.hpp%EF%BC%9AUDP%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%B1%BB-toc\" style=\"margin-left:80px\">5.2 UdpServer.hpp&#xff1a;UDP\u670d\u52a1\u5668\u7c7b<\/p>\n<p id=\"5.3%20UdpServer.cc%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89-toc\" style=\"margin-left:120px\">5.3 UdpServer.cc&#xff1a;\u670d\u52a1\u7aef\u4e3b\u7a0b\u5e8f<\/p>\n<p id=\"5.4%20UdpClient.cc%EF%BC%88%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89-toc\" style=\"margin-left:120px\">5.4 UdpClient.cc&#xff1a;\u5ba2\u6237\u7aef\u4e3b\u7a0b\u5e8f<\/p>\n<p id=\"5.5%20Makefile%EF%BC%9A%E9%A1%B9%E7%9B%AE%E7%BC%96%E8%AF%91%E8%A7%84%E5%88%99-toc\" style=\"margin-left:80px\">5.5 Makefile&#xff1a;\u9879\u76ee\u7f16\u8bd1\u89c4\u5219<\/p>\n<p id=\"5.6%20%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F-toc\" style=\"margin-left:120px\">5.6 \u7f16\u8bd1\u8fd0\u884c\u4e0e\u901a\u4fe1\u6d4b\u8bd5<\/p>\n<hr id=\"hr-toc\" \/>\n<h4 id=\"%E4%B8%80%E3%80%81UDP%20%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E4%B8%8E%E6%A0%B8%E5%BF%83%20API%20%E8%AF%A6%E8%A7%A3\">\u4e00\u3001UDP\u5957\u63a5\u5b57\u7f16\u7a0b\u57fa\u7840&#xff1a;\u4ece\u521b\u5efa\u5957\u63a5\u5b57\u5230\u63a5\u6536\u6570\u636e<\/h4>\n<p>\u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3&#xff0c;\u5c31\u662f\u501f\u52a9\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u5957\u63a5\u5b57&#xff08;Socket&#xff09;\u63a5\u53e3&#xff0c;\u8ba9\u8fdb\u7a0b\u4e4b\u95f4\u3001\u4e3b\u673a\u4e4b\u95f4\u80fd\u591f\u4ea4\u6362\u6570\u636e\u3002\u4f20\u8f93\u5c42\u534f\u8bae\u91cc&#xff0c;UDP&#xff08;\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae&#xff09;\u662f\u4e2a\u65e0\u8fde\u63a5\u3001\u4e0d\u53ef\u9760\u3001\u9762\u5411\u6570\u636e\u62a5\u7684\u534f\u8bae\u3002<\/p>\n<h5 id=\"1.1%20%E5%A5%97%E6%8E%A5%E5%AD%97%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%B8%B8%E7%94%A8%E5%A4%B4%E6%96%87%E4%BB%B6\">1.1 Socket\u7f16\u7a0b\u57fa\u7840\u4e0e\u5e38\u7528\u5934\u6587\u4ef6<\/h5>\n<p>\u5728Linux\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u641e\u5957\u63a5\u5b57\u5f00\u53d1\u901a\u5e38\u7ed5\u4e0d\u5f00\u8fd9\u56db\u4e2a\u57fa\u7840\u5934\u6587\u4ef6&#xff1a;<\/p>\n<p>#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt; <\/p>\n<ul>\n<li>sys\/types.h\u548csys\/socket.h&#xff1a;\u5b9a\u4e0b\u4e86\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u6838\u5fc3\u6570\u636e\u7c7b\u578b\u548c\u7cfb\u7edf\u8c03\u7528\u51fd\u6570&#xff0c;\u6bd4\u5982socket()\u3001recvfrom()\u3001sendto()\u8fd9\u4e9b\u3002<\/li>\n<li>netinet\/in.h\u548carpa\/inet.h&#xff1a;\u8d1f\u8d23\u7f51\u7edc\u5730\u5740\u7ed3\u6784\u4f53&#xff08;\u6bd4\u5982struct sockaddr_in&#xff09;&#xff0c;\u8fd8\u6709IP\u5730\u5740\u4e0e\u5b57\u8282\u5e8f\u4e4b\u95f4\u7684\u8f6c\u6362\u51fd\u6570\u3002<\/li>\n<\/ul>\n<h5 id=\"1.2%20%E5%88%9B%E5%BB%BA%E5%A5%97%E6%8E%A5%E5%AD%97%EF%BC%9Asocket%20%E5%87%BD%E6%95%B0\">1.2 \u521b\u5efa\u5957\u63a5\u5b57&#xff1a;socket\u51fd\u6570\u8be6\u89e3<\/h5>\n<p>\u7f51\u7edc\u901a\u4fe1\u7684\u7b2c\u4e00\u6b65&#xff0c;\u662f\u521b\u5efa\u5957\u63a5\u5b57\u3002Linux\u91cc\u9760socket()\u8fd9\u4e2a\u7cfb\u7edf\u8c03\u7528\u6765\u641e\u5b9a&#xff1a;<\/p>\n<p>int socket(int domain, int type, int protocol); <\/p>\n<h6 id=\"1.2.1%20%E5%8F%82%E6%95%B0%E8%A7%A3%E6%9E%90%E4%B8%8E%E5%B8%B8%E6%95%B0%E5%AE%9A%E4%B9%89\">1.2.1 \u53c2\u6570\u89e3\u6790\u4e0e\u5e38\u7528\u5e38\u91cf<\/h6>\n<p>domain&#xff08;\u534f\u8bae\u65cf\/\u5730\u5740\u65cf&#xff09;&#xff1a;\u6307\u5b9a\u901a\u4fe1\u7684\u9886\u57df&#xff0c;\u8bf4\u767d\u4e86\u5c31\u662f\u51b3\u5b9a\u8fd9\u6b21\u662f\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff0c;\u8fd8\u662f\u8d70\u7f51\u7edc\u901a\u4fe1\u3002<\/p>\n<ul>\n<li>AF_UNIX\/AF_LOCAL&#xff1a;\u7528\u4e8eUnix\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/li>\n<li>AF_INET&#xff1a;\u7528\u4e8eIPv4\u4e92\u8054\u7f51\u534f\u8bae\u3002<\/li>\n<li>AF_INET6&#xff1a;\u7528\u4e8eIPv6\u4e92\u8054\u7f51\u534f\u8bae\u3002<\/li>\n<\/ul>\n<p>type&#xff08;\u5957\u63a5\u5b57\u7c7b\u578b&#xff09;&#xff1a;\u6307\u5b9a\u901a\u4fe1\u7684\u6570\u636e\u4f20\u8f93\u670d\u52a1\u7c7b\u578b\u3002<\/p>\n<ul>\n<li>SOCK_STREAM&#xff1a;\u63d0\u4f9b\u987a\u5e8f\u3001\u53ef\u9760\u3001\u53cc\u5411\u3001\u57fa\u4e8e\u8fde\u63a5\u7684\u5b57\u8282\u6d41\u901a\u4fe1&#xff0c;\u4e5f\u5c31\u662fTCP\u3002<\/li>\n<li>SOCK_DGRAM&#xff1a;\u652f\u6301\u6570\u636e\u62a5\u901a\u4fe1&#xff0c;\u65e0\u8fde\u63a5\u3001\u4e0d\u53ef\u9760\u3001\u6d88\u606f\u6709\u56fa\u5b9a\u6700\u5927\u957f\u5ea6&#xff0c;\u4e5f\u5c31\u662fUDP\u3002<\/li>\n<\/ul>\n<p>protocol&#xff08;\u534f\u8bae\u7c7b\u578b&#xff09;&#xff1a;\u901a\u5e38\u586b0\u3002\u7cfb\u7edf\u4f1a\u6839\u636edomain\u548ctype\u81ea\u52a8\u63a8\u5bfc\u51fa\u9ed8\u8ba4\u534f\u8bae&#xff0c;\u6bd4\u5982AF_INET\u914dSOCK_DGRAM&#xff0c;\u9ed8\u8ba4\u5c31\u662fUDP\u534f\u8bae\u3002<\/p>\n<h6 id=\"1.2.2%20%E8%BF%94%E5%9B%9E%E5%80%BC%E4%B8%8E%E2%80%9C%E7%BD%91%E7%BB%9C%E7%9A%86%E6%96%87%E4%BB%B6%E2%80%9D%E5%93%B2%E5%AD%A6\">1.2.2 \u8fd4\u56de\u503c\u4e0eLinux\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u7684\u8bbe\u8ba1\u601d\u60f3<\/h6>\n<ul>\n<li>\u6210\u529f&#xff1a;\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;File Descriptor&#xff0c;FD&#xff09;\u3002<\/li>\n<li>\u5931\u8d25&#xff1a;\u8fd4\u56de-1&#xff0c;\u5e76\u8bbe\u7f6eerrno \u9519\u8bef\u7801\u3002<\/li>\n<\/ul>\n<p>\u5957\u63a5\u5b57\u5728Linux\u91cc\u88ab\u62bd\u8c61\u6210\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8fd9\u6070\u6070\u4f53\u73b0\u4e86\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u7684\u8bbe\u8ba1\u54f2\u5b66\u3002\u7f51\u7edc\u8bbe\u5907\u7684\u8bfb\u5199&#xff0c;\u548c\u666e\u901a\u6587\u4ef6\u7684\u8bfb\u5199&#xff0c;\u5728\u5185\u6838\u5c42\u9762\u6709\u7740\u9ad8\u5ea6\u7edf\u4e00\u7684\u64cd\u4f5c\u63a5\u53e3\u3002<\/p>\n<p>\u6253\u54ea\u513f\u8bfb\u3001\u5f80\u54ea\u513f\u5199&#xff0c;\u5e95\u5c42\u90fd\u662f\u540c\u4e00\u5957\u4e1c\u897f\u3002\u5957\u63a5\u5b57\u62ab\u7740\u7f51\u7edc\u7684\u5916\u8863&#xff0c;\u9aa8\u5b50\u91cc\u8fd8\u662f\u4e2a\u6587\u4ef6\u3002<\/p>\n<h5 id=\"1.3%20%E5%9C%B0%E5%9D%80%E7%BB%93%E6%9E%84%E4%BD%93%E8%AF%A6%E8%A7%A3%EF%BC%9Asockaddr_in\">1.3 IPv4\u5730\u5740\u7ed3\u6784&#xff1a;sockaddr_in\u8be6\u89e3<\/h5>\n<p>\u4e3a\u4e86\u517c\u5bb9\u4e94\u82b1\u516b\u95e8\u7684\u534f\u8bae\u65cf\u5730\u5740\u683c\u5f0f&#xff0c;Linux\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u901a\u7528\u7ed3\u6784\u4f53struct sockaddr\u3002\u4f46\u771f\u5230\u4e86IPv4\u7f51\u7edc\u901a\u4fe1\u7684\u5b9e\u6218\u91cc&#xff0c;\u7528\u7684\u5374\u662f\u4e13\u95e8\u7684struct sockaddr_in\u3002<\/p>\n<h6 id=\"1.3.1%20sockaddr_in%20%E7%BB%93%E6%9E%84%E4%BD%93%E6%88%90%E5%91%98%E5%AE%9A%E4%B9%89\">1.3.1 sockaddr_in\u7684\u7ed3\u6784\u6210\u5458\u4e0e\u542b\u4e49<\/h6>\n<p>struct sockaddr_in\u7684\u5185\u90e8\u7ec4\u7ec7&#xff0c;\u957f\u8fd9\u6837&#xff1a;<\/p>\n<p>struct sockaddr_in {<br \/>\n    __SOCKADDR_COMMON (sin_);  \/* 16 \u4f4d\u5730\u5740\u7c7b\u578b \/ \u534f\u8bae\u5bb6\u65cf&#xff0c;\u5982 AF_INET *\/<br \/>\n    in_port_t sin_port;        \/* 16 \u4f4d\u7aef\u53e3\u53f7&#xff08;\u7f51\u7edc\u5b57\u8282\u5e8f&#xff09; *\/<br \/>\n    struct in_addr sin_addr;   \/* 32 \u4f4d IP \u5730\u5740&#xff08;\u7f51\u7edc\u5b57\u8282\u5e8f&#xff09; *\/<br \/>\n    unsigned char sin_zero[8]; \/* \u586b\u5145\u9879&#xff0c;\u7528\u4e8e\u4e0e struct sockaddr \u4fdd\u6301\u5927\u5c0f\u4e00\u81f4 *\/<br \/>\n}; <\/p>\n<p>\u7ed3\u6784\u4f53\u6210\u5458\u9010\u4e2a\u62c6&#xff1a;<\/p>\n<p>\u5730\u5740\u7c7b\u578b&#xff08;sin_family&#xff09;&#xff1a;\u901a\u8fc7\u5185\u6838\u5b8f__SOCKADDR_COMMON(sin_)\u5c55\u5f00\u540e&#xff0c;\u5c31\u662fsa_family_t sin_family&#xff0c;\u7528\u6765\u6307\u5b9a\u534f\u8bae\u65cf&#xff0c;\u6bd4\u5982AF_INET\u3002\u90a3\u4e2a##\u662fC\u8bed\u8a00\u9884\u5904\u7406\u5668\u7684\u8fd0\u7b97\u7b26&#xff0c;\u4e13\u95e8\u8d1f\u8d23\u628a\u7b26\u53f7\u62fc\u63a5\u5408\u5e76\u3002<\/p>\n<p>\u7aef\u53e3\u53f7&#xff08;sin_port&#xff09;&#xff1a;16\u4f4d\u65e0\u7b26\u53f7\u6574\u6570&#xff0c;\u8868\u793a\u901a\u4fe1\u8fdb\u7a0b\u7684\u7aef\u53e3\u53f7\u3002\u6ce8\u610f&#xff0c;\u5b83\u5f97\u662f\u7f51\u7edc\u5b57\u8282\u5e8f\u3002<\/p>\n<p>IP \u5730\u5740&#xff08;sin_addr&#xff09;&#xff1a;\u5185\u90e8\u88c5\u7740\u4e00\u4e2a32\u4f4d\u65e0\u7b26\u53f7\u6574\u6570&#xff0c;\u8868\u793aIPv4\u5730\u5740\u3002\u540c\u6837&#xff0c;\u7f51\u7edc\u5b57\u8282\u5e8f\u3002<\/p>\n<p>\u586b\u5145\u7269&#xff08;sin_zero&#xff09;&#xff1a;8\u4e2a\u5b57\u8282\u7684\u586b\u5145\u3002\u4e3a\u4ec0\u4e48\u8981\u5b83&#xff1f;\u4e3a\u4e86\u8ba9\u6574\u4e2astruct sockaddr_in\u7684\u5927\u5c0f&#xff0c;\u8ddf\u901a\u7528\u7684struct sockaddr\u4e25\u4e1d\u5408\u7f1d\u5730\u5bf9\u4e0a&#xff0c;sockaddr\u90a3\u8fb9\u662f14\u5b57\u8282\u5730\u5740\u6570\u636e\u52a02\u5b57\u8282\u7c7b\u578b&#xff0c;\u6b63\u597d16\u5b57\u8282\u3002\u5927\u5c0f\u4e00\u81f4&#xff0c;\u4e24\u8005\u624d\u80fd\u5b89\u5168\u5730\u4e92\u76f8\u5f3a\u8f6c\u3002<\/p>\n<h5 id=\"1.4%20IP%20%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2%E4%B8%8E%E5%AD%97%E8%8A%82%E5%BA%8F%E5%A4%84%E7%90%86\">1.4 IP\u5730\u5740\u8f6c\u6362\u4e0e\u5b57\u8282\u5e8f\u5904\u7406<\/h5>\n<p>\u5728\u8ba1\u7b97\u673a\u5185\u90e8\u548c\u7f51\u7edc\u4f20\u8f93\u91cc&#xff0c;IP\u5730\u5740\u7684\u8868\u793a\u5f62\u5f0f\u5b8c\u5168\u662f\u4e24\u7801\u4e8b\u3002\u7528\u6237\u4e60\u60ef\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32&#xff0c;\u6bd4\u5982&#034;192.168.1.1&#034;&#xff1b;\u53ef\u7f51\u7edc\u5e95\u5c42\u4f20\u8f93\u548c\u7ed3\u6784\u4f53\u5b58\u50a8&#xff0c;\u8981\u7684\u662f32\u4f4d\u65e0\u7b26\u53f7\u6574\u6570&#xff08;uint32_t&#xff09;&#xff0c;\u800c\u4e14\u5fc5\u987b\u5b88\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u89c4\u77e9&#xff0c;\u4e5f\u5c31\u662f\u5927\u7aef\u3002<\/p>\n<h5 id=\"1.4.1%20%E7%82%B9%E5%88%86%E5%8D%81%E8%BF%9B%E5%88%B6%E5%9C%B0%E5%9D%80%E4%B8%8E%E7%BD%91%E7%BB%9C%E5%AD%97%E8%8A%82%E5%BA%8F%E7%9A%84%E8%BD%AC%E6%8D%A2\">1.4.1 \u70b9\u5206\u5341\u8fdb\u5236\u5730\u5740\u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u8f6c\u6362<\/h5>\n<p>IP\u5b57\u7b26\u4e32\u8f6c4\u5b57\u8282\u7f51\u7edc\u5b57\u8282\u5e8f&#xff1a;\u7528inet_addr\u8fd9\u4e2aAPI&#xff0c;\u4e00\u6b65\u5230\u4f4d&#xff0c;\u628a\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u53d8\u6210\u7f51\u7edc\u5b57\u8282\u5e8f\u768432\u4f4d\u6574\u6570&#xff1a;<\/p>\n<p>in_addr_t inet_addr(const char *cp); <\/p>\n<p>\u8f6c\u6362\u539f\u7406&#xff0c;\u62c6\u5f00\u770b\u4e09\u6b65&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u6b65&#xff0c;\u628a\u5b57\u7b26\u4e32&#034;192.168.1.1&#034;\u6309 . \u5207\u5f00&#xff0c;\u5206\u6210\u56db\u4e2a\u6574\u578b\u90e8\u5206&#xff1a;192\u3001168\u30011\u30011\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65&#xff0c;\u6bcf\u4e2a\u90e8\u5206\u5360\u4e00\u4e2a\u5b57\u8282&#xff08;char&#xff09;&#xff0c;\u56db\u5757\u62fc\u8d77\u6765&#xff0c;\u7ec4\u6210\u4e00\u4e2a32\u4f4d\u6574\u6570\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff0c;\u6309\u7f51\u7edc\u4f20\u8f93\u534f\u8bae\u7684\u8981\u6c42&#xff0c;\u628a\u6570\u503c\u8c03\u6574\u6210\u7f51\u7edc\u5b57\u8282\u5e8f&#xff0c;\u4e5f\u5c31\u662f\u5927\u7aef\u5e8f\u3002<\/p>\n<p>\u53cd\u5411\u8f6c\u6362\u7684\u539f\u7406&#xff1a;<\/p>\n<p>\u53cd\u8fc7\u6765&#xff0c;\u8981\u628a\u6536\u5230\u768432\u4f4d\u7f51\u7edc\u5b57\u8282\u5e8fIP\u53d8\u56de\u53ef\u8bfb\u5b57\u7b26\u4e32&#xff0c;\u601d\u8def\u4e5f\u4e0d\u590d\u6742&#xff0c;\u7528\u6307\u9488\u5f3a\u8f6c&#xff0c;\u628a\u56db\u4e2a\u5b57\u8282\u4e00\u4e2a\u4e2a\u53d6\u51fa\u6765&#xff0c;\u518d\u6309\u70b9\u5206\u5341\u8fdb\u5236\u7684\u683c\u5f0f\u62fc\u56de\u53bb\u3002<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u4e00\u4e2a\u6574\u6570&#xff0c;\u4e2d\u95f4\u9694\u7740\u4e00\u9053\u5b57\u8282\u5e8f\u7684\u8f6c\u6362\u3002\u6765\u6765\u56de\u56de&#xff0c;\u7f51\u7edc\u901a\u4fe1\u7684\u5730\u5740\u8bc6\u522b&#xff0c;\u5c31\u9760\u8fd9\u5957\u8f6c\u6362\u5728\u4e24\u7aef\u5bf9\u9f50\u3002<\/p>\n<h5 id=\"1.5%20%E6%95%B0%E6%8D%AE%E6%8E%A5%E6%94%B6%E4%B8%8E%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86%EF%BC%9Arecvfrom%20%E5%87%BD%E6%95%B0\">1.5 \u63a5\u6536\u6570\u636e&#xff1a;recvfrom\u51fd\u6570\u8be6\u89e3<\/h5>\n<p>\u7f51\u7edc\u670d\u52a1\u5668\u901a\u5e38\u5f977\u00d724\u5c0f\u65f6\u8fde\u8f74\u8f6c&#xff0c;\u6240\u4ee5UDP\u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91&#xff0c;\u5f80\u5f80\u5c31\u662f\u4e00\u4e2a\u6b7b\u5faa\u73af&#xff0c;\u4e0d\u505c\u5730\u76d1\u542c\u3001\u6536\u5305\u3001\u5904\u7406\u3002\u53ea\u8981\u670d\u52a1\u4e0d\u505c&#xff0c;\u8fd9\u4e2a\u5708\u5c31\u4e00\u76f4\u8f6c\u3002<\/p>\n<p>Linux\u7ed9\u65e0\u8fde\u63a5\u7684\u6570\u636e\u5305\u63a5\u6536&#xff0c;\u51c6\u5907\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684\u51fd\u6570&#xff1a;recvfrom\u3002<\/p>\n<p>ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,<br \/>\n                 struct sockaddr *src_addr, socklen_t *addrlen); <\/p>\n<h6 id=\"1.5.1%20recvfrom%20%E5%8F%82%E6%95%B0%E4%B8%8E%E8%BF%94%E5%9B%9E%E5%80%BC\">1.5.1 \u53c2\u6570\u542b\u4e49\u4e0e\u8fd4\u56de\u503c<\/h6>\n<ul>\n<li>sockfd&#xff1a;socket()\u8fd4\u56de\u7684\u90a3\u4e2a\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u544a\u8bc9\u51fd\u6570\u4ece\u54ea\u4e2a\u5957\u63a5\u5b57\u4e0a\u6536\u6570\u636e\u3002<\/li>\n<li>buf&#xff1a;\u7528\u6765\u5b58\u653e\u63a5\u6536\u6570\u636e\u7684\u5185\u5b58\u7f13\u51b2\u533a\u6307\u9488\u3002\u6536\u6765\u7684\u6570\u636e&#xff0c;\u5c31\u5f80\u8fd9\u513f\u653e\u3002<\/li>\n<li>len&#xff1a;\u7f13\u51b2\u533a\u7684\u6700\u5927\u53ef\u7528\u5b57\u8282\u6570\u3002\u522b\u8ba9\u6570\u636e\u6ea2\u51fa\u6765\u3002<\/li>\n<li>flags&#xff1a;\u63a5\u6536\u63a7\u5236\u6807\u5fd7\u4f4d&#xff0c;\u901a\u5e38\u586b0&#xff0c;\u8868\u793a\u963b\u585e\u5f0f\u63a5\u6536\u3002\u6ca1\u6570\u636e\u6765&#xff0c;\u5b83\u5c31\u8001\u8001\u5b9e\u5b9e\u7b49\u7740\u3002<\/li>\n<li>src_addr&#xff1a;\u8f93\u51fa\u578b\u53c2\u6570\u3002\u586b\u4e00\u4e2astruct sockaddr_in\u5f3a\u8f6c\u540e\u7684\u6307\u9488\u8fdb\u53bb&#xff0c;\u51fd\u6570\u6536\u5b8c\u6570\u636e&#xff0c;\u4f1a\u628a\u53d1\u9001\u65b9&#xff08;\u5ba2\u6237\u7aef&#xff09;\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u5199\u5230\u8fd9\u91cc\u3002\u8c01\u53d1\u7684&#xff0c;\u4e00\u67e5\u4fbf\u77e5\u3002<\/li>\n<li>addrlen&#xff1a;\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570\u3002\u4f20\u8fdb\u53bb\u65f6&#xff0c;\u662fsrc_addr\u7ed3\u6784\u4f53\u7684\u5927\u5c0f&#xff0c;\u4e5f\u5c31\u662fsizeof(struct sockaddr_in)&#xff1b;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u5b83\u53d8\u6210\u5185\u6838\u5b9e\u9645\u5199\u5165\u7684\u5730\u5740\u4fe1\u606f\u7ed3\u6784\u4f53\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u65f6&#xff0c;\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u5230\u7684\u6570\u636e\u5b57\u8282\u6570&#xff1b;\u5982\u679c\u8fde\u63a5\u5173\u95ed\u6216\u51fa\u9519&#xff0c;\u8fd4\u56de-1&#xff0c;\u5e76\u8bbe\u7f6eerrno\u3002<\/p>\n<p>recvfrom\u4e00\u6536&#xff0c;\u6570\u636e\u5230\u624b&#xff0c;\u53d1\u9001\u65b9\u7684\u5730\u5740\u4e5f\u987a\u5e26\u63e3\u515c\u91cc\u4e86\u3002UDP\u670d\u52a1\u5668\u9760\u5b83&#xff0c;\u624d\u80fd\u5728\u6536\u5230\u6d88\u606f\u4e4b\u540e&#xff0c;\u77e5\u9053\u8be5\u7ed9\u8c01\u56de\u8bdd\u3002<\/p>\n<h4 id=\"%E4%BA%8C%E3%80%81UDP%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8E%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%A0%B8%E5%BF%83%E9%80%9A%E4%BF%A1%E6%B5%81%E7%A8%8B%E5%AE%9E%E7%8E%B0\">\u4e8c\u3001UDP\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff1a;\u4ece\u521d\u59cb\u5316\u5230\u6570\u636e\u6536\u53d1<\/h4>\n<p>\u4e0a\u6587\u6211\u4eec\u628aUDP\u5957\u63a5\u5b57\u7684\u57fa\u7840\u6982\u5ff5\u3001\u5957\u63a5\u5b57\u521b\u5efa\u548c\u6570\u636e\u7ed3\u6784\u634b\u4e86\u4e00\u904d\u3002\u63a5\u4e0b\u6765&#xff0c;\u5c31\u6b63\u5f0f\u8fdb\u5165UDP\u670d\u52a1\u5668\u7684\u521d\u59cb\u5316\u7ed1\u5b9a&#xff08;bind&#xff09;\u3001\u6570\u636e\u6536\u53d1&#xff08;recvfrom \u4e0e sendto&#xff09;&#xff0c;\u4ee5\u53ca\u5ba2\u6237\u7aef\u8bf7\u6c42\u673a\u5236\u80cc\u540e\u7684\u5e95\u5c42\u903b\u8f91\u3002<\/p>\n<h5 id=\"2.1%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%A5%97%E6%8E%A5%E5%AD%97%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%8E%E5%9C%B0%E5%9D%80%E7%BB%91%E5%AE%9A\">2.1 \u670d\u52a1\u5668\u521d\u59cb\u5316&#xff1a;\u5730\u5740\u914d\u7f6e\u4e0e\u7aef\u53e3\u7ed1\u5b9a<\/h5>\n<p>\u8981\u521b\u5efa\u4e00\u4e2aUDP\u670d\u52a1\u5668&#xff0c;\u7b2c\u4e00\u6b65\u5c31\u662f\u5b8c\u6210\u5957\u63a5\u5b57\u521d\u59cb\u5316&#xff0c;\u7136\u540e\u628a\u7279\u5b9a\u7684IP\u548c\u7aef\u53e3\u4fe1\u606f\u7ed1\u5b9a&#xff08;Bind&#xff09;\u5230\u5185\u6838\u91cc\u3002\u5730\u5740\u4e0d\u7ed1\u4e0a&#xff0c;\u5ba2\u6237\u7aef\u5c31\u627e\u4e0d\u5230\u4f60\u3002<\/p>\n<h6 id=\"2.1.1%20%E7%BB%93%E6%9E%84%E4%BD%93%E6%B8%85%E9%9B%B6%EF%BC%9Abzero%20%E4%B8%8E%20explicit_bzero\">2.1.1 \u7ed3\u6784\u4f53\u6e05\u96f6&#xff1a;bzero\u4e0eexplicit_bzero<\/h6>\n<p>\u5728\u586b\u5145struct sockaddr_in\u4e4b\u524d&#xff0c;\u901a\u5e38\u5f97\u5148\u628a\u7ed3\u6784\u4f53\u5360\u7528\u7684\u5185\u5b58\u6e05\u96f6&#xff0c;\u514d\u5f97\u6b8b\u7559\u7684\u810f\u6570\u636e\u6df7\u8fdb\u53bb&#xff0c;\u628a\u901a\u4fe1\u6405\u4e71\u3002Linux\u73af\u5883\u4e0b&#xff0c;\u53ef\u4ee5\u7528\u4e0b\u9762\u8fd9\u4e24\u4e2a\u51fd\u6570&#xff1a;<\/p>\n<p>#include &lt;strings.h&gt;<\/p>\n<p>void bzero(void *s, size_t n);<br \/>\nvoid explicit_bzero(void *s, size_t n); <\/p>\n<ul>\n<li>bzero&#xff1a;\u628a\u6307\u5b9a\u5185\u5b58\u7a7a\u95f4\u7684\u524dn\u4e2a\u5b57\u8282\u5168\u90e8\u6e05\u96f6&#xff0c;\u4e5f\u5c31\u662f\u586b\u6210\\\\0\u3002<\/li>\n<li>explicit_bzero&#xff1a;\u529f\u80fd\u8ddfbzero\u4e00\u6837&#xff0c;\u4f46\u5b83\u80fd\u9632\u6b62\u7f16\u8bd1\u5668\u628a\u90a3\u4e9b\u201c\u770b\u8d77\u6765\u6ca1\u7528\u201d\u7684\u5185\u5b58\u6e05\u96f6\u64cd\u4f5c\u4f18\u5316\u6389\u3002\u5904\u7406\u654f\u611f\u6570\u636e\u3001\u6e05\u5b8c\u5c31\u4e0d\u8be5\u7559\u75d5\u7684\u573a\u666f&#xff0c;\u7528\u5b83\u66f4\u7a33\u59a5\u3002<\/li>\n<\/ul>\n<p>\u670d\u52a1\u5668\u521d\u59cb\u5316\u9636\u6bb5&#xff0c;\u5148\u628a\u5957\u63a5\u5b57\u5730\u5740\u7ed3\u6784\u4f53\u6e05\u96f6&#xff1a;<\/p>\n<p>struct sockaddr_in local;<br \/>\nbzero(&amp;local, sizeof(local)); <\/p>\n<p>\u5f53\u7136&#xff0c;\u7528memset\u6765\u8bbe\u7f6e\u4e5f\u4e00\u6837&#xff0c;\u6548\u679c\u5b8c\u5168\u76f8\u540c\u3002<\/p>\n<h6 id=\"2.1.2%20%E5%A1%AB%E5%85%85%20sockaddr_in%20%E4%B8%8E%E7%BD%91%E7%BB%9C%E5%AD%97%E8%8A%82%E5%BA%8F%E8%BD%AC%E6%8D%A2\">2.1.2 \u586b\u5145sockaddr_in\u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u6362<\/h6>\n<p>\u670d\u52a1\u5668\u7aef\u5f97\u660e\u786e\u544a\u8bc9\u5185\u6838\u4e09\u4ef6\u4e8b&#xff1a;\u76d1\u542c\u54ea\u4e2a\u534f\u8bae\u65cf\u3001\u7528\u54ea\u4e2a\u7aef\u53e3\u53f7\u3001\u7ed1\u54ea\u4e2aIP\u5730\u5740\u3002\u53ef\u4e3b\u673a\u548c\u7f51\u7edc\u7684\u5b57\u8282\u5e8f\u672a\u5fc5\u4e00\u81f4&#xff0c;\u5927\u7aef\u5c0f\u7aef&#xff0c;\u5404\u8bf4\u5404\u8bdd\u3002\u6240\u4ee5&#xff0c;\u4e3b\u673a\u6570\u636e\u5fc5\u987b\u5148\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef&#xff09;&#xff0c;\u624d\u80fd\u51fa\u95e8\u3002<\/p>\n<ul>\n<li>\n<p>\u7aef\u53e3\u53f7\u8f6c\u6362&#xff1a;\u7528htons(port)&#xff0c;\u628a\u4e3b\u673a\u5b57\u8282\u5e8f\u768416\u4f4d\u6574\u6570\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>IP \u5730\u5740\u8f6c\u6362&#xff1a;\u7528inet_addr(ip_str.c_str())&#xff0c;\u628a\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f\u768432\u4f4d\u6574\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p>local.sin_family &#061; AF_INET;<br \/>\nlocal.sin_port &#061; htons(_port);<br \/>\nlocal.sin_addr.s_addr &#061; inet_addr(_ip.c_str()); <\/p>\n<p>\u4e09\u884c\u586b\u5b8c&#xff0c;\u5730\u5740\u7ed3\u6784\u4f53\u7684\u201c\u7075\u9b42\u201d\u5c31\u9f50\u4e86\u3002\u534f\u8bae\u65cf\u3001\u7aef\u53e3\u3001IP&#xff0c;\u5404\u5c31\u5404\u4f4d\u3002<\/p>\n<h6 id=\"2.1.3%20%E7%BB%91%E5%AE%9A%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%EF%BC%9Abind%20%E5%87%BD%E6%95%B0\">2.1.3 \u7ed1\u5b9a\u672c\u5730\u5730\u5740&#xff1a;bind\u7cfb\u7edf\u8c03\u7528<\/h6>\n<p>\u5730\u5740\u7ed3\u6784\u4f53\u586b\u597d\u4e86&#xff0c;\u8fd8\u5f97\u628a\u5b83\u8ddf\u5957\u63a5\u5b57\u7ed1\u5728\u4e00\u8d77\u3002\u8fd9\u4e00\u6b65&#xff0c;\u9760bind()\u7cfb\u7edf\u8c03\u7528&#xff1a;<\/p>\n<p>int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); <\/p>\n<p>\u5b83\u505a\u7684\u4e8b\u60c5\u5f88\u76f4\u63a5&#xff1a;\u628a\u521b\u5efa\u597d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26_sockfd&#xff0c;\u8ddf\u672c\u5730\u7f51\u7edc\u5730\u5740&#xff08;IP &#043; \u7aef\u53e3&#xff09;\u5f3a\u5173\u8054\u8d77\u6765\u3002\u4ece\u6b64&#xff0c;\u8fd9\u4e2a\u5957\u63a5\u5b57\u5c31\u8ba4\u51c6\u4e86\u8fd9\u4e2a\u5730\u5740\u3002<\/p>\n<p>\u4ee3\u7801\u5b9e\u73b0\u4e0e\u6821\u9a8c\u903b\u8f91&#xff1a;<\/p>\n<p>int n &#061; bind(_sockfd, (struct sockaddr*)&amp;local, sizeof(local));<br \/>\nif (n &lt; 0) {<br \/>\n    LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n    exit(2);<br \/>\n} <\/p>\n<p>\u6ce8\u610f\u4e00\u4e2a\u7ec6\u8282&#xff1a;bind\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570&#xff0c;\u8981\u6c42\u662f\u901a\u7528\u5957\u63a5\u5b57\u6307\u9488\u7c7b\u578bstruct sockaddr*\u3002\u6240\u4ee5&#xff0c;\u4f20\u5165struct sockaddr_in\u7ed3\u6784\u4f53\u5730\u5740\u65f6&#xff0c;\u5f97\u505a\u4e00\u6b21\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u8fd9\u5176\u5b9e\u5c31\u662fC\u8bed\u8a00\u7248\u7684\u201c\u6307\u9488\u591a\u6001\u201d&#xff0c;\u5b50\u7c7b\u578b\u6307\u9488\u8f6c\u6210\u7236\u7c7b\u578b\u6307\u9488&#xff0c;\u51fd\u6570\u62ff\u5230\u901a\u7528\u5916\u58f3\u540e&#xff0c;\u6839\u636e\u5185\u90e8\u7684sin_family\u5b57\u6bb5&#xff0c;\u518d\u51b3\u5b9a\u6309\u54ea\u5957\u89c4\u5219\u89e3\u8bfb\u3002\u4f20\u7684\u65f6\u5019\u662fsockaddr_in&#xff0c;\u7528\u7684\u65f6\u5019\u8fd8\u662fsockaddr_in&#xff0c;\u4e2d\u95f4\u90a3\u5c42sockaddr*\u53ea\u662f\u4e2a\u901a\u7528\u5165\u53e3\u3002<\/p>\n<h5 id=\"2.2%20UDP%20%E5%85%A8%E5%8F%8C%E5%B7%A5%E9%80%9A%E4%BF%A1%E4%B8%8E%E6%95%B0%E6%8D%AE%E6%94%B6%E5%8F%91%E6%9C%BA%E5%88%B6\">2.2 UDP\u6570\u636e\u6536\u53d1\u673a\u5236&#xff1a;\u4ece\u63a5\u6536\u5230\u54cd\u5e94<\/h5>\n<p>UDP\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26sockfd&#xff0c;\u65e2\u80fd\u8bfb&#xff0c;\u4e5f\u80fd\u5199\u3002\u8bfb&#xff0c;\u5c31\u662f\u6536\u6570\u636e&#xff1b;\u5199&#xff0c;\u5c31\u662f\u53d1\u6570\u636e\u3002\u8fd9\u4e00\u8bfb\u4e00\u5199&#xff0c;\u5c31\u51b3\u5b9a\u4e86UDP\u901a\u4fe1\u662f\u5168\u53cc\u5de5&#xff08;Full-Duplex&#xff09;\u7684\u3002<\/p>\n<h6 id=\"2.2.1%20%E6%95%B0%E6%8D%AE%E6%8E%A5%E6%94%B6%E6%9C%BA%E5%88%B6%EF%BC%9Arecvfrom%20%E4%B8%8E%E9%98%BB%E5%A1%9E%20IO\">2.2.1 \u6570\u636e\u63a5\u6536&#xff1a;recvfrom\u4e0e\u963b\u585eI\/O<\/h6>\n<p>\u670d\u52a1\u7aef7\u00d724\u5c0f\u65f6\u8fde\u8f74\u8f6c&#xff0c;\u6b7b\u5faa\u73af\u91cc\u4e0d\u505c\u6b47&#xff0c;\u9760\u7684\u5c31\u662frecvfrom\u6293\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e\u3002<\/p>\n<p>ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,<br \/>\n                 struct sockaddr *src_addr, socklen_t *addrlen); <\/p>\n<p>\u963b\u585eIO\u7684\u884c\u4e3a\u7279\u5f81&#xff1a;\u5f53flags\u8bbe\u4e3a0\u65f6&#xff0c;recvfrom\u5c31\u662f\u963b\u585e\u5f0fIO&#xff08;Blocking IO&#xff09;\u3002\u5ba2\u6237\u7aef\u6ca1\u53d1\u6570\u636e&#xff1f;\u90a3\u670d\u52a1\u7aef\u8fdb\u7a0b\u5c31\u5728\u8fd9\u513f\u6302\u7740&#xff0c;\u4e00\u76f4\u7b49\u3002\u8ddfC\u8bed\u8a00\u91ccscanf\u7b49\u952e\u76d8\u8f93\u5165\u7684\u611f\u89c9\u4e00\u6a21\u4e00\u6837&#xff0c;\u6ca1\u8f93\u5165&#xff0c;\u5b83\u5c31\u4e0d\u5f80\u4e0b\u8d70\u3002\u76f4\u5230\u7f51\u7edc\u7f13\u51b2\u533a\u91cc\u6765\u4e86\u6570\u636e&#xff0c;\u5b83\u624d\u9192\u8fc7\u6765\u3002<\/p>\n<p>\u591a\u5bf9\u4e00\u901a\u4fe1&#xff1a;\u600e\u4e48\u77e5\u9053\u6570\u636e\u662f\u8c01\u53d1\u7684&#xff1f;<\/p>\n<p>\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668&#xff0c;\u901a\u5e38\u662f\u591a\u5bf9\u4e00\u7684\u5173\u7cfb\u3002\u4e00\u4e2a\u670d\u52a1\u5668&#xff0c;\u5bf9\u5e94\u4e00\u5806\u5ba2\u6237\u7aef\u3002\u670d\u52a1\u5668\u5904\u7406\u6570\u636e\u65f6&#xff0c;\u4e0d\u5149\u8981\u62ff\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u201c\u6587\u672c\u6570\u636e\u201d&#xff08;\u5b58\u5728buf\u91cc&#xff09;&#xff0c;\u8fd8\u5f97\u641e\u6e05\u695a&#xff0c;\u8fd9\u6570\u636e\u662f\u8c01\u53d1\u7684&#xff1f;\u5ba2\u6237\u7aef\u7684IP\u548c\u7aef\u53e3&#xff0c;\u4e00\u4e2a\u90fd\u4e0d\u80fd\u5c11\u3002<\/p>\n<ul>\n<li>src_addr&#xff1a;\u8f93\u51fa\u578b\u53c2\u6570\u3002\u5185\u6838\u6536\u6570\u636e\u7684\u65f6\u5019&#xff0c;\u4f1a\u628a\u5ba2\u6237\u7aef\u7684 struct sockaddr_in \u5730\u5740\u4fe1\u606f\u585e\u8fdb\u8fd9\u4e2a\u7ed3\u6784\u4f53\u3002<\/li>\n<li>addrlen&#xff1a;\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570\u3002\u4f20\u8fdb\u53bb\u65f6&#xff0c;\u8868\u793a\u4f20\u5165\u7ed3\u6784\u4f53\u7684\u5927\u5c0f&#xff0c;\u4e5f\u5c31\u662f sizeof(struct sockaddr_in)&#xff1b;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u5185\u6838\u628a\u5b83\u6539\u6210\u5b9e\u9645\u5199\u5165\u7684\u7ed3\u6784\u4f53\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<h6 id=\"2.2.2%20%E6%95%B0%E6%8D%AE%E5%8F%91%E9%80%81%E6%9C%BA%E5%88%B6%EF%BC%9Asendto%20%E5%87%BD%E6%95%B0\">2.2.2 \u6570\u636e\u53d1\u9001&#xff1a;sendto\u51fd\u6570<\/h6>\n<p>\u670d\u52a1\u5668\u8981\u7ed9\u5ba2\u6237\u7aef\u56de\u6d88\u606f&#xff0c;\u6216\u8005\u5ba2\u6237\u7aef\u8981\u7ed9\u670d\u52a1\u5668\u53d1\u6570\u636e&#xff0c;\u90fd\u7528sendto\u8fd9\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff1a;<\/p>\n<p>ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,<br \/>\n               const struct sockaddr *dest_addr, socklen_t addrlen); <\/p>\n<p>\u53c2\u6570\u8be6\u89e3<\/p>\n<ul>\n<li>sockfd&#xff1a;\u901a\u4fe1\u7528\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>buf\/len&#xff1a;\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u6307\u9488&#xff0c;\u4ee5\u53ca\u9700\u8981\u53d1\u9001\u7684\u6570\u636e\u957f\u5ea6\u3002<\/li>\n<li>flags&#xff1a;\u53d1\u9001\u63a7\u5236\u6807\u5fd7&#xff0c;\u901a\u5e38\u586b0\u3002<\/li>\n<li>dest_addr\/addrlen&#xff1a;\u76ee\u7684\u7aef\u7684\u5957\u63a5\u5b57\u5730\u5740\u4fe1\u606f&#xff08;\u7531struct sockaddr_in\u8f6c\u6362\u800c\u6765&#xff09;\u53ca\u5176\u7ed3\u6784\u4f53\u5927\u5c0f&#xff0c;\u660e\u786e\u6307\u5b9a\u6570\u636e\u53d1\u7ed9\u8c01\u3002<\/li>\n<\/ul>\n<p>\u8fd4\u56de\u503c<\/p>\n<p>\u6210\u529f\u65f6&#xff0c;\u8fd4\u56de\u5b9e\u9645\u53d1\u9001\u7684\u5b57\u8282\u6570&#xff1b;\u51fa\u9519\u65f6&#xff0c;\u8fd4\u56de-1&#xff0c;\u5e76\u8bbe\u7f6eerrno\u3002<\/p>\n<p>\u4e0d\u8fc7\u6709\u4e00\u70b9\u5f97\u8bf4\u6e05\u695a&#xff1a;UDP\u662f\u65e0\u8fde\u63a5\u7684\u3002sendto\u6210\u529f\u8c03\u7528&#xff0c;\u53ea\u610f\u5473\u7740\u6570\u636e\u88ab\u4ea4\u7ed9\u4e86\u64cd\u4f5c\u7cfb\u7edf\u7684\u7f51\u7edc\u6808\u53bb\u53d1\u9001&#xff0c;\u5e76\u4e0d\u4fdd\u8bc1\u5bf9\u7aef\u4e00\u5b9a\u80fd\u6536\u5230\u3002\u53d1\u51fa\u53bb&#xff0c;\u5c31\u7b97\u4ea4\u5dee\u4e86\u3002\u81f3\u4e8e\u8def\u4e0a\u4f1a\u4e0d\u4f1a\u4e22&#xff0c;\u80fd\u4e0d\u80fd\u5230&#xff0c;UDP\u672c\u8eab\u4e0d\u95fb\u4e0d\u95ee\u3002<\/p>\n<h5 id=\"2.3%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%AE%BF%E9%97%AE%E6%9C%BA%E5%88%B6%E4%B8%8E%E7%AB%AF%E5%8F%A3%E5%A4%84%E7%90%86\">2.3 \u5ba2\u6237\u7aef\u901a\u4fe1&#xff1a;\u670d\u52a1\u5668\u5730\u5740\u4e0e\u7aef\u53e3\u5206\u914d<\/h5>\n<p>\u5ba2\u6237\u7aef\u8981\u8bbf\u95eeUDP\u670d\u52a1\u7aef&#xff0c;\u9996\u5148\u5f97\u62ff\u5230\u670d\u52a1\u7aef\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\u8fd9\u662f\u6572\u95e8\u7816&#xff0c;\u6ca1\u6709\u5b83&#xff0c;\u8fde\u95e8\u90fd\u627e\u4e0d\u5230\u3002<\/p>\n<h6 id=\"2.3.1%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%9C%B0%E5%9D%80%E7%9A%84%E2%80%9C%E7%A1%AC%E7%BC%96%E7%A0%81%E2%80%9D%E4%B8%8E%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6\">2.3.1 \u670d\u52a1\u5668\u5730\u5740\u7684\u786c\u7f16\u7801\u4e0e\u914d\u7f6e\u6587\u4ef6\u7ba1\u7406<\/h6>\n<p>\u5ba2\u6237\u7aef\u600e\u4e48\u77e5\u9053\u670d\u52a1\u7aef\u7684IP\u548c\u7aef\u53e3&#xff1f;<\/p>\n<p>\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7a0b\u5e8f\u901a\u5e38\u51fa\u81ea\u540c\u4e00\u5bb6\u516c\u53f8&#xff0c;\u6216\u8005\u540c\u4e00\u4e2a\u5f00\u53d1\u56e2\u961f\u3002\u670d\u52a1\u7aef\u7684IP\u5730\u5740\u548c\u9ed8\u8ba4\u7aef\u53e3\u53f7&#xff0c;\u4e00\u822c\u4f1a\u76f4\u63a5\u786c\u7f16\u7801\u5728\u5ba2\u6237\u7aef\u4ee3\u7801\u91cc&#xff1b;\u4e5f\u53ef\u4ee5\u901a\u8fc7\u57df\u540d\u89e3\u6790&#xff08;DNS&#xff09;&#xff0c;\u6216\u8005\u4ece\u914d\u7f6e\u6587\u4ef6\u91cc\u52a8\u6001\u52a0\u8f7d\u3002\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u6765&#xff0c;\u5173\u952e\u662f\u628a\u5730\u5740\u5148\u5b9a\u4e0b\u6765\u3002<\/p>\n<h6 id=\"2.3.2%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84%E9%9A%90%E5%BC%8F%20Bind\">2.3.2 \u5ba2\u6237\u7aef\u7684\u9690\u5f0f\u7ed1\u5b9a&#xff1a;\u7aef\u53e3\u7531\u8c01\u5206\u914d<\/h6>\n<p>\u670d\u52a1\u7aef\u5fc5\u987b\u663e\u5f0f\u8c03\u7528bind()&#xff0c;\u628a\u56fa\u5b9a\u7aef\u53e3\u7ed1\u6b7b&#xff0c;\u4e0d\u7136\u5ba2\u6237\u7aef\u627e\u4e0d\u5230\u5b83\u3002\u53ef\u5ba2\u6237\u7aef\u4e0d\u4e00\u6837&#xff0c;\u901a\u5e38\u4e0d\u9700\u8981\u663e\u5f0fbind()\u3002<\/p>\n<p>\u539f\u56e0\u6709\u4e24\u70b9&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u663e\u5f0f\u7ed1\u5b9a\u56fa\u5b9a\u7aef\u53e3&#xff0c;\u5f88\u53ef\u80fd\u8ddf\u7528\u6237\u673a\u5668\u4e0a\u5176\u4ed6\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u649e\u7aef\u53e3&#xff0c;\u5bfc\u81f4\u5ba2\u6237\u7aef\u542f\u52a8\u5931\u8d25\u3002\u7528\u6237\u673a\u5668\u4e0a\u8dd1\u7740\u4ec0\u4e48&#xff0c;\u8c01\u4e5f\u8bf4\u4e0d\u51c6&#xff0c;\u786c\u7ed1\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u7b49\u4e8e\u7ed9\u81ea\u5df1\u57cb\u96f7\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u5ba2\u6237\u7aef\u5728\u7b2c\u4e00\u6b21\u8c03\u7528sendto()\u53d1\u9001\u6570\u636e\u5305\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u4f1a\u81ea\u52a8\u3001\u968f\u673a\u5730\u7ed9\u5b83\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u7a7a\u95f2\u7aef\u53e3\u53f7&#xff0c;\u987a\u5e26\u5b8c\u6210\u9690\u5f0f\u7ed1\u5b9a\u3002\u4e0d\u7528\u5ba2\u6237\u7aef\u64cd\u5fc3&#xff0c;\u5185\u6838\u5168\u5305\u4e86\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;\u670d\u52a1\u7aef\u7ed1\u5b9a\u662f\u4e3a\u4e86\u201c\u88ab\u627e\u5230\u201d&#xff0c;\u5ba2\u6237\u7aef\u4e0d\u7ed1\u5b9a\u662f\u4e3a\u4e86\u201c\u4e0d\u649e\u8f66\u201d\u3002\u4e00\u4e2a\u6c42\u7a33&#xff0c;\u4e00\u4e2a\u6c42\u6d3b\u3002<\/p>\n<h4 id=\"%E4%B8%89%E3%80%81Client%20%E7%AB%AF%E7%9A%84%20Bind%20%E7%AD%96%E7%95%A5%E4%B8%8E%20Server%20%E7%AB%AF%E7%9A%84%20Bind%20%E5%BF%85%E8%A6%81%E6%80%A7%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90\">\u4e09\u3001\u6df1\u5165\u7406\u89e3bind&#xff1a;\u5ba2\u6237\u7aef\u7b56\u7565\u4e0e\u670d\u52a1\u7aef\u9700\u6c42<\/h4>\n<p>\u524d\u6587\u628a UDP \u7684\u57fa\u7840 API \u548c\u6838\u5fc3\u901a\u4fe1\u6d41\u7a0b\u8bb2\u5b8c\u4e86\u3002\u63a5\u4e0b\u6765&#xff0c;\u6709\u51e0\u4e2a\u5173\u4e8e\u5957\u63a5\u5b57\u7ed1\u5b9a\u7684\u7ecf\u5178\u7591\u95ee&#xff0c;\u5f97\u597d\u597d\u63b0\u626f\u63b0\u626f&#xff1a;\u5ba2\u6237\u7aef\u5230\u5e95\u9700\u4e0d\u9700\u8981 bind&#xff1f;\u4e3a\u4ec0\u4e48\u5ba2\u6237\u7aef\u4e0d\u80fd\u663e\u5f0f bind&#xff1f;\u670d\u52a1\u5668\u53c8\u4e3a\u4ec0\u4e48\u5fc5\u987b\u663e\u5f0f bind&#xff1f;<\/p>\n<h5 id=\"3.1%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%A6%81%E4%B8%8D%E8%A6%81%20bind%EF%BC%9F\">3.1 UDP\u5ba2\u6237\u7aef\u662f\u5426\u9700\u8981\u663e\u5f0f\u8c03\u7528bind<\/h5>\n<p>\u7ed3\u8bba\u5148\u8bf4&#xff1a;\u5ba2\u6237\u7aef\u9700\u8981bind&#xff0c;\u4f46\u7edd\u5bf9\u4e0d\u9700\u8981\u201c\u663e\u5f0f&#xff08;\u624b\u52a8&#xff09;bind\u201d\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9700\u8981bind&#xff1f;<\/p>\n<p>\u4efb\u4f55\u7f51\u7edc\u901a\u4fe1&#xff0c;\u5728\u4f20\u8f93\u5c42\u90fd\u5f97\u660e\u786e\u6807\u8bc6\u53d1\u9001\u65b9\u548c\u63a5\u6536\u65b9\u3002IP\u5730\u5740\u8d1f\u8d23\u5728\u7f51\u7edc\u4e2d\u5b9a\u4f4d\u4e3b\u673a&#xff0c;\u7aef\u53e3\u53f7\u8d1f\u8d23\u5728\u4e3b\u673a\u4e0a\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u901a\u4fe1\u8fdb\u7a0b\u3002\u5ba2\u6237\u7aef\u53d1\u6570\u636e\u5305\u7684\u65f6\u5019&#xff0c;\u5fc5\u987b\u5e26\u4e0a\u201c\u6e90IP &#043; \u6e90\u7aef\u53e3\u201d&#xff0c;\u4e0d\u7136\u670d\u52a1\u5668\u5904\u7406\u5b8c\u8bf7\u6c42&#xff0c;\u538b\u6839\u4e0d\u77e5\u9053\u5f80\u54ea\u513f\u56de\u8bdd\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u7531\u64cd\u4f5c\u7cfb\u7edf\u9690\u5f0f\u5b8c\u6210&#xff1f;<\/p>\n<p>\u8fd9\u91cc\u9762\u6709\u4e24\u5c42\u8003\u8651\u3002<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u4fe1\u4efb\u7528\u6237\u3002 \u5982\u679c\u5141\u8bb8\u5ba2\u6237\u7aef\u4ee3\u7801\u663e\u5f0f\u5199\u6b7b\u7aef\u53e3\u53f7&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u6ca1\u6cd5\u4fdd\u8bc1\u8fd9\u4e2a\u7aef\u53e3\u5728\u7528\u6237\u673a\u5668\u4e0a\u6ca1\u88ab\u522b\u7684\u7a0b\u5e8f\u5360\u7740\u3002\u7aef\u53e3\u51b2\u7a81\u4e00\u65e6\u53d1\u751f&#xff0c;\u4e00\u4e2a\u7aef\u53e3\u53ea\u80fd\u88ab\u4e00\u4e2a\u8fdb\u7a0bbind&#xff0c;\u5ba2\u6237\u7aef\u7a0b\u5e8f\u5f53\u573a\u542f\u52a8\u5931\u8d25\u3002\u7528\u6237\u673a\u5668\u4e0a\u8dd1\u7740\u4ec0\u4e48&#xff0c;\u8c01\u4e5f\u8bf4\u4e0d\u51c6&#xff0c;\u786c\u7ed1\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u7b49\u4e8e\u7ed9\u81ea\u5df1\u57cb\u96f7\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u968f\u673a\u7aef\u53e3\u673a\u5236\u66f4\u7701\u5fc3\u3002 \u5ba2\u6237\u7aef\u7b2c\u4e00\u6b21\u8c03\u7528sendto()\u53d1\u6d88\u606f\u65f6&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u83b7\u53d6\u672c\u673aIP&#xff0c;\u518d\u4ece\u5f53\u524d\u7a7a\u95f2\u7684\u52a8\u6001\u7aef\u53e3\u6c60\u91cc&#xff0c;\u968f\u673a\u6311\u4e00\u4e2a\u6ca1\u88ab\u5360\u7528\u7684\u7aef\u53e3&#xff0c;\u5206\u914d\u7ed9\u8fd9\u4e2a\u5957\u63a5\u5b57&#xff0c;\u987a\u624b\u5b8c\u6210bind\u64cd\u4f5c\u3002<\/p>\n<p>\u7aef\u53e3\u503c\u5bf9\u5ba2\u6237\u7aef\u6765\u8bf4&#xff0c;\u65e0\u5173\u7d27\u8981\u3002 \u5177\u4f53\u5360\u54ea\u4e2a\u7aef\u53e3\u4e0d\u91cd\u8981&#xff0c;\u53ea\u8981\u5728\u8fd9\u6b21\u901a\u4fe1\u8fc7\u7a0b\u4e2d&#xff0c;\u8fd9\u4e2a\u7aef\u53e3\u662f\u552f\u4e00\u7684\u5c31\u884c\u3002\u5ba2\u6237\u7aef\u5728\u4e4e\u7684\u662f\u201c\u80fd\u53d1\u51fa\u53bb\u3001\u80fd\u6536\u56de\u6765\u201d&#xff0c;\u7aef\u53e3\u53f7\u662f\u51e0&#xff0c;\u65e0\u6240\u8c13\u3002<\/p>\n<h5 id=\"3.2%20%E4%B8%BA%E4%BB%80%E4%B9%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BF%85%E9%A1%BB%E6%98%BE%E5%BC%8F%20bind%EF%BC%9F\">3.2 \u4e3a\u4ec0\u4e48UDP\u670d\u52a1\u7aef\u901a\u5e38\u9700\u8981\u663e\u5f0f\u7ed1\u5b9a\u7aef\u53e3<\/h5>\n<p>\u8ddf\u5ba2\u6237\u7aef\u6b63\u597d\u76f8\u53cd&#xff0c;\u670d\u52a1\u5668\u7aef\u5fc5\u987b\u663e\u5f0f\u8c03\u7528bind()&#xff0c;\u6307\u5b9a\u56fa\u5b9a\u4e14\u4f17\u6240\u5468\u77e5\u7684IP\u548c\u7aef\u53e3\u53f7\u3002<\/p>\n<p>\u670d\u52a1\u5668\u7684\u5bf9\u5916\u670d\u52a1\u5c5e\u6027\u3002 \u670d\u52a1\u5668\u662f\u4e3a\u4e0d\u7279\u5b9a\u7684\u4f17\u591a\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\u7684\u3002\u5ba2\u6237\u7aef\u60f3\u4e3b\u52a8\u53d1\u8d77\u8bf7\u6c42&#xff0c;\u524d\u63d0\u662f&#xff0c;\u5b83\u5f97\u5728\u53d1\u9001\u4e4b\u524d&#xff0c;\u5c31\u77e5\u9053\u670d\u52a1\u5668\u7684IP\u548c\u7aef\u53e3\u53f7\u3002\u5730\u5740\u4e0d\u660e\u786e&#xff0c;\u5ba2\u6237\u7aef\u8fde\u95e8\u90fd\u627e\u4e0d\u5230\u3002<\/p>\n<p>\u4f17\u6240\u5468\u77e5&#xff0c;\u4e14\u4e0d\u53ef\u8f7b\u6613\u6539\u53d8\u3002 \u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7&#xff0c;\u5fc5\u987b\u662f\u56fa\u5b9a\u7684\u3002HTTP\u768480\u3001HTTPS\u7684443&#xff0c;\u90fd\u662f\u4f17\u6240\u5468\u77e5\u7684\u56fa\u5b9a\u670d\u52a1\u7aef\u53e3\u3002\u81ea\u5b9a\u4e49\u670d\u52a1\u4e5f\u4e00\u6837&#xff0c;\u7aef\u53e3\u4e00\u65e6\u5b9a\u4e0b\u6765&#xff0c;\u5c31\u4e0d\u80fd\u968f\u4fbf\u6539\u3002<\/p>\n<p>\u8981\u662f\u670d\u52a1\u5668\u6bcf\u6b21\u91cd\u542f&#xff0c;\u90fd\u7531\u64cd\u4f5c\u7cfb\u7edf\u968f\u673a\u5206\u914d\u7aef\u53e3&#xff0c;\u5ba2\u6237\u7aef\u5c31\u5f7b\u5e95\u61f5\u4e86&#xff0c;\u5b83\u4e0d\u77e5\u9053\u670d\u52a1\u5668\u8dd1\u5230\u54ea\u4e2a\u7aef\u53e3\u4e0a\u53bb\u4e86&#xff0c;\u8fde\u63a5\u6839\u672c\u5efa\u4e0d\u8d77\u6765\u3002<\/p>\n<p>\u540e\u679c\u5f88\u4e25\u91cd\u3002 \u5982\u679c\u670d\u52a1\u5668\u968f\u610f\u53d8\u66f4\u7aef\u53e3&#xff0c;\u6216\u8005\u538b\u6839\u6ca1\u505a\u663e\u5f0f bind&#xff0c;\u77ed\u65f6\u95f4\u5185&#xff0c;\u6240\u6709\u4f9d\u8d56\u8fd9\u4e2a\u7aef\u53e3\u7684\u5ba2\u6237\u7aef\u90fd\u4f1a\u6389\u7ebf&#xff0c;\u670d\u52a1\u76f4\u63a5\u4e2d\u65ad\u3002<\/p>\n<p>\u4e00\u4e2a\u7ed1\u5b9a&#xff0c;\u4e24\u79cd\u547d\u8fd0\u3002\u5ba2\u6237\u7aef\u6c42\u6d3b&#xff0c;\u670d\u52a1\u7aef\u6c42\u7a33\u3002\u6838\u5fc3\u5c31\u4e00\u53e5\u8bdd&#xff1a;\u670d\u52a1\u7aef\u8981\u201c\u88ab\u627e\u5230\u201d&#xff0c;\u5ba2\u6237\u7aef\u8981\u201c\u4e0d\u649e\u8f66\u201d\u3002<\/p>\n<h4 id=\"%E5%9B%9B%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%20IP%20%E7%BB%91%E5%AE%9A%E9%99%B7%E9%98%B1%E3%80%81INADDR_ANY%20%E5%8E%9F%E7%90%86%E4%B8%8E%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E6%BC%94%E8%BF%9B\">\u56db\u3001UDP\u670d\u52a1\u5668\u8fdb\u9636&#xff1a;\u5730\u5740\u7ed1\u5b9a\u3001\u4e1a\u52a1\u89e3\u8026\u4e0e\u8f6c\u6362\u63a5\u53e3<\/h4>\n<p>\u5b9e\u9645\u5f00\u53d1\u91cc&#xff0c;\u670d\u52a1\u5668\u7aef\u7684IP\u7ed1\u5b9a\u7b56\u7565\u85cf\u7740\u4e0d\u5c11\u5751\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u6df1\u5165\u5206\u6790\u4e91\u670d\u52a1\u5668\u73af\u5883\u4e0b\u7684IP\u7ed1\u5b9a\u7ea6\u675f\u3001INADDR_ANY\u7684\u5e95\u5c42\u903b\u8f91\u3001\u5e94\u7528\u5c42\u8f6f\u4ef6\u7684\u5206\u5c42\u8bbe\u8ba1&#xff0c;\u4ee5\u53ca\u5730\u5740\u8f6c\u6362\u51fd\u6570\u7684\u7ebf\u7a0b\u5b89\u5168\u6f14\u8fdb\u3002<\/p>\n<h5 id=\"4.1%20%E6%9C%8D%E5%8A%A1%E5%99%A8%20IP%20%E7%BB%91%E5%AE%9A%E7%9A%84%E2%80%9C%E5%9D%91%E2%80%9D%E4%B8%8E%20INADDR_ANY%20%E5%8E%9F%E7%90%86\">4.1 \u670d\u52a1\u5668\u5730\u5740\u7ed1\u5b9a\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u4e0eINADDR_ANY\u539f\u7406<\/h5>\n<p>\u5b9e\u9645\u90e8\u7f72UDP\u670d\u52a1\u5668\u65f6&#xff0c;\u76f4\u63a5\u6307\u5b9a\u5177\u4f53\u7684IP\u5730\u5740&#xff0c;\u4e0d\u7ba1\u662f\u516c\u7f51IP\u8fd8\u662f\u67d0\u4e2a\u5177\u4f53\u7684\u5185\u7f51IP&#xff0c;\u5f80\u5f80\u4f1a\u649e\u4e0a\u5404\u79cd\u7ed1\u5b9a\u9519\u8bef\u6216\u8005\u8bbf\u95ee\u9650\u5236\u3002<\/p>\n<p>                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502      \u4e91\u670d\u52a1\u5668 (\u4e3b\u673a)       \u2502<br \/>\n                    \u2502                           \u2502<br \/>\n  [\u5ba2\u6237\u7aef\u8bbf\u95ee] \u2500\u2500\u2500&gt; \u2502  \u7f51\u53611: \u672c\u5730\u73af\u56de 127.0.0.1 \u2502<br \/>\n                    \u2502  \u7f51\u53612: \u5185\u7f51 IP (\u79c1\u7f51)    \u2502<br \/>\n                    \u2502  (\u516c\u7f51 IP \u6302\u8f7d\u5728\u5916\u90e8\u7f51\u5173) \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                                  \u2502<br \/>\n                   \u63a8\u8350\u4f7f\u7528 INADDR_ANY (0.0.0.0)<br \/>\n                        \u76d1\u542c\u53d1\u9001\u5230\u672c\u673a\u6240\u6709\u7f51\u5361\u7684\u6570\u636e <\/p>\n<h6 id=\"4.1.1%20%E5%B8%B8%E8%A7%81%E7%9A%84%E7%BB%91%E5%AE%9A%E8%AF%AF%E5%8C%BA\">4.1.1 \u5e38\u89c1\u7684\u5730\u5740\u7ed1\u5b9a\u8bef\u533a<\/h6>\n<p>\u76f4\u63a5bind\u516c\u7f51IP\u2014\u2014\u5931\u8d25\u3002<\/p>\n<p>\u5728\u4e91\u5382\u5546&#xff08;\u963f\u91cc\u4e91\u3001\u817e\u8baf\u4e91\u8fd9\u4e9b&#xff09;\u63d0\u4f9b\u7684\u4e91\u670d\u52a1\u5668\u4e0a&#xff0c;\u7269\u7406\u7f51\u5361\u6216\u865a\u62df\u7f51\u5361\u4e0a\u914d\u7f6e\u7684&#xff0c;\u901a\u5e38\u662f\u5185\u7f51 IP&#xff08;\u79c1\u7f51IP&#xff09;\u3002\u516c\u7f51IP\u662f\u901a\u8fc7NAT&#xff08;\u7f51\u7edc\u5730\u5740\u8f6c\u6362&#xff09;\u6620\u5c04\u5230\u4e91\u670d\u52a1\u5668\u4e0a\u7684&#xff0c;\u5b83\u5e76\u6ca1\u6709\u76f4\u63a5\u914d\u7f6e\u5230\u4e91\u670d\u52a1\u5668\u7684\u672c\u5730\u7f51\u5361\u8bbe\u5907\u4e0a\u3002\u6240\u4ee5&#xff0c;\u5728\u4ee3\u7801\u91cc\u663e\u5f0fbind\u516c\u7f51IP&#xff0c;\u7cfb\u7edf\u4f1a\u76f4\u63a5\u62a5\u9519&#xff0c;\u7529\u4f60\u4e00\u53e5bind error\u3002<\/p>\n<p>bind 127.0.0.1\u6216\u7279\u5b9a\u5185\u7f51IP\u2014\u2014\u5c40\u9650\u6027\u592a\u5927\u3002<\/p>\n<p>\u5982\u679cbind\u4e86127.0.0.1&#xff08;\u672c\u5730\u73af\u56de\u5730\u5740&#xff09;&#xff0c;\u6570\u636e\u5305\u53ea\u80fd\u5728\u64cd\u4f5c\u7cfb\u7edf\u5185\u90e8\u8f6c\u5708&#xff0c;\u4ec5\u5141\u8bb8\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u7684\u5ba2\u6237\u7aef\u8bbf\u95ee\u3002\u5916\u754c\u8de8\u7f51\u7edc&#xff0c;\u6839\u672c\u8fdb\u4e0d\u6765\u3002<\/p>\n<p>\u5982\u679cbind\u4e86\u5177\u4f53\u7684\u5185\u7f51IP&#xff0c;\u90a3\u5ba2\u6237\u7aef\u7528127.0.0.1\u53d1\u6570\u636e&#xff0c;\u6536\u4e0d\u5230&#xff1b;\u53cd\u8fc7\u6765\u4e5f\u4e00\u6837\u3002\u663e\u5f0f\u7ed1\u5b9a\u67d0\u4e2a\u7279\u5b9a\u5730\u5740\u4e4b\u540e&#xff0c;\u5ba2\u6237\u7aef\u5c31\u5fc5\u987b\u4e14\u53ea\u80fd\u901a\u8fc7\u8fd9\u4e2a\u5730\u5740\u624d\u80fd\u6210\u529f\u8bbf\u95ee\u3002\u4e00\u6761\u8def\u8d70\u5230\u9ed1&#xff0c;\u522b\u7684\u95e8\u90fd\u5173\u4e86\u3002<\/p>\n<h6 id=\"4.1.2%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%EF%BC%9A%E4%BD%BF%E7%94%A8%20INADDR_ANY%EF%BC%88%E4%BB%BB%E6%84%8F%E5%9C%B0%E5%9D%80%E7%BB%91%E5%AE%9A%EF%BC%89\">4.1.2 \u4f7f\u7528INADDR_ANY&#xff1a;\u76d1\u542c\u672c\u673a\u7684\u591a\u4e2aIPv4\u5730\u5740<\/h6>\n<p>\u8981\u89e3\u51b3\u4e0a\u9762\u8fd9\u4e9b\u9ebb\u70e6&#xff0c;\u670d\u52a1\u5668\u521d\u59cb\u5316sockaddr_in\u7ed3\u6784\u4f53\u7684\u65f6\u5019&#xff0c;\u5c31\u522b\u7ed1\u5b9a\u5177\u4f53\u7684\u9759\u6001IP\u5730\u5740\u4e86&#xff0c;\u6539\u7528INADDR_ANY&#xff1a;<\/p>\n<p>\/\/ \u5728\u5b8f\u5b9a\u4e49\u4e2d&#xff0c;INADDR_ANY \u5373\u4e3a 0x00000000&#xff08;\u5168 0 \u5730\u5740&#xff09;<br \/>\n#define INADDR_ANY ((in_addr_t) 0x00000000)<\/p>\n<p>\/\/ \u670d\u52a1\u5668\u7aef\u521d\u59cb\u5316\u4ee3\u7801&#xff1a;<br \/>\nlocal.sin_addr.s_addr &#061; INADDR_ANY;  \/\/ \u81ea\u52a8\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u5168 0 <\/p>\n<p>INADDR_ANY \u7684\u4f18\u52bf\u4e0e\u5e95\u5c42\u539f\u7406&#xff1a;<\/p>\n<p>\u591a\u7f51\u5361\u76d1\u542c\u3002\u4e00\u53f0\u670d\u52a1\u5668\u53ef\u80fd\u6302\u8f7d\u591a\u5757\u7269\u7406\u7f51\u5361\u6216\u865a\u62df\u7f51\u5361&#xff0c;\u6bd4\u5982\u62e5\u6709\u5185\u7f51IP\u3001\u73af\u56de\u5730\u5740IP\u7b49\u3002\u6307\u5b9aINADDR_ANY\u4e4b\u540e&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u4f1a\u76d1\u542c\u6240\u6709\u5230\u8fbe\u8fd9\u53f0\u4e3b\u673a\u3001\u4e14\u76ee\u6807\u7aef\u53e3\u5339\u914d\u7684UDP\u6570\u636e\u5305&#xff0c;\u4e0d\u7ba1\u5b83\u662f\u4ece\u54ea\u5757\u7f51\u5361\u8fdb\u6765\u7684\u3002<\/p>\n<p>\u8de8\u7f51\u7edc\u8bbf\u95ee\u901a\u7545\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u5185\u7f51IP\u3001\u516c\u7f51IP\u7684NAT\u6620\u5c04&#xff0c;\u8fd8\u662f\u901a\u8fc7\u672c\u5730\u73af\u56de\u5730\u5740127.0.0.1\u8bbf\u95ee\u8fd9\u4e2a\u7aef\u53e3&#xff0c;\u670d\u52a1\u5668\u90fd\u80fd\u6b63\u5e38\u63a5\u6536\u5e76\u54cd\u5e94\u3002\u6765\u8005\u4e0d\u62d2&#xff0c;\u6761\u6761\u5927\u8def\u901a\u7f57\u9a6c\u3002<\/p>\n<h5 id=\"4.2%20%E8%BD%AF%E4%BB%B6%E5%88%86%E5%B1%82%E8%AE%BE%E8%AE%A1%EF%BC%9AUDP%20%E6%9C%8D%E5%8A%A1%E4%B8%8E%E4%B8%9A%E5%8A%A1%E9%80%BB%E8%BE%91%E8%A7%A3%E8%80%A6\">4.2 \u8f6f\u4ef6\u5206\u5c42\u8bbe\u8ba1&#xff1a;\u5b9e\u73b0UDP\u901a\u4fe1\u4e0e\u4e1a\u52a1\u903b\u8f91\u89e3\u8026<\/h5>\n<p>\u5199\u9ad8\u53ef\u9760\u7684\u670d\u52a1\u5668\u7a0b\u5e8f&#xff0c;\u7f51\u7edc\u901a\u4fe1\u903b\u8f91&#xff08;\u6536\u53d1\u6570\u636e&#xff09;\u548c\u4e0a\u5c42\u4e1a\u52a1\u5904\u7406&#xff08;\u8ba1\u7b97\u3001\u6570\u636e\u5e93\u4ea4\u4e92\u7b49&#xff09;\u5e94\u8be5\u89e3\u8026&#xff08;Decoupling&#xff09;\u3002<\/p>\n<h6 id=\"4.2.1%20%E4%BD%BF%E7%94%A8%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%E5%A4%84%E7%90%86%E4%B8%9A%E5%8A%A1%E9%80%BB%E8%BE%91\">4.2.1 \u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u4e1a\u52a1\u903b\u8f91<\/h6>\n<p>\u670d\u52a1\u5668\u7684\u6838\u5fc3\u5de5\u4f5c\u5faa\u73af&#xff0c;\u5206\u4e09\u6b65&#xff1a;<\/p>\n<li>\n<p>\u63a5\u6536\u6570\u636e&#xff1a;\u901a\u8fc7recvfrom\u83b7\u53d6\u5ba2\u6237\u7aef\u6570\u636e\u5305&#xff0c;\u4ee5\u53ca\u5ba2\u6237\u7aef\u7684sockaddr_in\u5730\u5740&#xff08;\u5bf9\u7aef\u4fe1\u606f&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e1a\u52a1\u5904\u7406&#xff1a;\u628a\u63a5\u6536\u5230\u7684\u539f\u59cb\u6570\u636e&#xff0c;\u4f20\u7ed9\u4e0a\u5c42\u56de\u8c03\u51fd\u6570\u6216\u8005\u4e1a\u52a1\u903b\u8f91\u6a21\u5757\u53bb\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u54cd\u5e94\u53d1\u9001&#xff1a;\u901a\u8fc7sendto&#xff0c;\u628a\u4e0a\u5c42\u5904\u7406\u5b8c\u6bd5\u7684\u7ed3\u679c\u53d1\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<\/li>\n<h6 id=\"4.2.2%20%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%E6%A8%A1%E5%BC%8F%E5%AE%9E%E7%8E%B0\">4.2.2 \u56de\u8c03\u51fd\u6570\u6a21\u5f0f\u5b9e\u73b0<\/h6>\n<p>UDP\u670d\u52a1\u5668\u5728\u7c7b\u5b9a\u4e49\u91cc\u7ef4\u62a4\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61&#xff0c;\u6bd4\u5982std::function&lt;std::string(const std::string&amp;)&gt;&#xff0c;\u628a\u6570\u636e\u5904\u7406\u7684\u5177\u4f53\u903b\u8f91\u4ea4\u7ed9\u5916\u90e8\u4e0a\u5c42\u4f20\u5165&#xff1a;<\/p>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e1a\u52a1\u5904\u7406\u51fd\u6570\u7c7b\u578b<br \/>\nusing func_t &#061; std::function&lt;std::string(const std::string&amp;)&gt;;<\/p>\n<p>class UdpServer {<br \/>\npublic:<br \/>\n    UdpServer(uint16_t port, func_t func)<br \/>\n        : _port(port), _func(func), _isrunning(false) {}<\/p>\n<p>    void Start() {<br \/>\n        _isrunning &#061; true;<br \/>\n        char buffer[1024];<\/p>\n<p>        while (_isrunning) {<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<\/p>\n<p>            \/\/ 1. \u63a5\u6536\u6d88\u606f<br \/>\n            ssize_t s &#061; recvfrom(_sockfd, buffer, sizeof(buffer) &#8211; 1, 0,<br \/>\n                                 (struct sockaddr*)&amp;peer, &amp;len);<br \/>\n            if (s &gt; 0) {<br \/>\n                buffer[s] &#061; 0;  \/\/ \u624b\u52a8\u8865\u5145\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7b26<br \/>\n                std::string request &#061; buffer;<\/p>\n<p>                \/\/ 2. \u8f6f\u4ef6\u5206\u5c42&#xff1a;\u4ea4\u7ed9\u4e0a\u5c42\u4e1a\u52a1\u56de\u8c03\u5904\u7406<br \/>\n                std::string response &#061; _func(request);<\/p>\n<p>                \/\/ 3. \u53d1\u9001\u54cd\u5e94\u7ed3\u679c\u7ed9\u5ba2\u6237\u7aef<br \/>\n                sendto(_sockfd, response.c_str(), response.size(), 0,<br \/>\n                       (struct sockaddr*)&amp;peer, len);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _sockfd;<br \/>\n    uint16_t _port;<br \/>\n    bool _isrunning;<br \/>\n    func_t _func;  \/\/ \u4e0a\u5c42\u56de\u8c03\u51fd\u6570<br \/>\n}; <\/p>\n<h5 id=\"4.3%20%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E7%9A%84%E6%BC%94%E8%BF%9B%E4%B8%8E%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E6%80%A7%E5%88%86%E6%9E%90\">4.3 IP\u5730\u5740\u8f6c\u6362\u51fd\u6570\u7684\u6f14\u8fdb\u4e0e\u7ebf\u7a0b\u5b89\u5168\u6027<\/h5>\n<p>\u5904\u7406IP\u5730\u5740\u7684\u5b57\u7b26\u4e32\u683c\u5f0f&#xff08;\u70b9\u5206\u5341\u8fdb\u5236&#xff0c;\u5982&#034;192.168.1.1&#034;&#xff09;\u548c\u7f51\u7edc\u5b57\u8282\u5e8f\u4e8c\u8fdb\u5236\u6574\u6570&#xff08;in_addr_t&#xff09;\u4e4b\u95f4\u7684\u8f6c\u6362\u65f6&#xff0c;Linux C API\u7ecf\u5386\u4e86\u4e00\u8f6e\u4ece\u4f20\u7edf\u65e7API\u5230\u73b0\u4ee3\u5316POSIX API\u7684\u6f14\u8fdb\u3002<\/p>\n<p>[\u5b57\u7b26\u4e32 IP &#034;192.168.1.1&#034;] \u00a0\u2500\u2500inet_pton \/ inet_addr\u2500\u2500&gt; \u00a0[\u7f51\u7edc\u5b57\u8282\u5e8f 32 \u4f4d\u6574\u6570] [\u5b57\u7b26\u4e32 IP &#034;192.168.1.1&#034;] \u00a0&lt;\u2500\u2500inet_ntop \/ inet_ntoa\u2500\u2500 \u00a0[\u7f51\u7edc\u5b57\u8282\u5e8f 32 \u4f4d\u6574\u6570]<\/p>\n<h6 id=\"4.3.1%20%E6%97%A7%E7%89%88%20API%20%E5%8F%8A%E5%85%B6%E7%BC%BA%E9%99%B7\">4.3.1 \u65e7\u7248\u5730\u5740\u8f6c\u6362API\u53ca\u5176\u5c40\u9650<\/h6>\n<p>\u2460inet_addr<\/p>\n<ul>\n<li>\n<p>\u529f\u80fd&#xff1a;\u628a\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6210in_addr_t\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c40\u9650&#xff1a;\u53ea\u8ba4IPv4&#xff1b;\u78b0\u4e0a\u5e7f\u64ad\u5730\u5740255.255.255.255&#xff0c;\u5b83\u8fd4\u56de-1&#xff08;\u5373INADDR_NONE&#xff09;&#xff0c;\u8fd9\u5c31\u6709\u4e86\u6b67\u4e49\u2014\u2014\u5230\u5e95\u662f\u5730\u5740\u65e0\u6548&#xff0c;\u8fd8\u662f\u771f\u5c31\u662f\u4e2a\u5e7f\u64ad\u5730\u5740&#xff1f;\u5206\u4e0d\u6e05&#xff0c;\u9519\u8bef\u5904\u7406\u4e5f\u96be\u505a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u2461 inet_ntoa\u4e0e\u7ebf\u7a0b\u5b89\u5168\u9690\u60a3<\/p>\n<ul>\n<li>\n<p>\u529f\u80fd&#xff1a;\u628a4\u5b57\u8282\u7f51\u7edc\u5b57\u8282\u5e8fIP\u8f6c\u6210\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>\u81f4\u547d\u7f3a\u9677&#xff08;\u7ebf\u7a0b\u4e0d\u5b89\u5168&#xff09;&#xff1a;inet_ntoa\u5185\u90e8\u7528\u4e86\u4e00\u4e2a\u9759\u6001\u5c40\u90e8\u7f13\u51b2\u533a&#xff08;static buffer&#xff09;\u6765\u5b58\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002\u591a\u7ebf\u7a0b\u5e76\u53d1\u73af\u5883\u4e0b&#xff0c;\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03inet_ntoa&#xff0c;\u540e\u9762\u7684\u7ebf\u7a0b\u4f1a\u628a\u524d\u9762\u7ebf\u7a0b\u7f13\u5b58\u5728\u9759\u6001\u533a\u91cc\u7684IP\u5b57\u7b26\u4e32\u8986\u76d6\u6389&#xff0c;\u5f15\u53d1\u4e25\u91cd\u7684\u5e76\u53d1\u6570\u636e\u7ade\u4e89\u3002<\/p>\n<\/li>\n<\/ul>\n<h6 id=\"4.3.2%20%E7%8E%B0%E4%BB%A3%E5%8C%96%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%20API%EF%BC%9Ainet_pton%20%E4%B8%8E%20inet_ntop\">4.3.2 \u73b0\u4ee3\u5730\u5740\u8f6c\u6362API&#xff1a;inet_pton\u4e0einet_ntop<\/h6>\n<p>\u73b0\u4ee3\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u63a8\u8350\u7528inet_pton&#xff08;Presentation to Network&#xff09;\u548cinet_ntop&#xff08;Network to Presentation&#xff09;\u3002<\/p>\n<p>API\u63a5\u53e3\u58f0\u660e&#xff1a;<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<\/p>\n<p>\/\/ \u5b57\u7b26\u4e32 IP -&gt; \u4e8c\u8fdb\u5236\u7f51\u7edc\u5b57\u8282\u5e8f IP<br \/>\nint inet_pton(int af, const char *src, void *dst);<\/p>\n<p>\/\/ \u4e8c\u8fdb\u5236\u7f51\u7edc\u5b57\u8282\u5e8f IP -&gt; \u5b57\u7b26\u4e32 IP<br \/>\nconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size); <\/p>\n<p>\u4e24\u5927\u6838\u5fc3\u4f18\u52bf&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u652f\u6301\u534f\u8bae\u65cf&#xff1a;\u7b2c\u4e00\u4e2a\u53c2\u6570af\u53ef\u4ee5\u663e\u5f0f\u6307\u5b9aAF_INET&#xff08;IPv4&#xff09;\u6216AF_INET6&#xff08;IPv6&#xff09;&#xff0c;\u6269\u5c55\u6027\u62c9\u6ee1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7edd\u5bf9\u7ebf\u7a0b\u5b89\u5168&#xff1a;inet_ntop\u8981\u6c42\u8c03\u7528\u8005\u663e\u5f0f\u4f20\u5165\u4e00\u4e2a\u7531\u8c03\u7528\u65b9\u7ef4\u62a4\u7684\u5185\u5b58\u7f13\u51b2\u533a\u6307\u9488&#xff08;\u6bd4\u5982\u6808\u4e0a\u5206\u914d\u7684char ipbuffer[64]&#xff09;\u3002\u5b83\u4e0d\u4f9d\u8d56\u5185\u90e8\u9759\u6001\u7f13\u51b2\u533a&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u81ea\u5df1\u7684\u6808\u5e27\u7a7a\u95f4\u91cc\u64cd\u4f5c&#xff0c;\u7ebf\u7a0b\u5b89\u5168\u9690\u60a3\u4ece\u6839\u4e0a\u88ab\u6d88\u9664\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5c01\u88c5\u73b0\u4ee3 IP \u5730\u5740\u8f6c\u6362\u7c7b\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<\/p>\n<p>class InetAddr {<br \/>\npublic:<br \/>\n    \/\/ \u4ece sockaddr_in \u7ed3\u6784\u63d0\u53d6 IP \u548c\u7aef\u53e3<br \/>\n    InetAddr(const struct sockaddr_in &amp;addr) : _addr(addr) {<br \/>\n        _port &#061; ntohs(_addr.sin_port);<\/p>\n<p>        \/\/ \u663e\u5f0f\u5728\u6808\u4e0a\u521b\u5efa\u7f13\u51b2\u533a&#xff0c;\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684 inet_ntop<br \/>\n        char ipbuffer[64];<br \/>\n        inet_ntop(AF_INET, &amp;_addr.sin_addr, ipbuffer, sizeof(ipbuffer));<br \/>\n        _ip &#061; ipbuffer;<br \/>\n    }<\/p>\n<p>    \/\/ \u4ece IP \u548c Port \u6784\u5efa sockaddr_in \u7ed3\u6784<br \/>\n    InetAddr(const std::string &amp;ip, uint16_t port) : _ip(ip), _port(port) {<br \/>\n        memset(&amp;_addr, 0, sizeof(_addr));<br \/>\n        _addr.sin_family &#061; AF_INET;<br \/>\n        _addr.sin_port &#061; htons(_port);<\/p>\n<p>        \/\/ \u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684 inet_pton<br \/>\n        inet_pton(AF_INET, _ip.c_str(), &amp;_addr.sin_addr);<br \/>\n    }<\/p>\n<p>    std::string Ip() const { return _ip; }<br \/>\n    uint16_t Port() const { return _port; }<br \/>\n    struct sockaddr_in GetAddr() const { return _addr; }<\/p>\n<p>private:<br \/>\n    struct sockaddr_in _addr;<br \/>\n    std::string _ip;<br \/>\n    uint16_t _port;<br \/>\n}; <\/p>\n<p>\u7528inet_pton\u548cinet_ntop\u642d\u914d\u6808\u7f13\u51b2\u533a\u6216\u8005\u5c01\u88c5\u7c7b&#xff0c;\u65e2\u5b8c\u7f8e\u652f\u6301IPv4\/IPv6\u6269\u5c55&#xff0c;\u53c8\u80fd\u786e\u4fdd\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u5e94\u7528\u4e0b\u7684\u7edd\u5bf9\u5b89\u5168\u3002\u65e7API\u90a3\u70b9\u5751&#xff0c;\u5230\u6b64\u5c31\u7b97\u5f7b\u5e95\u7ed5\u8fc7\u53bb\u4e86\u3002<\/p>\n<h4 id=\"%E4%BA%94%E3%80%81%E9%99%84%E4%B8%8A%E6%BA%90%E7%A0%81\">\u4e94\u3001\u5b8c\u6574\u6e90\u7801\u4e0e\u8fd0\u884c\u6d4b\u8bd5<\/h4>\n<p>\u4e0b\u9762\u628a\u6574\u5957UDP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684\u5b8c\u6574\u4ee3\u7801\u6574\u7406\u51fa\u6765&#xff0c;\u5305\u542bIP\u5730\u5740\u5c01\u88c5\u7c7b\u3001UDP\u670d\u52a1\u5668\u7c7b\u3001\u670d\u52a1\u7aef\u4e3b\u7a0b\u5e8f\u3001\u5ba2\u6237\u7aef\u4e3b\u7a0b\u5e8f&#xff0c;\u4ee5\u53caMakefile\u3002\u4ee3\u7801\u53ef\u76f4\u63a5\u7f16\u8bd1\u8fd0\u884c\u3002<\/p>\n<h5 id=\"5.1%20InetAddr.hpp%EF%BC%88IP%20%E5%9C%B0%E5%9D%80%E5%B0%81%E8%A3%85%E7%B1%BB%EF%BC%89\">5.1 InetAddr.hpp&#xff1a;IP\u5730\u5740\u5c01\u88c5\u7c7b<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>namespace NetModule<br \/>\n{<br \/>\n    class InetAddr<br \/>\n    {<br \/>\n    public:<br \/>\n        \/\/ \u4ece sockaddr_in \u7ed3\u6784\u63d0\u53d6 IP \u548c\u7aef\u53e3&#xff08;\u7528\u4e8e\u670d\u52a1\u7aef\u63a5\u6536\u5ba2\u6237\u7aef\u5730\u5740&#xff09;<br \/>\n        InetAddr(const struct sockaddr_in &amp;addr)<br \/>\n            : _addr(addr)<br \/>\n        {<br \/>\n            _port &#061; ntohs(_addr.sin_port);<\/p>\n<p>            \/\/ \u663e\u5f0f\u5728\u6808\u4e0a\u521b\u5efa\u7f13\u51b2\u533a&#xff0c;\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684 inet_ntop<br \/>\n            char ipbuffer[64];<br \/>\n            inet_ntop(AF_INET, &amp;_addr.sin_addr, ipbuffer, sizeof(ipbuffer));<br \/>\n            _ip &#061; ipbuffer;<br \/>\n        }<\/p>\n<p>        \/\/ \u4ece IP \u548c Port \u6784\u5efa sockaddr_in \u7ed3\u6784&#xff08;\u7528\u4e8e\u5ba2\u6237\u7aef\u6307\u5b9a\u670d\u52a1\u7aef\u5730\u5740&#xff09;<br \/>\n        InetAddr(const std::string &amp;ip, uint16_t port)<br \/>\n            : _ip(ip), _port(port)<br \/>\n        {<br \/>\n            memset(&amp;_addr, 0, sizeof(_addr));<br \/>\n            _addr.sin_family &#061; AF_INET;<br \/>\n            _addr.sin_port &#061; htons(_port);<\/p>\n<p>            \/\/ \u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684 inet_pton<br \/>\n            inet_pton(AF_INET, _ip.c_str(), &amp;_addr.sin_addr);<br \/>\n        }<\/p>\n<p>        std::string Ip() const { return _ip; }<br \/>\n        uint16_t Port() const { return _port; }<br \/>\n        struct sockaddr_in GetAddr() const { return _addr; }<\/p>\n<p>        ~InetAddr() {}<\/p>\n<p>    private:<br \/>\n        struct sockaddr_in _addr;<br \/>\n        std::string _ip;<br \/>\n        uint16_t _port;<br \/>\n    };<br \/>\n} <\/p>\n<h4 id=\"5.2%20UdpServer.hpp%EF%BC%9AUDP%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%B1%BB\">5.2 UdpServer.hpp&#xff1a;UDP\u670d\u52a1\u5668\u7c7b<\/h4>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<\/p>\n<p>#include &#034;InetAddr.hpp&#034;<\/p>\n<p>namespace NetModule<br \/>\n{<br \/>\n    \/\/ \u4e1a\u52a1\u5904\u7406\u51fd\u6570\u7c7b\u578b&#xff1a;\u8f93\u5165\u8bf7\u6c42\u5b57\u7b26\u4e32&#xff0c;\u8fd4\u56de\u54cd\u5e94\u5b57\u7b26\u4e32<br \/>\n    using func_t &#061; std::function&lt;std::string(const std::string &amp;)&gt;;<\/p>\n<p>    const uint16_t DEFAULT_PORT &#061; 8080;<br \/>\n    const int BACKLOG &#061; 10;<\/p>\n<p>    class UdpServer<br \/>\n    {<br \/>\n    public:<br \/>\n        UdpServer(uint16_t port &#061; DEFAULT_PORT, func_t func &#061; nullptr)<br \/>\n            : _port(port), _func(func), _isrunning(false), _sockfd(-1)<br \/>\n        {<br \/>\n        }<\/p>\n<p>        \/\/ \u521d\u59cb\u5316&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57 &#043; \u7ed1\u5b9a\u5730\u5740<br \/>\n        void Init()<br \/>\n        {<br \/>\n            \/\/ 1. \u521b\u5efa UDP \u5957\u63a5\u5b57<br \/>\n            _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(1);<br \/>\n            }<br \/>\n            std::cout &lt;&lt; &#034;socket create success, sockfd: &#034; &lt;&lt; _sockfd &lt;&lt; std::endl;<\/p>\n<p>            \/\/ 2. \u586b\u5145\u672c\u5730\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n            struct sockaddr_in local;<br \/>\n            memset(&amp;local, 0, sizeof(local));<br \/>\n            local.sin_family &#061; AF_INET;<br \/>\n            local.sin_port &#061; htons(_port);<br \/>\n            local.sin_addr.s_addr &#061; INADDR_ANY;  \/\/ \u76d1\u542c\u6240\u6709\u7f51\u5361<\/p>\n<p>            \/\/ 3. \u7ed1\u5b9a\u5730\u5740<br \/>\n            int n &#061; bind(_sockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n            if (n &lt; 0)<br \/>\n            {<br \/>\n                std::cerr &lt;&lt; &#034;bind error&#034; &lt;&lt; std::endl;<br \/>\n                exit(2);<br \/>\n            }<br \/>\n            std::cout &lt;&lt; &#034;bind success, port: &#034; &lt;&lt; _port &lt;&lt; std::endl;<br \/>\n        }<\/p>\n<p>        \/\/ \u542f\u52a8\u670d\u52a1\u5668&#xff1a;\u6b7b\u5faa\u73af\u63a5\u6536 &#043; \u5904\u7406 &#043; \u54cd\u5e94<br \/>\n        void Start()<br \/>\n        {<br \/>\n            _isrunning &#061; true;<br \/>\n            char buffer[1024];<\/p>\n<p>            while (_isrunning)<br \/>\n            {<br \/>\n                struct sockaddr_in peer;<br \/>\n                socklen_t len &#061; sizeof(peer);<\/p>\n<p>                \/\/ 1. \u63a5\u6536\u6d88\u606f<br \/>\n                ssize_t s &#061; recvfrom(_sockfd, buffer, sizeof(buffer) &#8211; 1, 0,<br \/>\n                                     (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n                if (s &lt; 0)<br \/>\n                {<br \/>\n                    std::cerr &lt;&lt; &#034;recvfrom error&#034; &lt;&lt; std::endl;<br \/>\n                    continue;<br \/>\n                }<\/p>\n<p>                buffer[s] &#061; 0;  \/\/ \u624b\u52a8\u8865\u5145\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7b26<br \/>\n                std::string request &#061; buffer;<\/p>\n<p>                \/\/ \u6784\u9020\u5bf9\u7aef\u5730\u5740\u5bf9\u8c61&#xff0c;\u65b9\u4fbf\u6253\u5370\u548c\u540e\u7eed\u4f7f\u7528<br \/>\n                InetAddr peerAddr(peer);<br \/>\n                std::cout &lt;&lt; &#034;[&#034; &lt;&lt; peerAddr.Ip() &lt;&lt; &#034;:&#034; &lt;&lt; peerAddr.Port() &lt;&lt; &#034;]# &#034;<br \/>\n                          &lt;&lt; request &lt;&lt; std::endl;<\/p>\n<p>                \/\/ 2. \u8f6f\u4ef6\u5206\u5c42&#xff1a;\u4ea4\u7ed9\u4e0a\u5c42\u4e1a\u52a1\u56de\u8c03\u5904\u7406<br \/>\n                std::string response;<br \/>\n                if (_func)<br \/>\n                {<br \/>\n                    response &#061; _func(request);<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    response &#061; &#034;Server received: &#034; &#043; request;<br \/>\n                }<\/p>\n<p>                \/\/ 3. \u53d1\u9001\u54cd\u5e94\u7ed3\u679c\u7ed9\u5ba2\u6237\u7aef<br \/>\n                sendto(_sockfd, response.c_str(), response.size(), 0,<br \/>\n                       (struct sockaddr *)&amp;peer, len);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        ~UdpServer()<br \/>\n        {<br \/>\n            if (_sockfd &gt;&#061; 0)<br \/>\n            {<br \/>\n                close(_sockfd);<br \/>\n            }<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _sockfd;<br \/>\n        uint16_t _port;<br \/>\n        bool _isrunning;<br \/>\n        func_t _func;  \/\/ \u4e0a\u5c42\u56de\u8c03\u51fd\u6570<br \/>\n    };<br \/>\n} <\/p>\n<h5 id=\"5.3%20UdpServer.cc%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89\">5.3 UdpServer.cc&#xff1a;\u670d\u52a1\u7aef\u4e3b\u7a0b\u5e8f<\/h5>\n<p>#include &#034;UdpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>\/\/ \u4e1a\u52a1\u5904\u7406\u56de\u8c03&#xff1a;\u628a\u6536\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6210\u5927\u5199\u518d\u8fd4\u56de<br \/>\nstd::string Handler(const std::string &amp;request)<br \/>\n{<br \/>\n    std::string response &#061; request;<br \/>\n    for (auto &amp;ch : response)<br \/>\n    {<br \/>\n        if (ch &gt;&#061; &#039;a&#039; &amp;&amp; ch &lt;&#061; &#039;z&#039;)<br \/>\n        {<br \/>\n            ch -&#061; 32;<br \/>\n        }<br \/>\n    }<br \/>\n    return response;<br \/>\n}<\/p>\n<p>\/\/ \u7528\u6cd5&#xff1a;.\/udp_server [\u7aef\u53e3\u53f7]<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    uint16_t port &#061; NetModule::DEFAULT_PORT;<\/p>\n<p>    if (argc &#061;&#061; 2)<br \/>\n    {<br \/>\n        port &#061; static_cast&lt;uint16_t&gt;(std::stoi(argv[1]));<br \/>\n    }<\/p>\n<p>    std::unique_ptr&lt;NetModule::UdpServer&gt; server &#061;<br \/>\n        std::make_unique&lt;NetModule::UdpServer&gt;(port, Handler);<\/p>\n<p>    server-&gt;Init();<br \/>\n    server-&gt;Start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h5 id=\"5.4%20UdpClient.cc%EF%BC%88%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89\">5.4 UdpClient.cc&#xff1a;\u5ba2\u6237\u7aef\u4e3b\u7a0b\u5e8f<\/h5>\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;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<\/p>\n<p>\/\/ \u7528\u6cd5&#xff1a;.\/udp_client [\u670d\u52a1\u7aefIP] [\u670d\u52a1\u7aef\u7aef\u53e3]<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 3)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; server_ip server_port&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    std::string server_ip &#061; argv[1];<br \/>\n    uint16_t server_port &#061; static_cast&lt;uint16_t&gt;(std::stoi(argv[2]));<\/p>\n<p>    \/\/ 1. \u521b\u5efa UDP \u5957\u63a5\u5b57&#xff08;\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u663e\u5f0f bind&#xff0c;\u7531\u5185\u6838\u9690\u5f0f\u5b8c\u6210&#xff09;<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        return 2;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u586b\u5145\u670d\u52a1\u7aef\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n    struct sockaddr_in server;<br \/>\n    memset(&amp;server, 0, sizeof(server));<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_port &#061; htons(server_port);<br \/>\n    inet_pton(AF_INET, server_ip.c_str(), &amp;server.sin_addr);<\/p>\n<p>    \/\/ 3. \u5faa\u73af\u53d1\u9001 &#043; \u63a5\u6536<br \/>\n    while (true)<br \/>\n    {<br \/>\n        std::string line;<br \/>\n        std::cout &lt;&lt; &#034;Please Enter# &#034;;<br \/>\n        std::getline(std::cin, line);<\/p>\n<p>        if (line &#061;&#061; &#034;quit&#034; || line &#061;&#061; &#034;exit&#034;)<br \/>\n        {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u53d1\u9001\u6570\u636e\u7ed9\u670d\u52a1\u7aef<br \/>\n        sendto(sockfd, line.c_str(), line.size(), 0,<br \/>\n               (struct sockaddr *)&amp;server, sizeof(server));<\/p>\n<p>        \/\/ \u63a5\u6536\u670d\u52a1\u7aef\u54cd\u5e94<br \/>\n        char buffer[1024];<br \/>\n        struct sockaddr_in peer;<br \/>\n        socklen_t len &#061; sizeof(peer);<\/p>\n<p>        ssize_t s &#061; recvfrom(sockfd, buffer, sizeof(buffer) &#8211; 1, 0,<br \/>\n                             (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n        if (s &gt; 0)<br \/>\n        {<br \/>\n            buffer[s] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;Server Response# &#034; &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(sockfd);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h4 id=\"5.5%20Makefile%EF%BC%9A%E9%A1%B9%E7%9B%AE%E7%BC%96%E8%AF%91%E8%A7%84%E5%88%99\">5.5 Makefile&#xff1a;\u9879\u76ee\u7f16\u8bd1\u89c4\u5219<\/h4>\n<p>.PHONY: all<br \/>\nall: udp_server udp_client<\/p>\n<p>udp_server: UdpServer.cc UdpServer.hpp InetAddr.hpp<br \/>\ng&#043;&#043; -o $&#064; $&lt; -std&#061;c&#043;&#043;17<\/p>\n<p>udp_client: UdpClient.cc<br \/>\ng&#043;&#043; -o $&#064; $&lt; -std&#061;c&#043;&#043;17<\/p>\n<p>.PHONY: clean<br \/>\nclean:<br \/>\nrm -f udp_server udp_client <\/p>\n<h5 id=\"5.6%20%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F\">5.6 \u7f16\u8bd1\u8fd0\u884c\u4e0e\u901a\u4fe1\u6d4b\u8bd5<\/h5>\n<p>\u7f16\u8bd1&#xff1a;<\/p>\n<p>make <\/p>\n<p>\u5148\u542f\u52a8\u670d\u52a1\u7aef&#xff08;\u9ed8\u8ba4\u7aef\u53e38080&#xff09;&#xff1a;<\/p>\n<p>.\/udp_server <\/p>\n<p>\u6216\u8005\u6307\u5b9a\u7aef\u53e3&#xff1a;<\/p>\n<p>.\/udp_server 9090 <\/p>\n<p>\u518d\u542f\u52a8\u5ba2\u6237\u7aef&#xff0c;\u6307\u5b9a\u670d\u52a1\u7aefIP\u548c\u7aef\u53e3&#xff1a;<\/p>\n<p>.\/udp_client 127.0.0.1 8080 <\/p>\n<p>\u5ba2\u6237\u7aef\u8f93\u5165\u5185\u5bb9&#xff0c;\u670d\u52a1\u7aef\u4f1a\u6536\u5230\u5e76\u6253\u5370\u51fa\u6765&#xff0c;\u540c\u65f6\u628a\u5b57\u7b26\u4e32\u8f6c\u6210\u5927\u5199\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u8f93\u5165quit\u6216exit\u9000\u51fa\u5ba2\u6237\u7aef\u3002<\/p>\n<hr \/>\n<p>\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u522b\u5fd8\u4e86\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u5173\u6ce8\u4e09\u8fde\u652f\u6301\u3002\u4f60\u7684\u6bcf\u4e00\u6b21\u53cd\u9988&#xff0c;\u90fd\u662f\u6211\u7ee7\u7eed\u786c\u6838\u8f93\u51fa\u7684\u6700\u5927\u52a8\u529b\u3002\u6211\u4eec\u4e0b\u7bc7\u89c1\u3002<\/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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,76,78,191],"topic":[],"class_list":["post-116821","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-linux","tag-vscode","tag-78","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/116821.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/116821.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-21T21:56:41+00:00\" \/>\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=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116821.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/116821.html\",\"name\":\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-21T21:56:41+00:00\",\"dateModified\":\"2026-10-21T21:56:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/116821.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/116821.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116821.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\"}]},{\"@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":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/116821.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/116821.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-21T21:56:41+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/116821.html","url":"https:\/\/www.wsisp.com\/helps\/116821.html","name":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-21T21:56:41+00:00","dateModified":"2026-10-21T21:56:41+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/116821.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/116821.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/116821.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e94\uff09\uff1aUDP\u5957\u63a5\u5b57\u7f16\u7a0b\u5b9e\u6218\u2014\u2014\u4ece\u6838\u5fc3API\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1"}]},{"@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\/116821","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=116821"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/116821\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=116821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=116821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=116821"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=116821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}