{"id":111646,"date":"2026-10-01T12:57:09","date_gmt":"2026-10-01T04:57:09","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/111646.html"},"modified":"2026-10-01T12:57:09","modified_gmt":"2026-10-01T04:57:09","slug":"tcpsocket%e7%bc%96%e7%a8%8b%e4%bb%8eaccept%e5%88%b0%e7%ba%bf%e7%a8%8b%e6%b1%a0%e8%bf%9e%e6%8e%a5%e5%9e%8b%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%9a%84%e7%ac%ac%e4%b8%80%e8%af%be","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/111646.html","title":{"rendered":"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe"},"content":{"rendered":"<h2>TCP socket \u7f16\u7a0b&#xff1a;\u4ece accept \u5230\u7ebf\u7a0b\u6c60&#xff0c;\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe<\/h2>\n<p>\u6211\u7684github&#xff1a;(https:\/\/github.com\/xcx55\/ubuntu-linux-project)<br \/>\n\u611f\u8c22\u5404\u4f4d\u5927\u4f6c\u53c2\u89c2\u6211\u7684github&#xff01;&#xff01;<\/p>\n<p>UDP \u90a3\u6761\u7ebf\u8d70\u5b8c\u540e&#xff0c;8 \u6708 30 \u53f7\u5230 9 \u6708 1 \u53f7\u8fdb\u5165\u4e86 TCP \u7f16\u7a0b\u3002\u548c UDP \u76f8\u6bd4&#xff0c;TCP \u7684\u63a5\u53e3\u591a\u4e86\u4e00\u5c42&#034;\u8fde\u63a5&#034;\u7684\u6982\u5ff5&#xff0c;\u800c\u8fd9\u4e00\u5c42\u8fde\u63a5\u5e26\u6765\u4e86\u4e00\u6574\u5957\u65b0\u7684\u95ee\u9898&#xff1a;\u670d\u52a1\u7aef\u600e\u4e48\u540c\u65f6\u670d\u52a1\u591a\u4e2a\u5ba2\u6237\u7aef&#xff1f;\u4e32\u884c\u3001\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u6c60\u8be5\u600e\u4e48\u9009&#xff1f;\u8fd9\u7bc7\u6309\u7b14\u8bb0\u7684\u63a8\u8fdb\u987a\u5e8f\u6574\u7406\u3002<\/p>\n<h3>\u4e00\u3001TCP \u548c UDP \u7f16\u7a0b\u6a21\u578b\u7684\u672c\u8d28\u5dee\u5f02<\/h3>\n<p>\u5148\u6293\u4f4f\u4e00\u53e5\u8bdd\u6838\u5fc3&#xff1a;UDP \u662f&#034;\u4e00\u4e2a socket \u6587\u4ef6\u6253\u5929\u4e0b&#034;&#xff0c;TCP \u662f&#034;\u4e00\u4e2a\u76d1\u542c\u6587\u4ef6 &#043; \u4e00\u5806\u8fde\u63a5\u6587\u4ef6&#034;\u3002<\/p>\n<ul>\n<li>UDP&#xff1a;\u6570\u636e\u76f4\u63a5\u5199\u8fdb socket \u6587\u4ef6\u91cc&#xff0c;\u62a5\u6587\u81ea\u5e26 sockaddr_in\u2014\u2014\u6bcf\u4e2a\u62a5\u6587\u90fd&#034;\u81ea\u5e26\u5730\u5740&#034;&#xff0c;recvfrom \u4e00\u6b21\u62ff\u6570\u636e\u4e00\u6b21\u62ff\u5bf9\u7aef\u5730\u5740&#xff0c;\u8c01\u53d1\u7684\u5c31\u56de\u7ed9\u8c01&#xff1b;<\/li>\n<li>TCP&#xff1a;\u5148 listen &#043; connect \u5efa\u7acb\u8fde\u63a5&#xff0c;\u7136\u540e accept \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 fd&#xff0c;\u540e\u7eed\u7684\u8bfb\u5199\u90fd\u53d1\u751f\u5728\u8fd9\u4e2a\u65b0 fd \u4e0a\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5230 accept \u7684\u52a8\u4f5c&#xff1a;<\/p>\n<li>accept \u7b49\u5f85\u5ba2\u6237\u7aef\u7684 connect&#xff0c;\u62ff\u5230\u5ba2\u6237\u7aef\u7684 sockaddr_in&#xff1b;<\/li>\n<li>\u628a\u8fd9\u4e2a sockaddr_in \u4ea4\u7ed9\u5f53\u524d\u6253\u5f00\u7684 sockfd \u6587\u4ef6\u8bb0\u5f55&#xff1b;<\/li>\n<li>\u518d\u6253\u5f00\u4e00\u4e2a\u65b0 file&#xff0c;\u4f5c\u4e3a\u540e\u7eed read\/write \u7684\u5bf9\u8c61\u3002<\/li>\n<p>\u4e00\u4e2a\u6709\u610f\u601d\u7684\u5bf9\u7167&#xff1a;TCP \u548c UDP \u62ff\u53d6 sockaddr_in \u7684\u65b9\u5f0f\u4e00\u6a21\u4e00\u6837\u2014\u2014\u533a\u522b\u53ea\u5728\u4e8e UDP \u662f\u6bcf\u4e2a\u62a5\u6587\u90fd\u5e26&#xff0c;TCP \u662f\u8fde\u63a5\u5efa\u7acb\u65f6\u5e26\u4e00\u6b21\u3001\u7ec8\u8eab\u6709\u6548\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5bf9\u7167&#xff1a;listensockfd \u4e00\u4e2a\u6587\u4ef6\u670d\u52a1\u591a\u4e2a ip \u548c\u7aef\u53e3\u2014\u2014\u5b83\u662f&#034;\u63a5\u5ba2\u7684\u95e8\u7ae5&#034;&#xff0c;\u81ea\u5df1\u4e0d\u8fd0\u8d27&#xff1b;\u8fd0\u8d27\u7684\u662f accept \u8fd4\u56de\u7684\u90a3\u4e00\u4e2a\u4e2a\u8fde\u63a5 fd\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u6761\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u5e95\u5c42\u4e8b\u5b9e&#xff1a;read \u51fd\u6570\u4f1a\u5206\u522b\u5224\u65ad fd \u7684\u7c7b\u578b\u2014\u2014\u7ba1\u9053\u3001\u666e\u901a\u6587\u4ef6\u3001\u7f51\u7edc\u6587\u4ef6&#xff08;TCP \u7684\u6587\u4ef6&#xff09;\u5404\u8d70\u5404\u7684\u5206\u652f\u3002\u8fd9\u5c31\u662f&#034;\u4e00\u5207\u7686\u6587\u4ef6&#034;\u7684\u4ee3\u4ef7\u4e0e\u56de\u62a5&#xff1a;\u63a5\u53e3\u7edf\u4e00&#xff0c;\u5185\u90e8\u5206\u6d41\u3002<\/p>\n<h3>\u4e8c\u3001\u670d\u52a1\u591a\u4e2a\u5ba2\u6237\u7aef&#xff1a;\u56db\u79cd\u59ff\u52bf\u7684\u6f14\u8fdb<\/h3>\n<p>\u5ba2\u6237\u7aef\u4e00\u591a&#xff0c;\u95ee\u9898\u7acb\u523b\u6765\u4e86&#xff1a;\u5982\u679c\u5728\u4e00\u4e2a\u5faa\u73af\u91cc\u4e32\u884c\u5730 ServerIO(fd, pq)&#xff0c;\u4e00\u4e2a\u5ba2\u6237\u7aef\u6ca1\u5904\u7406\u5b8c&#xff0c;\u4e0b\u4e00\u4e2a accept \u5c31\u88ab\u5835\u7740\u2014\u2014\u4e32\u884c\u6267\u884c\u662f\u963b\u585e\u7684\u3002\u7b14\u8bb0\u91cc\u628a\u89e3\u51b3\u65b9\u6848\u6309\u5386\u53f2\u987a\u5e8f\u6392\u4e86\u4e00\u904d&#xff1a;<\/p>\n<h4>1. \u4e32\u884c<\/h4>\n<p>\u6700\u6734\u7d20&#xff1a;while(true) { accept \u2192 ServerIO(fd, pq) \u2192 close(fd); }\u3002\u80fd\u7528&#xff0c;\u4f46\u4e00\u6b21\u53ea\u80fd\u4f3a\u5019\u4e00\u4e2a\u4eba\u3002<\/p>\n<h4>2. \u591a\u8fdb\u7a0b<\/h4>\n<p>\u6bcf\u4e2a\u5ba2\u6237\u7aef fork \u4e00\u4e2a\u5b50\u8fdb\u7a0b\u53bb\u5904\u7406\u3002\u8981\u914d\u4e00\u53e5 signal(SIGCHLD, SIG_IGN) \u8ba9\u5185\u6838\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b&#xff0c;\u5426\u5219\u50f5\u5c38\u8fdb\u7a0b\u5806\u79ef\u3002\u4f46\u7b14\u8bb0\u91cc\u76f4\u8a00\u8fd9\u6837&#034;\u5f88\u5bb9\u6613\u88ab\u641e\u6302\u6389&#034;\u2014\u2014fork \u7684\u6210\u672c\u6446\u5728\u90a3\u3002<\/p>\n<h4>3. \u591a\u7ebf\u7a0b<\/h4>\n<p>\u6bd4 fork \u4fbf\u5b9c&#xff0c;\u4f46\u957f\u670d\u52a1\u4e0b\u4f1a\u7ebf\u7a0b\u6ea2\u51fa\u3002<\/p>\n<p>\u8fd9\u91cc\u7b14\u8bb0\u91cc\u7ed9\u51fa\u4e86\u4e00\u7ec4\u5173\u952e\u6982\u5ff5&#xff1a;<\/p>\n<ul>\n<li>\u77ed\u670d\u52a1&#xff1a;\u5904\u7406\u903b\u8f91\u77ed&#xff0c;\u7ebf\u7a0b\u6ede\u7559\u65f6\u95f4\u77ed&#xff0c;\u7ebf\u7a0b\u4e0d\u4f1a\u592a\u6ea2\u51fa&#xff0c;\u201c\u6bd4\u8f83\u6e05\u723d\u201d\u2014\u2014\u5e73\u65f6\u7684\u767b\u5f55\u6ce8\u518c\u90fd\u662f\u77ed\u670d\u52a1&#xff1b;<\/li>\n<li>\u957f\u670d\u52a1&#xff1a;\u4e00\u4e2a\u5fae\u4fe1\u8981\u662f 1000000 \u4e2a\u5ba2\u6237\u7aef\u8fde\u8fc7\u6765&#xff0c;\u4e00\u4e2a\u8fde\u63a5\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u670d\u52a1\u5668\u4e0d\u5f97\u70b8\u4e86&#xff1f;<\/li>\n<\/ul>\n<h4>4. \u7ebf\u7a0b\u6c60<\/h4>\n<p>\u628a\u7ebf\u7a0b\u6570\u91cf\u9489\u6b7b&#xff0c;\u4efb\u52a1\u8fdb\u961f\u5217&#xff1a;<\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">Start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token function\">signal<\/span><span class=\"token punctuation\">(<\/span>SIGCHLD<span class=\"token punctuation\">,<\/span> SIG_IGN<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    threadpool<span class=\"token operator\">&lt;<\/span>callback_t<span class=\"token operator\">&gt;<\/span><span class=\"token operator\">*<\/span> pa <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">threadpool<\/span><span class=\"token operator\">&lt;<\/span>callback_t<span class=\"token operator\">&gt;<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">Getthreadpool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        sockaddr_in pq<span class=\"token punctuation\">;<\/span><br \/>\n        socklen_t a <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>sockaddr_in<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">accept<\/span><span class=\"token punctuation\">(<\/span>_listensockfd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span>sockaddr<span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>pq<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>a<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ listen\u5185\u90e8<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fd <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;accept err&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p>        pa<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">Enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">,<\/span> pq<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">this<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token keyword\">void<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span> <span class=\"token keyword\">this<\/span><span class=\"token operator\">-&gt;<\/span><span class=\"token function\">ServerIO<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> pq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e00\u53e5\u8bdd\u70b9\u8bc4&#xff1a;\u63a5\u5165\u7ebf\u7a0b\u6c60&#xff0c;\u9650\u5236\u4e86\u6548\u7387\u4e0a\u9650&#xff0c;\u4f46\u670d\u52a1\u5668\u4e0d\u4f1a\u6302\u6389\u2014\u2014\u7528\u5cf0\u503c\u6027\u80fd\u6362\u7a33\u5b9a\u6027&#xff0c;\u8fd9\u7b14\u8d26\u5728\u670d\u52a1\u7aef\u6c38\u8fdc\u662f\u5212\u7b97\u7684\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u7248\u672c\u8fd8\u5e26\u6765\u4e00\u4e2a\u8bbe\u8ba1\u7ea2\u5229&#xff1a;\u56de\u8c03\u53ef\u4ee5\u5728 ServerIO \u91cc\u518d\u6b21\u5305\u88c5\u2014\u2014\u6bd4\u5982\u5904\u7406\u6307\u4ee4\u5c31\u662f&#034;\u8fdc\u7a0b\u5904\u7406\u7c7b&#034;&#xff0c;\u4e1a\u52a1\u903b\u8f91\u901a\u8fc7 lambda \u6ce8\u518c\u8fdb\u6765&#xff0c;\u670d\u52a1\u5668\u9aa8\u67b6\u5b8c\u5168\u4e0d\u7528\u6539\u3002\u8fd9\u6b63\u597d\u63a5\u4e0a\u4e86\u7ebf\u7a0b\u6c60\u90a3\u7bc7&#034;\u56de\u8c03 &#061; \u5e95\u5c42\u8c03\u7528\u4e0a\u5c42&#034;\u7684\u7ed3\u8bba\u3002<\/p>\n<h3>\u4e09\u3001connect \u7684\u9690\u5f0f\u7ed1\u5b9a<\/h3>\n<p>UDP \u90a3\u7bc7\u8bb2\u8fc7 sendto \u4f1a\u9690\u5f0f bind&#xff0c;TCP \u8fd9\u8fb9\u5bf9\u79f0\u5730\u6765\u4e86\u4e00\u4e2a&#xff1a;<\/p>\n<p>connect \u548c sendto \u4e00\u6837&#xff0c;\u4f1a\u628a sockfd \u8fdb\u884c\u9690\u5f0f\u7ed1\u5b9a\u2014\u2014\u5ba2\u6237\u7aef\u4e0d\u7528\u663e\u5f0f bind&#xff0c;connect \u65f6\u82e5\u8fd8\u6ca1\u7ed1\u5b9a&#xff08;\u76f8\u5f53\u4e8e&#034;pa &#061;&#061; nullptr&#034;&#xff09;&#xff0c;\u5c31\u8fdb\u884c\u9ed8\u8ba4\u7ed1\u5b9a\u5e76\u9ed8\u8ba4\u53d1\u9001\u3002<\/p>\n<p>\u89c4\u5219\u662f\u901a\u7528\u7684&#xff1a;\u4e3b\u52a8\u53d1\u8d77\u901a\u4fe1\u7684\u4e00\u65b9&#xff0c;\u7aef\u53e3\u4ea4\u7ed9 OS \u968f\u673a\u5206\u914d\u3002<\/p>\n<h3>\u56db\u3001\u5730\u5740\u8f6c\u6362\u51fd\u6570\u7684\u4e24\u4e2a\u5751&#xff1a;inet_ntoa \u4e3a\u4ec0\u4e48\u4e0d\u53ef\u91cd\u5165<\/h3>\n<p>inet_ntoa \u628a 4 \u5b57\u8282\u6574\u6570 IP \u8f6c\u6210\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32&#xff0c;\u7528\u8d77\u6765\u5f88\u987a\u624b&#xff0c;\u4f46\u5b83\u6709\u4e2a\u9690\u853d\u7684\u95ee\u9898&#xff1a;<\/p>\n<p>\u8fd4\u56de\u7684 char \u662f\u51fd\u6570\u5185\u90e8 static \u7684&#xff01;\u6240\u4ee5\u5b83\u662f\u4e0d\u53ef\u91cd\u5165\u51fd\u6570\u3002*<\/p>\n<p>\u63a8\u6f14\u4e00\u4e0b\u4e8b\u6545\u73b0\u573a&#xff1a;\u7ebf\u7a0b A \u8c03\u7528 inet_ntoa \u8fd8\u6ca1\u6765\u5f97\u53ca\u4f7f\u7528\u8fd4\u56de\u503c&#xff0c;\u7ebf\u7a0b B \u4e5f\u8c03\u7528\u4e86 inet_ntoa\u2014\u2014static \u7f13\u51b2\u533a\u88ab\u8986\u76d6&#xff0c;\u7ebf\u7a0b A \u62ff\u5230\u7684\u662f\u522b\u4eba\u7684 IP \u5b57\u7b26\u4e32\u3002\u800c inet_ntoa \u6ca1\u6709\u52a0\u9501&#xff0c;\u8fd9\u4e2a\u5751\u662f\u65e0\u58f0\u7684\u3002<\/p>\n<p>\u6b63\u89e3\u662f\u7528\u8fd9\u4e00\u5bf9&#xff1a;<\/p>\n<ul>\n<li>inet_ntop()&#xff1a;\u7f51\u7edc\u5e8f\u6574\u6570 \u2192 \u5b57\u7b26\u4e32&#xff08;n &#061; network&#xff0c;p &#061; presentation&#xff0c;\u8fdb\u7a0b\u5e8f\u5217&#xff09;&#xff0c;\u591a\u4f20\u4e00\u4e2a\u7f13\u51b2\u533a\u53c2\u6570&#xff1b;<\/li>\n<li>inet_pton()&#xff1a;\u5b57\u7b26\u4e32 \u2192 \u7f51\u7edc\u5e8f\u6574\u6570\u3002<\/li>\n<\/ul>\n<p>\u7f13\u51b2\u533a &#043; &#xff08;\u5e93\u5185\u7684&#xff09;\u9501&#xff0c;\u540c\u65f6\u89e3\u51b3\u4e86\u7ebf\u7a0b\u5b89\u5168\u548c\u5b58\u50a8\u5b89\u5168\u2014\u2014\u8c03\u7528\u8005\u81ea\u5df1\u7684\u7f13\u51b2\u533a\u8c01\u4e5f\u8986\u76d6\u4e0d\u4e86\u3002\u8fd9\u4e2a&#034;\u7528\u8c03\u7528\u8005\u63d0\u4f9b\u7684\u7f13\u51b2\u533a\u4ee3\u66ff\u51fd\u6570\u5185 static \u7f13\u51b2\u533a&#034;\u7684\u624b\u6cd5&#xff0c;\u662f\u53ef\u91cd\u5165\u51fd\u6570\u8bbe\u8ba1\u7684\u6807\u51c6\u8303\u5f0f&#xff0c;\u548c\u4fe1\u53f7\u5904\u7406\u90a3\u7bc7&#034;\u4e0d\u53ef\u91cd\u5165\u51fd\u6570&#034;\u7684\u8ba8\u8bba\u5b8c\u5168\u547c\u5e94\u3002<\/p>\n<h3>\u4e94\u3001\u5c0f\u7ed3<\/h3>\n<table>\n<tr>UDPTCP<\/tr>\n<tbody>\n<tr>\n<td>socket \u6587\u4ef6<\/td>\n<td>\u4e00\u4e2a&#xff0c;\u8bfb\u5199\u90fd\u5728\u5b83<\/td>\n<td>listensockfd &#043; \u6bcf\u8fde\u63a5\u4e00\u4e2a fd<\/td>\n<\/tr>\n<tr>\n<td>\u5bf9\u7aef\u5730\u5740<\/td>\n<td>\u6bcf\u4e2a\u62a5\u6587\u81ea\u5e26<\/td>\n<td>accept \u65f6\u62ff\u5230\u4e00\u6b21<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u6237\u7aef bind<\/td>\n<td>sendto \u9690\u5f0f\u7ed1\u5b9a<\/td>\n<td>connect \u9690\u5f0f\u7ed1\u5b9a<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u5ba2\u6237\u7aef<\/td>\n<td>\u65e0\u8fde\u63a5\u5929\u7136\u5e76\u53d1<\/td>\n<td>\u4e32\u884c \u2192 \u591a\u8fdb\u7a0b \u2192 \u591a\u7ebf\u7a0b \u2192 \u7ebf\u7a0b\u6c60<\/td>\n<\/tr>\n<tr>\n<td>\u63a8\u8350\u7684\u5730\u5740\u8f6c\u6362<\/td>\n<td>inet_ntop \/ inet_pton<\/td>\n<td>\u4e0d\u8981\u7528 inet_ntoa&#xff08;\u4e0d\u53ef\u91cd\u5165&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>TCP \u7f16\u7a0b\u7684\u7b2c\u4e00\u8bfe\u5176\u5b9e\u4e0d\u662f\u63a5\u53e3&#xff0c;\u800c\u662f**&#034;\u8fde\u63a5&#034;\u8fd9\u4e2a\u6982\u5ff5\u7ed9\u670d\u52a1\u7aef\u5e26\u6765\u7684\u5e76\u53d1\u538b\u529b**\u2014\u2014UDP \u5929\u751f\u65e0\u8fde\u63a5\u65e0\u6240\u8c13\u5e76\u53d1\u6a21\u578b&#xff0c;TCP \u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u662f\u4e00\u4efd\u8981\u957f\u671f\u6301\u6709\u7684\u8d44\u6e90&#xff0c;\u4e8e\u662f\u624d\u6709\u77ed\u670d\u52a1\/\u957f\u670d\u52a1\u7684\u53d6\u820d\u548c\u7ebf\u7a0b\u6c60\u7684\u767b\u573a\u3002<\/p>\n<p>\u800c\u8fde\u63a5\u7ef4\u6301\u4f4f\u4e86\u4e4b\u540e&#xff0c;\u65b0\u7684\u95ee\u9898\u9a6c\u4e0a\u51fa\u73b0&#xff1a;TCP \u53d1\u7684\u6570\u636e\u662f&#034;\u4e00\u7247\u4e00\u7247&#034;\u7684&#xff0c;\u8bfb\u5230\u7684\u4e1c\u897f\u53ef\u80fd\u534a\u622a\u2014\u2014\u8fd9\u5c31\u5f15\u51fa\u4e86\u4e0b\u4e00\u7bc7\u7684\u4e3b\u89d2&#xff1a;\u5e8f\u5217\u5316\u4e0e\u81ea\u5b9a\u4e49\u534f\u8bae\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TCP socket \u7f16\u7a0b&#xff1a;\u4ece accept \u5230\u7ebf\u7a0b\u6c60&#xff0c;\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe<br \/>\n\u6211\u7684github&#xff1a;(https:\/\/github.com\/xcx55\/ubuntu-linux-project)<br \/>\n\u611f\u8c22\u5404\u4f4d\u5927\u4f6c\u53c2\u89c2\u6211\u7684github&#xff01;&#xff01;<br \/>\nUDP \u90a3\u6761\u7ebf\u8d70\u5b8c\u540e&#xff0c;8 \u6708 30 \u53f7\u5230 9 \u6708 1 \u53f7\u8fdb\u5165\u4e86 TCP \u7f16\u7a0b\u3002\u548c UDP \u76f8\u6bd4&#xff0c;TCP \u7684\u63a5\u53e3\u591a\u4e86\u4e00\u5c42\\&#8221;\u8fde\u63a5\\&#8221;\u7684\u6982\u5ff5&#xff0c;\u800c\u8fd9<\/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":[43,44],"topic":[],"class_list":["post-111646","post","type-post","status-publish","format-standard","hentry","category-server","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \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\/111646.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"TCP socket \u7f16\u7a0b&#xff1a;\u4ece accept \u5230\u7ebf\u7a0b\u6c60&#xff0c;\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe \u6211\u7684github&#xff1a;(https:\/\/github.com\/xcx55\/ubuntu-linux-project) \u611f\u8c22\u5404\u4f4d\u5927\u4f6c\u53c2\u89c2\u6211\u7684github&#xff01;&#xff01; UDP \u90a3\u6761\u7ebf\u8d70\u5b8c\u540e&#xff0c;8 \u6708 30 \u53f7\u5230 9 \u6708 1 \u53f7\u8fdb\u5165\u4e86 TCP \u7f16\u7a0b\u3002\u548c UDP \u76f8\u6bd4&#xff0c;TCP \u7684\u63a5\u53e3\u591a\u4e86\u4e00\u5c42&quot;\u8fde\u63a5&quot;\u7684\u6982\u5ff5&#xff0c;\u800c\u8fd9\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/111646.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-01T04:57:09+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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111646.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/111646.html\",\"name\":\"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-01T04:57:09+00:00\",\"dateModified\":\"2026-10-01T04:57:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/111646.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/111646.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111646.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe\"}]},{\"@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":"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \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\/111646.html","og_locale":"zh_CN","og_type":"article","og_title":"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"TCP socket \u7f16\u7a0b&#xff1a;\u4ece accept \u5230\u7ebf\u7a0b\u6c60&#xff0c;\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe \u6211\u7684github&#xff1a;(https:\/\/github.com\/xcx55\/ubuntu-linux-project) \u611f\u8c22\u5404\u4f4d\u5927\u4f6c\u53c2\u89c2\u6211\u7684github&#xff01;&#xff01; UDP \u90a3\u6761\u7ebf\u8d70\u5b8c\u540e&#xff0c;8 \u6708 30 \u53f7\u5230 9 \u6708 1 \u53f7\u8fdb\u5165\u4e86 TCP \u7f16\u7a0b\u3002\u548c UDP \u76f8\u6bd4&#xff0c;TCP \u7684\u63a5\u53e3\u591a\u4e86\u4e00\u5c42\"\u8fde\u63a5\"\u7684\u6982\u5ff5&#xff0c;\u800c\u8fd9","og_url":"https:\/\/www.wsisp.com\/helps\/111646.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-01T04:57:09+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/111646.html","url":"https:\/\/www.wsisp.com\/helps\/111646.html","name":"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-01T04:57:09+00:00","dateModified":"2026-10-01T04:57:09+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/111646.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/111646.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/111646.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"TCPsocket\u7f16\u7a0b\u4eceaccept\u5230\u7ebf\u7a0b\u6c60\u8fde\u63a5\u578b\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u8bfe"}]},{"@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\/111646","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=111646"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/111646\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=111646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=111646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=111646"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=111646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}