{"id":82197,"date":"2026-07-25T07:00:50","date_gmt":"2026-07-24T23:00:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82197.html"},"modified":"2026-07-25T07:00:50","modified_gmt":"2026-07-24T23:00:50","slug":"linux%ef%bc%9a%e5%9f%ba%e4%ba%8etcp-socket%e7%9a%84%e5%ae%a2%e6%88%b7%e7%ab%af-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%9e%e7%8e%b0%e7%9a%84%e8%bf%9c%e7%a8%8b%e5%91%bd%e4%bb%a4%e8%a1%8c%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82197.html","title":{"rendered":"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee"},"content":{"rendered":"<h3>\u672c\u6587\u5c06\u57fa\u4e8e\u4e00\u4e2a\u5b8c\u6574\u7684\u9879\u76ee\u4ee3\u7801&#xff0c;\u7cfb\u7edf\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 C&#043;&#043; \u5b9e\u73b0\u4e00\u4e2a TCP \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff0c;\u5e76\u91cd\u70b9\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u6240\u6d89\u53ca\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9<\/h3>\n<hr \/>\n<h2>\u4e00\u3001\u9879\u76ee\u6574\u4f53\u7ed3\u6784<\/h2>\n<p>\u672c\u9879\u76ee\u91c7\u7528\u5178\u578b\u7684 \u5ba2\u6237\u7aef &#8211; \u670d\u52a1\u5668\u6a21\u578b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5ba2\u6237\u7aef&#xff1a;\u8d1f\u8d23\u8fde\u63a5\u670d\u52a1\u5668\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u63a5\u6536\u54cd\u5e94<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668&#xff1a;\u8d1f\u8d23\u76d1\u542c\u8fde\u63a5\u3001\u5904\u7406\u8bf7\u6c42\u3001\u8fd4\u56de\u7ed3\u679c<\/p>\n<\/li>\n<\/ul>\n<p>\u6838\u5fc3\u6a21\u5757\u5305\u62ec&#xff1a;<\/p>\n<p>InetAddr      \u2014\u2014 \u7f51\u7edc\u5730\u5740\u5c01\u88c5<br \/>\nTcpServer     \u2014\u2014 TCP\u670d\u52a1\u5668\u6846\u67b6<br \/>\nCommand       \u2014\u2014 \u4e1a\u52a1\u5904\u7406\u903b\u8f91<br \/>\nClient        \u2014\u2014 \u5ba2\u6237\u7aef\u7a0b\u5e8f<\/p>\n<hr \/>\n<h2>\u4e8c\u3001TCP \u7f16\u7a0b\u57fa\u7840\u77e5\u8bc6<\/h2>\n<p>\u5728\u5b9e\u73b0\u4e4b\u524d&#xff0c;\u9700\u8981\u638c\u63e1\u4ee5\u4e0b\u57fa\u7840\u77e5\u8bc6&#xff1a;<\/p>\n<hr \/>\n<h3>1. Socket \u7f16\u7a0b\u6a21\u578b<\/h3>\n<p>TCP \u901a\u4fe1\u57fa\u4e8e socket&#xff0c;\u57fa\u672c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<h4>\u670d\u52a1\u7aef\u6d41\u7a0b<\/h4>\n<p>socket \u2192 bind \u2192 listen \u2192 accept \u2192 read\/write<\/p>\n<h4>\u5ba2\u6237\u7aef\u6d41\u7a0b<\/h4>\n<p>socket \u2192 connect \u2192 read\/write<\/p>\n<hr \/>\n<h3>2. \u7f51\u7edc\u5b57\u8282\u5e8f<\/h3>\n<p>\u7f51\u7edc\u4f20\u8f93\u7edf\u4e00\u4f7f\u7528 \u5927\u7aef\u5b57\u8282\u5e8f&#xff0c;\u56e0\u6b64\u9700\u8981&#xff1a;<\/p>\n<ul>\n<li>\n<p>htons&#xff1a;\u4e3b\u673a\u5e8f \u2192 \u7f51\u7edc\u5e8f&#xff08;\u7aef\u53e3&#xff09;<\/p>\n<\/li>\n<li>\n<p>ntohs&#xff1a;\u7f51\u7edc\u5e8f \u2192 \u4e3b\u673a\u5e8f<\/p>\n<\/li>\n<li>\n<p>inet_pton&#xff1a;\u5b57\u7b26\u4e32 IP \u2192 \u4e8c\u8fdb\u5236<\/p>\n<\/li>\n<li>\n<p>inet_ntop&#xff1a;\u4e8c\u8fdb\u5236 \u2192 \u5b57\u7b26\u4e32 IP<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>3. sockaddr \u7ed3\u6784<\/h3>\n<p>TCP \u4f7f\u7528&#xff1a;<\/p>\n<p>struct sockaddr_in<\/p>\n<p>\u4f46\u63a5\u53e3\u7edf\u4e00\u4f7f\u7528&#xff1a;<\/p>\n<p>struct sockaddr*<\/p>\n<p>\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<hr \/>\n<h2>\u4e09\u3001InetAddr&#xff1a;\u7f51\u7edc\u5730\u5740\u5c01\u88c5<\/h2>\n<p>\u4e3a\u4e86\u907f\u514d\u91cd\u590d\u64cd\u4f5c\u5e95\u5c42\u7ed3\u6784&#xff0c;\u672c\u9879\u76ee\u5c01\u88c5\u4e86 InetAddr \u7c7b\u3002<\/p>\n<hr \/>\n<h3>1. \u529f\u80fd<\/h3>\n<ul>\n<li>\n<p>\u5c01\u88c5 IP &#043; Port<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4f9b\u7f51\u7edc\u5e8f\u4e0e\u4e3b\u673a\u5e8f\u8f6c\u6362<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4f9b\u63a5\u53e3\u7ed9 socket \u4f7f\u7528<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>2. \u6784\u9020\u65b9\u5f0f<\/h3>\n<h4>&#xff08;1&#xff09;\u4ece\u7f51\u7edc\u7ed3\u6784\u6784\u9020&#xff08;\u670d\u52a1\u7aef accept&#xff09;<\/h4>\n<p>InetAddr(const sockaddr_in &amp;addr)<\/p>\n<p>\u4f5c\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63d0\u53d6 IP<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u6362\u7aef\u53e3\u4e3a\u4e3b\u673a\u5e8f<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>&#xff08;2&#xff09;\u5ba2\u6237\u7aef\u6784\u9020<\/h4>\n<p>InetAddr(const std::string&amp; ip, uint16_t port)<\/p>\n<p>\u4f5c\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6784\u9020\u7528\u4e8e connect \u7684\u5730\u5740\u7ed3\u6784<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>&#xff08;3&#xff09;\u670d\u52a1\u7aef\u6784\u9020<\/h4>\n<p>InetAddr(uint16_t port)<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>IP \u8bbe\u7f6e\u4e3a INADDR_ANY<\/p>\n<\/li>\n<li>\n<p>\u76d1\u542c\u6240\u6709\u7f51\u5361<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>3. \u6838\u5fc3\u63a5\u53e3<\/h3>\n<p>const struct sockaddr* NetAddrPtr();<br \/>\nconst size_t NetAddrLen();<\/p>\n<p>&#x1f449; \u7528\u4e8e socket API \u8c03\u7528<\/p>\n<hr \/>\n<h2>\u56db\u3001\u5ba2\u6237\u7aef\u5b9e\u73b0<\/h2>\n<p>\u5ba2\u6237\u7aef\u903b\u8f91\u8f83\u4e3a\u7b80\u5355&#xff0c;\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<hr \/>\n<h3>1. \u521b\u5efa socket<\/h3>\n<p>int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<ul>\n<li>\n<p>SOCK_STREAM \u8868\u793a TCP<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>2. \u8fde\u63a5\u670d\u52a1\u5668<\/h3>\n<p>connect(sockfd, cilent.NetAddrPtr(), cilent.NetAddrLen());<\/p>\n<hr \/>\n<h3>3. \u6570\u636e\u901a\u4fe1<\/h3>\n<p>write(sockfd, line.c_str(), line.size());<br \/>\nread(sockfd, buffer, sizeof(buffer));<\/p>\n<p>\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u7ed9\u670d\u52a1\u5668<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u670d\u52a1\u5668\u8fd4\u56de\u7ed3\u679c<\/p>\n<\/li>\n<li>\n<p>\u8f93\u51fa\u5230\u7ec8\u7aef<\/p>\n<\/li>\n<hr \/>\n<h3>4. \u5173\u95ed\u8fde\u63a5<\/h3>\n<p>close(sockfd);<\/p>\n<hr \/>\n<h2>\u4e94\u3001TcpServer&#xff1a;\u670d\u52a1\u5668\u5b9e\u73b0<\/h2>\n<p>\u670d\u52a1\u5668\u662f\u6574\u4e2a\u9879\u76ee\u7684\u6838\u5fc3\u3002<\/p>\n<hr \/>\n<h3>1. \u6210\u5458\u53d8\u91cf<\/h3>\n<p>uint16_t _port;<br \/>\nint _listenSockfd;<br \/>\nbool _isrunning;<br \/>\nfunc_t _func;<\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<p>using func_t &#061; std::function&lt;std::string(const std::string&amp;, InetAddr&amp;)&gt;;<\/p>\n<p>&#x1f449; \u8868\u793a\u4e1a\u52a1\u5904\u7406\u51fd\u6570<\/p>\n<hr \/>\n<h3>2. \u521d\u59cb\u5316&#xff08;Init&#xff09;<\/h3>\n<h4>&#xff08;1&#xff09;\u521b\u5efa socket<\/h4>\n<p>_listenSockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<hr \/>\n<h4>&#xff08;2&#xff09;\u7ed1\u5b9a\u7aef\u53e3<\/h4>\n<p>InetAddr local(_port);<br \/>\nbind(_listenSockfd, local.NetAddrPtr(), local.NetAddrLen());<\/p>\n<hr \/>\n<h4>&#xff08;3&#xff09;\u76d1\u542c<\/h4>\n<p>listen(_listenSockfd, backlog);<\/p>\n<hr \/>\n<h3>3. \u83b7\u53d6\u8fde\u63a5&#xff08;accept&#xff09;<\/h3>\n<p>int sockfd &#061; accept(_listenSockfd, CONV(peer), &amp;len);<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u65b0\u7684\u901a\u4fe1 socket<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>4. \u8bf7\u6c42\u5904\u7406&#xff08;Service&#xff09;<\/h3>\n<p>ssize_t n &#061; read(sockfd, buffer, sizeof(buffer));<\/p>\n<p>\u5904\u7406\u903b\u8f91&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u4e1a\u52a1\u51fd\u6570 _func<\/p>\n<\/li>\n<li>\n<p>\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<\/p>\n<\/li>\n<\/ul>\n<p>std::string result &#061; _func(buffer, cilent);<br \/>\nwrite(sockfd, result.c_str(), result.size());<\/p>\n<hr \/>\n<h3>5. \u5e76\u53d1\u5904\u7406&#xff08;\u591a\u7ebf\u7a0b&#xff09;<\/h3>\n<p>\u4e3a\u4e86\u652f\u6301\u591a\u4e2a\u5ba2\u6237\u7aef\u540c\u65f6\u8bbf\u95ee&#xff0c;\u4f7f\u7528 pthread&#xff1a;<\/p>\n<hr \/>\n<h4>\u7ebf\u7a0b\u6570\u636e\u5c01\u88c5<\/h4>\n<p>class ThreadData<br \/>\n{<br \/>\n    TcpServer* _tsvr;<br \/>\n    int _sockfd;<br \/>\n    InetAddr _addr;<br \/>\n};<\/p>\n<hr \/>\n<h4>\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570<\/h4>\n<p>static void* Routine(void* args)<\/p>\n<p>\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u5206\u79bb\u7ebf\u7a0b pthread_detach<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c Service<\/p>\n<\/li>\n<li>\n<p>\u91ca\u653e\u8d44\u6e90<\/p>\n<\/li>\n<hr \/>\n<h4>\u521b\u5efa\u7ebf\u7a0b<\/h4>\n<p>pthread_create(&amp;tid, nullptr, Routine, td);<\/p>\n<p>&#x1f449; \u6bcf\u4e2a\u5ba2\u6237\u7aef\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n<hr \/>\n<h2>\u516d\u3001\u4e1a\u52a1\u89e3\u8026&#xff08;\u56de\u8c03\u673a\u5236&#xff09;<\/h2>\n<p>\u670d\u52a1\u5668\u4e0d\u76f4\u63a5\u5904\u7406\u4e1a\u52a1\u903b\u8f91&#xff0c;\u800c\u662f\u901a\u8fc7\u56de\u8c03\u51fd\u6570&#xff1a;<\/p>\n<p>std::function&lt;std::string(const std::string&amp;, InetAddr&amp;)&gt;<\/p>\n<p>\u5728 main \u4e2d\u7ed1\u5b9a&#xff1a;<\/p>\n<p>std::bind(&amp;Command::Execute, &amp;cmd, std::placeholders::_1, std::placeholders::_2)<\/p>\n<hr \/>\n<h3>\u4f18\u70b9<\/h3>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668\u53ea\u8d1f\u8d23\u901a\u4fe1<\/p>\n<\/li>\n<li>\n<p>\u4e1a\u52a1\u903b\u8f91\u72ec\u7acb<\/p>\n<\/li>\n<li>\n<p>\u53ef\u6269\u5c55\u6027\u5f3a<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e03\u3001Command \u6a21\u5757&#xff08;\u4e1a\u52a1\u5c42&#xff09;<\/h2>\n<p>\u8be5\u6a21\u5757\u8d1f\u8d23\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff1a;<\/p>\n<p>std::string Execute(const std::string&amp; req, InetAddr&amp; client);<\/p>\n<p>\u529f\u80fd\u53ef\u4ee5\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b57\u7b26\u4e32\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u6267\u884c<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u67e5\u8be2<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>\u516b\u3001\u7a0b\u5e8f\u8fd0\u884c\u6d41\u7a0b\u603b\u7ed3<\/h2>\n<hr \/>\n<h3>\u670d\u52a1\u7aef<\/h3>\n<p>\u542f\u52a8 \u2192 \u521d\u59cb\u5316socket \u2192 \u76d1\u542c\u7aef\u53e3 \u2192 accept\u8fde\u63a5 \u2192 \u521b\u5efa\u7ebf\u7a0b \u2192 \u5904\u7406\u8bf7\u6c42<\/p>\n<hr \/>\n<h3>\u5ba2\u6237\u7aef<\/h3>\n<p>\u542f\u52a8 \u2192 \u521b\u5efasocket \u2192 connect\u670d\u52a1\u5668 \u2192 \u8f93\u5165\u6570\u636e \u2192 \u63a5\u6536\u54cd\u5e94<\/p>\n<hr \/>\n<h2>\u4e5d\u3001\u6d89\u53ca\u7684\u6838\u5fc3\u77e5\u8bc6\u603b\u7ed3<\/h2>\n<p>\u5b9e\u73b0\u672c\u9879\u76ee\u9700\u8981\u638c\u63e1\u4ee5\u4e0b\u77e5\u8bc6&#xff1a;<\/p>\n<hr \/>\n<h3>1. C&#043;&#043; \u57fa\u7840<\/h3>\n<ul>\n<li>\n<p>\u7c7b\u4e0e\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p>\u6784\u9020\u51fd\u6570<\/p>\n<\/li>\n<li>\n<p>STL&#xff08;string\u3001function&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u667a\u80fd\u6307\u9488&#xff08;unique_ptr&#xff09;<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>2. Linux \u7cfb\u7edf\u7f16\u7a0b<\/h3>\n<ul>\n<li>\n<p>socket API<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<\/li>\n<li>\n<p>read \/ write<\/p>\n<\/li>\n<li>\n<p>close<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>3. \u7f51\u7edc\u7f16\u7a0b<\/h3>\n<ul>\n<li>\n<p>TCP \u534f\u8bae<\/p>\n<\/li>\n<li>\n<p>\u4e09\u6b21\u63e1\u624b&#xff08;\u5efa\u7acb\u8fde\u63a5&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u56db\u6b21\u6325\u624b&#xff08;\u65ad\u5f00\u8fde\u63a5&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u7f51\u7edc\u5b57\u8282\u5e8f<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>4. \u591a\u7ebf\u7a0b<\/h3>\n<ul>\n<li>\n<p>pthread<\/p>\n<\/li>\n<li>\n<p>\u7ebf\u7a0b\u521b\u5efa\u4e0e\u5206\u79bb<\/p>\n<\/li>\n<li>\n<p>\u5e76\u53d1\u5904\u7406\u6a21\u578b<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>5. \u8bbe\u8ba1\u601d\u60f3<\/h3>\n<ul>\n<li>\n<p>\u5c01\u88c5&#xff08;InetAddr&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u89e3\u8026&#xff08;\u56de\u8c03\u51fd\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5757\u5316&#xff08;TcpServer \/ Command&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee&#xff0c;\u53ef\u4ee5\u5b8c\u6574\u7406\u89e3 TCP \u7f16\u7a0b\u4ece\u5e95\u5c42 API \u5230\u7ed3\u6784\u8bbe\u8ba1\u7684\u5168\u8fc7\u7a0b\u3002\u76f8\u6bd4\u53ea\u8c03\u7528\u63a5\u53e3&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u66f4\u6709\u52a9\u4e8e\u6df1\u5165\u7406\u89e3\u7f51\u7edc\u901a\u4fe1\u673a\u5236&#xff0c;\u4ee5\u53ca\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u57fa\u672c\u6784\u5efa\u65b9\u5f0f<\/p>\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u4e0d\u4ec5\u5177\u5907\u57fa\u672c\u901a\u4fe1\u80fd\u529b&#xff0c;\u540c\u65f6\u5177\u5907\u826f\u597d\u7684\u6269\u5c55\u6027&#xff0c;\u662f\u8fdb\u4e00\u6b65\u5b66\u4e60\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u7684\u91cd\u8981\u57fa\u7840&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u5b66\u4e60\u81ea\u5b9a\u4e49\u534f\u8bae&#xff0c;\u656c\u8bf7\u671f\u5f85\u5566<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c06\u57fa\u4e8e\u4e00\u4e2a\u5b8c\u6574\u7684\u9879\u76ee\u4ee3\u7801&#xff0c;\u7cfb\u7edf\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 C \u5b9e\u73b0\u4e00\u4e2a TCP \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff0c;\u5e76\u91cd\u70b9\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u6240\u6d89\u53ca\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e00\u3001\u9879\u76ee\u6574\u4f53\u7ed3\u6784\u672c\u9879\u76ee\u91c7\u7528\u5178\u578b\u7684 \u5ba2\u6237\u7aef &#8211; \u670d\u52a1\u5668\u6a21\u578b&#xff1a;\u5ba2\u6237\u7aef&#xff1a;\u8d1f\u8d23\u8fde\u63a5\u670d\u52a1\u5668\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u63a5\u6536\u54cd\u5e94\u670d\u52a1\u5668&#xff1a;\u8d1f\u8d23\u76d1\u542c\u8fde\u63a5\u3001\u5904\u7406\u8bf7\u6c42\u3001\u8fd4\u56de\u7ed3\u679c\u6838\u5fc3\u6a21\u5757\u5305\u62ec&#xff1a;InetAddr      \u2014\u2014 \u7f51\u7edc\u5730\u5740\u5c01\u88c5<br \/>\nTcpServer     \u2014\u2014 TCP\u670d\u52a1\u5668\u6846\u67b6<br \/>\nCommand  <\/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,205,43,78,44],"topic":[],"class_list":["post-82197","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-tcp-ip","tag-43","tag-78","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \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\/82197.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u5c06\u57fa\u4e8e\u4e00\u4e2a\u5b8c\u6574\u7684\u9879\u76ee\u4ee3\u7801&#xff0c;\u7cfb\u7edf\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 C \u5b9e\u73b0\u4e00\u4e2a TCP \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff0c;\u5e76\u91cd\u70b9\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u6240\u6d89\u53ca\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e00\u3001\u9879\u76ee\u6574\u4f53\u7ed3\u6784\u672c\u9879\u76ee\u91c7\u7528\u5178\u578b\u7684 \u5ba2\u6237\u7aef - \u670d\u52a1\u5668\u6a21\u578b&#xff1a;\u5ba2\u6237\u7aef&#xff1a;\u8d1f\u8d23\u8fde\u63a5\u670d\u52a1\u5668\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u63a5\u6536\u54cd\u5e94\u670d\u52a1\u5668&#xff1a;\u8d1f\u8d23\u76d1\u542c\u8fde\u63a5\u3001\u5904\u7406\u8bf7\u6c42\u3001\u8fd4\u56de\u7ed3\u679c\u6838\u5fc3\u6a21\u5757\u5305\u62ec&#xff1a;InetAddr   \u2014\u2014 \u7f51\u7edc\u5730\u5740\u5c01\u88c5 TcpServer   \u2014\u2014 TCP\u670d\u52a1\u5668\u6846\u67b6 Command\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82197.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T23:00:50+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\/82197.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82197.html\",\"name\":\"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T23:00:50+00:00\",\"dateModified\":\"2026-07-24T23:00:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82197.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82197.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82197.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee\"}]},{\"@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\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \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\/82197.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u672c\u6587\u5c06\u57fa\u4e8e\u4e00\u4e2a\u5b8c\u6574\u7684\u9879\u76ee\u4ee3\u7801&#xff0c;\u7cfb\u7edf\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 C \u5b9e\u73b0\u4e00\u4e2a TCP \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff0c;\u5e76\u91cd\u70b9\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u6240\u6d89\u53ca\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e00\u3001\u9879\u76ee\u6574\u4f53\u7ed3\u6784\u672c\u9879\u76ee\u91c7\u7528\u5178\u578b\u7684 \u5ba2\u6237\u7aef - \u670d\u52a1\u5668\u6a21\u578b&#xff1a;\u5ba2\u6237\u7aef&#xff1a;\u8d1f\u8d23\u8fde\u63a5\u670d\u52a1\u5668\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u63a5\u6536\u54cd\u5e94\u670d\u52a1\u5668&#xff1a;\u8d1f\u8d23\u76d1\u542c\u8fde\u63a5\u3001\u5904\u7406\u8bf7\u6c42\u3001\u8fd4\u56de\u7ed3\u679c\u6838\u5fc3\u6a21\u5757\u5305\u62ec&#xff1a;InetAddr   \u2014\u2014 \u7f51\u7edc\u5730\u5740\u5c01\u88c5 TcpServer   \u2014\u2014 TCP\u670d\u52a1\u5668\u6846\u67b6 Command","og_url":"https:\/\/www.wsisp.com\/helps\/82197.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T23:00:50+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\/82197.html","url":"https:\/\/www.wsisp.com\/helps\/82197.html","name":"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T23:00:50+00:00","dateModified":"2026-07-24T23:00:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82197.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82197.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82197.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\uff1a\u57fa\u4e8eTCP Socket\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5b9e\u73b0\u7684\u8fdc\u7a0b\u547d\u4ee4\u884c\u9879\u76ee"}]},{"@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\/82197","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=82197"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82197"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}