{"id":22741,"date":"2025-04-19T06:21:25","date_gmt":"2025-04-18T22:21:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/22741.html"},"modified":"2025-04-19T06:21:25","modified_gmt":"2025-04-18T22:21:25","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%9c%ac%e5%9c%b0%e6%90%ad%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/22741.html","title":{"rendered":"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa"},"content":{"rendered":"<h2>socket\u51fd\u6570<\/h2>\n<p>\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u3002<\/p>\n<h5>\u51fd\u6570\u539f\u578b<\/h5>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\nint socket(int domain, int type, int protocol);<\/p>\n<h5>\u53c2\u6570\u89e3\u91ca<\/h5>\n<p>domain&#xff1a;\u5b83\u6307\u5b9a\u4e86\u901a\u4fe1\u6240\u4f7f\u7528\u7684\u534f\u8bae\u65cf&#xff0c;\u5e38\u89c1\u7684\u53d6\u503c\u5982\u4e0b&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AF_INET&#xff1a;\u4ee3\u8868 IPv4 \u534f\u8bae\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AF_INET6&#xff1a;\u4ee3\u8868 IPv6 \u534f\u8bae\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AF_UNIX\u6216AF_LOCAL&#xff1a;\u7528\u4e8e\u672c\u5730&#xff08;Unix \u57df&#xff09;\u5957\u63a5\u5b57\u901a\u4fe1\u3002<\/p>\n<p>type&#xff1a;\u5b83\u6307\u5b9a\u4e86\u5957\u63a5\u5b57\u7684\u7c7b\u578b&#xff0c;\u5e38\u7528\u7684\u7c7b\u578b\u6709&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SOCK_STREAM&#xff1a;\u8868\u793a\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684 TCP \u5957\u63a5\u5b57\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SOCK_DGRAM&#xff1a;\u8868\u793a\u65e0\u8fde\u63a5\u7684\u3001\u4e0d\u53ef\u9760\u7684 UDP \u5957\u63a5\u5b57\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SOCK_RAW&#xff1a;\u5141\u8bb8\u7a0b\u5e8f\u76f4\u63a5\u8bbf\u95ee\u5e95\u5c42\u534f\u8bae&#xff0c;\u5982 IP\u3001ICMP \u7b49\u3002<\/p>\n<p>protocol&#xff1a;\u901a\u5e38\u8bbe\u7f6e\u4e3a 0&#xff0c;\u8868\u793a\u8ba9\u7cfb\u7edf\u6839\u636edomain\u548ctype\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u534f\u8bae\u3002<\/p>\n<h5>\u8fd4\u56de\u503c<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u51fd\u6570\u8c03\u7528\u6210\u529f&#xff0c;\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff08;\u975e\u8d1f\u6574\u6570&#xff09;&#xff0c;\u540e\u7eed\u7684\u7f51\u7edc\u64cd\u4f5c\u4f1a\u7528\u5230\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u8c03\u7528\u5931\u8d25&#xff0c;\u4f1a\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6eerrno\u6765\u6307\u793a\u5177\u4f53\u7684\u9519\u8bef\u539f\u56e0\u3002<\/p>\n<h4>\u793a\u4f8b<\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n\/\/ \u521d\u59cb\u5316\u7f51\u7edc<br \/>\nint socketfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\nif (socketfd &lt; 0)<br \/>\n{<br \/>\n    \/*\u53ef\u80fd\u60c5\u51b5<br \/>\n*1\u3001\u6ca1\u6709\u8fde\u63a5\u7f51\u7edc<br \/>\n*2\u3001\u7f51\u5361\u574f\u4e86<br \/>\n*\/<br \/>\nperror(&#034;socket error&#034;);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\ncout &lt;&lt; &#034;\u670d\u52a1\u5668\u7f51\u7edc\u521d\u59cb\u5316 socketfd&#061;&#034; &lt;&lt; socketfd &lt;&lt; endl;<br \/>\n}<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<h4>\u00a0\u7ed3\u679c<\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"65\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418222124-6802d06457fef.png\" width=\"238\" \/><\/p>\n<\/p>\n<h2>\u00a0bind\u51fd\u6570<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7528\u4e8e\u5c06\u4e00\u4e2a\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u4e0e\u7279\u5b9a\u7684\u7f51\u7edc\u5730\u5740\u548c\u7aef\u53e3\u53f7\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002<\/p>\n<h5>\u51fd\u6570\u539f\u578b<\/h5>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\nint bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/p>\n<h5>\u53c2\u6570\u89e3\u91ca<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sockfd&#xff1a;\u8fd9\u662f\u7531socket\u51fd\u6570\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u4ee3\u8868\u8981\u8fdb\u884c\u7ed1\u5b9a\u64cd\u4f5c\u7684\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addr&#xff1a;\u6307\u5411\u4e00\u4e2asockaddr\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u6307\u9488&#xff0c;\u8be5\u7ed3\u6784\u4f53\u5305\u542b\u4e86\u8981\u7ed1\u5b9a\u7684\u5730\u5740\u548c\u7aef\u53e3\u4fe1\u606f\u3002\u4e0d\u8fc7\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d&#xff0c;\u901a\u5e38\u4f1a\u4f7f\u7528\u5177\u4f53\u7684\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u5982struct sockaddr_in&#xff08;\u7528\u4e8e IPv4&#xff09;\u6216struct sockaddr_in6&#xff08;\u7528\u4e8e IPv6&#xff09;&#xff0c;\u7136\u540e\u8fdb\u884c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addrlen&#xff1a;\u8868\u793aaddr\u6240\u6307\u5411\u7684\u7ed3\u6784\u4f53\u7684\u957f\u5ea6\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u8865&#xff08;struct sockaddr_in&#xff09;&#xff1a;<\/span><\/p>\n<p> #include &lt;netinet\/in.h&gt;<\/p>\n<p>struct sockaddr_in {<br \/>\n    sa_family_t    sin_family;  \/\/ \u5730\u5740\u65cf&#xff0c;\u901a\u5e38\u662f AF_INET&#xff08;IPv4&#xff09;<br \/>\n    in_port_t      sin_port;    \/\/ \u7aef\u53e3\u53f7&#xff0c;\u4f7f\u7528\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef\u5e8f&#xff09;&#xff08;\u670d\u52a1\u5668\u7cfb\u7edf\u9ed8\u8ba4IP\u5730\u5740&#xff1a;INADDR_ANY&#xff09;<br \/>\n    struct in_addr sin_addr;    \/\/ IPv4 \u5730\u5740&#xff0c;\u4f7f\u7528\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n    unsigned char  sin_zero[8]; \/\/ \u586b\u5145\u5b57\u6bb5&#xff0c;\u4f7f\u5176\u5927\u5c0f\u4e0e struct sockaddr \u76f8\u540c&#xff0c;\u4e00\u822c\u8bbe\u7f6e\u4e3a\u5168 0<br \/>\n}; <\/p>\n<p>\u8fd4\u56de\u503c<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u7ed1\u5b9a\u6210\u529f&#xff0c;\u8fd4\u56de 0\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u5931\u8d25&#xff0c;\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6eerrno\u6765\u6307\u793a\u5177\u4f53\u7684\u9519\u8bef\u539f\u56e0\u3002<\/p>\n<\/p>\n<h2>listen\u51fd\u6570<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3b\u8981\u7528\u4e8e\u5c06\u4e00\u4e2a\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u8bbe\u7f6e\u4e3a\u76d1\u542c\u72b6\u6001&#xff0c;\u4ee5\u4fbf\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u00a0<\/p>\n<h5>\u51fd\u6570\u539f\u578b<\/h5>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\nint listen(int sockfd, int backlog);<\/p>\n<h5>\u53c2\u6570\u89e3\u91ca<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sockfd&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u7531socket\u51fd\u6570\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u4ee3\u8868\u8981\u8bbe\u7f6e\u4e3a\u76d1\u542c\u72b6\u6001\u7684\u5957\u63a5\u5b57\u3002\u8be5\u5957\u63a5\u5b57\u5fc5\u987b<span style=\"color:#fe2c24\">\u5df2\u7ecf\u901a\u8fc7bind\u51fd\u6570\u7ed1\u5b9a<\/span>\u5230\u4e86\u4e00\u4e2a\u7279\u5b9a\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0backlog&#xff1a;\u6307\u5b9a\u4e86\u5141\u8bb8\u5728\u961f\u5217\u4e2d\u7b49\u5f85\u5904\u7406\u7684\u6700\u5927\u8fde\u63a5\u8bf7\u6c42\u6570\u91cf\u3002\u5f53\u6709\u591a\u4e2a\u5ba2\u6237\u7aef\u540c\u65f6\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\u65f6&#xff0c;\u670d\u52a1\u5668\u65e0\u6cd5\u7acb\u5373\u5904\u7406\u6240\u6709\u8bf7\u6c42&#xff0c;\u8fd9\u4e9b\u8bf7\u6c42\u4f1a\u88ab\u653e\u5165\u4e00\u4e2a\u961f\u5217\u4e2d\u7b49\u5f85\u5904\u7406\u3002backlog\u7684\u503c\u51b3\u5b9a\u4e86\u8fd9\u4e2a\u961f\u5217\u7684\u6700\u5927\u957f\u5ea6\u3002\u4e0d\u540c\u7cfb\u7edf\u5bf9backlog\u7684\u6700\u5927\u503c\u6709\u4e0d\u540c\u7684\u9650\u5236&#xff0c;\u4e00\u822c\u6765\u8bf4&#xff0c;\u5e38\u89c1\u7684\u503c\u53ef\u4ee5\u8bbe\u7f6e\u4e3a<span style=\"color:#fe2c24\"> 5 \u6216 10<\/span>\u3002<\/p>\n<h5>\u8fd4\u56de\u503c<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u51fd\u6570\u8c03\u7528\u6210\u529f&#xff0c;\u8fd4\u56de 0\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u8c03\u7528\u5931\u8d25&#xff0c;\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6eerrno\u6765\u6307\u793a\u5177\u4f53\u7684\u9519\u8bef\u539f\u56e0\u3002<\/p>\n<\/p>\n<h2>accept\u51fd\u6570\u00a0<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3b\u8981\u7528\u4e8e\u670d\u52a1\u5668\u7aef&#xff0c;\u5b83\u4f1a\u8ba9\u670d\u52a1\u5668\u5904\u4e8e\u963b\u585e\u72b6\u6001&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u4e00\u65e6\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;accept\u51fd\u6570\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u7528\u4e8e\u548c\u5ba2\u6237\u7aef\u8fdb\u884c\u6570\u636e\u901a\u4fe1\u3002\u800c\u539f\u6765\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u4f9d\u65e7\u8d1f\u8d23\u76d1\u542c\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<h5>\u51fd\u6570\u539f\u578b<\/h5>\n<p>#include &lt;sys\/socket.h&gt;<\/p>\n<p>int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<\/p>\n<h5>\u53c2\u6570\u89e3\u91ca<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sockfd&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u5df2\u7ecf\u5904\u4e8e\u76d1\u542c\u72b6\u6001\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u5b83\u662f\u901a\u8fc7socket()\u521b\u5efa&#xff0c;\u518d\u7531bind()\u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u6700\u540e\u4f7f\u7528listen()\u5f00\u59cb\u76d1\u542c\u7684\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addr&#xff08;\u524d\u9762\u8bbe\u7f6e\u8fc7\u7684\u5316\u4e00\u822c\u4e3aNULL&#xff09;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u6307\u5411sockaddr\u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u8be5\u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u50a8\u5ba2\u6237\u7aef\u7684\u5730\u5740\u4fe1\u606f\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u65f6&#xff0c;\u901a\u5e38\u4f1a\u4f7f\u7528sockaddr_in&#xff08;\u7528\u4e8e IPv4&#xff09;\u6216sockaddr_in6&#xff08;\u7528\u4e8e IPv6&#xff09;\u7ed3\u6784\u4f53&#xff0c;\u7136\u540e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addrlen&#xff08;\u524d\u9762\u8bbe\u7f6e\u8fc7\u7684\u5316\u4e00\u822c\u4e3aNULL&#xff09;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u6307\u5411socklen_t\u7c7b\u578b\u7684\u6307\u9488&#xff0c;\u5b83\u8868\u793aaddr\u6240\u6307\u5411\u7684\u7ed3\u6784\u4f53\u7684\u957f\u5ea6\u3002\u5728\u8c03\u7528accept\u4e4b\u524d&#xff0c;\u9700\u8981\u5c06\u5176\u521d\u59cb\u5316\u4e3aaddr\u7ed3\u6784\u4f53\u7684\u5927\u5c0f&#xff1b;\u8c03\u7528\u5b8c\u6210\u540e&#xff0c;\u8be5\u6307\u9488\u6307\u5411\u7684\u503c\u4f1a\u88ab\u66f4\u65b0\u4e3a\u5b9e\u9645\u5b58\u50a8\u7684\u5ba2\u6237\u7aef\u5730\u5740\u4fe1\u606f\u7684\u957f\u5ea6\u3002<\/p>\n<h5>\u8fd4\u56de\u503c<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u8c03\u7528\u6210\u529f&#xff0c;accept\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u6b64\u63cf\u8ff0\u7b26\u7528\u4e8e\u548c\u5ba2\u6237\u7aef\u8fdb\u884c\u6570\u636e\u901a\u4fe1\u3002\u539f\u6765\u7684sockfd\u4ecd\u7136\u7528\u4e8e\u76d1\u542c\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u82e5\u8c03\u7528\u5931\u8d25&#xff0c;\u4f1a\u8fd4\u56de -1\u3002<\/p>\n<\/p>\n<h2>\u6574\u5408\u793a\u4f8b<\/h2>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\nstruct sockaddr_in addr;<br \/>\nint length &#061; 0;<br \/>\nint acceptfd &#061; 0;<br \/>\n\/\/ \u521d\u59cb\u5316\u7f51\u7edc<br \/>\nint socketfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\nif (socketfd &lt; 0)<br \/>\n{<br \/>\n    \/*\u53ef\u80fd\u60c5\u51b5<br \/>\n*1\u3001\u6ca1\u6709\u8fde\u63a5\u7f51\u7edc<br \/>\n*2\u3001\u7f51\u5361\u574f\u4e86<br \/>\n*\/<br \/>\nperror(&#034;socket error&#034;);<br \/>\nreturn 0;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\naddr.sin_family &#061; AF_INET;<br \/>\n\/\/ \u670d\u52a1\u5668\u7cfb\u7edf\u9ed8\u8ba4IP\u5730\u5740<br \/>\naddr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\naddr.sin_port &#061; htons(10001);<br \/>\nlength &#061; sizeof(addr);<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7aef\u53e3\u53f7<br \/>\nif (bind(socketfd, (struct sockaddr*)(&amp;addr), length) &#061;&#061; -1)<br \/>\n{<br \/>\nperror(&#034;bind error&#034;);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>\/\/ \u76d1\u542c\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u662f\u5426\u6709\u5ba2\u6237\u7aef\u6765\u8fde\u63a5<br \/>\nif (listen(socketfd, 10) &#061;&#061; -1)<br \/>\n{<br \/>\nperror(&#034;lister error&#034;);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>cout &lt;&lt; &#034;\u670d\u52a1\u5668\u7f51\u7edc\u642d\u5efa\u6210\u529f&#034; &lt;&lt; endl;<\/p>\n<p>\/\/ \u56e0\u4e3a\u670d\u52a1\u566824h\u957f\u65f6\u95f4\u5f00\u673a<br \/>\nwhile (1)<br \/>\n{<br \/>\n\/\/ acceptfd\u4ee3\u8868\u5df2\u7ecf\u8fde\u63a5\u6210\u529f\u7684\u5ba2\u6237\u7aef<br \/>\n\/\/ \u963b\u585e\u5f0f\u51fd\u6570&#xff08;\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u5230\u6765&#xff09;<br \/>\nacceptfd &#061; accept(socketfd, NULL, NULL);<br \/>\ncout &lt;&lt; &#034;\u5ba2\u6237\u7aef\u4e0a\u7ebf,acceptfd&#061;&#034; &lt;&lt; acceptfd &lt;&lt; endl;<br \/>\n}<br \/>\n}<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<h2>\u7ed3\u679c\u00a0<\/h2>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"61\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418222124-6802d064706df.png\" width=\"436\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\uff08socket\uff09\u3002<\/p>\n","protected":false},"author":2,"featured_media":22739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,1748,78],"topic":[],"class_list":{"0":"post-22741","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-c","9":"tag-linux","11":"tag-78"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \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\/22741.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\uff08socket\uff09\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/22741.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-18T22:21:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418222124-6802d06457fef.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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/22741.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/22741.html\",\"name\":\"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T22:21:25+00:00\",\"dateModified\":\"2025-04-18T22:21:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/22741.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/22741.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/22741.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa\"}]},{\"@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":"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \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\/22741.html","og_locale":"zh_CN","og_type":"article","og_title":"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\uff08socket\uff09\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/22741.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T22:21:25+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418222124-6802d06457fef.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/22741.html","url":"https:\/\/www.wsisp.com\/helps\/22741.html","name":"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T22:21:25+00:00","dateModified":"2025-04-18T22:21:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/22741.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/22741.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/22741.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u670d\u52a1\u5668\u672c\u5730\u642d\u5efa"}]},{"@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\/22741","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=22741"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/22741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/22739"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=22741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=22741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=22741"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=22741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}