{"id":25222,"date":"2025-04-19T12:03:07","date_gmt":"2025-04-19T04:03:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/25222.html"},"modified":"2025-04-19T12:03:07","modified_gmt":"2025-04-19T04:03:07","slug":"%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%a5%97%e6%8e%a5%e5%ad%97%ef%bc%88%e4%ba%8c%ef%bc%89%e4%b9%8budp%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/25222.html","title":{"rendered":"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E4%B8%80%E3%80%81%E6%9C%8D%E5%8A%A1%E7%AB%AFUdpServer-toc\" style=\"margin-left:0px\">\u4e00\u3001\u670d\u52a1\u7aefUdpServer<\/p>\n<p id=\"1%E3%80%81udp_server.hpp-toc\" style=\"margin-left:40px\">1\u3001udp_server.hpp<\/p>\n<p id=\"1%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96-toc\" style=\"margin-left:80px\">1\u3001\u670d\u52a1\u5668\u7684\u521d\u59cb\u5316<\/p>\n<p id=\"2%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E8%BF%90%E8%A1%8C-toc\" style=\"margin-left:80px\">2\u3001\u670d\u52a1\u5668\u7684\u8fd0\u884c<\/p>\n<p id=\"2%E3%80%81udp_server.cc-toc\" style=\"margin-left:40px\">2\u3001udp_server.cc<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E5%AE%A2%E6%88%B7%E7%AB%AFUdpClient-toc\" style=\"margin-left:0px\">\u4e8c\u3001\u5ba2\u6237\u7aefUdpClient<\/p>\n<p id=\"udp_client.cc-toc\" style=\"margin-left:40px\">udp_client.cc<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:0px\">\u4e09\u3001\u5b8c\u6574\u4ee3\u7801<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E4%B8%80%E3%80%81%E6%9C%8D%E5%8A%A1%E7%AB%AFUdpServer\">\u4e00\u3001\u670d\u52a1\u7aefUdpServer<\/h2>\n<h3 id=\"1%E3%80%81udp_server.hpp\">1\u3001udp_server.hpp<\/h3>\n<p>\u9996\u5148&#xff0c;\u6211\u4eec\u5728\u8be5\u6587\u4ef6\u4e2d&#xff0c;\u5c06\u670d\u52a1\u5668\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b&#xff0c;\u800c\u4f5c\u4e3a\u4e00\u6b3e\u670d\u52a1\u5668&#xff0c;\u5fc5\u987b\u8981\u6709\u81ea\u5df1\u7684\u7aef\u53e3\u53f7&#xff0c;\u540c\u65f6\u7f51\u7edc\u670d\u52a1\u5668\u9700\u8981\u6709\u5bf9\u5e94\u7684IP\u5730\u5740&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26sock_&#xff1a;\u8fdb\u884c\u5404\u79cd\u5404\u6837\u7684\u6570\u636e\u901a\u4fe1&#xff0c;\u5728\u7c7b\u5185\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u7136\u540e\u5bf9\u5916\u63d0\u4f9b\u521d\u59cb\u5316\u548c\u8fd0\u884c\u7684\u63a5\u53e3\u3002<\/p>\n<h4 id=\"1%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96\">1\u3001\u670d\u52a1\u5668\u7684\u521d\u59cb\u5316<\/h4>\n<p>\u6211\u4eec\u6700\u5f00\u59cb\u9700\u8981\u5148\u5c06\u5b83\u8fdb\u884c\u521d\u59cb\u5316\u3002\u521d\u59cb\u5316\u7684\u7b2c\u4e00\u6b65\u5c31\u662f\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u800c\u521b\u5efa\u5957\u63a5\u5b57\u6211\u4eec\u9700\u8981\u7528\u5230\u4e0b\u9762\u7684\u51fd\u6570\u3002<\/p>\n<p>socket&#xff1a;\u5176\u4f5c\u7528\u5c31\u662f\u521b\u5efa\u5957\u63a5\u5b57\u3002<\/p>\n<p>NAME<br \/>\n       socket &#8211; create an endpoint for communication<\/p>\n<p>SYNOPSIS<br \/>\n       #include &lt;sys\/types.h&gt;          \/* See NOTES *\/<br \/>\n       #include &lt;sys\/socket.h&gt;<\/p>\n<p>       int socket(int domain, int type, int protocol); <\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<p>~ domain&#xff1a;\u57df&#xff0c;\u7528\u6765\u8868\u660e\u5957\u63a5\u5b57\u662f\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u8fd8\u662f\u672c\u5730\u901a\u4fe1&#xff0c;\u4e3b\u8981\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e24\u79cd &#xff1a;AF_UNIX&#xff08;\u672c\u5730\u901a\u4fe1&#xff09;\u00a0 \u00a0AF_INET&#xff08;\u7f51\u7edc\u901a\u4fe1&#xff09;\u3002<\/p>\n<p>~ type&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57\u65f6\u6240\u9700\u7684\u670d\u52a1\u7c7b\u578b\u3002\u5176\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u662fSOCK_STREAM\u548cSOCK_DGRAM\u3002\u5982&#xff1a;UDP\u662f\u6570\u636e\u62a5\u7684\u7f51\u7edc\u901a\u4fe1\u5f62\u5f0f&#xff0c;\u6211\u4eec\u91c7\u7528\u7684\u5c31\u662fSOCK_DGRAM&#xff08;\u7528\u6237\u6570\u636e\u62a5\u670d\u52a1&#xff09;&#xff0c;TCP\u662f\u9762\u5411\u5b57\u8282\u6d41\u5f0f\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u6211\u4eec\u91c7\u7528\u7684\u5c31\u662fSOCK_STREAM&#xff08;\u53eb\u505a\u6d41\u5f0f\u5957\u63a5\u5b57&#xff0c;\u63d0\u4f9b\u7684\u662f\u6d41\u5f0f\u670d\u52a1&#xff09;\u3002<\/p>\n<p>~ protocol&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57\u7684\u534f\u8bae\u7c7b\u522b\u3002\u8be5\u5b57\u6bb5\u4e00\u822c\u76f4\u63a5\u8bbe\u7f6e\u4e3a0\u5c31\u53ef\u4ee5\u4e86&#xff0c;\u8bbe\u7f6e\u4e3a0\u8868\u793a\u7684\u5c31\u662f\u9ed8\u8ba4&#xff0c;\u6b64\u65f6\u4f1a\u6839\u636e\u4f20\u5165\u7684\u524d\u4e24\u4e2a\u53c2\u6570\u81ea\u52a8\u63a8\u5bfc\u51fa\u4f60\u6700\u7ec8\u9700\u8981\u4f7f\u7528\u7684\u662f\u54ea\u79cd\u534f\u8bae\u3002<\/p>\n<p>~ \u8fd4\u56de\u503c&#xff1a;\u5957\u63a5\u5b57\u521b\u5efa\u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u521b\u5efa\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u540c\u65f6\u9519\u8bef\u7801\u4f1a\u88ab\u8bbe\u7f6e\u3002<\/p>\n<p>\u521d\u59cb\u5316\u7684\u7b2c\u4e8c\u6b65\u5c31\u662f\u7ed1\u5b9a\u7aef\u53e3\u53f7\u548cIP&#xff0c;\u6211\u4eec\u9700\u8981\u7528\u5230bind\u51fd\u6570<\/p>\n<p>NAME<br \/>\n       bind &#8211; bind a name to a socket<\/p>\n<p>SYNOPSIS<br \/>\n       #include &lt;sys\/types.h&gt;          \/* See NOTES *\/<br \/>\n       #include &lt;sys\/socket.h&gt;<\/p>\n<p>       int bind(int sockfd, const struct sockaddr *addr,<br \/>\n                socklen_t addrlen); <\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<p>~ sockfd&#xff1a;\u7ed1\u5b9a\u7684\u6587\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u4e5f\u5c31\u662f\u6211\u4eec\u521b\u5efa\u5957\u63a5\u5b57\u65f6\u83b7\u53d6\u5230\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>~ addr&#xff1a;\u7f51\u7edc\u76f8\u5173\u7684\u5c5e\u6027\u4fe1\u606f&#xff0c;\u5305\u62ec\u534f\u8bae\u5bb6\u65cf\u3001IP\u5730\u5740\u3001\u7aef\u53e3\u53f7\u7b49\u3002<\/p>\n<p>~ addrlen&#xff1a;\u4f20\u5165\u7684addr\u7ed3\u6784\u4f53\u7684\u957f\u5ea6\u3002\u00a0<\/p>\n<p>~ \u8fd4\u56de\u503c&#xff1a;\u7ed1\u5b9a\u6210\u529f\u8fd4\u56de0&#xff0c;\u7ed1\u5b9a\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u540c\u65f6\u9519\u8bef\u7801\u4f1a\u88ab\u8bbe\u7f6e\u3002<\/p>\n<p>\u5728\u7ed1\u5b9a\u65f6\u9700\u8981\u5c06\u670d\u52a1\u5668\u7f51\u7edc\u76f8\u5173\u7684\u5c5e\u6027\u4fe1\u606f\u586b\u5145\u5230\u7ed3\u6784\u4f53struct addr_in\u5f53\u4e2d&#xff0c;\u5176\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419040306-6803207a3f510.png\" \/><\/p>\n<p>\u6211\u4eec\u4e00\u822c\u9700\u8981\u586b\u5145\u4e0b\u9762\u4e09\u4e2a\u6210\u5458&#xff1a;<\/p>\n<p>sin_family&#xff1a;\u586b\u5145AF_INET\u3002<\/p>\n<p>sin_port&#xff1a;\u8868\u793a\u670d\u52a1\u5668\u7aef\u53e3\u53f7&#xff0c;\u662f\u4e00\u4e2a16\u4f4d\u7684\u6574\u6570\u3002\u9700\u8981\u6ce8\u610f\u4e3b\u673a\u5e8f\u5217\u548c\u7f51\u7edc\u5e8f\u5217\u7684\u8f6c\u5316\u3002<\/p>\n<p>sin_addr&#xff1a;\u8868\u793a\u670d\u52a1\u5668IP\u5730\u5740&#xff0c;\u662f\u4e00\u4e2a32\u4f4d\u7684\u6574\u6570\u3002\u6211\u4eec\u53d1\u73b0\u8fd9\u4e2a\u7ed3\u6784\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u6240\u4ee5\u4e00\u822c\u662f\u586b\u5145\u5176\u4e2d\u7684\u6210\u5458&#xff1a;\u662f\u4e00\u4e2a32\u4f4d\u7684\u6574\u6570\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419040306-6803207a7643a.png\" \/><\/p>\n<p>\u6211\u4eec\u6240\u770b\u5230\u7684IP\u5730\u5740\u662f\u70b9\u5206\u5341\u8fdb\u5236\u7684&#xff0c;\u4f46\u662f\u771f\u6b63\u7684IP\u5730\u5740\u662f\u6574\u6570&#xff0c;\u6240\u4ee5\u6211\u4eec\u5728\u4f7f\u7528\u662f\u9700\u8981\u5c06IP\u5730\u5740\u8f6c\u6362\u6210\u7cfb\u7edf\u80fd\u8bc6\u522b32\u4f4d\u7684\u6574\u6570&#xff0c;\u4e5f\u9700\u8981\u6ce8\u610f\u4e3b\u673a\u5e8f\u5217\u548c\u7f51\u7edc\u5e8f\u5217\u7684\u8f6c\u5316&#xff0c;\u8fd9\u4e9b\u64cd\u4f5c\u6211\u4eec\u4f7f\u7528\u51fd\u6570inet_addr\u53ef\u4ee5\u4e00\u5e76\u5b9e\u73b0\u3002<\/p>\n<p>#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>in_addr_t inet_addr(const char *cp);<br \/>\nchar *inet_ntoa(struct in_addr in); <\/p>\n<p>\u6ce8&#xff1a;\u5b9e\u9645\u4e0a&#xff0c;\u4e00\u6b3e\u7f51\u7edc\u670d\u52a1\u5668\u4e0d\u5efa\u8bae\u6307\u660e\u4e00\u4e2aIP&#xff0c;\u4e5f\u5c31\u662f\u4e0d\u8981\u663e\u793a\u5730\u7ed1\u5b9aIP&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u670d\u52a1\u5668\u4e0a\u53ef\u80fd\u4f1a\u6709\u591a\u5f20\u7f51\u5361&#xff0c;\u6240\u4ee5IP\u53ef\u80fd\u4e0d\u6b62\u4e00\u4e2a&#xff0c;\u5982\u679c\u53ea\u7ed1\u5b9a\u4e00\u4e2a\u660e\u786e\u7684IP&#xff0c;\u6700\u7ec8\u7684\u6570\u636e\u53ef\u80fd\u7528\u522b\u7684IP\u6765\u8bbf\u95ee\u7aef\u53e3\u53f7&#xff0c;\u8fd9\u5c31\u65e0\u6cd5\u8bbf\u95ee&#xff0c;\u6240\u4ee5\u771f\u5b9e\u7684\u670d\u52a1\u5668IP\u4e00\u822c\u91c7\u7528INADDR_ANY(\u51680&#xff0c;\u4efb\u610f\u5730\u5740)\u4ee3\u8868\u4efb\u610f\u5730\u5740bind\u00a0\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5728\u586b\u5145IP\u5730\u5740\u65f6\u6700\u597d\u4f7f\u7528INADDR_ANY\u3002<\/p>\n<h4 id=\"2%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E8%BF%90%E8%A1%8C\">2\u3001\u670d\u52a1\u5668\u7684\u8fd0\u884c<\/h4>\n<p>\u9996\u5148&#xff0c;\u4f5c\u4e3a\u4e00\u6b3e\u670d\u52a1\u5668&#xff0c;\u6211\u4eec\u5fc5\u987b\u80fd\u591f\u968f\u65f6\u7ed9\u7528\u6237\u63d0\u4f9b\u670d\u52a1&#xff0c;\u6240\u4ee5\u670d\u52a1\u5668\u662f\u4e00\u4e2a\u4e0d\u80fd\u591f\u9000\u51fa\u7684\u8fdb\u7a0b&#xff0c;\u9700\u8981\u4f7f\u7528\u6b7b\u5faa\u73af\u3002\u7136\u540e\u4e0d\u65ad\u63a5\u6536\u4ece\u5ba2\u6237\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u8bf7\u6c42&#xff0c;\u8fdb\u884c\u5904\u7406&#xff0c;\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u6211\u4eec\u4e00\u822c\u4f7f\u7528recvform\u51fd\u6570\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u8bf7\u6c42&#xff1a;<\/p>\n<p>NAME<br \/>\n       recv, recvfrom, recvmsg &#8211; receive a message from a socket<\/p>\n<p>SYNOPSIS<br \/>\n       #include &lt;sys\/types.h&gt;<br \/>\n       #include &lt;sys\/socket.h&gt;<\/p>\n<p>       ssize_t recv(int sockfd, void *buf, size_t len, int flags);<\/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>       ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); <\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<p>sockfd&#xff1a;\u670d\u52a1\u5668\u7ed1\u5b9a\u7684\u5957\u63a5\u5b57&#xff0c;buf&#xff1a;\u8bfb\u53d6\u5230\u7279\u5b9a\u7f13\u51b2\u533a&#xff0c;len&#xff1a;\u7f13\u51b2\u533a\u957f\u5ea6\u3002<\/p>\n<p>flags&#xff1a;\u8bfb\u53d6\u7684\u65b9\u5f0f&#xff0c;\u9ed8\u8ba4\u4e3a0&#xff0c;\u963b\u585e\u8bfb\u53d6\u3002<\/p>\n<p>src_addr&#xff1a;\u6536\u5230\u9664\u4e86\u6d88\u606f\u672c\u8eab&#xff0c;\u8fd8\u5f97\u77e5\u9053\u662f\u8c01\u53d1\u7684&#xff0c;\u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u8fd4\u56de\u5bf9\u5e94\u7684\u6d88\u606f\u5185\u5bb9\u662f\u4ece\u54ea\u4e00\u4e2a\u5ba2\u6237\u7aef\u6765\u7684&#xff0c;len&#xff1a;src_addr\u5927\u5c0f\u3002<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u8fd4\u56de-1\u8868\u793a\u5931\u8d25&#xff0c;\u6210\u529f\u8fd4\u56de\u5b57\u8282\u6570<\/p>\n<p>\u670d\u52a1\u5668\u6536\u5230\u6d88\u606f&#xff0c;\u8fdb\u884c\u5904\u7406\u540e&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u7ed3\u679c\u53d1\u56de\u7ed9\u5ba2\u6237\u7aef&#xff0c;\u6211\u4eec\u4e00\u822c\u4f7f\u7528sendto\u51fd\u6570&#xff1a;<\/p>\n<p>NAME<br \/>\n       send, sendto, sendmsg &#8211; send a message on a socket<\/p>\n<p>SYNOPSIS<br \/>\n       #include &lt;sys\/types.h&gt;<br \/>\n       #include &lt;sys\/socket.h&gt;<\/p>\n<p>       ssize_t send(int sockfd, const void *buf, size_t len, int flags);<\/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>       ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); <\/p>\n<h3 id=\"2%E3%80%81udp_server.cc\">2\u3001udp_server.cc<\/h3>\n<p>\u670d\u52a1\u7aef\u8fdb\u884c\u8c03\u7528\u7684\u4ee3\u7801\u903b\u8f91&#xff1a;\u6784\u5efaudpServer\u7684\u5bf9\u8c61&#xff0c;\u7136\u540e\u8fdb\u884c\u521d\u59cb\u5316&#xff0c;\u5728\u8fdb\u884c\u542f\u52a8\u8d77\u6765&#xff1b;\u8c03\u7528\u903b\u8f91\u5982\u4e0b&#xff1a;<\/p>\n<p>\u56e0\u4e3a\u8fd0\u884c\u540e\u670d\u52a1\u5668\u4f1a\u81ea\u52a8\u7ed1\u5b9a\u6240\u6709\u7684IP&#xff0c;\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u7ed1\u5b9a\u7aef\u53e3\u53f7\u5373\u53ef\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &#034;udp_server.hpp&#034;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>static void usage(const std::string &amp;proc)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;\\\\nusage: &#034; &lt;&lt; proc &lt;&lt; &#034;  port\\\\n&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/udpserver 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(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; atoi(argv[1]);<br \/>\n    std::unique_ptr&lt;UdpServer&gt; usvr(new UdpServer(port));<br \/>\n    usvr-&gt;InitServer();<br \/>\n    usvr-&gt;start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h2 id=\"%E4%BA%8C%E3%80%81%E5%AE%A2%E6%88%B7%E7%AB%AFUdpClient\">\u4e8c\u3001\u5ba2\u6237\u7aefUdpClient<\/h2>\n<h3 id=\"udp_client.cc\">udp_client.cc<\/h3>\n<p>\u5ba2\u6237\u7aef\u8c03\u7528\u65b9\u5f0f&#xff1a;.\/udpClient server_ip server_port&#xff0c;\u5ba2\u6237\u7aef\u60f3\u8fde\u63a5\u670d\u52a1\u5668&#xff0c;\u5fc5\u987b\u5f97\u77e5\u9053\u670d\u52a1\u5668\u7684IP&#xff08;\u516c\u7f51IP&#xff09;\u4ee5\u53ca\u7aef\u53e3\u53f7\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u65b9\u5f0f&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u53d1\u9001\u8bf7\u6c42\u7ed9\u670d\u52a1\u5668&#xff0c;\u63a5\u6536\u670d\u52a1\u5668\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff1a;\u5728\u670d\u52a1\u7aefbind\u7684\u65f6\u5019&#xff0c;\u6700\u91cd\u8981\u7684\u4e0d\u662f\u7ed1\u5b9aIP&#xff0c;\u800c\u662f\u7ed1\u5b9a\u7aef\u53e3\u53f7&#xff0c;\u670d\u52a1\u5668\u9700\u8981\u663e\u793a\u5730\u7ed1\u5b9a\u7aef\u53e3\u53f7\u662f\u4e3a\u4e86\u5ba2\u6237\u7aef\u672a\u6765\u80fd\u591f\u660e\u786e\u5730\u627e\u5230\u670d\u52a1\u5668\u662f\u5bf9\u5e94\u7684\u670d\u52a1\u7aef\u8fdb\u7a0b&#xff0c;\u4e0d\u80fd\u968f\u610f\u6539\u53d8\u3002<\/p>\n<p>\u800c\u5ba2\u6237\u7aef\u867d\u7136\u4e5f\u9700\u8981\u7aef\u53e3\u53f7&#xff0c;\u4f46\u662f\u4e0d\u91cd\u8981&#xff0c;\u81ea\u5df1\u542f\u52a8\u540e\u6709\u7aef\u53e3\u53f7\u5c31\u53ef\u4ee5\u4e86&#xff0c;\u4e0d\u9700\u8981\u663e\u793a\u5730\u7ed1\u5b9a\u662f\u54ea\u4e00\u4e2a\u3002\u56e0\u4e3a\u5982\u679c\u4e0d\u540c\u516c\u53f8\u7684\u7a0b\u5e8f\u5458\u5728\u5199\u4e0d\u540c\u8f6f\u4ef6\u5ba2\u6237\u7aef\u65f6&#xff0c;\u7ed1\u5b9a\u4e86\u540c\u4e00\u4e2a\u7aef\u53e3\u53f7\u7684\u8bdd&#xff0c;\u5c31\u4f1a\u53d1\u751f\u51b2\u7a81&#xff0c;\u6240\u4ee5\u6211\u4eec\u5728\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u53bb\u7ed1\u5b9a\u7aef\u53e3\u53f7&#xff0c;\u800c\u662f\u7531\u7cfb\u7edf\u968f\u673a\u5206\u914d\u3002<\/p>\n<h2 id=\"%E4%B8%89%E3%80%81%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81\">\u4e09\u3001\u5b8c\u6574\u4ee3\u7801<\/h2>\n<p>udp_server.hpp<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>class UdpServer<br \/>\n{<br \/>\npublic:<br \/>\n    UdpServer(const uint16_t &amp;port, std::string ip &#061; &#034;&#034;)<br \/>\n        : ip_(ip), port_(port), sock_(-1)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    void InitServer()<br \/>\n    {<br \/>\n        \/\/ 1.\u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        sock_ &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n        if (sock_ &lt; 0)<br \/>\n        {<br \/>\n            std::cerr &lt;&lt; &#034;\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25&#xff01;&#034; &lt;&lt; errno &lt;&lt; strerror(errno) &lt;&lt; std::endl;<br \/>\n            exit(0);<br \/>\n        }<\/p>\n<p>        \/\/ 2.\u8fdb\u884c\u7ed1\u5b9a:\u7ed1\u5b9aip\u548c\u7aef\u53e3\u53f7<br \/>\n        \/\/ 2.1 \u670d\u52a1\u5668\u7684\u5957\u63a5\u5b57\u586b\u5145\u7ed3\u6784<br \/>\n        struct sockaddr_in local_server;<br \/>\n        bzero(&amp;local_server, sizeof(local_server));<br \/>\n        local_server.sin_family &#061; AF_INET;<br \/>\n        local_server.sin_port &#061; htons(port_);                                             \/\/ \u7aef\u53e3\u53f7\u9700\u8981\u4ece\u4e3b\u673a\u8f6c\u7f51\u7edc<br \/>\n        local_server.sin_addr.s_addr &#061; ip_.empty() ? INADDR_ANY : inet_addr(ip_.c_str()); \/\/ ip\u5730\u5740\u9700\u8981\u4e3b\u673a\u8f6c\u7f51\u7edc&#xff0c;\u5e76\u4e14\u8981\u8f6c\u5316\u62104\u5b57\u8282\u5f62\u5f0f<br \/>\n        int len &#061; sizeof(local_server);<br \/>\n        if (bind(sock_, (struct sockaddr *)&amp;local_server, len))<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;\u7ed1\u5b9a\u5931\u8d25&#xff01;&#034; &lt;&lt; errno &lt;&lt; strerror(errno) &lt;&lt; std::endl;<br \/>\n            exit(1);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;\u670d\u52a1\u5668\u521d\u59cb\u5316\u5b8c\u6210&#xff01;&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void start()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;\u670d\u52a1\u5668\u8fd0\u884c\u6210\u529f!&#034; &lt;&lt; std::endl;<br \/>\n        char buffer[1024];<br \/>\n        char result[1024];<br \/>\n        std::string server_echo;<br \/>\n        \/\/ \u670d\u52a1\u5668\u4e0d\u80fd\u505c\u4e0b\u6765&#xff0c;\u9664\u975e\u6302\u6389\u2014\u2014\u6b7b\u5faa\u73af<br \/>\n        for (;;)<br \/>\n        {<br \/>\n            struct sockaddr_in src_client; \/\/ \u670d\u52a1\u5668\u6536\u5230\u7684\u5ba2\u6237\u7aef\u6765\u6e90<br \/>\n            socklen_t len &#061; sizeof(src_client);<br \/>\n            ssize_t s &#061; recvfrom(sock_, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr *)&amp;src_client, &amp;len);<br \/>\n            if (s &lt; 0)<br \/>\n            {<br \/>\n                std::cout &lt;&lt; &#034;\u63a5\u6536\u6d88\u606f\u5931\u8d25&#xff01;&#034; &lt;&lt; std::endl;<br \/>\n            }<br \/>\n            buffer[s] &#061; 0;<br \/>\n            \/\/std::string src_client_ip &#061; inet_ntoa(src_client.sin_addr);<br \/>\n            \/\/uint16_t src_client_port &#061; ntohs(src_client.sin_port);<br \/>\n            \/\/std::cout &lt;&lt; &#034;[&#034; &lt;&lt; src_client_ip &lt;&lt; &#034;,&#034; &lt;&lt; src_client_port &lt;&lt; &#034;]: &#034; &lt;&lt; buffer &lt;&lt; std::endl;<\/p>\n<p>             \/\/ \u5ba2\u6237\u7aef\u53d1\u8fc7\u6765\u7684\u4fe1\u606f\u662f\u6307\u4ee4&#xff0c;\u670d\u52a1\u5668\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<br \/>\n            FILE *fp &#061; popen(buffer, &#034;r&#034;);<br \/>\n            if (fp &#061;&#061; nullptr)<br \/>\n            {<br \/>\n                std::cout &lt;&lt; &#034;popen\u5931\u8d25!&#034; &lt;&lt; std::endl;<br \/>\n                continue;<br \/>\n            }<br \/>\n            while (fgets(result, sizeof(result), fp) !&#061; nullptr)<br \/>\n            {<br \/>\n                server_echo &#043;&#061; result;<br \/>\n            }<\/p>\n<p>            \/\/ \u670d\u52a1\u5668\u53d1\u56de\u6d88\u606f<br \/>\n            sendto(sock_, server_echo.c_str(), server_echo.size(), 0, (struct sockaddr *)&amp;src_client, len);<br \/>\n            \/\/sendto(sock_, buffer, sizeof(buffer), 0, (struct sockaddr *)&amp;src_client, len);<br \/>\n        }<br \/>\n    }<\/p>\n<p>private:<br \/>\n    std::string ip_; \/\/ \u670d\u52a1\u5668ip\u5730\u5740<br \/>\n    uint16_t port_;  \/\/ \u670d\u52a1\u5668\u7aef\u53e3\u53f7<br \/>\n    int sock_;       \/\/ \u5957\u63a5\u5b57<br \/>\n}; <\/p>\n<p>udp_server.cc<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &#034;udp_server.hpp&#034;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>static void usage(const std::string &amp;proc)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;\\\\nusage: &#034; &lt;&lt; proc &lt;&lt; &#034;  port\\\\n&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/udpserver 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(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; atoi(argv[1]);<br \/>\n    std::unique_ptr&lt;UdpServer&gt; usvr(new UdpServer(port));<br \/>\n    usvr-&gt;InitServer();<br \/>\n    usvr-&gt;start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>udp_client.cc<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;cstring&gt;<\/p>\n<p>static void usage(const std::string &amp;proc)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;\\\\nusage: &#034;<br \/>\n              &lt;&lt; proc &lt;&lt; &#034;  port ip\\\\n&#034;<br \/>\n              &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/udpclient ip port  \u5e26\u4e0a\u670d\u52a1\u5668\u7684ip\u548c\u7aef\u53e3\u53f7<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(0);<br \/>\n    }<br \/>\n    \/\/ 1.\u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    int sock &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n    if (sock &lt; 0)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25&#xff01;&#034; &lt;&lt; errno &lt;&lt; strerror(errno) &lt;&lt; std::endl;<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    \/\/ \u4f5c\u4e3a\u5ba2\u6237\u7aef&#xff0c;\u5176\u4e0d\u9700\u8981\u8fdb\u884c\u7ed1\u5b9a&#xff0c;\u7531os\u81ea\u52a8\u8fdb\u884c\u7ed1\u5b9a<br \/>\n    std::string message;<br \/>\n    \/\/ \u7ed9\u54ea\u4e2a\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f&#xff0c;\u586b\u5145\u670d\u52a1\u5668\u4fe1\u606f<br \/>\n    struct sockaddr_in send_server;<br \/>\n    bzero(&amp;send_server, sizeof(send_server));<br \/>\n    send_server.sin_family &#061; AF_INET;<br \/>\n    send_server.sin_port &#061; htons(atoi(argv[2]));<br \/>\n    send_server.sin_addr.s_addr &#061; inet_addr(argv[1]);<br \/>\n    while (true)<br \/>\n    {<br \/>\n        \/\/ 2.\u4ece\u952e\u76d8\u83b7\u53d6\u4fe1\u606f<br \/>\n        std::cout &lt;&lt; &#034;\u8bf7\u8f93\u5165#&#034;;<br \/>\n        std::getline(std::cin, message);<br \/>\n        if (message &#061;&#061; &#034;quit&#034;)<br \/>\n            break;<br \/>\n        \/\/ 3.\u53d1\u9001\u6d88\u606f<br \/>\n        sendto(sock, message.c_str(), message.size(), 0, (struct sockaddr *)&amp;send_server, sizeof(send_server));<\/p>\n<p>        \/\/ 4.\u63a5\u6536\u670d\u52a1\u5668\u7684\u6d88\u606f<br \/>\n        char buffer[1024];<br \/>\n        struct sockaddr_in revc_server;<br \/>\n        bzero(&amp;revc_server, sizeof(revc_server));<br \/>\n        socklen_t len &#061; sizeof(revc_server);<br \/>\n        ssize_t s &#061; recvfrom(sock, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr *)&amp;revc_server, &amp;len);<br \/>\n        std::cout &lt;&lt; &#034;\u670d\u52a1\u5668\u8bf4&#xff1a;&#034; &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    close(sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.1k\u6b21\uff0c\u70b9\u8d5e61\u6b21\uff0c\u6536\u85cf68\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528C++\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684UDP\u670d\u52a1\u5668\uff08UdpServer\uff09\u548c\u5ba2\u6237\u7aef\uff08UdpClient\uff09\uff0c\u5305\u62ec\u670d\u52a1\u5668\u7aef\u53e3\u7ed1\u5b9a\u3001\u5957\u63a5\u5b57\u64cd\u4f5c\u3001\u6570\u636e\u6536\u53d1\u4ee5\u53ca\u5ba2\u6237\u7aef\u8fde\u63a5\u8fc7\u7a0b\u3002\u5c55\u793a\u4e86\u670d\u52a1\u7aef\u521d\u59cb\u5316\u3001\u8fd0\u884c\u903b\u8f91\u548c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u57fa\u672c\u6b65\u9aa4\u3002<\/p>\n","protected":false},"author":2,"featured_media":25220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,193,43,44],"topic":[],"class_list":["post-25222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-udp","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \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\/25222.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.1k\u6b21\uff0c\u70b9\u8d5e61\u6b21\uff0c\u6536\u85cf68\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528C++\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684UDP\u670d\u52a1\u5668\uff08UdpServer\uff09\u548c\u5ba2\u6237\u7aef\uff08UdpClient\uff09\uff0c\u5305\u62ec\u670d\u52a1\u5668\u7aef\u53e3\u7ed1\u5b9a\u3001\u5957\u63a5\u5b57\u64cd\u4f5c\u3001\u6570\u636e\u6536\u53d1\u4ee5\u53ca\u5ba2\u6237\u7aef\u8fde\u63a5\u8fc7\u7a0b\u3002\u5c55\u793a\u4e86\u670d\u52a1\u7aef\u521d\u59cb\u5316\u3001\u8fd0\u884c\u903b\u8f91\u548c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u57fa\u672c\u6b65\u9aa4\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/25222.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-19T04:03:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419040306-6803207a3f510.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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/25222.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/25222.html\",\"name\":\"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T04:03:07+00:00\",\"dateModified\":\"2025-04-19T04:03:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/25222.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/25222.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/25222.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0\"}]},{\"@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":"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \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\/25222.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.1k\u6b21\uff0c\u70b9\u8d5e61\u6b21\uff0c\u6536\u85cf68\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528C++\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684UDP\u670d\u52a1\u5668\uff08UdpServer\uff09\u548c\u5ba2\u6237\u7aef\uff08UdpClient\uff09\uff0c\u5305\u62ec\u670d\u52a1\u5668\u7aef\u53e3\u7ed1\u5b9a\u3001\u5957\u63a5\u5b57\u64cd\u4f5c\u3001\u6570\u636e\u6536\u53d1\u4ee5\u53ca\u5ba2\u6237\u7aef\u8fde\u63a5\u8fc7\u7a0b\u3002\u5c55\u793a\u4e86\u670d\u52a1\u7aef\u521d\u59cb\u5316\u3001\u8fd0\u884c\u903b\u8f91\u548c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u57fa\u672c\u6b65\u9aa4\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/25222.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T04:03:07+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419040306-6803207a3f510.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/25222.html","url":"https:\/\/www.wsisp.com\/helps\/25222.html","name":"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T04:03:07+00:00","dateModified":"2025-04-19T04:03:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/25222.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/25222.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/25222.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7f51\u7edc\u7f16\u7a0b\u5957\u63a5\u5b57\uff08\u4e8c\uff09\u4e4bUDP\u670d\u52a1\u5668\u7b80\u5355\u5b9e\u73b0"}]},{"@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\/25222","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=25222"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/25222\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/25220"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=25222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=25222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=25222"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=25222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}