{"id":14313,"date":"2025-04-18T21:08:07","date_gmt":"2025-04-18T13:08:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/14313.html"},"modified":"2025-04-18T21:08:07","modified_gmt":"2025-04-18T13:08:07","slug":"linux%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e4%b9%8budp-%e5%9b%9e%e5%a3%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/14313.html","title":{"rendered":"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef"},"content":{"rendered":"<p>\u8fd9\u4e2aDemo\u4e3b\u8981\u6f14\u793a\u4e86\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528UDP\u534f\u8bae\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u58f0\u670d\u52a1\u5668&#xff08;Echo Server&#xff09;\u4ee5\u53ca\u4e0e\u4e4b\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f&#xff1a;<\/p>\n<h4>\u670d\u52a1\u5668\u7aef<\/h4>\n<p>#include &lt;stdio.h&gt; \u00a0 \u00a0 \u00a0\/\/ \u5f15\u5165\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5e93&#xff0c;\u7528\u4e8e\u6253\u5370\u4fe1\u606f<br \/>\n#include &lt;stdlib.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165\u6807\u51c6\u5e93&#xff0c;\u63d0\u4f9bexit()\u7b49\u51fd\u6570<br \/>\n#include &lt;string.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u5e93<br \/>\n#include &lt;unistd.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165UNIX\u6807\u51c6\u51fd\u6570\u5e93&#xff0c;\u5305\u542b\u5982close()\u51fd\u6570<br \/>\n#include &lt;arpa\/inet.h&gt; \u00a0\/\/ \u5f15\u5165\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u8f6c\u6362\u548c\u7f51\u7edc\u534f\u8bae\u76f8\u5173\u51fd\u6570\u5e93<br \/>\n#include &lt;sys\/socket.h&gt; \/\/ \u5f15\u5165 socket \u521b\u5efa\u548c\u64cd\u4f5c\u51fd\u6570\u5e93<br \/>\n\u200b<br \/>\n#define BUF_SIZE 30 \u00a0 \u00a0 \/\/ \u5b9a\u4e49\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a30\u5b57\u8282<br \/>\n\u200b<br \/>\nvoid error_handling(char *message); \/\/ \u9519\u8bef\u5904\u7406\u51fd\u6570\u7684\u58f0\u660e<br \/>\n\u200b<br \/>\nint main(int argc, char *argv[]) \/\/ \u7a0b\u5e8f\u4e3b\u5165\u53e3<br \/>\n{<br \/>\n \u00a0 \u00a0int serv_sock; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7528\u4e8e\u5b58\u50a8\u670d\u52a1\u7aefsocket\u63cf\u8ff0\u7b26<br \/>\n \u00a0 \u00a0char message[BUF_SIZE]; \u00a0 \u00a0 \u00a0 \/\/ \u5b9a\u4e49\u7f13\u51b2\u533a&#xff0c;\u5b58\u50a8\u63a5\u6536\u5230\u7684\u6d88\u606f<br \/>\n \u00a0 \u00a0int str_len; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5b58\u50a8\u63a5\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6<br \/>\n \u00a0 \u00a0socklen_t clnt_adr_sz; \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784\u5927\u5c0f<br \/>\n\u200b<br \/>\n \u00a0 \u00a0struct sockaddr_in serv_adr, clnt_adr; \/\/ \u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784<br \/>\n\u200b<br \/>\n \u00a0 \u00a0if(argc !&#061; 2){ \u00a0\/\/ \u5982\u679c\u547d\u4ee4\u884c\u53c2\u6570\u4e0d\u7b49\u4e8e2&#xff08;\u7a0b\u5e8f\u540d\u548c\u7aef\u53e3\u53f7&#xff09;&#xff0c;\u5219\u8f93\u51fa\u7528\u6cd5\u63d0\u793a<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0printf(&#034;Usage : %s &lt;port&gt;\\\\n&#034;, argv[0]);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0exit(1); \u00a0\/\/ \u9000\u51fa\u7a0b\u5e8f<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u521b\u5efa UDP socket<br \/>\n \u00a0 \u00a0serv_sock &#061; socket(PF_INET, SOCK_DGRAM, 0);<br \/>\n \u00a0 \u00a0if(serv_sock &#061;&#061; -1) \u00a0\/\/ \u5982\u679c socket \u521b\u5efa\u5931\u8d25&#xff0c;\u5219\u8c03\u7528\u9519\u8bef\u5904\u7406\u51fd\u6570<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0error_handling(&#034;UDP socket creation error&#034;);<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u521d\u59cb\u5316\u670d\u52a1\u7aef\u5730\u5740\u7ed3\u6784<br \/>\n \u00a0 \u00a0memset(&amp;serv_adr, 0, sizeof(serv_adr)); \u00a0\/\/ \u6e05\u7a7a\u5730\u5740\u7ed3\u6784<br \/>\n \u00a0 \u00a0serv_adr.sin_family &#061; AF_INET; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u8bbe\u7f6e\u534f\u8bae\u65cf\u4e3a IPv4<br \/>\n \u00a0 \u00a0serv_adr.sin_addr.s_addr &#061; htonl(INADDR_ANY); \u00a0\/\/ \u8bbe\u7f6e\u670d\u52a1\u5668\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e<br \/>\n \u00a0 \u00a0serv_adr.sin_port &#061; htons(atoi(argv[1])); \/\/ \u5c06\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f&#xff0c;\u5e76\u8bbe\u7f6e\u7aef\u53e3<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u7ed1\u5b9a socket \u5230\u6307\u5b9a\u7684 IP \u5730\u5740\u548c\u7aef\u53e3<br \/>\n \u00a0 \u00a0if(bind(serv_sock, (struct sockaddr*)&amp;serv_adr, sizeof(serv_adr)) &#061;&#061; -1)<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0error_handling(&#034;bind() error&#034;);<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u8fdb\u5165\u5faa\u73af&#xff0c;\u7b49\u5f85\u5e76\u5904\u7406\u5ba2\u6237\u7aef\u6d88\u606f<br \/>\n \u00a0 \u00a0while(1)<br \/>\n \u00a0  {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0clnt_adr_sz &#061; sizeof(clnt_adr); \u00a0\/\/ \u8bbe\u7f6e\u5ba2\u6237\u7aef\u5730\u5740\u7684\u5927\u5c0f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0str_len &#061; recvfrom(serv_sock, message, BUF_SIZE, 0,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (struct sockaddr*)&amp;clnt_adr, &amp;clnt_adr_sz); \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u53d1\u9001\u56de\u5ba2\u6237\u7aef&#xff0c;\u5b9e\u73b0\u56de\u58f0\u529f\u80fd<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0sendto(serv_sock, message, str_len, 0,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (struct sockaddr*)&amp;clnt_adr, clnt_adr_sz);<br \/>\n \u00a0  } \u00a0<br \/>\n\u200b<br \/>\n \u00a0 \u00a0close(serv_sock); \u00a0\/\/ \u5173\u95ed socket<br \/>\n \u00a0 \u00a0return 0;<br \/>\n}<br \/>\n\u200b<br \/>\n\/\/ \u9519\u8bef\u5904\u7406\u51fd\u6570<br \/>\nvoid error_handling(char *message)<br \/>\n{<br \/>\n \u00a0 \u00a0fputs(message, stderr); \u00a0\/\/ \u5c06\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u6807\u51c6\u9519\u8bef\u6d41<br \/>\n \u00a0 \u00a0fputc(&#039;\\\\n&#039;, stderr); \u00a0 \u00a0 \/\/ \u8f93\u51fa\u6362\u884c\u7b26<br \/>\n \u00a0 \u00a0exit(1); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u9000\u51fa\u7a0b\u5e8f<br \/>\n} <\/p>\n<p>\u8fd9\u4e2a UDP \u56de\u58f0\u670d\u52a1\u5668\u7684\u6d41\u7a0b\u53ef\u4ee5\u603b\u7ed3\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4&#xff1a;<\/p>\n<h5>1. \u53c2\u6570\u68c0\u67e5<\/h5>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668\u542f\u52a8\u65f6&#xff0c;\u4f1a\u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570\u662f\u5426\u6b63\u786e&#xff0c;\u786e\u4fdd\u7528\u6237\u8f93\u5165\u4e86\u7aef\u53e3\u53f7\u3002\u5982\u679c\u53c2\u6570\u4e0d\u6b63\u786e&#xff0c;\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u7528\u6cd5\u8bf4\u660e\u5e76\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5982&#xff0c;\u5982\u679c\u547d\u4ee4\u884c\u8f93\u5165\u662f .\/server 12345&#xff0c;12345 \u5c31\u662f\u6307\u5b9a\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>2. \u521b\u5efa UDP Socket<\/h5>\n<ul>\n<li>\n<p>\u4f7f\u7528 socket() \u51fd\u6570\u521b\u5efa\u4e00\u4e2a UDP socket\u3002PF_INET \u8868\u793a\u4f7f\u7528 IPv4 \u534f\u8bae&#xff0c;SOCK_DGRAM \u8868\u793a\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u62a5&#xff08;UDP&#xff09;socket\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c socket \u521b\u5efa\u5931\u8d25&#xff0c;\u8c03\u7528 error_handling() \u51fd\u6570\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>3. \u7ed1\u5b9a Socket \u5230\u7aef\u53e3<\/h5>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668\u6784\u5efa\u4e00\u4e2asockaddr_in\u7c7b\u578b\u7684\u5730\u5740\u7ed3\u6784&#xff0c;\u5e76\u8bbe\u7f6e&#xff1a;<\/p>\n<ul>\n<li>\n<p>sin_family &#061; AF_INET&#xff1a;\u6307\u5b9a\u4f7f\u7528 IPv4 \u534f\u8bae\u3002<\/p>\n<\/li>\n<li>\n<p>sin_addr.s_addr &#061; htonl(INADDR_ANY)&#xff1a;\u5141\u8bb8\u670d\u52a1\u5668\u76d1\u542c\u6765\u81ea\u4efb\u4f55 IP \u5730\u5740\u7684\u8bf7\u6c42\u3002<\/p>\n<\/li>\n<li>\n<p>sin_port &#061; htons(atoi(argv[1]))&#xff1a;\u8bbe\u7f6e\u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f7f\u7528 bind() \u51fd\u6570\u5c06\u521b\u5efa\u7684 UDP socket \u4e0e\u8be5\u5730\u5740\u7ed3\u6784\u7ed1\u5b9a&#xff0c;\u786e\u4fdd\u670d\u52a1\u5668\u901a\u8fc7\u6307\u5b9a\u7684\u7aef\u53e3\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u8c03\u7528 error_handling() \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u9000\u51fa\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>4. \u8fdb\u5165\u4e3b\u5faa\u73af&#xff0c;\u7b49\u5f85\u5e76\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42<\/h5>\n<ul>\n<li>\n<p>\u5728\u65e0\u9650\u5faa\u73af\u4e2d&#xff0c;\u670d\u52a1\u5668\u7b49\u5f85\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 recvfrom() \u51fd\u6570\u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u6570\u636e\u3002\u8be5\u51fd\u6570\u5c06\u6536\u5230\u7684\u6570\u636e\u5b58\u50a8\u5728 message \u7f13\u51b2\u533a\u4e2d&#xff0c;str_len \u8868\u793a\u63a5\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u7684\u5730\u5740\u4fe1\u606f&#xff08;\u5982 IP \u5730\u5740\u548c\u7aef\u53e3&#xff09;\u88ab\u4fdd\u5b58\u5728 clnt_adr \u4e2d&#xff0c;clnt_adr_sz \u4fdd\u5b58\u5ba2\u6237\u7aef\u5730\u5740\u7684\u5927\u5c0f\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>5. \u56de\u9001\u63a5\u6536\u5230\u7684\u6570\u636e<\/h5>\n<ul>\n<li>\n<p>\u4e00\u65e6\u63a5\u6536\u5230\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u4f1a\u4f7f\u7528 sendto() \u51fd\u6570\u5c06\u76f8\u540c\u7684\u6570\u636e\u53d1\u9001\u56de\u5ba2\u6237\u7aef\u3002\u8fd9\u5b9e\u73b0\u4e86\u56de\u58f0\u670d\u52a1\u5668\u7684\u529f\u80fd&#xff0c;\u5373\u5c06\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6d88\u606f\u539f\u5c01\u4e0d\u52a8\u5730\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>6. \u91cd\u590d\u5904\u7406<\/h5>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668\u4f1a\u7ee7\u7eed\u91cd\u590d\u6b65\u9aa4 4 \u548c 5&#xff0c;\u5904\u7406\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e&#xff0c;\u76f4\u5230\u7a0b\u5e8f\u88ab\u5916\u90e8\u7ec8\u6b62\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>7. \u5173\u95ed Socket<\/h5>\n<ul>\n<li>\n<p>\u867d\u7136\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u5165\u4e86\u65e0\u9650\u5faa\u73af&#xff0c;\u4f46\u5982\u679c\u9000\u51fa\u5faa\u73af&#xff08;\u4f8b\u5982\u9047\u5230\u9519\u8bef\u6216\u7a0b\u5e8f\u88ab\u5916\u90e8\u4fe1\u53f7\u7ec8\u6b62&#xff09;&#xff0c;\u670d\u52a1\u5668\u4f1a\u5173\u95ed socket&#xff0c;\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u5ba2\u6237\u7aef<\/h4>\n<p>#include &lt;stdio.h&gt; \u00a0 \u00a0 \u00a0\/\/ \u5f15\u5165\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5e93&#xff0c;\u7528\u4e8e\u6253\u5370\u4fe1\u606f<br \/>\n#include &lt;stdlib.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165\u6807\u51c6\u5e93&#xff0c;\u63d0\u4f9b exit() \u7b49\u51fd\u6570<br \/>\n#include &lt;string.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u5e93<br \/>\n#include &lt;unistd.h&gt; \u00a0 \u00a0 \/\/ \u5f15\u5165 UNIX \u6807\u51c6\u51fd\u6570\u5e93&#xff0c;\u5305\u542b close() \u51fd\u6570<br \/>\n#include &lt;arpa\/inet.h&gt; \u00a0\/\/ \u5f15\u5165\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u8f6c\u6362\u548c\u7f51\u7edc\u534f\u8bae\u76f8\u5173\u51fd\u6570\u5e93<br \/>\n#include &lt;sys\/socket.h&gt; \/\/ \u5f15\u5165 socket \u521b\u5efa\u548c\u64cd\u4f5c\u51fd\u6570\u5e93<br \/>\n\u200b<br \/>\n#define BUF_SIZE 30 \u00a0 \u00a0 \/\/ \u5b9a\u4e49\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 30 \u5b57\u8282<br \/>\n\u200b<br \/>\nvoid error_handling(char *message); \/\/ \u9519\u8bef\u5904\u7406\u51fd\u6570\u7684\u58f0\u660e<br \/>\n\u200b<br \/>\nint main(int argc, char *argv[]) \/\/ \u7a0b\u5e8f\u4e3b\u5165\u53e3<br \/>\n{<br \/>\n \u00a0 \u00a0int sock; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7528\u4e8e\u5b58\u50a8\u5ba2\u6237\u7aef socket \u63cf\u8ff0\u7b26<br \/>\n \u00a0 \u00a0char message[BUF_SIZE]; \u00a0 \u00a0\/\/ \u5b9a\u4e49\u7f13\u51b2\u533a&#xff0c;\u5b58\u50a8\u53d1\u9001\u7684\u6d88\u606f<br \/>\n \u00a0 \u00a0int str_len; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u5b58\u50a8\u63a5\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6<br \/>\n \u00a0 \u00a0socklen_t adr_sz; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5b58\u50a8\u670d\u52a1\u5668\u5730\u5740\u7684\u5927\u5c0f<br \/>\n\u200b<br \/>\n \u00a0 \u00a0struct sockaddr_in serv_adr, from_adr; \u00a0\/\/ \u670d\u52a1\u5668\u548c\u63a5\u6536\u6570\u636e\u7684\u5730\u5740\u7ed3\u6784<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0if(argc !&#061; 3){ \u00a0\/\/ \u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570\u662f\u5426\u4e3a 3 \u4e2a&#xff08;\u7a0b\u5e8f\u540d\u3001\u670d\u52a1\u5668 IP \u548c\u7aef\u53e3\u53f7&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0printf(&#034;Usage : %s &lt;IP&gt; &lt;port&gt;\\\\n&#034;, argv[0]);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0exit(1); \u00a0\/\/ \u5982\u679c\u53c2\u6570\u4e0d\u6b63\u786e&#xff0c;\u5219\u9000\u51fa\u7a0b\u5e8f<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0\/\/ \u521b\u5efa UDP socket<br \/>\n \u00a0 \u00a0sock &#061; socket(PF_INET, SOCK_DGRAM, 0); \u00a0<br \/>\n \u00a0 \u00a0if(sock &#061;&#061; -1) \u00a0\/\/ \u5982\u679c socket \u521b\u5efa\u5931\u8d25&#xff0c;\u5219\u8c03\u7528\u9519\u8bef\u5904\u7406\u51fd\u6570<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0error_handling(&#034;socket() error&#034;);<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0\/\/ \u521d\u59cb\u5316\u670d\u52a1\u5668\u5730\u5740\u7ed3\u6784<br \/>\n \u00a0 \u00a0memset(&amp;serv_adr, 0, sizeof(serv_adr)); \u00a0\/\/ \u6e05\u7a7a\u5730\u5740\u7ed3\u6784<br \/>\n \u00a0 \u00a0serv_adr.sin_family &#061; AF_INET; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u8bbe\u7f6e\u534f\u8bae\u65cf\u4e3a IPv4<br \/>\n \u00a0 \u00a0serv_adr.sin_addr.s_addr &#061; inet_addr(argv[1]); \u00a0\/\/ \u5c06\u547d\u4ee4\u884c\u63d0\u4f9b\u7684 IP \u5730\u5740\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f<br \/>\n \u00a0 \u00a0serv_adr.sin_port &#061; htons(atoi(argv[2])); \u00a0\/\/ \u5c06\u547d\u4ee4\u884c\u63d0\u4f9b\u7684\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0\/\/ \u8fdb\u5165\u53d1\u9001\u6d88\u606f\u5faa\u73af<br \/>\n \u00a0 \u00a0while(1)<br \/>\n \u00a0  {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0fputs(&#034;Insert message(q to quit): &#034;, stdout); \u00a0\/\/ \u63d0\u793a\u7528\u6237\u8f93\u5165\u6d88\u606f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0fgets(message, sizeof(message), stdin); \u00a0\/\/ \u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u7528\u6237\u6d88\u606f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5982\u679c\u7528\u6237\u8f93\u5165 &#034;q&#034; \u6216 &#034;Q&#034;&#xff0c;\u5219\u9000\u51fa\u5faa\u73af<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0if(!strcmp(message,&#034;q\\\\n&#034;) || !strcmp(message,&#034;Q\\\\n&#034;)) \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0break;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5411\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0sendto(sock, message, strlen(message), 0,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (struct sockaddr*)&amp;serv_adr, sizeof(serv_adr));<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0adr_sz &#061; sizeof(from_adr); \u00a0\/\/ \u83b7\u53d6\u670d\u52a1\u5668\u5730\u5740\u7ed3\u6784\u7684\u5927\u5c0f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u63a5\u6536\u6765\u81ea\u670d\u52a1\u5668\u7684\u56de\u663e\u6d88\u606f<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0str_len &#061; recvfrom(sock, message, BUF_SIZE, 0,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (struct sockaddr*)&amp;from_adr, &amp;adr_sz);<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0message[str_len] &#061; 0; \u00a0\/\/ \u6dfb\u52a0\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0printf(&#034;Message from server: %s&#034;, message); \u00a0\/\/ \u6253\u5370\u4ece\u670d\u52a1\u5668\u63a5\u6536\u5230\u7684\u6d88\u606f<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0close(sock); \u00a0\/\/ \u5173\u95ed socket<br \/>\n \u00a0 \u00a0return 0;<br \/>\n\u200b<br \/>\n}<br \/>\n\u200b<br \/>\n\/\/ \u9519\u8bef\u5904\u7406\u51fd\u6570<br \/>\nvoid error_handling(char *message)<br \/>\n{<br \/>\n \u00a0 \u00a0fputs(message, stderr); \u00a0\/\/ \u5c06\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u6807\u51c6\u9519\u8bef\u6d41<br \/>\n \u00a0 \u00a0fputc(&#039;\\\\n&#039;, stderr); \u00a0 \u00a0 \/\/ \u8f93\u51fa\u6362\u884c\u7b26<br \/>\n \u00a0 \u00a0exit(1); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u9000\u51fa\u7a0b\u5e8f<br \/>\n} <\/p>\n<p>\u5ba2\u6237\u7aef\u7684\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<h5>1.\u547d\u4ee4\u884c\u53c2\u6570\u68c0\u67e5<\/h5>\n<p>\u9996\u5148\u68c0\u67e5\u8f93\u5165\u7684\u53c2\u6570\u662f\u5426\u6b63\u786e&#xff08;\u9700\u8981\u63d0\u4f9b\u670d\u52a1\u5668\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7&#xff09;\u3002\u5982\u679c\u4e0d\u6b63\u786e&#xff0c;\u5219\u8f93\u51fa\u7528\u6cd5\u8bf4\u660e\u5e76\u9000\u51fa\u3002<\/p>\n<h5>2.\u521b\u5efa UDP Socket<\/h5>\n<p>\u901a\u8fc7 socket() \u51fd\u6570\u521b\u5efa\u4e00\u4e2a UDP socket&#xff0c;\u51c6\u5907\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u3002\u5982\u679c\u521b\u5efa\u5931\u8d25&#xff0c;\u5219\u8c03\u7528 error_handling() \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u9000\u51fa\u3002<\/p>\n<h5>3.\u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740<\/h5>\n<p>\u8bbe\u7f6e sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u914d\u7f6e\u670d\u52a1\u5668\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002inet_addr() \u51fd\u6570\u5c06\u8f93\u5165\u7684 IP \u5730\u5740\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f&#xff0c;htons() \u51fd\u6570\u5c06\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u3002<\/p>\n<h5>4.\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u6536<\/h5>\n<ul>\n<li>\n<p>\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u63d0\u793a\u7528\u6237\u8f93\u5165\u6d88\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u8f93\u5165\u7684\u6d88\u606f\u901a\u8fc7 sendto() \u53d1\u9001\u5230\u670d\u52a1\u5668\u3002\u5982\u679c\u8f93\u5165\u7684\u662f &#034;q&#034; \u6216 &#034;Q&#034;&#xff0c;\u5219\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u7b49\u5f85\u670d\u52a1\u5668\u56de\u663e\u6d88\u606f&#xff0c;\u901a\u8fc7 recvfrom() \u51fd\u6570\u63a5\u6536\u670d\u52a1\u5668\u7684\u56de\u5e94\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u5230\u670d\u52a1\u5668\u7684\u6d88\u606f\u540e&#xff0c;\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>5.\u5173\u95ed Socket<\/h5>\n<p>\u5f53\u9000\u51fa\u5faa\u73af\u540e&#xff0c;\u5173\u95ed socket \u8fde\u63a5&#xff0c;\u7ed3\u675f\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002<\/p>\n<h4>\u7a0b\u5e8f\u6267\u884c<\/h4>\n<p>void main(char* argv0) { \u00a0 \/\/ \u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3&#xff0c;\u63a5\u6536\u7a0b\u5e8f\u7684\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570&#xff08;argv0&#xff09;<br \/>\n \u00a0 \u00a0pid_t pid &#061; fork(); \u00a0 \u00a0\/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;fork() \u4f1a\u8fd4\u56de\u4e24\u6b21&#xff0c;\u7236\u8fdb\u7a0b\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684 PID&#xff0c;\u5b50\u8fdb\u7a0b\u8fd4\u56de 0<br \/>\n\u200b<br \/>\n \u00a0 \u00a0if (pid &gt; 0) { \u00a0\/\/ \u5982\u679c pid \u5927\u4e8e 0&#xff0c;\u8868\u793a\u5f53\u524d\u4ee3\u7801\u5728\u7236\u8fdb\u7a0b\u4e2d\u6267\u884c<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u4e3b\u8fdb\u7a0b\u90e8\u5206<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0int argc &#061; 3; \u00a0\/\/ \u4e3b\u8fdb\u7a0b\u7684\u53c2\u6570\u4e2a\u6570\u8bbe\u4e3a 3&#xff08;\u7a0b\u5e8f\u540d\u3001IP \u5730\u5740\u3001\u7aef\u53e3\u53f7&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6784\u9020\u4e3b\u8fdb\u7a0b\u7684\u53c2\u6570\u6570\u7ec4&#xff0c;\u7528\u4e8e\u8c03\u7528 client \u51fd\u6570<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0char* argv[] &#061; {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0argv0, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7a0b\u5e8f\u540d&#xff08;\u4ece\u53c2\u6570\u4f20\u5165&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (char*)&#034;127.0.0.1&#034;, \u00a0\/\/ \u670d\u52a1\u5668\u7684 IP \u5730\u5740<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (char*)&#034;9999&#034;, \u00a0\/\/ \u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0NULL \u00a0\/\/ \u786e\u4fdd\u6570\u7ec4\u4ee5 NULL \u7ed3\u5c3e&#xff0c;\u7b26\u5408 C \u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u89c4\u8303<br \/>\n \u00a0 \u00a0 \u00a0  };<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u8c03\u7528 client \u51fd\u6570&#xff0c;\u4f20\u5165\u4e3b\u8fdb\u7a0b\u7684\u53c2\u6570&#xff08;argc \u548c argv&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0client(argc, argv);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0int status &#061; 0; \u00a0\/\/ \u5b58\u50a8\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\u5e76\u83b7\u53d6\u5176\u9000\u51fa\u72b6\u6001<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0wait(&amp;status);<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0else if (pid &#061;&#061; 0) { \u00a0\/\/ \u5982\u679c pid \u7b49\u4e8e 0&#xff0c;\u8868\u793a\u5f53\u524d\u4ee3\u7801\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5b50\u8fdb\u7a0b\u90e8\u5206<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0int argc &#061; 2; \u00a0\/\/ \u5b50\u8fdb\u7a0b\u7684\u53c2\u6570\u4e2a\u6570\u8bbe\u4e3a 2&#xff08;\u7a0b\u5e8f\u540d\u3001\u7aef\u53e3\u53f7&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6784\u9020\u5b50\u8fdb\u7a0b\u7684\u53c2\u6570\u6570\u7ec4&#xff0c;\u7528\u4e8e\u8c03\u7528 server \u51fd\u6570<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0char* argv[] &#061; {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0argv0, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7a0b\u5e8f\u540d&#xff08;\u4ece\u53c2\u6570\u4f20\u5165&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  (char*)&#034;9999&#034;, \u00a0\/\/ \u76d1\u542c\u7684\u7aef\u53e3\u53f7<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0NULL \u00a0\/\/ \u786e\u4fdd\u6570\u7ec4\u4ee5 NULL \u7ed3\u5c3e<br \/>\n \u00a0 \u00a0 \u00a0  };<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u8c03\u7528 server \u51fd\u6570&#xff0c;\u4f20\u5165\u5b50\u8fdb\u7a0b\u7684\u53c2\u6570&#xff08;argc \u548c argv&#xff09;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0server(argc, argv);<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0else { \u00a0\/\/ \u5982\u679c pid \u5c0f\u4e8e 0&#xff0c;\u8868\u793a fork \u5931\u8d25<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6253\u5370\u9519\u8bef\u4fe1\u606f&#xff0c;\u8868\u793a fork \u8c03\u7528\u5931\u8d25<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0perror(&#034;fork failed&#034;);<br \/>\n \u00a0  }<br \/>\n} <\/p>\n<h4>\u91cd\u8981\u6982\u5ff5\u89e3\u91ca<\/h4>\n<p>serv_adr.sin_family &#061; AF_INET; <\/p>\n<h6>\u529f\u80fd\u6982\u8ff0<\/h6>\n<p>\u8fd9\u884c\u4ee3\u7801\u8bbe\u7f6e serv_adr \u7ed3\u6784\u4f53\u4e2d\u7684 sin_family \u6210\u5458\u4e3a AF_INET&#xff0c;\u8868\u793a\u4f7f\u7528 IPv4 \u534f\u8bae\u65cf\u3002\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;sin_family \u5b57\u6bb5\u7528\u4e8e\u6307\u5b9a\u5730\u5740\u65cf&#xff0c;\u4e0d\u540c\u7684\u5730\u5740\u65cf\u5bf9\u5e94\u4e0d\u540c\u7684\u7f51\u7edc\u534f\u8bae\u3002<\/p>\n<h6>\u77e5\u8bc6\u70b9\u8be6\u89e3<\/h6>\n<ul>\n<li>\n<p>struct sockaddr_in \u7ed3\u6784\u4f53&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8 IPv4 \u5730\u5740\u4fe1\u606f\u7684\u7ed3\u6784\u4f53&#xff0c;\u5b9a\u4e49\u5728 &lt;arpa\/inet.h&gt; \u5934\u6587\u4ef6\u4e2d\u3002serv_adr \u5c31\u662f\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>sin_family \u6210\u5458&#xff1a;\u8be5\u6210\u5458\u7528\u4e8e\u6307\u5b9a\u5730\u5740\u65cf&#xff0c;AF_INET \u662f\u4e00\u4e2a\u5e38\u91cf&#xff0c;\u8868\u793a IPv4 \u534f\u8bae\u65cf\u3002\u5e38\u89c1\u7684\u5730\u5740\u65cf\u8fd8\u6709 AF_INET6 \u8868\u793a IPv6 \u534f\u8bae\u65cf\u3002<\/p>\n<\/li>\n<\/ul>\n<p>serv_adr.sin_addr.s_addr &#061; inet_addr(argv[1]); <\/p>\n<h6>\u529f\u80fd\u6982\u8ff0<\/h6>\n<p>\u8fd9\u884c\u4ee3\u7801\u5c06\u547d\u4ee4\u884c\u63d0\u4f9b\u7684 IP \u5730\u5740&#xff08;argv[1]&#xff09;\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f&#xff0c;\u5e76\u5b58\u50a8\u5728 serv_adr \u7ed3\u6784\u4f53\u7684 sin_addr.s_addr \u6210\u5458\u4e2d\u3002<\/p>\n<h6>\u77e5\u8bc6\u70b9\u8be6\u89e3<\/h6>\n<ul>\n<li>\n<p>sin_addr \u6210\u5458&#xff1a;struct sockaddr_in \u7ed3\u6784\u4f53\u4e2d\u7684 sin_addr \u6210\u5458\u4e5f\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u5176\u5b9a\u4e49\u4e3a struct in_addr sin_addr;&#xff0c;\u7528\u4e8e\u5b58\u50a8 IP \u5730\u5740\u3002sin_addr.s_addr \u662f\u4e00\u4e2a 32 \u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u6570&#xff0c;\u7528\u4e8e\u5b58\u50a8 IPv4 \u5730\u5740\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u3002<\/p>\n<\/li>\n<li>\n<p>inet_addr \u51fd\u6570<\/p>\n<p>\u8be5\u51fd\u6570\u4f4d\u4e8e&lt;arpa\/inet.h&gt;\u5934\u6587\u4ef6\u4e2d&#xff0c;\u5176\u539f\u578b\u4e3ain_addr_t inet_addr(const char *cp);<\/p>\n<ul>\n<li>\n<p>\u53c2\u6570&#xff1a;cp \u662f\u4e00\u4e2a\u6307\u5411\u70b9\u5206\u5341\u8fdb\u5236\u8868\u793a\u7684 IPv4 \u5730\u5740\u5b57\u7b26\u4e32\u7684\u6307\u9488&#xff0c;\u8fd9\u91cc\u662f argv[1]&#xff0c;\u5373\u547d\u4ee4\u884c\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c&#xff1a;\u8fd4\u56de\u4e00\u4e2a in_addr_t \u7c7b\u578b\u7684\u503c&#xff0c;\u8868\u793a\u8f6c\u6362\u540e\u7684\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u7684 IP \u5730\u5740\u3002\u5982\u679c\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u662f\u6709\u6548\u7684 IP \u5730\u5740&#xff0c;inet_addr \u4f1a\u8fd4\u56de INADDR_NONE\u3002\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;inet_addr \u5728\u5904\u7406 255.255.255.255 \u65f6\u53ef\u80fd\u4f1a\u6709\u95ee\u9898&#xff0c;\u73b0\u5728\u66f4\u63a8\u8350\u4f7f\u7528 inet_pton \u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>serv_adr.sin_port &#061; htons(atoi(argv[2])); <\/p>\n<h6>\u529f\u80fd\u6982\u8ff0<\/h6>\n<p>\u8fd9\u884c\u4ee3\u7801\u5c06\u547d\u4ee4\u884c\u63d0\u4f9b\u7684\u7aef\u53e3\u53f7&#xff08;argv[2]&#xff09;\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f&#xff0c;\u5e76\u5b58\u50a8\u5728 serv_adr \u7ed3\u6784\u4f53\u7684 sin_port \u6210\u5458\u4e2d\u3002<\/p>\n<h6>\u77e5\u8bc6\u70b9\u8be6\u89e3<\/h6>\n<ul>\n<li>\n<p>sin_port \u6210\u5458&#xff1a;struct sockaddr_in \u7ed3\u6784\u4f53\u4e2d\u7684 sin_port \u6210\u5458\u662f\u4e00\u4e2a 16 \u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u6570&#xff0c;\u7528\u4e8e\u5b58\u50a8\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>atoi \u51fd\u6570&#xff1a;\u8be5\u51fd\u6570\u4f4d\u4e8e &lt;stdlib.h&gt; \u5934\u6587\u4ef6\u4e2d&#xff0c;\u5176\u539f\u578b\u4e3a int atoi(const char *nptr);\u3002\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570&#xff0c;\u8fd9\u91cc\u5c06 argv[2]&#xff08;\u547d\u4ee4\u884c\u4e2d\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570&#xff09;\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>htons \u51fd\u6570<\/p>\n<p>\u8be5\u51fd\u6570\u4f4d\u4e8e&lt;arpa\/inet.h&gt;\u5934\u6587\u4ef6\u4e2d&#xff0c;\u5176\u539f\u578b\u4e3auint16_t htons(uint16_t hostshort);<\/p>\n<ul>\n<li>\n<p>\u529f\u80fd&#xff1a;htons \u8868\u793a \u201chost to network short\u201d&#xff0c;\u5373\u4ece\u4e3b\u673a\u5b57\u8282\u987a\u5e8f\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u3002\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d&#xff0c;\u5b57\u8282\u5e8f\u53ef\u80fd\u4e0d\u540c&#xff08;\u5927\u7aef\u5e8f\u6216\u5c0f\u7aef\u5e8f&#xff09;&#xff0c;\u800c\u7f51\u7edc\u901a\u4fe1\u4e2d\u8981\u6c42\u4f7f\u7528\u5927\u7aef\u5e8f&#xff08;\u7f51\u7edc\u5b57\u8282\u987a\u5e8f&#xff09;&#xff0c;\u56e0\u6b64\u9700\u8981\u4f7f\u7528 htons \u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p>\u53c2\u6570&#xff1a;hostshort \u662f\u4e00\u4e2a 16 \u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u6570&#xff0c;\u8868\u793a\u4e3b\u673a\u5b57\u8282\u987a\u5e8f\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c&#xff1a;\u8fd4\u56de\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>UDP\u7f51\u7edc\u7f16\u7a0b\u4e00\u822c\u6b65\u9aa4<\/h4>\n<p>\u540c\u65f6\u4e5f\u53ef\u4ee5\u603b\u7ed3\u51faUDP\u7f51\u7edc\u7f16\u7a0b\u7684\u4e00\u822c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<h5>\u521b\u5efaUDP Socket<\/h5>\n<ul>\n<li>\n<p>\u4f7f\u7528\u00a0socket()\u00a0\u51fd\u6570\u6307\u5b9a\u534f\u8bae\u65cf&#xff08;\u901a\u5e38\u4e3a\u00a0PF_INET\u00a0\u6216\u00a0AF_INET&#xff09;\u548c\u901a\u4fe1\u7c7b\u578b&#xff08;\u4f7f\u7528\u00a0SOCK_DGRAM\u00a0\u8868\u793aUDP&#xff09;&#xff0c;\u5efa\u7acb\u4e00\u4e2aUDP\u5957\u63a5\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p>\u6b64\u6b65\u9aa4\u662f\u6574\u4e2a\u901a\u4fe1\u7684\u8d77\u70b9&#xff0c;\u5957\u63a5\u5b57\u5145\u5f53\u7f51\u7edc\u901a\u4fe1\u7684\u7aef\u70b9\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5>\u914d\u7f6e\u5730\u5740\u7ed3\u6784<\/h5>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u5e76\u521d\u59cb\u5316 sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u7528\u6765\u5b58\u50a8IP\u5730\u5740\u548c\u7aef\u53e3\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u670d\u52a1\u5668\u7aef&#xff0c;\u53ef\u4ee5\u5c06 IP \u5730\u5740\u8bbe\u7f6e\u4e3a INADDR_ANY \u4ee5\u5141\u8bb8\u63a5\u6536\u4efb\u610f\u6e90\u7684\u6570\u636e&#xff1b;\u5bf9\u4e8e\u5ba2\u6237\u7aef&#xff0c;\u5219\u9700\u8981\u6307\u5b9a\u76ee\u6807\u670d\u52a1\u5668\u7684\u5177\u4f53IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7 inet_addr() \u548c htons() \u7b49\u51fd\u6570\u8fdb\u884c\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u6362&#xff0c;\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7ed1\u5b9aSocket&#xff08;\u4ec5\u5bf9\u670d\u52a1\u5668&#xff09;<\/p>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668\u7aef\u9700\u8c03\u7528 bind() \u51fd\u6570&#xff0c;\u5c06\u521b\u5efa\u7684socket\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ed1\u5b9a\u540e&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u63a5\u6536\u53d1\u5f80\u8be5\u7aef\u53e3\u7684UDP\u6570\u636e\u62a5\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5>\u6570\u636e\u53d1\u9001\u4e0e\u63a5\u6536<\/h5>\n<ul>\n<li>\n<p>\u63a5\u6536\u6570\u636e&#xff08;\u670d\u52a1\u5668\u7aef&#xff09;&#xff1a; \u4f7f\u7528 recvfrom() \u51fd\u6570\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u6570\u636e\u62a5&#xff0c;\u540c\u65f6\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u5730\u5740\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u6570\u636e&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5bf9\u4e8e\u670d\u52a1\u5668\u7aef&#xff1a;\u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u901a\u8fc7 sendto() \u51fd\u6570\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef&#xff0c;\u5b9e\u73b0\u56de\u58f0\u6216\u54cd\u5e94\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u5ba2\u6237\u7aef&#xff1a;\u4f7f\u7528 sendto() \u5c06\u6d88\u606f\u53d1\u9001\u5230\u670d\u52a1\u5668\u6307\u5b9a\u7684\u5730\u5740&#xff0c;\u7136\u540e\u4f7f\u7528 recvfrom() \u63a5\u6536\u670d\u52a1\u5668\u56de\u590d\u7684\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>UDP\u4e0d\u4fdd\u8bc1\u6570\u636e\u4f20\u8f93\u7684\u53ef\u9760\u6027&#xff0c;\u56e0\u6b64\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u8bbe\u8ba1\u91cd\u53d1\u3001\u4e22\u5305\u68c0\u6d4b\u7b49\u673a\u5236\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5>\u9519\u8bef\u5904\u7406<\/h5>\n<p> \u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff08;\u5982 socket()\u3001bind()\u3001sendto()\u3001recvfrom() \u7b49&#xff09;\u90fd\u5e94\u68c0\u67e5\u5176\u8fd4\u56de\u503c&#xff0c;\u53d1\u73b0\u9519\u8bef\u65f6&#xff0c;\u901a\u8fc7\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236&#xff08;\u4f8b\u5982\u8c03\u7528\u81ea\u5b9a\u4e49\u7684 error_handling() \u51fd\u6570&#xff09;\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u5b89\u5168\u9000\u51fa\u7a0b\u5e8f\u3002<\/li>\n<li>\n<h5>\u8d44\u6e90\u5173\u95ed\u4e0e\u6e05\u7406<\/h5>\n<p> \u5f53\u6570\u636e\u4f20\u8f93\u5b8c\u6210\u6216\u7a0b\u5e8f\u9000\u51fa\u524d&#xff0c;\u52a1\u5fc5\u8c03\u7528 close() \u51fd\u6570\u5173\u95edsocket&#xff0c;\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u907f\u514d\u5185\u5b58\u6cc4\u9732\u548c\u7aef\u53e3\u5360\u7528\u95ee\u9898\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb962\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf30\u6b21\u3002Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\uff1a\u8fd9\u4e2aDemo\u4e3b\u8981\u6f14\u793a\u4e86\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528UDP\u534f\u8bae\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u58f0\u670d\u52a1\u5668\uff08Echo Server\uff09\u4ee5\u53ca\u4e0e\u4e4b\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,78],"topic":[],"class_list":["post-14313","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \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\/14313.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb962\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf30\u6b21\u3002Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\uff1a\u8fd9\u4e2aDemo\u4e3b\u8981\u6f14\u793a\u4e86\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528UDP\u534f\u8bae\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u58f0\u670d\u52a1\u5668\uff08Echo Server\uff09\u4ee5\u53ca\u4e0e\u4e4b\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/14313.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:08:07+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/14313.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/14313.html\",\"name\":\"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:08:07+00:00\",\"dateModified\":\"2025-04-18T13:08:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/14313.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/14313.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/14313.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \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\/14313.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb962\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf30\u6b21\u3002Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\uff1a\u8fd9\u4e2aDemo\u4e3b\u8981\u6f14\u793a\u4e86\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528UDP\u534f\u8bae\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u58f0\u670d\u52a1\u5668\uff08Echo Server\uff09\u4ee5\u53ca\u4e0e\u4e4b\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/14313.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:08:07+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/14313.html","url":"https:\/\/www.wsisp.com\/helps\/14313.html","name":"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:08:07+00:00","dateModified":"2025-04-18T13:08:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/14313.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/14313.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/14313.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u7f51\u7edc\u7f16\u7a0b\u4e4bUDP \u56de\u58f0\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef"}]},{"@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\/14313","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=14313"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/14313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=14313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=14313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=14313"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=14313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}