{"id":76296,"date":"2026-02-22T06:17:35","date_gmt":"2026-02-21T22:17:35","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76296.html"},"modified":"2026-02-22T06:17:35","modified_gmt":"2026-02-21T22:17:35","slug":"csocket%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b-udp%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76296.html","title":{"rendered":"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E4%B8%80.%E7%AB%AF%E5%8F%A3%E5%8F%B7%20VS%C2%A0%20PID-toc\" style=\"margin-left:0px\">\u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID<\/p>\n<p id=\"%E4%BA%8C.%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B%E7%9A%84%E7%B1%BB%E5%9E%8B-toc\" style=\"margin-left:0px\">\u4e8c.\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u7c7b\u578b<\/p>\n<p id=\"%E4%B8%89.socket%E7%BC%96%E7%A8%8B%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:0px\">\u4e09.socket\u7f16\u7a0b\u63a5\u53e3<\/p>\n<p id=\"%E5%9B%9B.%E5%9F%BA%E4%BA%8Eudp%E7%9A%84%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%92%8C%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:0px\">\u56db.\u57fa\u4e8eudp\u7684\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5168\u90e8\u4ee3\u7801<\/p>\n<p id=\"%E5%AE%A2%E6%88%B7%E7%AB%AF-toc\" style=\"margin-left:40px\">\u5ba2\u6237\u7aef<\/p>\n<p id=\"%E6%9C%8D%E5%8A%A1%E7%AB%AF-toc\" style=\"margin-left:40px\">\u670d\u52a1\u7aef<\/p>\n<p id=\"%E4%BA%94.%E8%A7%A3%E9%87%8A%E4%B8%8E%E8%BF%90%E8%A1%8C-toc\" style=\"margin-left:0px\">\u4e94.\u89e3\u91ca\u4e0e\u8fd0\u884c<\/p>\n<p id=\"%E4%B8%80%E4%BA%9B%E7%BB%86%E8%8A%82%EF%BC%9A-toc\" style=\"margin-left:40px\">\u4e00\u4e9b\u7ec6\u8282&#xff1a;<\/p>\n<p id=\"%E5%85%AD.%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:0px\">\u516d.\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E4%B8%80.%E7%AB%AF%E5%8F%A3%E5%8F%B7%20VS%C2%A0%20PID\">\u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID<\/h2>\n<p>pid\u5df2\u7ecf\u80fd\u591f\u6807\u8bc6\u4e00\u53f0\u4e3b\u673a\u4e0a\u7684\u4e00\u4e2a\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\u4e86&#xff0c;\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7aef\u53e3\u53f7&#xff1f;<\/p>\n<li>\u4e0d\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u90fdpid&#xff1b;<\/li>\n<li>\u7cfb\u7edf\u548c\u7f51\u7edc\u529f\u80fd\u89e3\u8026\u3002<\/li>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53e6\u5916&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u7ed1\u5b9a\u591a\u4e2a\u7aef\u53e3&#xff0c;\u4f46\u4e00\u4e2a\u7aef\u53e3\u53ea\u80fd\u88ab\u4e00\u4e2a\u8fdb\u7a0b\u7ed1\u5b9a\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7cfb\u7edf\u5185\u5b9a\u7684\u7aef\u53e3\u53f7\u30100,1023\u3011\u4e00\u822c\u90fd\u8981\u6709\u56fa\u5b9a\u7684\u5e94\u7528\u5c42\u534f\u8bae\u4f7f\u7528&#xff0c;\u5982http&#xff1a;80&#xff0c;https&#xff1a;443\u3002<\/p>\n<h2 id=\"%E4%BA%8C.%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B%E7%9A%84%E7%B1%BB%E5%9E%8B\">\u4e8c.\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u7c7b\u578b<\/h2>\n<li>\u57df\u95f4\u5957\u63a5\u5b57\u7f16\u7a0b\u2014\u2014\u540c\u4e00\u4e2a\u673a\u5668\u5185<\/li>\n<li>\u539f\u59cb\u5957\u63a5\u5b57\u7f16\u7a0b\u2014\u2014\u7f51\u7edc\u5de5\u5177<\/li>\n<li>\u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\u2014\u2014\u7528\u6237\u95f4\u7684\u7f51\u7edc\u901a\u4fe1<\/li>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e0d\u540c\u7684\u5957\u63a5\u5b57\u7f16\u7a0b\u7c7b\u578b\u7684\u63a5\u53e3\u9700\u8981\u662f\u76f8\u540c\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"544\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221733-699a2efda2e88.png\" width=\"543\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5982\u540c\u7528\u4e00\u4e2a\u7edf\u4e00\u7684sockaddr\u4f5c\u4e3a\u57fa\u7c7b&#xff0c;sockaddr_in\u548csockaddr_un\u4f5c\u4e3a\u5b50\u7c7b\u3002\u4f7f\u7528\u7684\u65f6\u5019\u6211\u4eec\u7528if\u8bed\u53e5\u533a\u5206\u5177\u4f53\u662f\u4ec0\u4e48\u7c7b\u578b&#xff0c;\u5982\u540c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801&#xff1a;<\/p>\n<p>if(address-&gt;type &#061;&#061; AF_INET)<br \/>\n{<br \/>\n    \/\/ \u7f51\u7edc\u5957\u63a5\u5b57<br \/>\n}<br \/>\nelse if(address-&gt;type &#061;&#061; AF_UNIX)<br \/>\n{<br \/>\n    \/\/ \u57df\u95f4\u5957\u63a5\u5b57<br \/>\n} <\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e3a\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\u8bbe\u8ba1\u8005\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\u4e0d\u76f4\u63a5\u4f7f\u7528void*\u5462&#xff0c;\u56e0\u4e3a\u90a3\u65f6\u5019C\u8bed\u8a00\u8fd8\u6ca1\u6709void*\u3002<\/p>\n<h2 id=\"%E4%B8%89.socket%E7%BC%96%E7%A8%8B%E6%8E%A5%E5%8F%A3\">\u4e09.socket\u7f16\u7a0b\u63a5\u53e3<\/h2>\n<p>\/\/ \u521b\u5efa socket \u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;TCP\/UDP&#xff0c;\u5ba2\u6237\u7aef &#043; \u670d\u52a1\u5668&#xff09;<br \/>\nint socket(int domain, int type, int protocol);<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7aef\u53e3\u53f7&#xff08;TCP\/UDP&#xff0c;\u670d\u52a1\u5668&#xff09;<br \/>\nint bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/p>\n<p>\/\/ \u5f00\u59cb\u76d1\u542csocket&#xff08;TCP&#xff0c;\u670d\u52a1\u5668&#xff09;<br \/>\nint listen(int sockfd, int backlog);<\/p>\n<p>\/\/  \u63a5\u6536\u8bf7\u6c42&#xff08;TCP&#xff0c;\u670d\u52a1\u5668&#xff09;<br \/>\nint accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<\/p>\n<p>\/\/ \u5efa\u7acb\u8fde\u63a5&#xff08;TCP&#xff0c;\u5ba2\u6237\u7aef&#xff09;<br \/>\nint connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/p>\n<h2 id=\"%E5%9B%9B.%E5%9F%BA%E4%BA%8Eudp%E7%9A%84%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%92%8C%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81\">\u56db.\u57fa\u4e8eudp\u7684\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5168\u90e8\u4ee3\u7801<\/h2>\n<h3 id=\"%E5%AE%A2%E6%88%B7%E7%AB%AF\">\u5ba2\u6237\u7aef<\/h3>\n<p>\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>client.cc:<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n\/\/ #include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;strings.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;          \/\/ sockaddr_in<br \/>\nusing namespace std;<\/p>\n<p>void usage(const std::string&amp; proc)<br \/>\n{<br \/>\n    cout &lt;&lt; &#034;Please use like this : &#034; &#043; proc &#043; &#034; [ip] [port]&#034; &lt;&lt; endl;<br \/>\n}<\/p>\n<p>int main(int argc, char* argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 3)<br \/>\n    {<br \/>\n        usage(argv[0]);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    string serverip &#061; argv[1];<br \/>\n    uint16_t serverport &#061; stoi(argv[2]);<\/p>\n<p>    int sockFd &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n    if(sockFd &lt; 0) cerr &lt;&lt; &#034;sockFd error&#034; &lt;&lt; endl;<\/p>\n<p>    struct sockaddr_in server;<\/p>\n<p>    socklen_t sz &#061; sizeof(server);<br \/>\n    bzero(&amp;server, sz);<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_addr.s_addr &#061; inet_addr(serverip.c_str());<br \/>\n    server.sin_port &#061; htons(serverport);<\/p>\n<p>    std::string temp;<br \/>\n    while(true)<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;\u8bf7\u8f93\u5165\u6d88\u606f&#064; &#034;;<br \/>\n        getline(cin, temp);<\/p>\n<p>        int ret &#061; sendto(sockFd, temp.c_str(), temp.size(), 0, (struct sockaddr*)&amp;server, sz);<br \/>\n        if(ret &lt; 0) std::cerr &lt;&lt; &#034;send error&#034; &lt;&lt; std::endl;<\/p>\n<p>        char buffer[1024];<\/p>\n<p>        struct sockaddr_in retServer;<br \/>\n        socklen_t len &#061; sizeof(retServer);<br \/>\n        int n &#061; recvfrom(sockFd, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr*)&amp;retServer, &amp;len);<br \/>\n        if(n &gt; 0)<br \/>\n        {<br \/>\n            buffer[n] &#061; 0;<br \/>\n            cout &lt;&lt; &#034;\u6536\u5230\u6d88\u606f&#xff1a;&#034; &lt;&lt; buffer &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<br \/>\n    close(sockFd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3 id=\"%E6%9C%8D%E5%8A%A1%E7%AB%AF\">\u670d\u52a1\u7aef<\/h3>\n<p>\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>UdpServer.hpp:<\/p>\n<p>#include &lt;string&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;strings.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n\/\/ #include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;          \/\/ sockaddr_in<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;functional&gt;<\/p>\n<p>using func_t &#061; std::function&lt;std::string(const std::string&amp;)&gt;;<\/p>\n<p>const std::string defaultip &#061; &#034;0.0.0.0&#034;;<br \/>\nconst uint16_t defaultport &#061; 8080;<\/p>\n<p>class UdpServer<br \/>\n{<br \/>\npublic:<br \/>\n    UdpServer():_sockFd(-1)<br \/>\n    {}<br \/>\n    ~UdpServer()<br \/>\n    {<br \/>\n        if(_sockFd &gt;&#061; 0) close(_sockFd);<br \/>\n    }<br \/>\n    void init(const std::string&amp; ip &#061; defaultip, uint16_t port &#061; defaultport)<br \/>\n    {<br \/>\n        _sockFd &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n        if(_sockFd &lt; 0) std::cerr &lt;&lt; &#034;\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25&#034; &lt;&lt; std::endl;<\/p>\n<p>        struct sockaddr_in local;<br \/>\n        bzero(&amp;local, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_addr.s_addr &#061; inet_addr(ip.c_str());<br \/>\n        local.sin_port &#061; htons(port);<\/p>\n<p>        int ret &#061; bind(_sockFd, (const struct sockaddr*)&amp;local, sizeof(local));<br \/>\n        if(ret &lt; 0) std::cerr &lt;&lt; &#034;\u7ed1\u5b9a\u672c\u5730\u5957\u63a5\u5b57\u4fe1\u606f\u5931\u8d25&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    void run(func_t func)<br \/>\n    {<br \/>\n        while(true)<br \/>\n        {<br \/>\n            struct sockaddr_in client;<br \/>\n            socklen_t sz &#061; sizeof(client);<br \/>\n            char buffer[1024];<br \/>\n            int n &#061; recvfrom(_sockFd, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr*)&amp;client, &amp;sz);<br \/>\n            buffer[n] &#061; 0;<\/p>\n<p>            std::string clientip &#061; inet_ntoa(client.sin_addr);<br \/>\n            uint16_t clientport &#061; ntohs(client.sin_port);<\/p>\n<p>            std::cout &lt;&lt; &#034;[&#034; &lt;&lt; clientip &#043; &#034;:&#034; &lt;&lt; clientport &lt;&lt; &#034;] send information : &#034; &lt;&lt; buffer &lt;&lt; std::endl;<\/p>\n<p>            std::string temp &#061; func(buffer);<\/p>\n<p>            int ret &#061; sendto(_sockFd, temp.c_str(), temp.size(), 0, (const struct sockaddr*)&amp;client, sz);<br \/>\n            if(ret &lt; 0) std::cerr &lt;&lt; &#034;send error&#034; &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<br \/>\nprivate:<br \/>\n    int _sockFd;<br \/>\n};<\/p>\n<p>main.cc:<\/p>\n<p>#include &#034;UdpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>std::string echo(const std::string&amp; cData)<br \/>\n{<br \/>\n    return &#034;server : &#034; &#043; cData;<br \/>\n}<\/p>\n<p>std::string command(const std::string&amp; cData)<br \/>\n{<br \/>\n    std::string ret;<br \/>\n    FILE* fp &#061; popen(cData.c_str(), &#034;r&#034;);<br \/>\n    if(fp &#061;&#061; nullptr)<br \/>\n    {<br \/>\n        perror(&#034;popen&#034;);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    while(true)<br \/>\n    {<br \/>\n        char buffer[1024];<br \/>\n        memset(buffer, sizeof(buffer), 0);<br \/>\n        char* ok &#061; fgets(buffer, sizeof(buffer) &#8211; 1, fp);<br \/>\n        if(ok &#061;&#061; nullptr) break;<br \/>\n        ret &#043;&#061; buffer;<br \/>\n    }<br \/>\n    pclose(fp);<\/p>\n<p>    return ret;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::unique_ptr&lt;UdpServer&gt; udpServer(new UdpServer());<br \/>\n    udpServer-&gt;init();<br \/>\n    udpServer-&gt;run(command);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>makefile:<\/p>\n<p>.PHONY:all<br \/>\nall:UdpServer client<br \/>\nUdpServer:main.cc<br \/>\ng&#043;&#043; -o  $&#064; $^ -std&#061;c&#043;&#043;11<br \/>\nclient:client.cc<br \/>\ng&#043;&#043; -o  $&#064; $^ -std&#061;c&#043;&#043;11 -lpthread<br \/>\n.PHONY:clean<br \/>\nclean:<br \/>\nrm -f UdpServer client <\/p>\n<h2 id=\"%E4%BA%94.%E8%A7%A3%E9%87%8A%E4%B8%8E%E8%BF%90%E8%A1%8C\">\u4e94.\u89e3\u91ca\u4e0e\u8fd0\u884c<\/h2>\n<ul>\n<li>\u5ba2\u6237\u7aef&#xff1a;\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32&#xff08;\u53ef\u4ee5\u662f\u666e\u901a\u6d88\u606f\u6216\u7cfb\u7edf\u547d\u4ee4&#xff09;&#xff0c;\u901a\u8fc7 UDP \u53d1\u9001\u7ed9\u6307\u5b9a IP \u548c\u7aef\u53e3\u7684\u670d\u52a1\u7aef&#xff0c;\u7136\u540e\u7b49\u5f85\u5e76\u6253\u5370\u670d\u52a1\u7aef\u7684\u54cd\u5e94\u3002<\/li>\n<li>\u670d\u52a1\u7aef&#xff1a;\u4ee5\u7c7b\u7684\u5f62\u5f0f\u5c01\u88c5&#xff08;UdpServer&#xff09;&#xff0c;\u76d1\u542c\u6307\u5b9a\u7aef\u53e3&#xff0c;\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u6d88\u606f\u540e&#xff0c;\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u5904\u7406&#xff08;\u9ed8\u8ba4\u662f\u6267\u884c\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u7cfb\u7edf\u547d\u4ee4\u5e76\u8fd4\u56de\u6267\u884c\u7ed3\u679c&#xff0c;\u4e5f\u53ef\u5207\u6362\u4e3a\u7b80\u5355\u56de\u663e&#xff09;&#xff0c;\u518d\u5c06\u5904\u7406\u7ed3\u679c\u901a\u8fc7 UDP \u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"636\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221733-699a2efdc79c6.png\" width=\"1591\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u6837&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdc\u7a0b\u8f93\u5165\u547d\u4ee4\u4e86&#xff0c;xshell\u8fdc\u7a0b\u8fde\u63a5\u670d\u52a1\u5668\u5c31\u662f\u8fd9\u6837\u7c7b\u4f3c\u7684\u539f\u7406\u3002<\/p>\n<h3 id=\"%E4%B8%80%E4%BA%9B%E7%BB%86%E8%8A%82%EF%BC%9A\">\u4e00\u4e9b\u7ec6\u8282&#xff1a;<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"28\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221734-699a2efe3eea4.png\" width=\"492\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AF_INET\u6307\u660e\u4f7f\u7528ipv4\u534f\u8bae&#xff0c;sock_DGRAM\u6307\u660e\u9762\u5411\u6570\u636e\u62a5\u7684udp\u534f\u8bae&#xff0c;\u7b2c2\u4e2a\u53c2\u6570\u534f\u8bae\u7c7b\u578b\u4e0d\u7528\u7ba1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"77\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221734-699a2efe4d0aa.png\" width=\"454\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7ed1\u5b9a\u4e4b\u524d\u5c06\u81ea\u5df1\u7684\u4fe1\u606f\u586b\u5165sockaddr_in\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\u3002AF_INET\u8868\u660e\u662f\u7f51\u7edcsocket\u7f16\u7a0b&#xff0c;s_addr\u8868\u660e\u670d\u52a1\u7aef\u7684ip\u5730\u5740&#xff0c;\u8c03\u7528inet _addr\u51fd\u6570\u5c06\u5b83\u8f6c\u6362\u621032\u4f4d\u6570\u5b57\u5e76\u4e14\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u5217&#xff0c;\u4e5f\u5c31\u662f\u5927\u7aef\u3002htons\u4e5f\u5c31\u662f\u5c06short\u7c7b\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u5217&#xff0c;\u4e5f\u5c31\u662f\u5927\u7aef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"21\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221734-699a2efe5b5b0.png\" width=\"785\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5012\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570len \u65e2\u662f\u8f93\u5165\u578b\u53c2\u6570&#xff0c;\u4e5f\u662f\u8f93\u51fa\u578b\u53c2\u6570&#xff1b;\u5012\u6570\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8f93\u51fa\u578b\u53c2\u6570\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"59\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221734-699a2efe6a3a4.png\" width=\"527\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7f51\u7edc\u8f6c\u6210\u4e3b\u673a\u7684\u5b57\u8282\u5e8f&#xff0c;\u5e76\u4e1432\u4f4d\u6570\u5b57\u7684ip\u8f6c\u6362\u4e3a\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<h2 id=\"%E5%85%AD.%E6%80%BB%E7%BB%93\">\u516d.\u603b\u7ed3<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6269\u5c55\u3002\u5c06\u8fd9\u4e2a\u670d\u52a1\u5f04\u6210\u591a\u4eba\u804a\u5929\u3002\u670d\u52a1\u7aef\u7528\u4e00\u4e2a\u54c8\u5e0c\u548c\u5ba2\u6237\u7aef\u7684ip\u5730\u5740\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u7ba1\u7406\u8d77\u6765\u4e4b\u540e\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u6761\u6d88\u606f\u8fdb\u884c\u5e7f\u64ad\u7ed9\u6bcf\u4e00\u4e2a\u5ba2\u6237\u7aef\u4e86&#xff0c;\u5ba2\u6237\u7aef\u662f\u591a\u7ebf\u7a0b\u7684&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u53d1\u9001\u6d88\u606f&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u63a5\u6536\u6d88\u606f&#xff0c;\u4e3a\u4e86\u53d1\u9001\u548c\u63a5\u6536\u4e0d\u5728\u4e00\u4e2a\u7ec8\u7aef\u8fdb\u884c&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5c06\u63a5\u6536\u7ebf\u7a0b\u91cc\u7684\u6807\u51c6\u8f93\u51fa\u6216\u8005\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\/dev\/pts\/\u67d0\u67d0\u5e8f\u53f7\u7684\u8fd9\u4e2a\u6587\u4ef6\u5939&#xff0c;\u7528dup2\u8fd9\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u505a\u5230&#xff0c;\u7136\u540e\u6211\u4eec\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ec8\u7aef\u53d1\u9001\u6d88\u606f&#xff0c;\u4e00\u4e2a\u7ec8\u7aef\u63a5\u6536\u6d88\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID<br \/>\n\u4e8c.\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u7c7b\u578b<br \/>\n\u4e09.socket\u7f16\u7a0b\u63a5\u53e3<br \/>\n\u56db.\u57fa\u4e8eudp\u7684\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5168\u90e8\u4ee3\u7801<br \/>\n\u5ba2\u6237\u7aef<br \/>\n\u670d\u52a1\u7aef<br \/>\n\u4e94.\u89e3\u91ca\u4e0e\u8fd0\u884c<br \/>\n\u4e00\u4e9b\u7ec6\u8282&#xff1a;<br \/>\n\u516d.\u603b\u7ed3 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID<br \/>\npid\u5df2\u7ecf\u80fd\u591f\u6807\u8bc6\u4e00\u53f0\u4e3b\u673a\u4e0a\u7684\u4e00\u4e2a\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\u4e86&#xff0c;\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7aef\u53e3\u53f7&#xff1f;<br \/>\n\u4e0d\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u90fdpid&#xff1b;\u7cfb\u7edf\u548c\u7f51\u7edc\u529f\u80fd\u89e3\u8026\u3002 \u53e6\u5916&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5<\/p>\n","protected":false},"author":2,"featured_media":76290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,193,78],"topic":[],"class_list":["post-76296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-udp","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \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\/76296.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID \u4e8c.\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u7c7b\u578b \u4e09.socket\u7f16\u7a0b\u63a5\u53e3 \u56db.\u57fa\u4e8eudp\u7684\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5168\u90e8\u4ee3\u7801 \u5ba2\u6237\u7aef \u670d\u52a1\u7aef \u4e94.\u89e3\u91ca\u4e0e\u8fd0\u884c \u4e00\u4e9b\u7ec6\u8282&#xff1a; \u516d.\u603b\u7ed3 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID pid\u5df2\u7ecf\u80fd\u591f\u6807\u8bc6\u4e00\u53f0\u4e3b\u673a\u4e0a\u7684\u4e00\u4e2a\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\u4e86&#xff0c;\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7aef\u53e3\u53f7&#xff1f; \u4e0d\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u90fdpid&#xff1b;\u7cfb\u7edf\u548c\u7f51\u7edc\u529f\u80fd\u89e3\u8026\u3002 \u53e6\u5916&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76296.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-21T22:17:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221733-699a2efda2e88.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\/76296.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76296.html\",\"name\":\"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-21T22:17:35+00:00\",\"dateModified\":\"2026-02-21T22:17:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76296.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76296.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76296.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668\"}]},{\"@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":"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \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\/76296.html","og_locale":"zh_CN","og_type":"article","og_title":"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID \u4e8c.\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u7c7b\u578b \u4e09.socket\u7f16\u7a0b\u63a5\u53e3 \u56db.\u57fa\u4e8eudp\u7684\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5168\u90e8\u4ee3\u7801 \u5ba2\u6237\u7aef \u670d\u52a1\u7aef \u4e94.\u89e3\u91ca\u4e0e\u8fd0\u884c \u4e00\u4e9b\u7ec6\u8282&#xff1a; \u516d.\u603b\u7ed3 \u4e00.\u7aef\u53e3\u53f7 VS\u00a0 PID pid\u5df2\u7ecf\u80fd\u591f\u6807\u8bc6\u4e00\u53f0\u4e3b\u673a\u4e0a\u7684\u4e00\u4e2a\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\u4e86&#xff0c;\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7aef\u53e3\u53f7&#xff1f; \u4e0d\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u9700\u8981\u90fdpid&#xff1b;\u7cfb\u7edf\u548c\u7f51\u7edc\u529f\u80fd\u89e3\u8026\u3002 \u53e6\u5916&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5","og_url":"https:\/\/www.wsisp.com\/helps\/76296.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-21T22:17:35+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260221221733-699a2efda2e88.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\/76296.html","url":"https:\/\/www.wsisp.com\/helps\/76296.html","name":"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-21T22:17:35+00:00","dateModified":"2026-02-21T22:17:35+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76296.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76296.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76296.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++socket\u7f51\u7edc\u7f16\u7a0b\u2014\u2014udp\u670d\u52a1\u5668"}]},{"@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\/76296","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=76296"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/76290"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76296"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}