{"id":14572,"date":"2025-04-18T21:18:11","date_gmt":"2025-04-18T13:18:11","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/14572.html"},"modified":"2025-04-18T21:18:11","modified_gmt":"2025-04-18T13:18:11","slug":"%e9%94%99%e8%af%af%e7%bb%8f%e9%aa%8c-%e5%9d%91%e5%85%b3%e4%ba%8eudp%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e5%ae%a2%e6%88%b7%e7%ab%af%e9%80%9a%e4%bf%a1%e4%bd%bf%e7%94%a8%e7%9a%84recvfrom%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/14572.html","title":{"rendered":"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898"},"content":{"rendered":"<p>[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 &#064;\u6c34\u58a8\u4e0d\u5199bug<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418131809-68025111912a9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e00\u3001\u56f0\u60d1&#xff1a;<\/li>\n<li>\u4e8c\u3001\u89e3\u7b54&#xff1a;<\/li>\n<li>\n<ul>\n<li>&#xff08;1&#xff09;\u51fd\u6570\u539f\u578b<\/li>\n<li>\n<ul>\n<li>1. &#096;int sockfd&#096;<\/li>\n<li>2. &#096;void *buf&#096;<\/li>\n<li>3. &#096;size_t len&#096;<\/li>\n<li>4. &#096;int flags&#096;<\/li>\n<li>5. &#096;struct sockaddr *src_addr&#096;<\/li>\n<li>6. &#096;socklen_t *addrlen&#096;<\/li>\n<\/ul>\n<\/li>\n<li>&#xff08;2&#xff09;\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u8003\u8651\u5982\u4e0b\u60c5\u666f&#xff1a;&#xff08;\u4e00\u5957\u7528\u4e8e\u901a\u4fe1\u7684UDP\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef&#xff09;<\/li>\n<li>\n<ul>\n<li>\u95ee\u9898\u6240\u5728<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u7ed3\u8bba&#xff1a;<\/li>\n<\/ul>\n<h2>\u4e00\u3001\u56f0\u60d1&#xff1a;<\/h2>\n<p>\u770b\u5230\u6807\u9898&#xff0c;\u4f60\u4e00\u5b9a\u5f88\u56f0\u60d1&#xff0c;\u8f93\u51fa\u578b\u53c2\u6570&#xff1f;\u4e3a\u4ec0\u4e48\u9700\u8981\u521d\u59cb\u5316&#xff1f;<\/p>\n<h2>\u4e8c\u3001\u89e3\u7b54&#xff1a;<\/h2>\n<p>Linux\u4e0b\u642d\u5efaUDP\u901a\u4fe1\u7684\u65f6\u5019&#xff0c;\u4f1a\u7528\u5230recvfrom\u51fd\u6570&#xff0c;\u51fd\u6570\u539f\u578b\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/socket.h&gt;<\/span><\/span><\/p>\n<p>ssize_t <span class=\"token function\">recvfrom<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> sockfd<span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">\/\/ \u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>buf<span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">\/\/ \u63a5\u6536\u6570\u636e\u7684\u7f13\u51b2\u533a<\/span><br \/>\n    size_t len<span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">\/\/ \u7f13\u51b2\u533a\u7684\u6700\u5927\u5bb9\u91cf<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> flags<span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">\/\/ \u63a7\u5236\u63a5\u6536\u884c\u4e3a\u7684\u6807\u5fd7\u4f4d<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span> <span class=\"token operator\">*<\/span>src_addr<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f&#xff08;\u8f93\u51fa\u53c2\u6570&#xff09;<\/span><br \/>\n    socklen_t <span class=\"token operator\">*<\/span>addrlen         <span class=\"token comment\">\/\/ \u5730\u5740\u7ed3\u6784\u4f53\u7684\u957f\u5ea6&#xff08;\u8f93\u5165\/\u8f93\u51fa\u53c2\u6570&#xff09;<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>recvfrom \u662f Linux \u7f51\u7edc\u7f16\u7a0b\u4e2d\u7528\u4e8e \u63a5\u6536\u6570\u636e\u62a5 \u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528&#xff0c;\u5e38\u7528\u4e8e\u65e0\u8fde\u63a5\u7684\u5957\u63a5\u5b57&#xff08;\u5982 UDP&#xff09;\u3002\u5b83\u4e0d\u4ec5\u80fd\u63a5\u6536\u6570\u636e&#xff0c;\u8fd8\u80fd\u83b7\u53d6\u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u5176\u51fd\u6570\u539f\u578b\u53ca\u5404\u53c2\u6570\u7684\u8be6\u7ec6\u8bf4\u660e&#xff1a;<\/p>\n<hr \/>\n<h3>&#xff08;1&#xff09;\u51fd\u6570\u539f\u578b<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/socket.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token class-name\">ssize_t<\/span> <span class=\"token function\">recvfrom<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> sockfd<span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">\/\/ \u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>buf<span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">\/\/ \u63a5\u6536\u6570\u636e\u7684\u7f13\u51b2\u533a<\/span><br \/>\n    <span class=\"token class-name\">size_t<\/span> len<span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">\/\/ \u7f13\u51b2\u533a\u7684\u6700\u5927\u5bb9\u91cf<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> flags<span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">\/\/ \u63a7\u5236\u63a5\u6536\u884c\u4e3a\u7684\u6807\u5fd7\u4f4d<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span> <span class=\"token operator\">*<\/span>src_addr<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f&#xff08;\u8f93\u51fa\u53c2\u6570&#xff09;<\/span><br \/>\n    <span class=\"token class-name\">socklen_t<\/span> <span class=\"token operator\">*<\/span>addrlen         <span class=\"token comment\">\/\/ \u5730\u5740\u7ed3\u6784\u4f53\u7684\u957f\u5ea6&#xff08;\u8f93\u5165\/\u8f93\u51fa\u53c2\u6570&#xff09;<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<hr \/>\n<h4>1. int sockfd<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u5df2\u521b\u5efa\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>\u8981\u6c42&#xff1a;\n<ul>\n<li>\u5fc5\u987b\u4e3a \u65e0\u8fde\u63a5\u534f\u8bae&#xff08;\u5982 UDP&#xff09;\u7684\u5957\u63a5\u5b57&#xff0c;\u6216\u672a\u8c03\u7528 connect \u7684\u9762\u5411\u8fde\u63a5\u534f\u8bae\u5957\u63a5\u5b57\u3002<\/li>\n<li>\u82e5\u5957\u63a5\u5b57\u5df2\u901a\u8fc7 connect \u5efa\u7acb\u8fde\u63a5&#xff08;\u5982 TCP&#xff09;&#xff0c;\u5e94\u4f7f\u7528 recv \u800c\u975e recvfrom\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2. void *buf<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u6307\u5411\u63a5\u6536\u6570\u636e\u7684\u7f13\u51b2\u533a\u3002<\/li>\n<li>\u8981\u6c42&#xff1a;\n<ul>\n<li>\u7f13\u51b2\u533a\u5fc5\u987b\u7531\u7528\u6237\u9884\u5148\u5206\u914d\u5185\u5b58&#xff0c;\u5927\u5c0f\u81f3\u5c11\u4e3a len \u5b57\u8282\u3002<\/li>\n<li>\u6570\u636e\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u5b58\u50a8&#xff0c;\u9700\u6839\u636e\u534f\u8bae\u81ea\u884c\u89e3\u6790\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>3. size_t len<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u7f13\u51b2\u533a buf \u7684\u6700\u5927\u5bb9\u91cf\u3002<\/li>\n<li>\u8981\u6c42&#xff1a;\n<ul>\n<li>\u5fc5\u987b\u5c0f\u4e8e\u6216\u7b49\u4e8e\u7f13\u51b2\u533a\u7684\u5b9e\u9645\u5927\u5c0f&#xff0c;\u5426\u5219\u53ef\u80fd\u5f15\u53d1\u7f13\u51b2\u533a\u6ea2\u51fa\u3002<\/li>\n<li>\u82e5\u6570\u636e\u957f\u5ea6\u8d85\u8fc7 len&#xff0c;\u591a\u4f59\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03&#xff08;UDP \u4e0b&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4. int flags<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u63a7\u5236\u63a5\u6536\u884c\u4e3a\u7684\u6807\u5fd7\u4f4d&#xff0c;\u591a\u4e2a\u6807\u5fd7\u53ef\u901a\u8fc7 | \u7ec4\u5408\u3002<\/li>\n<li>\u5e38\u89c1\u6807\u5fd7&#xff1a;<br \/>\n<table>\n<tr>\u6807\u5fd7\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>MSG_WAITALL<\/td>\n<td>\u963b\u585e\u76f4\u5230\u8bf7\u6c42\u7684 len \u5b57\u8282\u5168\u90e8\u63a5\u6536\u5b8c\u6210&#xff08;\u901a\u5e38\u7528\u4e8e\u6d41\u5f0f\u534f\u8bae\u5982 TCP&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>MSG_DONTWAIT<\/td>\n<td>\u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u82e5\u65e0\u6570\u636e\u53ef\u8bfb&#xff0c;\u7acb\u5373\u8fd4\u56de EAGAIN \u6216 EWOULDBLOCK<\/td>\n<\/tr>\n<tr>\n<td>MSG_PEEK<\/td>\n<td>\u7aa5\u89c6\u6570\u636e\u4f46\u4e0d\u4ece\u5185\u6838\u7f13\u51b2\u533a\u79fb\u9664&#xff08;\u4e0b\u6b21\u8c03\u7528\u4ecd\u53ef\u8bfb\u5230\u76f8\u540c\u6570\u636e&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>MSG_TRUNC<\/td>\n<td>\u82e5\u6570\u636e\u8d85\u8fc7 len&#xff0c;\u8fd4\u56de\u5b9e\u9645\u6570\u636e\u957f\u5ea6&#xff08;\u9700\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u5927\u4e8e len&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<\/ul>\n<h4>5. struct sockaddr *src_addr<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u8f93\u51fa\u53c2\u6570&#xff0c;\u4fdd\u5b58\u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f&#xff08;\u5982 IP \u548c\u7aef\u53e3&#xff09;\u3002<\/li>\n<li>\u8981\u6c42&#xff1a;\n<ul>\n<li>\u82e5\u65e0\u9700\u83b7\u53d6\u53d1\u9001\u65b9\u5730\u5740&#xff0c;\u53ef\u8bbe\u4e3a NULL&#xff08;\u6b64\u65f6 addrlen \u4e5f\u5e94\u4e3a NULL&#xff09;\u3002<\/li>\n<li>\u9700\u9884\u5148\u5206\u914d\u8db3\u591f\u5927\u7684\u5185\u5b58&#xff08;\u5982 struct sockaddr_in \u6216 struct sockaddr_in6&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>6. socklen_t *addrlen<\/h4>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\n<ul>\n<li>\u8f93\u5165&#xff1a;\u6307\u5b9a src_addr \u7f13\u51b2\u533a\u7684\u6700\u5927\u5bb9\u91cf\u3002<\/li>\n<li>\u8f93\u51fa&#xff1a;\u5b9e\u9645\u5199\u5165\u7684\u5730\u5740\u7ed3\u6784\u4f53\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8981\u6c42&#xff1a;\n<ul>\n<li>\u5fc5\u987b\u521d\u59cb\u5316\u4e3a src_addr \u7f13\u51b2\u533a\u7684\u5b9e\u9645\u5927\u5c0f&#xff08;\u5982 sizeof(struct sockaddr_in)&#xff09;\u3002<\/li>\n<li>\u82e5\u5b9e\u9645\u5730\u5740\u957f\u5ea6\u8d85\u8fc7 *addrlen&#xff0c;\u5730\u5740\u4f1a\u88ab\u622a\u65ad&#xff08;\u53ef\u901a\u8fc7\u8fd4\u56de\u503c\u68c0\u67e5&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>&#xff08;2&#xff09;\u8fd4\u56de\u503c<\/h3>\n<ul>\n<li>\u6210\u529f&#xff1a;\u8fd4\u56de\u63a5\u6536\u5230\u7684\u5b57\u8282\u6570&#xff08;&gt;&#061;0&#xff09;\u3002<\/li>\n<li>\u5931\u8d25&#xff1a;\u8fd4\u56de -1&#xff0c;\u9519\u8bef\u7801\u5b58\u50a8\u5728 errno \u4e2d\u3002<\/li>\n<li>\u7279\u6b8a\u573a\u666f&#xff1a;\n<ul>\n<li>UDP \u6570\u636e\u62a5\u957f\u5ea6\u8d85\u8fc7 len&#xff1a;\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u7684\u5b57\u8282\u6570&#xff08;\u53ef\u80fd\u5c0f\u4e8e\u539f\u59cb\u957f\u5ea6&#xff09;&#xff0c;\u9700\u68c0\u67e5 flags \u662f\u5426\u5305\u542b MSG_TRUNC\u3002<\/li>\n<li>\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff08;TCP&#xff09;&#xff1a;\u8fd4\u56de 0&#xff08;\u9700\u7ed3\u5408\u534f\u8bae\u903b\u8f91\u5904\u7406&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e09\u3001\u8003\u8651\u5982\u4e0b\u60c5\u666f&#xff1a;&#xff08;\u4e00\u5957\u7528\u4e8e\u901a\u4fe1\u7684UDP\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef&#xff09;<\/h2>\n<p><span class=\"token comment\">\/\/udp\u670d\u52a1\u5668<\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstring&gt;<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cassert&gt;<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/socket.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;arpa\/inet.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;netinet\/in.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/1.\u521b\u5efa\u5957\u63a5\u5b57socket fd,\u5e76\u4e14\u586b\u5145\u7ed3\u6784\u4f53\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> udpsockfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span>SOCK_DGRAM<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>udpsockfd <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;create sockfd failed!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;create success!\\\\n&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> self<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">bzero<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>self<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    self<span class=\"token punctuation\">.<\/span>sin_family <span class=\"token operator\">&#061;<\/span> AF_INET<span class=\"token punctuation\">;<\/span><br \/>\n    self<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8888<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6682\u65f6\u786c\u7f16\u7801<\/span><br \/>\n    self<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">.<\/span>s_addr <span class=\"token operator\">&#061;<\/span> INADDR_ANY<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/2.\u7ed1\u5b9a:\u8bbe\u7f6e\u8fdb\u5185\u6838<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> res <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>self<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>res <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;socket bind fail!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;bind success!\\\\n&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/3.\u6536\u53d1\u4fe1\u606f<\/span><br \/>\n    <span class=\"token comment\">\/\/\u670d\u52a1\u5668\u9996\u5148\u662f\u6536\u5230\u4fe1\u606f&#xff0c;\u5e76\u4e14\u83b7\u5f97\u53d1\u9001\u4fe1\u606f\u7684\u5ba2\u6237\u7aef\u7684sockaddr\u4fe1\u606f<\/span><br \/>\n    <span class=\"token comment\">\/\/\u7136\u540e\u624d\u80fd\u5f80\u5ba2\u6237\u7aef\u53d1\u9001\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> client<span class=\"token punctuation\">;<\/span><br \/>\n        socklen_t len <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>client<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\/\u4e8b\u5148\u521d\u59cb\u5316\u4e3asizeof client<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span>len<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">char<\/span> buf<span class=\"token punctuation\">[<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        ssize_t n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">recvfrom<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span>buf<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>buf<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>client<span class=\"token punctuation\">,<\/span><span class=\"token operator\">&amp;<\/span>len<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span>len<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u5728\u8fd9\u91cc\u5c31\u83b7\u53d6\u5230\u4e86\u5ba2\u6237\u7aefsockaddr\u7ed3\u6784\u4f53\u4fe1\u606f\u4e86<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;recvfrom fail\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">else<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            buf<span class=\"token punctuation\">[<\/span>n<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;GetFromClient: &#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>buf<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> buf <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;hello client!\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            ssize_t m <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">sendto<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span>buf<span class=\"token punctuation\">,<\/span><span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>buf<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>client<span class=\"token punctuation\">,<\/span>len<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>m <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sendto fail!\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/udp\u5ba2\u6237\u7aef<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstring&gt;<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cassert&gt;<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/socket.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;arpa\/inet.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;netinet\/in.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/1.\u521b\u5efa\u5957\u63a5\u5b57socket fd,\u5e76\u4e14\u586b\u5145\u7ed3\u6784\u4f53\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> udpsockfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span>SOCK_DGRAM<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>udpsockfd <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;create sockfd failed!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;create success!\\\\n&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5ba2\u6237\u7aef\u4e0d\u9700\u8981bind&#xff0c;OS\u81ea\u52a8\u7ed1\u5b9a\u968f\u673a\u7aef\u53e3<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/2.\u586b\u5145server\u4fe1\u606f,\u5728\u540e\u9762send\u7684\u65f6\u5019\u9700\u8981\u7528\u5230server\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> server<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">bzero<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>server<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>server<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_family <span class=\"token operator\">&#061;<\/span> AF_INET<span class=\"token punctuation\">;<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8888<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                      <span class=\"token comment\">\/\/\u8fd9\u91cc\u6682\u65f6\u5148\u786c\u7f16\u7801<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">.<\/span>s_addr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">inet_addr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/3.\u6536\u53d1\u6570\u636e<\/span><br \/>\n    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>string inbuf <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;I am client,hello server!&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        ssize_t n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">sendto<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span>inbuf<span class=\"token punctuation\">.<\/span><span class=\"token function\">c_str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>inbuf<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>server<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>server<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sendto fail!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">else<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">char<\/span> buf<span class=\"token punctuation\">[<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr_in<\/span> tem<span class=\"token punctuation\">;<\/span><br \/>\n            socklen_t len <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>tem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\u4e8b\u5148\u521d\u59cb\u5316\u4e3asizeof tem<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span>len<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>            ssize_t m <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">recvfrom<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span>buf<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>buf<span class=\"token punctuation\">)<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>tem<span class=\"token punctuation\">,<\/span><span class=\"token operator\">&amp;<\/span>len<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span>len<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;&#8212;-&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>m <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;recvfrom fail!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token keyword\">else<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><br \/>\n                buf<span class=\"token punctuation\">[<\/span>m<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;GetFromServer: &#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>buf<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n                std<span class=\"token operator\">::<\/span>string toserver <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;I Get your message\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">sendto<\/span><span class=\"token punctuation\">(<\/span>udpsockfd<span class=\"token punctuation\">,<\/span>toserver<span class=\"token punctuation\">.<\/span><span class=\"token function\">c_str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>toserver<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>tem<span class=\"token punctuation\">,<\/span>len<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token punctuation\">{<\/span><br \/>\n                    <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sendto fail!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u95ee\u9898\u6240\u5728<\/h3>\n<p>\u5b9e\u9645\u5728\u586b\u5165len\u8fd9\u4e2a\u53c2\u6570\u7684\u65f6\u5019&#xff0c;\u9700\u8981\u9996\u5148\u628alen\u521d\u59cb\u5316\u4e3asizeof(tem)&#xff08;\u65e0\u8bba\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef&#xff09;&#xff0c;\u770b\u4f3clen\u662f\u4f20\u5730\u5740&#xff0c;\u662f\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u4f46\u662f\u5b9e\u9645\u4e0a&#xff0c;\u5b83\u4e0d\u4ec5\u4ec5\u8d77\u5230\u8f93\u51fa\u578b\u53c2\u6570\u7684\u4f5c\u7528\u3002\u5982\u679c\u628alen\u6309\u7167\u8f93\u51fa\u578b\u53c2\u6570\u6765\u770b\u5f85&#xff0c;\u90a3\u4e48\u4e0d\u53d1\u9001\u6570\u636e\u8fd8\u597d\u8bf4&#xff0c;\u4e00\u65e6\u5728\u540e\u6587\u4f7f\u7528\u4e86\u8fd9\u4e2alen\u901a\u8fc7sendto\u6765\u53d1\u9001\u6570\u636e&#xff0c;\u5c31\u4f1a\u62a5\u9519&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418131809-68025111bea0a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<hr \/>\n<h2>\u56db\u3001\u7ed3\u8bba&#xff1a;<\/h2>\n<p>\u5728\u4f7f\u7528recvfrom\u4e4b\u524d&#xff0c;\u51b7\u4e0d\u80fd\u4ec5\u4ec5\u5f53\u4f5c\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570\u6765\u770b\u5f85&#xff01;\u4e5f\u9700\u8981\u628alen\u521d\u59cb\u5316\u4e3a\u4f20\u8fdb\u53bb\u7684(sockaddr_in)\u7ed3\u6784\u4f53\u7684\u5927\u5c0f&#xff01;<\/p>\n<hr \/>\n<p>\u5b8c~ <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418131810-6802511212798.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb674\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf33\u6b21\u3002[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898\u5b9e\u9645\u5728\u586b\u5165len\u8fd9\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u9700\u8981\u9996\u5148\u628alen\u521d\u59cb\u5316\u4e3asizeof(tem)\uff08\u65e0\u8bba\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef\uff09\uff0c\u770b\u4f3clen\u662f\u4f20\u5730\u5740\uff0c\u662f\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\uff0c\u5b83\u4e0d\u4ec5\u4ec5\u8d77\u5230\u8f93\u51fa\u578b\u53c2\u6570\u7684\u4f5c\u7528\u3002\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\uff0c\u5e38\u7528\u4e8e\u65e0\u8fde\u63a5\u7684\u5957\u63a5\u5b57\uff08\u5982 UDP\uff09\u3002\u5b83\u4e0d\u4ec5\u80fd\u63a5\u6536\u6570\u636e\uff0c\u8fd8\u80fd\u83b7\u53d6\u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f\u3002\u770b\u5230\u6807\u9898\uff0c\u4f60\u4e00\u5b9a\u5f88\u56f0\u60d1\uff0c\u8f93\u51fa\u578b\u53c2\u6570\uff1f_recvfrom len\u70ba0<\/p>\n","protected":false},"author":2,"featured_media":14569,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,918,193,270,43,78,191],"topic":[],"class_list":{"0":"post-14572","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-linux","10":"tag-udp","11":"tag-270","12":"tag-43","13":"tag-78","14":"tag-191"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \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\/14572.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb674\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf33\u6b21\u3002[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898\u5b9e\u9645\u5728\u586b\u5165len\u8fd9\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u9700\u8981\u9996\u5148\u628alen\u521d\u59cb\u5316\u4e3asizeof(tem)\uff08\u65e0\u8bba\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef\uff09\uff0c\u770b\u4f3clen\u662f\u4f20\u5730\u5740\uff0c\u662f\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\uff0c\u5b83\u4e0d\u4ec5\u4ec5\u8d77\u5230\u8f93\u51fa\u578b\u53c2\u6570\u7684\u4f5c\u7528\u3002\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\uff0c\u5e38\u7528\u4e8e\u65e0\u8fde\u63a5\u7684\u5957\u63a5\u5b57\uff08\u5982 UDP\uff09\u3002\u5b83\u4e0d\u4ec5\u80fd\u63a5\u6536\u6570\u636e\uff0c\u8fd8\u80fd\u83b7\u53d6\u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f\u3002\u770b\u5230\u6807\u9898\uff0c\u4f60\u4e00\u5b9a\u5f88\u56f0\u60d1\uff0c\u8f93\u51fa\u578b\u53c2\u6570\uff1f_recvfrom len\u70ba0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/14572.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-18T13:18:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418131809-68025111912a9.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/14572.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/14572.html\",\"name\":\"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:18:11+00:00\",\"dateModified\":\"2025-04-18T13:18:11+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/14572.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/14572.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/14572.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898\"}]},{\"@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":"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \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\/14572.html","og_locale":"zh_CN","og_type":"article","og_title":"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb674\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf33\u6b21\u3002[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898\u5b9e\u9645\u5728\u586b\u5165len\u8fd9\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u9700\u8981\u9996\u5148\u628alen\u521d\u59cb\u5316\u4e3asizeof(tem)\uff08\u65e0\u8bba\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef\uff09\uff0c\u770b\u4f3clen\u662f\u4f20\u5730\u5740\uff0c\u662f\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\uff0c\u5b83\u4e0d\u4ec5\u4ec5\u8d77\u5230\u8f93\u51fa\u578b\u53c2\u6570\u7684\u4f5c\u7528\u3002\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\uff0c\u5e38\u7528\u4e8e\u65e0\u8fde\u63a5\u7684\u5957\u63a5\u5b57\uff08\u5982 UDP\uff09\u3002\u5b83\u4e0d\u4ec5\u80fd\u63a5\u6536\u6570\u636e\uff0c\u8fd8\u80fd\u83b7\u53d6\u53d1\u9001\u65b9\u7684\u5730\u5740\u4fe1\u606f\u3002\u770b\u5230\u6807\u9898\uff0c\u4f60\u4e00\u5b9a\u5f88\u56f0\u60d1\uff0c\u8f93\u51fa\u578b\u53c2\u6570\uff1f_recvfrom len\u70ba0","og_url":"https:\/\/www.wsisp.com\/helps\/14572.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:18:11+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418131809-68025111912a9.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/14572.html","url":"https:\/\/www.wsisp.com\/helps\/14572.html","name":"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:18:11+00:00","dateModified":"2025-04-18T13:18:11+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/14572.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/14572.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/14572.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[\u9519\u8bef\u7ecf\u9a8c \u5751]\u5173\u4e8eUDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u4f7f\u7528\u7684recvfrom\u7684\u8f93\u51fa\u578b\u53c2\u6570len\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u95ee\u9898"}]},{"@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\/14572","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=14572"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/14572\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/14569"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=14572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=14572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=14572"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=14572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}