{"id":82196,"date":"2026-07-25T07:00:45","date_gmt":"2026-07-24T23:00:45","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82196.html"},"modified":"2026-07-25T07:00:45","modified_gmt":"2026-07-24T23:00:45","slug":"linux-%e7%bd%91%e7%bb%9c%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b%ef%bc%88%e4%ba%94%ef%bc%89tcp-%e5%9b%9e%e5%a3%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%9a%84%e5%ae%9e%e7%8e%b0%ef%bc%88%e5%8d%95%e8%bf%9b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82196.html","title":{"rendered":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E4%BB%8E%20UDP%20%E8%BF%87%E6%B8%A1%E5%88%B0%20TCP%20%E5%8D%8F%E8%AE%AE-toc\" style=\"margin-left:0px\">\u4e00\u3001\u4ece UDP \u8fc7\u6e21\u5230 TCP \u534f\u8bae<\/p>\n<p id=\"%E4%BA%8C%E3%80%81TCP%20%E7%BC%96%E7%A0%81%E7%8E%AF%E8%8A%82-toc\" style=\"margin-left:0px\">\u4e8c\u3001TCP \u7f16\u7801\u73af\u8282<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E9%98%B6%E6%AE%B5%E4%B8%80%20%3A%20%E5%8D%95%E8%BF%9B%E7%A8%8B%2F%E5%8D%95%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC-toc\" style=\"margin-left:0px\">\u4e09\u3001\u9636\u6bb5\u4e00 : \u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c<\/p>\n<p id=\"%E9%93%BA%E5%9E%AB-toc\" style=\"margin-left:40px\">\u94fa\u57ab<\/p>\n<p id=\"%E6%80%9D%E8%B7%AF%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">\u601d\u8def\u603b\u7ed3<\/p>\n<p id=\"EchoTcpServer.hpp%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%3A-toc\" style=\"margin-left:40px\">EchoTcpServer.hpp \u670d\u52a1\u7aef:<\/p>\n<p id=\"%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:80px\">\u6784\u9020\u51fd\u6570<\/p>\n<p id=\"%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0%20InitServer()-toc\" style=\"margin-left:80px\">\u521d\u59cb\u5316\u51fd\u6570 InitServer()<\/p>\n<p id=\"listen()-toc\" style=\"margin-left:120px\">listen()<\/p>\n<p id=\"%E5%90%AF%E5%8A%A8%E4%B8%BB%E5%BE%AA%E7%8E%AF%20Start()%20%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:80px\">\u542f\u52a8\u4e3b\u5faa\u73af Start() \u51fd\u6570<\/p>\n<p id=\"accept()-toc\" style=\"margin-left:120px\">accept()<\/p>\n<p id=\"%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0%20serviceIO()-toc\" style=\"margin-left:80px\">\u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO()<\/p>\n<p id=\"EchoTcpServerMain.cc%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%3A-toc\" style=\"margin-left:40px\">EchoTcpServerMain.cc \u670d\u52a1\u7aef:<\/p>\n<p id=\"EchoTcpClient.cc%20%E5%AE%A2%E6%88%B7%E7%AB%AF%3A-toc\" style=\"margin-left:40px\">EchoTcpClient.cc \u5ba2\u6237\u7aef:<\/p>\n<p id=\"connect()-toc\" style=\"margin-left:80px\">connect()<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A-toc\" style=\"margin-left:40px\">\u8fd0\u884c\u7ed3\u679c:<\/p>\n<p id=\"%E6%80%BB%E7%BB%93%3A-toc\" style=\"margin-left:40px\">\u603b\u7ed3:<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E9%98%B6%E6%AE%B5%E4%BA%8C%C2%A0%3A%20%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%89%88%E6%9C%AC-toc\" style=\"margin-left:0px\">\u56db\u3001\u9636\u6bb5\u4e8c\u00a0: \u591a\u8fdb\u7a0b\u7248\u672c<\/p>\n<p id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A-toc\" style=\"margin-left:40px\">\u903b\u8f91\u68b3\u7406:<\/p>\n<p id=\"%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98%3A-toc\" style=\"margin-left:40px\">\u5f15\u53d1\u7684\u95ee\u9898:<\/p>\n<p id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%3A-toc\" style=\"margin-left:40px\">\u89e3\u51b3\u65b9\u6cd5:<\/p>\n<p id=\"%E6%96%B9%E6%B3%95%E4%B8%80%20%3A%20%E5%BC%95%E5%85%A5%E5%AD%99%E5%AD%90%E8%BF%9B%E7%A8%8B-toc\" style=\"margin-left:120px\">\u65b9\u6cd5\u4e00 : \u5f15\u5165\u5b59\u5b50\u8fdb\u7a0b<\/p>\n<p id=\"%E6%96%B9%E6%B3%95%E4%BA%8C%20%3A%20%E5%BC%95%E5%85%A5%E4%BF%A1%E5%8F%B7-toc\" style=\"margin-left:120px\">\u65b9\u6cd5\u4e8c : \u5f15\u5165\u4fe1\u53f7<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A-toc\" style=\"margin-left:40px\">\u8fd0\u884c\u7ed3\u679c:<\/p>\n<p id=\"%E4%BB%A3%E7%A0%81%3A-toc\" style=\"margin-left:40px\">\u4ee3\u7801:<\/p>\n<p id=\"telnet-toc\" style=\"margin-left:0px\">telnet<\/p>\n<p id=\"netstat%20-natp-toc\" style=\"margin-left:0px\">netstat -natp<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E9%98%B6%E6%AE%B5%E4%B8%89%3A%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC-toc\" style=\"margin-left:0px\">\u4e94\u3001\u9636\u6bb5\u4e09: \u591a\u7ebf\u7a0b\u7248\u672c<\/p>\n<p id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A-toc\" style=\"margin-left:40px\">\u903b\u8f91\u68b3\u7406:<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%20%3A-toc\" style=\"margin-left:40px\">\u8fd0\u884c\u7ed3\u679c :<\/p>\n<p id=\"%E4%BB%A3%E7%A0%81%3A-toc\" style=\"margin-left:40px\">\u4ee3\u7801:<\/p>\n<p id=\"%E5%85%AD%E3%80%81%E9%98%B6%E6%AE%B5%E5%9B%9B%3A%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%89%88%E6%9C%AC-toc\" style=\"margin-left:0px\">\u516d\u3001\u9636\u6bb5\u56db: \u7ebf\u7a0b\u6c60\u7248\u672c<\/p>\n<p id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A-toc\" style=\"margin-left:40px\">\u903b\u8f91\u68b3\u7406:<\/p>\n<p id=\"%E6%94%B9%E5%8A%A8%E7%9A%84%E5%9C%B0%E6%96%B9%3A-toc\" style=\"margin-left:40px\">\u6539\u52a8\u7684\u5730\u65b9:<\/p>\n<p id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A-toc\" style=\"margin-left:40px\">\u8fd0\u884c\u7ed3\u679c:<\/p>\n<p id=\"%E9%95%BF%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%9F%AD%E8%BF%9E%E6%8E%A5-toc\" style=\"margin-left:40px\">\u957f\u8fde\u63a5\u548c\u77ed\u8fde\u63a5<\/p>\n<p id=\"%E4%BB%A3%E7%A0%81%3A-toc\" style=\"margin-left:40px\">\u4ee3\u7801:<\/p>\n<p id=\"%E4%B8%83%E3%80%81%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:0px\">\u4e03\u3001\u603b\u7ed3<\/p>\n<hr \/>\n<h2 style=\"background-color:transparent\">\u4e00\u3001\u4ece UDP \u8fc7\u6e21\u5230 TCP \u534f\u8bae<\/h2>\n<p>\u5728\u524d\u9762\u7684\u51e0\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u5b8c\u6574\u7684\u624b\u5199\u3001\u8c03\u8bd5\u3001\u8fd0\u884c\u4e86\u4e00\u5957 UDP \u591a\u4eba\u5e7f\u64ad\u804a\u5929\u5ba4\u7684\u4ee3\u7801&#xff0c;\u4ece\u6a21\u5757\u5316\u5c01\u88c5\u3001\u56de\u8c03\u89e3\u8026\u3001\u7ebf\u7a0b\u6c60\u5f02\u6b65\u8c03\u5ea6&#xff0c;\u5230\u6700\u540e\u591a\u5ba2\u6237\u7aef\u540c\u65f6\u5728\u7ebf\u804a\u5929&#xff0c;\u6574\u5957\u7f51\u7edc\u901a\u4fe1\u6d41\u7a0b\u53ef\u4ee5\u51c6\u786e\u8dd1\u901a\u3002<\/p>\n<p>\u4e0b\u6765\u6211\u4eec\u5c31 TCP \u7ee7\u7eed\u5c55\u5f00\u5b66\u4e60&#xff0c;\u548c\u4e4b\u524d\u4e00\u6837&#xff0c;\u6211\u4eec\u5b66\u4e60 TCP \u7684\u65f6\u5019\u8fd8\u662f\u4ee5\u7f16\u7801\u4e3a\u4e3b&#xff0c;\u5728\u7f16\u7801\u7684\u540c\u65f6\u8bb2\u89e3\u5404\u4e2a TCP \u63a5\u53e3API\u7684\u542b\u4e49\u3002\u5728\u6b63\u5f0f\u5b66\u4e60\u00a0TCP \u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u7cfb\u7edf\u6027\u590d\u76d8 UDP \u7684\u6574\u4f53\u7279\u70b9&#xff0c;\u518d\u5e73\u6ed1\u8fc7\u6e21\u5230 TCP \u534f\u8bae&#xff0c;\u5bf9\u6bd4\u4e24\u79cd\u534f\u8bae\u7684\u5e95\u5c42\u5dee\u5f02\u548c\u4f7f\u7528\u533a\u522b&#xff0c;\u4e3a\u63a5\u4e0b\u6765\u00a0TCP \u7f16\u7801\u505a\u597d\u51c6\u5907\u3002<\/p>\n<p>1. \u5feb\u901f\u590d\u76d8&#xff1a;\u6211\u4eec\u521a\u521a\u5199\u5b8c\u7684 UDP \u6709\u4ec0\u4e48\u6838\u5fc3\u7279\u70b9 :<\/p>\n<p>\u7b2c\u4e00&#xff0c;<span style=\"color:#fe2c24\">UDP \u662f\u65e0\u8fde\u63a5\u534f\u8bae<\/span>\u3002\u901a\u4fe1\u53cc\u65b9\u4e0d\u9700\u8981\u63d0\u524d\u6253\u62db\u547c\u3001\u4e0d\u9700\u8981\u5efa\u7acb\u4e13\u5c5e\u901a\u8def&#xff0c;\u5ba2\u6237\u7aef\u76f4\u63a5\u6253\u5305\u6570\u636e\u5f80\u5916\u53d1\u5c31\u884c&#xff0c;\u670d\u52a1\u7aef\u88ab\u52a8\u63a5\u6536\u6570\u636e\u5373\u53ef&#xff0c;\u6574\u4e2a\u6d41\u7a0b\u8f7b\u91cf\u5316&#xff0c;\u6ca1\u6709\u591a\u4f59\u5f00\u9500\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;<span style=\"color:#fe2c24\">UDP \u901a\u4fe1\u4e0d\u4fdd\u8bc1\u53ef\u9760\u4f20\u8f93<\/span>\u3002\u534f\u8bae\u672c\u8eab\u4e0d\u4f1a\u7ed9\u6570\u636e\u505a\u786e\u8ba4\u5e94\u7b54\u3001\u4e0d\u4f1a\u81ea\u52a8\u91cd\u4f20\u4e22\u5931\u7684\u6570\u636e\u3002\u7f51\u7edc\u4fe1\u53f7\u6ce2\u52a8\u65f6&#xff0c;\u804a\u5929\u6d88\u606f\u6709\u53ef\u80fd\u76f4\u63a5\u4e22\u5305\u3001\u5bfc\u81f4\u5bf9\u65b9\u6536\u4e0d\u5230&#xff0c;\u7b80\u5355\u6765\u8bf4\u5c31\u662f \u201c\u53d1\u51fa\u53bb\u5c31\u4e0d\u7ba1\u4e86\u201d\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;<span style=\"color:#fe2c24\">UDP \u7f16\u7a0b\u63a5\u53e3\u7b80\u5355\u7edf\u4e00\u3002<\/span>\u6211\u4eec\u5728\u7f16\u7801\u65f6\u53ea\u7528\u4e86 socket\u3001bind\u3001sendto\u3001recvfrom \u56db\u4e2a\u6838\u5fc3 API&#xff0c;\u5c31\u80fd\u5b9e\u73b0\u6536\u53d1\u6d88\u606f&#xff0c;\u6ca1\u6709\u6d89\u53ca\u5176\u4ed6\u7684\u63a5\u53e3API\u548c\u989d\u5916\u7684&#xff0c;\u67b6\u6784\u7b80\u5355&#xff0c;\u9002\u5408\u5165\u95e8\u7ec3\u624b\u3002<\/p>\n<p>\u7b2c\u56db&#xff0c;<span style=\"color:#fe2c24\">UDP \u662f\u9762\u5411\u6570\u636e\u62a5\u7684\u534f\u8bae<\/span>&#xff0c;UDP \u662f\u4ee5\u6570\u636e\u62a5\u4e3a\u5355\u4f4d\u6536\u53d1\u6570\u636e\u7684&#xff0c;\u53d1\u9001\u65b9\u8c03\u7528\u4e00\u6b21 sendto&#xff0c;\u5c31\u4f1a\u5f62\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u6570\u636e\u5305&#xff1b;\u63a5\u6536\u65b9\u8c03\u7528\u4e00\u6b21 recvfrom&#xff0c;\u4e5f\u53ea\u80fd\u5b8c\u6574\u63a5\u6536\u4e00\u4e2a\u6570\u636e\u5305\u3002\u6570\u636e\u6536\u53d1\u7684\u8fb9\u754c\u662f\u5929\u7136\u4fdd\u7559\u7684\u3002<\/p>\n<p>\u7b2c\u4e94&#xff0c;<span style=\"color:#fe2c24\">UDP \u7b80\u5355\u8f7b\u4fbf<\/span>&#xff0c;\u6b63\u662f\u56e0\u4e3a UDP \u7b80\u5355\u8f7b\u4fbf&#xff0c;\u6240\u4ee5\u6211\u4eec\u624d\u80fd\u8f7b\u677e\u642d\u914d\u56de\u8c03\u51fd\u6570\u3001\u7ebf\u7a0b\u6c60&#xff0c;\u628a\u5e7f\u64ad\u4efb\u52a1\u5f02\u6b65\u89e3\u8026\u3002<\/p>\n<p>2. \u8fc7\u6e21\u5230 TCP<\/p>\n<p>UDP \u7684\u4f18\u70b9\u662f\u7b80\u5355\u3001\u5feb\u901f\u3001\u5f00\u9500\u5c0f&#xff0c;\u4f46\u7f3a\u70b9\u4e5f\u975e\u5e38\u660e\u663e&#xff1a;<span style=\"color:#fe2c24\">\u4e0d\u53ef\u9760\u3001\u5bb9\u6613\u4e22\u6d88\u606f\u3001\u65e0\u6cd5\u611f\u77e5\u5ba2\u6237\u7aef\u4e0a\u4e0b\u7ebf\u3001\u4e0d\u9002\u5408\u6b63\u5f0f\u4e1a\u52a1\u573a\u666f\u3002<\/span><\/p>\n<p>\u56e0\u6b64\u5728\u771f\u6b63\u5de5\u4f5c\u4e2d&#xff0c;\u6bd4\u5982\u804a\u5929\u8f6f\u4ef6\u3001\u6d4f\u89c8\u5668\u8bbf\u95ee\u3001\u6587\u4ef6\u4f20\u8f93\u3001\u8fdc\u7a0b\u767b\u5f55&#xff0c;\u8fd9\u4e9b\u4e0d\u80fd\u4e22\u6570\u636e\u7684\u573a\u666f\u4e2d&#xff0c;\u6211\u4eec\u4f7f\u7528\u7684\u90fd\u662f\u00a0TCP \u534f\u8bae\u3002UDP \u53ea\u80fd\u7528\u5728\u76f4\u64ad\u3001\u8bed\u97f3\u901a\u8bdd\u3001\u6e38\u620f\u5b9e\u65f6\u5e27\u540c\u6b65\u8fd9\u7c7b\u53ef\u4ee5\u5bb9\u5fcd\u5c11\u91cf\u4e22\u5305\u3001\u4f18\u5148\u4fdd\u901f\u5ea6\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>3. \u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b TCP \u534f\u8bae\u7684\u7279\u70b9 :<\/p>\n<p>\u7b2c\u4e00&#xff0c;<span style=\"color:#fe2c24\">TCP \u662f\u9762\u5411\u6709\u8fde\u63a5\u7684\u534f\u8bae<\/span>\u3002\u5c31\u50cf\u6253\u7535\u8bdd&#xff0c;\u5fc5\u987b\u5148\u62e8\u53f7\u63a5\u901a\u3001\u53cc\u65b9\u5efa\u7acb\u4e13\u5c5e\u901a\u9053&#xff0c;\u4e4b\u540e\u624d\u80fd\u8bf4\u8bdd\u901a\u4fe1\u3002\u5ba2\u6237\u7aef\u5fc5\u987b\u4e3b\u52a8\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u670d\u52a1\u7aef\u5fc5\u987b\u4e3b\u52a8\u63a5\u6536\u8fde\u63a5&#xff0c;\u94fe\u8def\u5efa\u597d\u4e4b\u540e&#xff0c;\u53cc\u65b9\u624d\u80fd\u6b63\u5e38\u6536\u53d1\u6570\u636e&#xff0c;\u4e0d\u662f\u60f3\u53d1\u5c31\u53d1\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;<span style=\"color:#fe2c24\">TCP \u4fdd\u8bc1\u53ef\u9760\u4f20\u8f93<\/span>\u3002\u534f\u8bae\u5e95\u5c42\u81ea\u5e26\u786e\u8ba4\u5e94\u7b54\u3001\u8d85\u65f6\u91cd\u4f20\u3001\u6570\u636e\u5305\u6392\u5e8f\u3001\u6d41\u91cf\u63a7\u5236\u3001\u62e5\u585e\u63a7\u5236\u5168\u5957\u673a\u5236\u3002\u53ea\u8981\u7f51\u7edc\u6ca1\u65ad&#xff0c;\u6570\u636e\u4e00\u5b9a\u80fd\u5b8c\u6574\u3001\u6709\u5e8f\u3001\u4e0d\u91cd\u590d\u3001\u4e0d\u4e22\u5931\u5730\u9001\u5230\u5bf9\u65b9\u624b\u91cc&#xff0c;\u975e\u5e38\u9002\u5408\u6b63\u5f0f\u4e1a\u52a1\u5f00\u53d1\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;<span style=\"color:#fe2c24\">TCP \u662f\u5b57\u8282\u6d41\u4f20\u8f93<\/span>&#xff0c;\u6ca1\u6709\u6570\u636e\u5305\u8fb9\u754c\u3002\u6570\u636e\u50cf\u6d41\u6c34\u4e00\u6837\u6e90\u6e90\u4e0d\u65ad\u5f80\u5916\u6d41\u3002\u8fde\u7eed\u53d1\u4e24\u6761\u804a\u5929\u6d88\u606f&#xff0c;\u5bf9\u65b9\u53ef\u80fd\u4e00\u6b21\u6027\u6536\u4e24\u6761\u3001\u4e5f\u53ef\u80fd\u62c6\u5f00\u4e71\u6536&#xff0c;\u56e0\u6b64\u4f1a\u51fa\u73b0\u7c98\u5305\u3001\u62c6\u5305\u95ee\u9898&#xff0c;\u6240\u4ee5\u00a0TCP \u7f16\u7801\u65f6\u5fc5\u987b\u81ea\u5df1\u624b\u52a8\u8bbe\u8ba1\u6d88\u606f\u8fb9\u754c&#xff0c;\u8fd9\u4e5f\u662f TCP \u6700\u5927\u7684\u5f00\u53d1\u96be\u70b9\u3002<\/p>\n<p>\u7b2c\u56db&#xff0c;<span style=\"color:#fe2c24\">TCP \u53ef\u4ee5\u5b9e\u65f6\u611f\u77e5\u5ba2\u6237\u7aef\u5728\u7ebf\u3001\u79bb\u7ebf\u72b6\u6001<\/span>\u3002\u5ba2\u6237\u7aef\u6389\u7ebf\u65ad\u7f51\u9000\u51fa&#xff0c;\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u4f1a\u76f4\u63a5\u611f\u77e5\u5230&#xff0c;\u80fd\u81ea\u52a8\u628a\u7528\u6237\u4ece\u5728\u7ebf\u5217\u8868\u5254\u9664\u3002<\/p>\n<p>4. UDP \u548c TCP \u6838\u5fc3 API \u5bf9\u6bd4<\/p>\n<p>UDP \u7528\u5230\u7684\u6838\u5fc3\u63a5\u53e3\u5982\u4e0b&#xff0c;\u6bd4\u8f83\u7b80\u5355:<\/p>\n<li>\u521b\u5efa\u5957\u63a5\u5b57&#xff1a;socket (AF_INET, <span style=\"color:#fe2c24\">SOCK_DGRAM<\/span>, 0)<\/li>\n<li>\u670d\u52a1\u7aef\u7ed1\u5b9a\u7aef\u53e3&#xff1a;bind()<\/li>\n<li>\u6536\u6240\u6709\u5ba2\u6237\u7aef\u6d88\u606f&#xff1a;recvfrom()<\/li>\n<li>\u7ed9\u6240\u6709\u5ba2\u6237\u7aef\u53d1\u6d88\u606f&#xff1a;sendto()<\/li>\n<p>\u670d\u52a1\u7aef\u6574\u4e2a\u8fc7\u7a0b\u53ea\u9700\u8981\u4e00\u4e2a\u5957\u63a5\u5b57&#xff0c;\u5c31\u80fd\u7ba1\u7406\u5168\u90e8\u7684\u5ba2\u6237\u7aef\u3002<\/p>\n<p>TCP \u8981\u7528\u5230\u7684\u63a5\u53e3\u5219\u4f1a\u591a\u4e00\u70b9&#xff0c;\u5e76\u4e14\u4f7f\u7528\u903b\u8f91\u548c UDP \u4e5f\u4e0d\u4e00\u6837:<\/p>\n<li>\u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57&#xff1a;socket (AF_INET,<span style=\"color:#fe2c24\"> SOCK_STREAM<\/span>, 0)<\/li>\n<li>\u670d\u52a1\u7aef\u7ed1\u5b9a\u7aef\u53e3&#xff1a;bind()<\/li>\n<li>\u670d\u52a1\u7aef\u5f00\u542f\u76d1\u542c&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u4e0a\u95e8&#xff1a;listen()<\/li>\n<li>\u670d\u52a1\u7aef\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u751f\u6210\u4e13\u5c5e\u901a\u4fe1\u5957\u63a5\u5b57&#xff1a;accept()<\/li>\n<li>\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u6536\u6570\u636e&#xff1a;read() \/ recv()<\/li>\n<li>\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u6536\u53d1\u6570\u636e&#xff1a;write() \/ send()<\/li>\n<li>\u6700\u540e\u5173\u95ed\u5957\u63a5\u5b57&#xff1a;close()<\/li>\n<p>UDP \u9760 recvfrom \/ sendto \u5c31\u80fd\u76f4\u63a5\u901a\u4fe1&#xff1b;\u800c TCP \u9700\u8981\u5148\u76d1\u542c\u3001\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u751f\u6210\u4e00\u5bf9\u4e00\u7684\u4e13\u5c5e\u5957\u63a5\u5b57\u6536\u53d1\u6570\u636e&#xff0c;\u7ed3\u6784\u66f4\u4e25\u8c28&#xff0c;\u4e5f\u66f4\u590d\u6742\u3002<\/p>\n<p>5. \u5b9e\u9645\u5f00\u53d1\u4f7f\u7528\u4e0a&#xff0c;\u4e8c\u8005\u6700\u76f4\u89c2\u7684\u533a\u522b:<\/p>\n<p>UDP \u670d\u52a1\u7aef\u4f7f\u7528\u4e00\u4e2a\u5957\u63a5\u5b57 socket \u5c31\u80fd\u901a\u5403\u5168\u573a&#xff0c;\u4e0d\u7528\u7ba1\u8c01\u5728\u7ebf\u3001\u8c01\u79bb\u7ebf\u3002<\/p>\n<p>TCP \u6bcf\u589e\u52a0\u4e00\u4e2a\u5ba2\u6237\u7aef\u5c31\u8981\u591a\u5f00\u4e00\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57 socket&#xff0c;\u5fc5\u987b\u591a\u7ebf\u7a0b\u7ba1\u7406\u6bcf\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u67b6\u6784\u66f4\u8d34\u8fd1\u4f01\u4e1a\u771f\u5b9e\u540e\u7aef\u670d\u52a1\u3002<\/p>\n<p>UDP \u4e0d\u7528\u5904\u7406\u7c98\u5305&#xff0c;TCP \u9700\u8981\u624b\u52a8\u89e3\u51b3\u7c98\u5305\u62c6\u5305\u95ee\u9898\u3002<\/p>\n<p>UDP \u53ea\u7ba1\u6536\u53d1&#xff0c;\u4e0d\u7ba1\u53ef\u9760&#xff1b;TCP \u5168\u7a0b\u4fdd\u969c\u6d88\u606f\u7edd\u5bf9\u9001\u8fbe&#xff0c;\u9002\u5408\u6b63\u5f0f\u9879\u76ee\u3002<\/p>\n<h2 id=\"%E4%BA%8C%E3%80%81TCP%20%E7%BC%96%E7%A0%81%E7%8E%AF%E8%8A%82\">\u4e8c\u3001TCP \u7f16\u7801\u73af\u8282<\/h2>\n<p>\u5728\u7cfb\u7edf\u5bf9\u6bd4\u4e86 UDP \u4e0e TCP \u7684\u6838\u5fc3\u5dee\u5f02\u540e&#xff0c;\u6211\u4eec\u6b63\u5f0f\u8fdb\u5165 TCP \u534f\u8bae\u7684\u7f16\u7801\u73af\u8282\u3002\u4e3a\u4e86\u5faa\u5e8f\u6e10\u8fdb\u5730\u7406\u89e3 TCP \u670d\u52a1\u7aef\u7684\u901a\u4fe1\u6a21\u578b\u4e0e\u5e76\u53d1\u901a\u4fe1&#xff0c;\u6211\u4eec\u5c06\u6309\u7167\u4ece\u7b80\u5355\u5230\u590d\u6742\u3001\u4ece\u57fa\u7840\u5230\u4f01\u4e1a\u7ea7\u7684\u601d\u8def&#xff0c;\u5206\u56db\u4e2a\u9636\u6bb5\u6765\u5b9e\u73b0\u6211\u4eec\u7684 TCP \u4ee3\u7801&#xff0c;\u548c\u4e4b\u524d\u7684 UDP \u6a21\u578b\u4e00\u6837&#xff0c;\u6211\u4eec\u4f9d\u7136\u4f7f\u7528\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u4e92\u76f8\u901a\u4fe1\u7684\u7ed3\u6784\u6a21\u578b&#xff1a;<\/p>\n<p>1. <span style=\"color:#fe2c24\">\u9636\u6bb5\u4e00&#xff1a;\u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c<\/span>&#xff1a;\u6211\u4eec\u5148\u4e0d\u8ffd\u6c42\u5e76\u53d1&#xff0c;\u7528\u6700\u7b80\u5355\u7684\u4e32\u884c\u6a21\u578b&#xff0c;\u628a TCP \u901a\u4fe1\u7684\u6838\u5fc3\u63a5\u53e3\u3001\u8c03\u7528\u6d41\u7a0b\u548c\u6570\u636e\u6536\u53d1\u903b\u8f91\u68b3\u7406\u6e05\u695a\u3002\u4e3a\u540e\u7eed\u7684\u4f18\u5316\u6253\u57fa\u7840\u3002<\/p>\n<p>2. <span style=\"color:#fe2c24\">\u9636\u6bb5\u4e8c&#xff1a;\u591a\u8fdb\u7a0b\u7248\u672c&#xff1a;<\/span>\u5728\u5355\u7ebf\u7a0b\u7684\u57fa\u7840\u4e0a&#xff0c;\u5f15\u5165 fork() \u7cfb\u7edf\u8c03\u7528&#xff0c;\u8ba9\u670d\u52a1\u7aef\u80fd\u591f\u540c\u65f6\u4e3a\u591a\u4e2a\u5ba2\u6237\u7aef\u670d\u52a1&#xff0c;\u4f53\u9a8c\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u7a0b\u6a21\u578b\u5728\u5e76\u53d1\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<p>3. <span style=\"color:#fe2c24\">\u9636\u6bb5\u4e09&#xff1a;\u591a\u7ebf\u7a0b\u7248\u672c<\/span>&#xff1a;\u57fa\u4e8e\u591a\u8fdb\u7a0b\u6a21\u578b\u8fdb\u884c\u4f18\u5316&#xff0c;\u670d\u52a1\u7aef\u6539\u7528\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u53d1&#xff0c;\u611f\u53d7\u7ebf\u7a0b\u6bd4\u8fdb\u7a0b\u66f4\u8f7b\u91cf\u3001\u66f4\u9ad8\u6548\u7684\u4f18\u52bf\u3002<\/p>\n<p>4. <span style=\"color:#fe2c24\">\u9636\u6bb5\u56db&#xff1a;\u7ebf\u7a0b\u6c60\u7248\u672c<\/span>&#xff1a;\u5f15\u5165\u7ebf\u7a0b\u6c60&#xff0c;\u89e3\u51b3\u591a\u7ebf\u7a0b\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u7684\u5f00\u9500\u95ee\u9898&#xff0c;\u5b9e\u73b0\u4f01\u4e1a\u7ea7\u670d\u52a1\u7aef\u5e38\u7528\u7684\u9ad8\u6548\u3001\u53ef\u63a7\u7684\u5e76\u53d1\u6a21\u578b\u3002<\/p>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c31\u4ece\u9636\u6bb5\u4e00&#xff1a;\u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c\u5f00\u59cb&#xff0c;\u4e00\u6b65\u6b65\u642d\u5efa\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a TCP \u670d\u52a1\u7aef\u3002\u8fd9\u4e2a\u7248\u672c\u7684\u76ee\u6807\u4e0d\u662f\u9ad8\u5e76\u53d1&#xff0c;\u800c\u662f\u8ba9\u6211\u4eec\u771f\u6b63\u719f\u6089 TCP \u7684\u6bcf\u4e00\u4e2a\u63a5\u53e3\u548c\u6bcf\u4e00\u6b65\u6d41\u7a0b\u3002<\/p>\n<h2 id=\"%E4%B8%89%E3%80%81%E9%98%B6%E6%AE%B5%E4%B8%80%20%3A%20%E5%8D%95%E8%BF%9B%E7%A8%8B%2F%E5%8D%95%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC\">\u4e09\u3001\u9636\u6bb5\u4e00 : \u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c<\/h2>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c31\u5f00\u59cb\u7f16\u5199\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u3002\u6211\u4eec\u8fb9\u5199\u4ee3\u7801\u8fb9\u8bb2\u89e3\u6bcf\u4e2a\u63a5\u53e3\u7684\u4f5c\u7528:<\/p>\n<p>\u5728\u6b63\u5f0f\u7f16\u7801\u4e4b\u524d&#xff0c;\u6211\u4eec\u5fc5\u987b\u5148\u7406\u6e05 TCP \u5173\u952e\u7684\u63a5\u53e3\u4ee5\u53ca\u63a5\u53e3\u4e4b\u95f4\u7684\u8054\u7cfb\u3002\u56e0\u4e3a TCP \u662f\u9762\u5411\u8fde\u63a5\u7684\u534f\u8bae&#xff0c;\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u7684\u7cfb\u7edf\u8c03\u7528\u4e0d\u80fd\u968f\u610f\u8c03\u7528&#xff0c;\u5fc5\u987b\u9075\u5faa\u56fa\u5b9a\u7684\u65f6\u5e8f\u3001\u4e92\u76f8\u914d\u5408&#xff0c;\u624d\u80fd\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u4e0e\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<h3 id=\"%E9%93%BA%E5%9E%AB\">\u94fa\u57ab<\/h3>\n<p>1. \u4e24\u7aef\u90fd\u5fc5\u987b\u8c03\u7528\u7684\u516c\u5171\u57fa\u7840\u63a5\u53e3<\/p>\n<p>socket() : \u65e0\u8bba\u670d\u52a1\u7aef\u8fd8\u662f\u5ba2\u6237\u7aef&#xff0c;\u901a\u4fe1\u7684\u7b2c\u4e00\u6b65\u90fd\u662f\u8c03\u7528 socket()\u3002\u4f5c\u7528&#xff1a;\u5728\u5185\u6838\u4e2d\u521b\u5efa\u5957\u63a5\u5b57\u6587\u4ef6&#xff0c;\u751f\u6210\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4f5c\u4e3a\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u7840\u8f7d\u4f53\u3002\u6ca1\u6709 socket \u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u540e\u7eed\u6240\u6709\u7f51\u7edc\u64cd\u4f5c\u90fd\u65e0\u6cd5\u8fdb\u884c\u3002<\/p>\n<p>2. \u4ec5\u670d\u52a1\u7aef\u9700\u8981\u7684\u4e13\u5c5e\u63a5\u53e3<\/p>\n<p>bind() : \u670d\u52a1\u7aef\u5fc5\u987b\u624b\u52a8 bind&#xff0c;\u7ed1\u5b9a\u56fa\u5b9a\u7684\u672c\u673a IP &#043; \u56fa\u5b9a\u7aef\u53e3( INADDR_ANY )\u3002\u8ba9\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7 IP &#043; \u7aef\u53e3\u7cbe\u51c6\u627e\u5230\u670d\u52a1\u7aef\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p>listen() : \u5c06\u666e\u901a\u7ed1\u5b9a\u540e\u7684\u5957\u63a5\u5b57<span style=\"color:#fe2c24\">\u5f00\u542f\u7aef\u53e3\u76d1\u542c<\/span>&#xff0c;\u8ba9\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u81ea\u52a8\u7ef4\u62a4\u8fde\u63a5\u961f\u5217\u3001\u5904\u7406\u4e09\u6b21\u63e1\u624b\u3002<\/p>\n<\/p>\n<p>accept() : <span style=\"color:#fe2c24\">\u963b\u585e\u7b49\u5f85\u5e76\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5<\/span>&#xff0c;\u4ece\u5185\u6838\u5b8c\u6210\u63e1\u624b\u7684\u961f\u5217\u4e2d\u53d6\u51fa\u8fde\u63a5&#xff0c;\u751f\u6210\u5168\u65b0\u7684\u901a\u4fe1\u5957\u63a5\u5b57&#xff0c;\u4e13\u95e8\u7528\u6765\u548c\u5355\u4e2a\u5ba2\u6237\u7aef\u505a\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<p>3. \u4ec5\u5ba2\u6237\u7aef\u9700\u8981\u7684\u4e13\u5c5e\u63a5\u53e3<\/p>\n<p>connect() : \u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u624b\u52a8 bind&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5206\u914d\u968f\u673a\u672c\u5730\u7aef\u53e3\u4e0e\u4e34\u65f6 IP&#xff1b;<span style=\"color:#fe2c24\">\u5ba2\u6237\u7aef\u53ea\u9700\u901a\u8fc7 connect()&#xff0c;\u643a\u5e26\u670d\u52a1\u7aef IP &#043; \u670d\u52a1\u7aef\u56fa\u5b9a\u7aef\u53e3<\/span>&#xff0c;\u4e3b\u52a8\u53d1\u8d77\u4e09\u6b21\u63e1\u624b\u8bf7\u6c42&#xff0c;<span style=\"color:#fe2c24\">\u548c\u670d\u52a1\u7aef\u5efa\u7acb\u4e13\u5c5e TCP \u8fde\u63a5\u901a\u9053\u3002<\/span><\/p>\n<p>4. \u8fde\u63a5\u5efa\u7acb\u540e&#xff0c;\u4e24\u7aef\u5b8c\u5168\u5171\u7528\u7684\u6536\u53d1\u63a5\u53e3<\/p>\n<p>TCP \u8fde\u63a5\u4e00\u65e6\u5efa\u7acb\u6210\u529f&#xff0c;\u670d\u52a1\u7aef accept \u8fd4\u56de\u7684\u901a\u4fe1\u6587\u4ef6\u00a0fd \u4e0e \u5ba2\u6237\u7aef socket \u7684\u6587\u4ef6 fd \u5f62\u6210\u53cc\u5411\u901a\u8def\u3002\u4e24\u7aef\u5730\u4f4d\u5bf9\u7b49&#xff0c;\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e24\u5957\u5b8c\u5168\u7b49\u4ef7\u7684\u6536\u53d1\u51fd\u6570&#xff1a;<\/p>\n<\/p>\n<p>\u901a\u7528\u6587\u4ef6 IO : read() \/ write()<\/p>\n<\/p>\n<p>\u7f51\u7edc\u4e13\u7528 IO : recv() \/ send()<\/p>\n<p>\u8865\u5145\u77e5\u8bc6\u70b9 :<\/p>\n<p>\u5728TCP \u8fde\u63a5\u6210\u529f\u7684\u524d\u63d0\u4e0b&#xff1a;read &#061;&#061; recv\u3001write &#061;&#061; send&#xff0c;\u529f\u80fd\u672c\u8d28\u4e00\u81f4&#xff0c;\u5e95\u5c42\u90fd\u662f\u62f7\u8d1d\u6570\u636e\u5230\u5185\u6838\u7f13\u51b2\u533a&#xff1b;\u533a\u522b\u4ec5\u5728\u4e8e recv \/ send \u591a\u4e86\u7b2c\u56db\u4e2a\u6807\u5fd7\u4f4d\u53c2\u6570&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u7279\u6b8a\u884c\u4e3a&#xff1b;\u65e5\u5e38\u666e\u901a TCP \u56de\u663e\u901a\u4fe1&#xff0c;\u76f4\u63a5\u4f7f\u7528 read \/ write \u5b8c\u5168\u591f\u7528&#xff0c;\u4ee3\u7801\u66f4\u7b80\u6d01\u901a\u7528\u3002<\/p>\n<h3 id=\"%E6%80%9D%E8%B7%AF%E6%80%BB%E7%BB%93\">\u601d\u8def\u603b\u7ed3<\/h3>\n<p>\u53cc\u65b9\u901a\u4fe1\u7684\u8d77\u59cb&#xff0c;\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u90fd\u9700\u8981\u5148\u901a\u8fc7 socket \u521b\u5efa\u5957\u63a5\u5b57\u3002\u670d\u52a1\u7aef\u521b\u5efa\u5957\u63a5\u5b57\u540e&#xff0c;\u9700\u8981\u4e3b\u52a8\u8c03\u7528 bind \u7ed1\u5b9a\u56fa\u5b9a IP \u4e0e\u7aef\u53e3&#xff0c;\u518d\u901a\u8fc7 listen \u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u76d1\u542c\u72b6\u6001&#xff0c;\u6700\u540e\u4f9d\u9760 accept \u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u5ba2\u6237\u7aef\u540c\u6837\u9700\u8981\u5148\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u4f46\u65e0\u9700\u624b\u52a8\u7ed1\u5b9a\u672c\u5730\u5730\u5740\u4e0e\u7aef\u53e3&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5b8c\u6210\u5206\u914d&#xff0c;\u968f\u540e\u5ba2\u6237\u7aef\u76f4\u63a5\u8c03\u7528 connect&#xff0c;\u643a\u5e26\u670d\u52a1\u7aef\u7684 IP \u548c\u7aef\u53e3\u4e3b\u52a8\u53d1\u8d77\u8fde\u63a5\u3002\u5f53\u670d\u52a1\u7aef accept \u6355\u83b7\u5230\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u53cc\u65b9\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u3001\u5efa\u7acb\u4e13\u5c5e TCP \u901a\u4fe1\u901a\u9053\u540e&#xff0c;\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u901a\u8fc7 read\/write \u6216 recv\/send \u63a5\u53e3&#xff0c;\u5b9e\u73b0\u53cc\u5411\u7684\u6570\u636e\u6536\u53d1\u4ea4\u4e92\u3002<\/p>\n<p>\u4e0b\u6765\u6211\u4eec\u5148\u770b TCP \u670d\u52a1\u7aef\u5934\u6587\u4ef6\u7684\u4ee3\u7801:<\/p>\n<h3 id=\"EchoTcpServer.hpp%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%3A\">EchoTcpServer.hpp \u670d\u52a1\u7aef:<\/h3>\n<p>\u56e0\u4e3a\u6211\u4eec\u7b2c\u4e00\u4e2a\u9636\u6bb5\u662f\u5355\u8fdb\u7a0b \/ \u5355\u7ebf\u7a0b\u4e32\u884c\u6267\u884c\u7684&#xff0c;\u7ed3\u5408\u4e0a\u9762\u6211\u4eec\u68b3\u7406\u7684\u00a0TCP \u5404\u4e2a\u63a5\u53e3\u8c03\u7528\u7684\u987a\u5e8f&#xff0c;\u5b8c\u6574\u8fd8\u539f\u670d\u52a1\u7aef&#xff1a;<span style=\"color:#fe2c24\">socket -&gt; bind -&gt; listen -&gt; accept -&gt; \u6570\u636e\u6536\u53d1<\/span> \u7684\u5b8c\u6574\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<p>\u670d\u52a1\u7aef\u6574\u4f53\u6267\u884c\u903b\u8f91\u9075\u5faa&#xff1a;<span style=\"color:#ff9900\">\u5148\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u56fa\u5b9a\u7aef\u53e3\u3001\u5f00\u542f\u76d1\u542c&#xff0c;\u5faa\u73af\u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u83b7\u53d6\u901a\u4fe1\u5957\u63a5\u5b57\u540e&#xff0c;\u4e32\u884c\u5b8c\u6210\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u8bfb\u5199\u4ea4\u4e92&#xff0c;\u76f4\u5230\u5ba2\u6237\u7aef\u65ad\u5f00&#xff0c;\u624d\u4f1a\u91cd\u65b0\u56de\u5230 accept \u63a5\u6536\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002<\/span><\/p>\n<p>\u4e0b\u9762\u76f4\u63a5\u653e\u51fa\u670d\u52a1\u7aef\u5b8c\u6574\u4ee3\u7801&#xff0c;\u6211\u4eec\u9010\u884c\u62c6\u89e3\u8bb2\u89e3&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3775\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230036-6a63ee947c2f6.png\" width=\"1170\" \/><\/p>\n<p>\u9996\u5148\u662f\u5934\u6587\u4ef6\u90e8\u5206&#xff0c;\u6211\u4eec\u5f15\u5165\u4e86\u6807\u51c6\u5e93\u3001\u7cfb\u7edf\u5957\u63a5\u5b57\u76f8\u5173\u5934\u6587\u4ef6&#xff0c;\u540c\u65f6\u5305\u542b\u4e86\u4e4b\u524d\u5c01\u88c5\u597d\u7684 Logger.hpp&#xff08;\u65e5\u5fd7\u5de5\u5177&#xff09;\u548c InetAddr.hpp&#xff08;IP\u5730\u5740\u5de5\u5177\u7c7b&#xff09;&#xff0c;\u65b9\u4fbf\u540e\u7eed\u65e5\u5fd7\u6253\u5370\u548c\u5ba2\u6237\u7aef\u5730\u5740\u89e3\u6790\u3002\u5168\u5c40\u90e8\u5206\u8fd8\u5b9a\u4e49\u4e86\u9519\u8bef\u7801\u679a\u4e3e\u3001\u76d1\u542c\u961f\u5217\u957f\u5ea6 gbacklog\u00a0\u548c\u9ed8\u8ba4\u7aef\u53e3 gport&#xff0c;\u662f\u670d\u52a1\u7aef\u7684\u57fa\u7840\u914d\u7f6e\u3002<\/p>\n<p>\u6574\u4e2a\u670d\u52a1\u7aef\u88ab\u5c01\u88c5\u6210\u4e86 TcpServer\u00a0\u7c7b&#xff0c;\u6838\u5fc3\u7ed3\u6784\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b&#xff1a;<span style=\"color:#ff9900\">\u6784\u9020\u51fd\u6570\u3001\u521d\u59cb\u5316\u51fd\u6570 InitServer()\u3001\u542f\u52a8\u4e3b\u5faa\u73af Start()\u3001\u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO()&#xff0c;\u4ee5\u53ca\u79c1\u6709\u6210\u5458\u53d8\u91cf<\/span>\u3002<\/p>\n<p>\u5728\u8fdb\u5165\u51fd\u6570\u7ec6\u8282\u4e4b\u524d&#xff0c;\u6211\u4eec\u9700\u8981\u660e\u786e TCP \u670d\u52a1\u7aef\u4e24\u4e2a\u5173\u952e\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8fd9\u662f\u548c UDP \u6700\u5927\u7684\u533a\u522b&#xff1a;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 1. \u76d1\u542c\u5957\u63a5\u5b57 _listensockfd&#xff1a;\u5b83\u662f\u670d\u52a1\u7aef\u7684\u5168\u5c40\u552f\u4e00\u8d44\u6e90&#xff0c;\u4ece InitServer() \u521b\u5efa\u51fa\u6765&#xff0c;\u5c31\u4e00\u76f4\u56fa\u5b9a\u4e0d\u53d8&#xff0c;\u6574\u4e2a\u8fdb\u7a0b\u91cc\u53ea\u6709\u8fd9\u4e00\u4e2a&#xff0c;\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u628a\u5b83\u5b58\u4e3a\u7c7b\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u4ece\u5934\u5230\u5c3e\u590d\u7528\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 2. \u901a\u4fe1\u5957\u63a5\u5b57 sockfd&#xff1a;\u5b83\u662f\u670d\u52a1\u7aef\u5185\u6838\u52a8\u6001\u521b\u5efa\u7684\u3002\u6bcf\u6b21\u8c03\u7528 accept() \u6210\u529f\u65f6&#xff0c;\u5185\u6838\u4f1a\u7ed9\u670d\u52a1\u7aef\u5206\u914d\u4e00\u4e2a\u5168\u65b0\u7684\u3001\u72ec\u7acb\u7684 sockfd&#xff0c;\u4e13\u4f9b\u670d\u52a1\u7aef\u4e0e\u5f53\u524d\u5ba2\u6237\u7aef\u4e00\u5bf9\u4e00\u6536\u53d1\u6570\u636e\u4f7f\u7528\u3002\u8fd9\u4e2a sockfd \u5f52\u670d\u52a1\u7aef\u6240\u6709&#xff0c;\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u540e&#xff0c;\u8be5 sockfd \u5c31\u4f1a\u88ab\u5173\u95ed\u91ca\u653e\u3002\u56e0\u6b64\u6211\u4eec\u4e0d\u628a\u8fd9\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57\u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u50a8\u5b58\u3002<\/p>\n<h4 id=\"%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\"><span style=\"color:#494949\">\u6784\u9020\u51fd\u6570<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"178\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230038-6a63ee964af70.png\" width=\"978\" \/>1. \u6211\u4eec\u5148\u4ece TcpServer \u7c7b\u7684\u6784\u9020\u51fd\u6570\u8bb2\u8d77&#xff0c;\u5b83\u662f\u6574\u4e2a\u670d\u52a1\u7aef\u7684\u8d77\u70b9&#xff0c;\u6838\u5fc3\u4f5c\u7528\u5c31\u662f\u521d\u59cb\u5316\u670d\u52a1\u7aef\u7684\u57fa\u7840\u914d\u7f6e&#xff0c;\u6700\u5173\u952e\u7684\u5c31\u662f\u7ed1\u5b9a\u7aef\u53e3\u53f7\u3002\u5f53\u6211\u4eec\u542f\u52a8\u670d\u52a1\u7aef\u7a0b\u5e8f\u65f6&#xff0c;\u4f1a\u7ed9\u670d\u52a1\u7aef\u4f20\u5165\u4e00\u4e2a\u7aef\u53e3\u53f7&#xff0c;\u8fd9\u4e2a\u7aef\u53e3\u53f7\u5c31\u662f\u7528\u6765\u552f\u4e00\u6807\u8bc6\u8fd9\u53f0\u670d\u52a1\u5668\u4e0a\u7684 TCP \u670d\u52a1\u7684\u3002<\/p>\n<\/p>\n<p>2. \u5ba2\u6237\u7aef\u8981\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u5fc5\u987b\u6307\u5b9a \u670d\u52a1\u7aef IP &#043; \u8fd9\u4e2a\u7aef\u53e3\u53f7 &#xff0c;\u624d\u80fd\u7cbe\u51c6\u627e\u5230\u5bf9\u5e94\u7684\u670d\u52a1&#xff1b;<\/p>\n<\/p>\n<p>3. \u670d\u52a1\u7aef\u62ff\u5230\u8fd9\u4e2a\u7aef\u53e3\u53f7\u540e&#xff0c;\u4f1a\u5728\u540e\u7eed\u7684 InitServer() \u4e2d&#xff0c;\u901a\u8fc7 bind() \u7cfb\u7edf\u8c03\u7528&#xff0c;\u628a\u76d1\u542c\u5957\u63a5\u5b57 _listensockfd \u548c\u8fd9\u4e2a\u7aef\u53e3\u53f7\u7ed1\u5b9a\u5728\u4e00\u8d77&#xff0c;\u8ba9\u5185\u6838\u77e5\u9053&#xff1a;\u6240\u6709\u53d1\u5f80\u8fd9\u4e2a\u7aef\u53e3\u7684 TCP \u8fde\u63a5\u8bf7\u6c42&#xff0c;\u90fd\u8981\u4ea4\u7ed9\u8fd9\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\u6765\u5904\u7406\u3002<\/p>\n<h4 id=\"%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0%20InitServer()\"><span style=\"color:#494949\">\u521d\u59cb\u5316\u51fd\u6570 InitServer()<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"987\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230038-6a63ee965d04e.png\" width=\"1396\" \/><span style=\"color:#fe2c24\">1. \u7b2c\u4e00\u6b65 socket() \u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57<\/span>&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570 AF_INET \u4ee3\u8868\u4f7f\u7528 IPv4 \u7f51\u7edc\u534f\u8bae&#xff1b;\u7b2c\u4e8c\u4e2a\u53c2\u6570 SOCK_STREAM \u4ee3\u8868\u9762\u5411\u8fde\u63a5\u3001\u6d41\u5f0f\u4f20\u8f93&#xff0c;\u8fd9\u662f TCP \u7684\u4e13\u5c5e\u7279\u5f81&#xff1b;\u7b2c\u4e09\u4e2a\u53c2\u6570 0 \u8868\u793a\u9ed8\u8ba4\u534f\u8bae&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u9002\u914d TCP\u3002\u8fd9\u4e24\u4e2a\u6838\u5fc3\u53c2\u6570\u7ec4\u5408&#xff0c;\u660e\u786e\u544a\u8bc9\u5185\u6838&#xff1a;\u5f53\u524d\u8981\u521b\u5efa\u7684\u5957\u63a5\u5b57&#xff0c;\u662f\u4e13\u95e8\u7528\u4e8e TCP \u901a\u4fe1\u7684\u5957\u63a5\u5b57\u3002\u5957\u63a5\u5b57\u521b\u5efa\u6210\u529f\u540e&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u7684 _listensockfd\u3002\u4ece\u5185\u6838\u5c42\u9762\u770b&#xff0c;Socket \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7f51\u7edc\u6587\u4ef6\u5bf9\u8c61\u3002\u8fdb\u7a0b\u8c03\u7528 socket() \u7cfb\u7edf\u8c03\u7528\u65f6&#xff0c;\u5185\u6838\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u7684\u6587\u4ef6\u5bf9\u8c61&#xff0c;\u5e76\u5206\u914d\u552f\u4e00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 fd\u3002\u8fd9\u4e2a\u6587\u4ef6\u5bf9\u8c61\u5185\u90e8\u7ef4\u62a4\u7740\u7f51\u7edc\u72b6\u6001\u3001\u534f\u8bae\u4fe1\u606f&#xff0c;\u4ee5\u53ca\u7528\u4e8e\u6536\u53d1\u6570\u636e\u7684\u5185\u6838\u7f13\u51b2\u533a\u3002\u540e\u7eed\u6240\u6709\u7f51\u7edc\u64cd\u4f5c&#xff0c;\u672c\u8d28\u4e0a\u90fd\u662f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26 fd&#xff0c;\u6765\u64cd\u4f5c\u8fd9\u4e2a\u5185\u6838\u5bf9\u8c61\u3002<\/p>\n<p><span style=\"color:#fe2c24\">2.\u00a0\u7b2c\u4e8c\u6b65\u586b\u5145\u672c\u5730 sockaddr_in \u7ed3\u6784\u4f53<\/span>&#xff0c;\u6211\u4eec\u624b\u52a8\u5b9a\u4e49\u5e76\u586b\u5145\u672c\u673a\u5730\u5740\u4fe1\u606f&#xff0c;\u5730\u5740\u534f\u8bae\u5bb6\u65cf\u540c\u6837\u8bbe\u4e3a AF_INET&#xff0c;\u548c socket() \u521b\u5efa\u65f6\u4fdd\u6301\u4e00\u81f4&#xff1b;\u586b\u5165\u670d\u52a1\u7aef\u56fa\u5b9a\u7aef\u53e3&#xff0c;\u5e76\u901a\u8fc7 htons() \u8f6c\u6362\u4e3a\u7f51\u7edc\u5927\u7aef\u5b57\u8282\u5e8f&#xff1b;IP \u5730\u5740\u586b\u5199 INADDR_ANY&#xff0c;\u542b\u4e49\u662f\u7ed1\u5b9a\u672c\u673a\u6240\u6709\u7f51\u5361\u3001\u6240\u6709\u53ef\u7528 IP&#xff0c;\u5916\u6765\u8fde\u63a5\u53ea\u8981\u8bbf\u95ee\u5f53\u524d\u7aef\u53e3&#xff0c;\u90fd\u4f1a\u88ab\u63a5\u6536\u3002\u8fd9\u4e00\u6b65\u53ea\u662f\u5728\u7528\u6237\u6001\u4ee3\u7801\u4e2d&#xff0c;\u5199\u597d\u4e00\u4efd \u201c\u672c\u673a\u5730\u5740\u914d\u7f6e\u4fe1\u606f\u201d&#xff0c;\u5b83\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7ed3\u6784\u4f53\u53d8\u91cf&#xff0c;\u5c1a\u672a\u548c\u4efb\u4f55\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u5185\u6838\u5bf9\u8c61\u4ea7\u751f\u5173\u8054\u3002<\/p>\n<p><span style=\"color:#fe2c24\">3.\u00a0\u7b2c\u4e09\u6b65 bind() \u7ed1\u5b9a&#xff0c;<\/span>\u5c06\u7b2c\u4e8c\u6b65\u586b\u5145\u597d\u7684\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u5199\u5165\u5230 _listensockfd \u7d22\u5f15\u5bf9\u5e94\u7684\u5185\u6838 Socket \u6587\u4ef6\u5bf9\u8c61\u4e2d&#xff0c;\u628a\u672c\u673a IP\u3001\u7aef\u53e3\u4fe1\u606f\u8bb0\u5f55\u8fdb\u53bb\u3002\u7ed1\u5b9a\u5b8c\u6210\u540e&#xff0c;\u5185\u6838\u5c31\u660e\u786e\u4e86&#xff1a;\u8fd9\u4e2a _listensockfd \u5bf9\u5e94\u7684\u5957\u63a5\u5b57&#xff0c;\u4e13\u95e8\u8d1f\u8d23\u76d1\u542c\u7ed3\u6784\u4f53 local \u4e2d\u6307\u5b9a\u7684\u300c\u672c\u673a IP &#043; \u7aef\u53e3\u300d\u7684 TCP \u8fde\u63a5\u8bf7\u6c42\u3002\u540e\u7eed\u53ea\u8981\u6709\u5ba2\u6237\u7aef\u8bbf\u95ee\u8be5\u670d\u52a1\u5668\u7684\u7aef\u53e3&#xff0c;\u5185\u6838\u5c31\u4f1a\u628a\u8fde\u63a5\u8bf7\u6c42\u4ea4\u7ed9\u8fd9\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\u5904\u7406\u3002<\/p>\n<h5 id=\"listen()\">listen()<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"224\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230038-6a63ee96bdb59.png\" width=\"843\" \/><\/p>\n<p><span style=\"color:#fe2c24\">4.\u00a0\u7b2c\u56db\u6b65 listen() \u5f00\u542f\u76d1\u542c<\/span>&#xff1a;\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u5f00\u542f\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5fc5\u987b\u662f\u5df2\u7ecf bind() \u7ed1\u5b9a\u597d\u7684\u76d1\u542c\u5957\u63a5\u5b57\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7b49\u5f85\u961f\u5217\u957f\u5ea6&#xff1a;\u5185\u6838\u4f1a\u4e3a\u5f53\u524d\u76d1\u542c\u7aef\u53e3\u7ef4\u62a4\u4e00\u6761\u961f\u5217&#xff0c;\u7528\u4e8e\u5b58\u653e\u672a\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u3001\u6216\u5df2\u5b8c\u6210\u63e1\u624b\u4f46\u5c1a\u672a\u88ab accept() \u53d6\u8d70\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662f\u8fd9\u6761\u961f\u5217\u7684\u6700\u5927\u5bb9\u91cf\u3002listen() \u7684\u4f5c\u7528&#xff0c;\u662f\u628a\u539f\u672c\u666e\u901a\u7684 TCP \u5957\u63a5\u5b57&#xff0c;\u5347\u7ea7\u4e3a\u76d1\u542c\u72b6\u6001&#xff1a;\u544a\u8bc9\u5185\u6838\u5f53\u524d _listensockfd \u4e0d\u518d\u7528\u4e8e\u666e\u901a\u901a\u4fe1&#xff0c;\u4e13\u95e8\u7528\u6765\u88ab\u52a8\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff1b;\u5185\u6838\u4f1a\u5f00\u59cb\u5728\u5e95\u5c42\u7f51\u5361\u5c42\u9762&#xff0c;\u76d1\u542c\u5bf9\u5e94\u7aef\u53e3\u7684 TCP \u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u4e09\u6b21\u63e1\u624b\u8bf7\u6c42\u4f1a\u88ab\u5185\u6838\u81ea\u52a8\u5904\u7406&#xff0c;\u653e\u5165\u7b49\u5f85\u961f\u5217&#xff0c;\u6392\u961f\u7b49\u5f85\u670d\u52a1\u7aef accept() \u53d6\u7528\u3002<\/p>\n<h4 id=\"%E5%90%AF%E5%8A%A8%E4%B8%BB%E5%BE%AA%E7%8E%AF%20Start()%20%E5%87%BD%E6%95%B0\"><span style=\"color:#494949\">\u542f\u52a8\u4e3b\u5faa\u73af Start() \u51fd\u6570<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"554\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230038-6a63ee96d20b8.png\" width=\"1242\" \/>1. \u9996\u5148\u5b9a\u4e49\u51fa\u6765\u7684 client_addr \u662f\u4e00\u4e2a\u7528\u6237\u6001\u7684 sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u7528\u6765\u5b58\u653e\u8fde\u63a5\u8fdb\u6765\u7684\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\u65b9\u4fbf\u6211\u4eec\u5728\u65e5\u5fd7\u91cc\u6253\u5370\u5ba2\u6237\u7aef\u4fe1\u606f\u3002<\/p>\n<h5 id=\"accept()\">accept()<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"228\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230039-6a63ee97177b8.png\" width=\"925\" \/><\/p>\n<p>accept \u7684\u540e\u4e24\u4e2a\u53c2\u6570\u662f\u8f93\u51fa\u578b\u53c2\u6570\u3002\u6211\u4eec\u5df2\u7ecf\u63d0\u524d\u5b9a\u4e49\u4e86\u4e00\u5757\u7a7a\u7684\u5730\u5740\u7ed3\u6784\u4f53\u5185\u5b58&#xff0c;\u65e0\u9700\u624b\u52a8\u586b\u5199\u5ba2\u6237\u7aef\u4fe1\u606f\u3002\u5f53\u5ba2\u6237\u7aef\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u8fde\u63a5\u670d\u52a1\u7aef\u65f6&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u83b7\u53d6\u5ba2\u6237\u7aef\u7684 IP \u4e0e\u7aef\u53e3&#xff0c;\u5e76\u4e3b\u52a8\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5199\u5165\u6211\u4eec\u4f20\u5165\u7684\u7ed3\u6784\u4f53\u4e2d\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u670d\u52a1\u7aef\u81ea\u8eab\u7684\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7528\u4e8e\u7b49\u5f85\u65b0\u8fde\u63a5&#xff1b;\u540e\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u5e26\u51fa\u5bf9\u7aef\u5ba2\u6237\u7aef\u5730\u5740&#xff0c;\u4e24\u8005\u4f5c\u7528\u72ec\u7acb&#xff0c;\u4e92\u4e0d\u51b2\u7a81\u3002accept \u7684\u672c\u8d28\u662f\u4ece\u5185\u6838\u7684\u8fde\u63a5\u7b49\u5f85\u961f\u5217\u91cc&#xff0c;\u53d6\u51fa\u4e00\u6761\u5df2\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u7684\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u4e3a\u5176\u521b\u5efa\u4e13\u5c5e\u7684\u901a\u4fe1\u5957\u63a5\u5b57&#xff0c;\u5e76\u8fd4\u56de\u7ed9\u7528\u6237\u6001\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p>accept() \u6210\u529f\u540e\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5168\u65b0\u7684\u3001\u5ba2\u6237\u7aef\u4e13\u5c5e\u7684\u901a\u4fe1\u5957\u63a5\u5b57 fd&#xff1b;\u56e0\u4e3a TCP \u662f\u9762\u5411\u8fde\u63a5\u7684\u534f\u8bae&#xff0c;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u9700\u8981\u72ec\u7acb\u7684\u901a\u4fe1\u4e0a\u4e0b\u6587&#xff08;\u6536\u53d1\u7f13\u51b2\u533a\u3001\u8fde\u63a5\u72b6\u6001\u3001\u5730\u5740\u4fe1\u606f&#xff09;\u3002\u5185\u6838\u4e3a\u6bcf\u4e2a\u5ba2\u6237\u7aef\u5355\u72ec\u521b\u5efa socket \u7ed3\u6784\u4f53\u548c fd&#xff0c;\u80fd\u4fdd\u8bc1\u4e0d\u540c\u5ba2\u6237\u7aef\u4e4b\u95f4\u7684\u6570\u636e\u5b8c\u5168\u9694\u79bb&#xff0c;\u4e0d\u4f1a\u4e92\u76f8\u5e72\u6270\u3002<\/p>\n<p>2. accept() \u540e\u9762\u7684\u4ee3\u7801 InetAddr clientaddr(client_addr) \u5c31\u662f\u628a accept() \u586b\u597d\u7684 struct sockaddr_in client_addr&#xff0c;\u5305\u88c5\u6210\u6211\u4eec\u81ea\u5df1\u7684 InetAddr \u5bf9\u8c61&#xff1b;\u65b9\u4fbf\u540e\u7eed\u8c03\u7528 ToString() \u65b9\u6cd5&#xff0c;\u6253\u5370\u51fa [IP:\u7aef\u53e3] \u683c\u5f0f\u7684\u65e5\u5fd7\u3002<\/p>\n<p>3. \u4e0b\u4e00\u884c\u7684 serviceIO(sockfd, clientaddr) \u5c31\u662f\u628a accept() \u8fd4\u56de\u7684\u901a\u4fe1\u5957\u63a5\u5b57 sockfd&#xff0c;\u548c\u5305\u88c5\u597d\u7684\u5ba2\u6237\u7aef\u5730\u5740\u5bf9\u8c61&#xff0c;\u4e00\u8d77\u4f20\u7ed9 serviceIO \u51fd\u6570&#xff1b;\u8ba9 serviceIO \u77e5\u9053&#xff1a;\u8fd9\u6b21\u8981\u548c\u54ea\u4e2a\u5ba2\u6237\u7aef(clientaddr) &#xff0c;\u7528\u54ea\u4e2a fd&#xff08;sockfd&#xff09;\u6765\u6536\u53d1\u6570\u636e\u3002<\/p>\n<h4 id=\"%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0%20serviceIO()\"><span style=\"color:#494949\">\u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO()<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"933\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230039-6a63ee972bddc.png\" width=\"1268\" \/><\/p>\n<p>1. serviceIO() \u51fd\u6570\u662f\u670d\u52a1\u7aef\u4e0e\u5355\u4e2a\u5ba2\u6237\u7aef\u901a\u4fe1\u7684\u6838\u5fc3\u903b\u8f91&#xff0c;\u5b83\u63a5\u6536 accept() \u8fd4\u56de\u7684\u5ba2\u6237\u7aef\u901a\u4fe1\u5957\u63a5\u5b57 sockfd \u548c\u5730\u5740\u4fe1\u606f&#xff0c;\u9996\u5148\u6253\u5370\u5ba2\u6237\u7aef\u5730\u5740\u65e5\u5fd7&#xff1b;<\/p>\n<p>2.\u00a0\u5148\u901a\u8fc7 address.ToString() \u548c\u65e5\u5fd7\u6253\u5370\u51fa\u5ba2\u6237\u7aef\u7684 IP \u548c\u7aef\u53e3&#xff0c;\u65b9\u4fbf\u6211\u4eec\u5728\u65e5\u5fd7\u91cc\u533a\u5206\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<p>3. \u4e0b\u9762\u5c31\u8fdb\u5165\u8fd9\u4e2a while(true) \u5faa\u73af&#xff0c;\u8fd9\u662f\u00a0\u201c\u957f\u8fde\u63a5\u201d \u7684\u4f53\u73b0&#xff1a;\u53ea\u8981\u5ba2\u6237\u7aef\u4e0d\u65ad\u5f00&#xff0c;\u670d\u52a1\u7aef\u5c31\u4f1a\u4e00\u76f4\u5faa\u73af&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u53d1\u9001\u65b0\u6570\u636e&#xff0c;\u4e0d\u4f1a\u4e00\u6b21\u8bfb\u5199\u5c31\u9000\u51fa\u3002<\/p>\n<p>4. \u518d\u4e0b\u6765\u5c31\u662f\u5ba2\u6237\u7aef\u901a\u8fc7 read() \u7cfb\u7edf\u8c03\u7528\u6765\u8bfb\u53d6\u6570\u636e\u4e86&#xff0c;read() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u00a0sockfd \u5c31\u662f accept() \u8fd4\u56de\u7684\u3001\u5ba2\u6237\u7aef\u4e13\u5c5e\u7684\u901a\u4fe1\u5957\u63a5\u5b57 fd\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570 inbuffer \u662f\u7528\u6237\u6001\u7684\u7f13\u51b2\u533a&#xff0c;\u7528\u6765\u5b58\u653e\u4ece\u5185\u6838\u7f13\u51b2\u533a\u8bfb\u51fa\u6765\u7684\u6570\u636e\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570 sizeof(inbuffer) &#8211; 1 \u662f\u6700\u591a\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u3002<\/p>\n<p>5. \u5185\u6838\u5728\u5e95\u5c42\u4f1a\u68c0\u67e5 sockfd \u5bf9\u5e94\u7684 socket \u7f51\u7edc\u6587\u4ef6\u7684\u63a5\u6536\u7f13\u51b2\u533a&#xff1b;\u5982\u679c\u7f13\u51b2\u533a\u91cc\u6709\u6570\u636e&#xff0c;\u5c31\u628a\u6570\u636e\u62f7\u8d1d\u5230\u7528\u6237\u6001\u7684 inbuffer \u91cc&#xff0c;\u5e76\u8fd4\u56de\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u8282\u6570 n&#xff1b;\u5982\u679c\u7f13\u51b2\u533a\u4e3a\u7a7a&#xff0c;read() \u4f1a\u963b\u585e\u7b49\u5f85&#xff0c;\u76f4\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u8fc7\u6765&#xff1b;\u5982\u679c\u5ba2\u6237\u7aef\u5df2\u7ecf\u6b63\u5e38\u65ad\u5f00\u8fde\u63a5&#xff0c;read() \u4f1a\u76f4\u63a5\u8fd4\u56de 0&#xff1b;\u5982\u679c\u53d1\u751f\u9519\u8bef&#xff08;\u6bd4\u5982\u5ba2\u6237\u7aef\u5f02\u5e38\u65ad\u5f00\u3001\u7f51\u7edc\u9519\u8bef&#xff09;&#xff0c;read() \u4f1a\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno\u3002<\/p>\n<p>6.\u00a0\u8bfb\u6210\u529f\u540e\u7684\u5904\u7406( n&gt;0)&#xff0c;\u628a\u5ba2\u6237\u7aef\u5730\u5740\u548c\u6536\u5230\u7684\u6d88\u606f\u4e00\u8d77\u6253\u5370&#xff0c;\u65b9\u4fbf\u6211\u4eec\u5728\u65e5\u5fd7\u91cc\u770b\u5230 \u201c\u54ea\u4e2a\u5ba2\u6237\u7aef\u8bf4\u4e86\u4ec0\u4e48\u201d\u3002\u518d\u628a\u6536\u5230\u7684\u6d88\u606f\u524d\u9762\u52a0\u4e0a server echo# \u524d\u7f00&#xff0c;\u6784\u9020\u4e00\u4e2a\u56de\u663e\u5b57\u7b26\u4e32&#xff0c;\u51c6\u5907\u56de\u53d1\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>7. \u8bfb\u5b8c\u4e4b\u540e\u670d\u52a1\u7aef\u5c31\u53ef\u4ee5\u5411\u5ba2\u6237\u7aef\u901a\u8fc7 write() \u7cfb\u7edf\u8c03\u7528\u5199\u4e86&#xff0c;write() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570 sockfd \u540c\u6837\u662f\u5ba2\u6237\u7aef\u4e13\u5c5e\u7684\u901a\u4fe1\u5957\u63a5\u5b57 fd&#xff1b;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5199\u7684\u5185\u5bb9&#xff0c;\u6211\u4eec\u5c06\u5b83\u8f6c\u6362\u4e3a C \u98ce\u683c\u7684\u5b57\u7b26\u5f62\u5f0f&#xff1b; \u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8981\u53d1\u9001\u7684\u5b57\u8282\u6570\u3002<\/p>\n<p>8. write() \u65f6\u5e95\u5c42\u4f1a\u628a\u7528\u6237\u6001\u7684\u5b57\u7b26\u4e32\u6570\u636e&#xff0c;\u62f7\u8d1d\u5230 sockfd \u5bf9\u5e94\u7684\u5185\u6838 socket \u7f51\u7edc\u6587\u4ef6\u7684\u53d1\u9001\u7f13\u51b2\u533a\u91cc&#xff1b;\u5185\u6838\u7684 TCP \u534f\u8bae\u6808\u4f1a\u81ea\u52a8\u628a\u53d1\u9001\u7f13\u51b2\u533a\u7684\u6570\u636e\u5c01\u88c5\u6210 TCP \u62a5\u6587&#xff0c;\u901a\u8fc7\u7f51\u7edc\u53d1\u7ed9\u5ba2\u6237\u7aef&#xff1b;\u5982\u679c\u53d1\u9001\u7f13\u51b2\u533a\u6ee1\u4e86&#xff0c;write() \u4f1a\u963b\u585e&#xff0c;\u76f4\u5230\u7f13\u51b2\u533a\u6709\u7a7a\u95f4\u5199\u5165\u3002<\/p>\n<p>9. \u518d\u540e\u9762 n &#061;&#061; 0 \u4ee3\u8868\u5ba2\u6237\u7aef\u5df2\u7ecf\u6b63\u5e38\u5173\u95ed\u4e86\u8fde\u63a5&#xff0c;\u6b64\u65f6\u670d\u52a1\u7aef\u4e5f\u5e94\u8be5\u9000\u51fa\u5faa\u73af&#xff0c;\u540e\u7eed\u5173\u95ed sockfd&#xff0c;\u91ca\u653e\u8d44\u6e90&#xff1b;n &lt; 0 \u4ee3\u8868\u8bfb\u53d6\u51fa\u9519&#xff08;\u6bd4\u5982\u5ba2\u6237\u7aef\u5f02\u5e38\u6389\u7ebf\u3001\u7f51\u7edc\u9519\u8bef&#xff09;&#xff0c;\u6b64\u65f6\u4e5f\u8981\u9000\u51fa\u5faa\u73af&#xff0c;\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<h3 id=\"EchoTcpServerMain.cc%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%3A\">EchoTcpServerMain.cc \u670d\u52a1\u7aef:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"732\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230039-6a63ee9788147.png\" width=\"1209\" \/>\u4e3b\u6587\u4ef6\u662f TCP \u56de\u663e\u670d\u52a1\u5668\u7684\u5165\u53e3&#xff0c;\u9996\u5148\u6821\u9a8c\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u8981\u6c42\u8fd8\u8981\u7528\u6237\u4f20\u5165\u7aef\u53e3\u53f7&#xff1b;\u968f\u540e\u5f00\u542f\u63a7\u5236\u53f0\u65e5\u5fd7\u7cfb\u7edf&#xff0c;\u5c06\u7aef\u53e3\u53f7\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570&#xff0c;\u7528\u4e8e\u521b\u5efa\u670d\u52a1\u5668\u5b9e\u4f8b&#xff1b;\u901a\u8fc7\u667a\u80fd\u6307\u9488\u7ba1\u7406 TcpServer \u5bf9\u8c61&#xff0c;\u8c03\u7528 InitServer() \u5b8c\u6210\u5957\u63a5\u5b57\u521b\u5efa\u3001\u7ed1\u5b9a\u4e0e\u76d1\u542c\u7684\u521d\u59cb\u5316\u5de5\u4f5c&#xff0c;\u518d\u8c03\u7528 Start() \u542f\u52a8\u670d\u52a1&#xff0c;\u8fdb\u5165\u76d1\u542c\u5faa\u73af&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u5e76\u5904\u7406\u901a\u4fe1\u3002<\/p>\n<h3 id=\"EchoTcpClient.cc%20%E5%AE%A2%E6%88%B7%E7%AB%AF%3A\">EchoTcpClient.cc \u5ba2\u6237\u7aef:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2240\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230039-6a63ee97d1feb.png\" width=\"1478\" \/><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u4e3b\u51fd\u6570\u4ee3\u7801&#xff0c;\u5b83\u7684\u6838\u5fc3\u6d41\u7a0b\u662f&#xff1a;<\/p>\n<li>\u63a5\u6536\u547d\u4ee4\u884c\u4f20\u5165\u7684\u670d\u52a1\u7aef IP \u548c\u7aef\u53e3\u53f7<\/li>\n<li>\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff08;socket&#xff09;<\/li>\n<li>\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42&#xff08;connect&#xff09;<\/li>\n<li>\u4e0e\u670d\u52a1\u7aef\u8fdb\u884c\u4ea4\u4e92&#xff1a;\u7528\u6237\u8f93\u5165\u6d88\u606f \u2192 \u53d1\u9001\u7ed9\u670d\u52a1\u7aef \u2192 \u63a5\u6536\u56de\u663e\u5e76\u6253\u5370<\/li>\n<li>\u5904\u7406\u8fde\u63a5\u65ad\u5f00\u4e0e\u9519\u8bef<\/li>\n<p>1. \u7b2c\u4e00\u6b65\u662f\u547d\u4ee4\u884c\u53c2\u6570\u6821\u9a8c\u4e0e\u89e3\u6790&#xff0c;\u5ba2\u6237\u7aef\u8fd0\u884c\u65f6\u5fc5\u987b\u4f20\u5165\u4e24\u4e2a\u53c2\u6570&#xff1a;\u670d\u52a1\u7aef IP\u548c\u670d\u52a1\u7aef\u7aef\u53e3\u53f7\u3002\u5f53\u53c2\u6570\u4e2a\u6570\u4e0d\u5bf9\u65f6&#xff0c;\u6253\u5370\u7528\u6cd5\u63d0\u793a\u3002\u628a\u547d\u4ee4\u884c\u4f20\u5165\u7684 IP \u5b57\u7b26\u4e32\u5b58\u5165 server_ip&#xff0c;\u7aef\u53e3\u53f7\u5b57\u7b26\u4e32\u8f6c\u6210\u6574\u6570\u5b58\u5165 server_port&#xff0c;\u4e3a\u540e\u7eed\u8fde\u63a5\u505a\u51c6\u5907\u3002<\/p>\n<p>2. \u7b2c\u4e8c\u6b65\u5c31\u662f\u5ba2\u6237\u7aef\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff08;socket&#xff09;&#xff0c;\u8fd9\u4e00\u6b65\u548c\u670d\u52a1\u7aef\u521b\u5efa\u00a0socket \u4e00\u6837&#xff0c;\u521b\u5efa\u00a0socket() \u65f6&#xff0c;\u5185\u6838\u4f1a\u4e3a\u5ba2\u6237\u7aef\u521b\u5efa\u4e00\u4e2a\u4e13\u5c5e\u7684\u7f51\u7edc\u6587\u4ef6\u5bf9\u8c61&#xff0c;\u5305\u542b\u6536\u53d1\u7f13\u51b2\u533a\u3001\u534f\u8bae\u72b6\u6001\u7b49\u4fe1\u606f&#xff0c;\u5e76\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26 sockfd \u7ed9\u7528\u6237\u6001\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u624b\u52a8 bind() \u7ed1\u5b9a\u7aef\u53e3&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u5ba2\u6237\u7aef\u5206\u914d\u4e00\u4e2a\u4e34\u65f6\u7aef\u53e3&#xff0c;\u907f\u514d\u7aef\u53e3\u51b2\u7a81\u3002<\/p>\n<p>3. \u7b2c\u4e09\u90e8\u6784\u9020\u670d\u52a1\u7aef\u5730\u5740\u4fe1\u606f&#xff0c;InetAddr serveraddress(server_port, server_ip) \u5c31\u662f\u628a\u4f20\u5165\u7684\u670d\u52a1\u7aef IP \u548c\u7aef\u53e3&#xff0c;\u5c01\u88c5\u6210\u6211\u4eec\u81ea\u5df1\u7684 InetAddr \u5bf9\u8c61&#xff0c;\u5e95\u5c42\u662f struct sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u7528\u6765\u4f20\u9012\u7ed9 connect \u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<h4 id=\"connect()\">connect()<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"248\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee993f9e9.png\" width=\"779\" \/> connect() \u7684\u4f5c\u7528\u5c31\u662f\u5411\u6307\u5b9a\u7684\u670d\u52a1\u7aef\u53d1\u8d77 TCP \u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5ba2\u6237\u7aef\u81ea\u5df1\u7684 socket fd&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u670d\u52a1\u7aef\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u5730\u5740\u7ed3\u6784\u4f53\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p>connect \u5c31\u662f\u5728\u544a\u8bc9\u5185\u6838\u4e09\u4ef6\u4e8b : \u6211\u5ba2\u6237\u7aef\u8981\u7528 sockfd \u8fd9\u4e2a\u5957\u63a5\u5b57&#xff0c;\u53bb\u8fde\u63a5 serveraddress \u91cc\u8fd9\u4e2a\u670d\u52a1\u7aef\u5730\u5740&#xff0c;\u800c\u8fd9\u4e2a\u5730\u5740\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u662f addrlen\u3002<\/p>\n<p>4. \u7b2c\u56db\u90e8\u5c31\u662f\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u7aef\u7684\u901a\u4fe1\u73af\u8282&#xff0c;\u8fd9\u90e8\u5206\u662f\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u4ea4\u4e92\u903b\u8f91&#xff0c;\u548c\u670d\u52a1\u7aef\u7684 serviceIO \u51fd\u6570\u662f\u53cc\u5411\u5bf9\u5e94\u7684&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"205\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee995584d.png\" width=\"1296\" \/>\u4ece\u7ec8\u7aef\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e00\u884c\u6d88\u606f&#xff0c;\u5b58\u5165 line \u5b57\u7b26\u4e32&#xff1b;write() \u628a\u7528\u6237\u8f93\u5165\u7684\u6d88\u606f\u5199\u5165\u5ba2\u6237\u7aef socket \u7684\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u5185\u6838\u81ea\u52a8\u901a\u8fc7 TCP \u534f\u8bae\u53d1\u9001\u7ed9\u670d\u52a1\u7aef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"197\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee9970585.png\" width=\"950\" \/> read() \u4ece\u5ba2\u6237\u7aef socket \u7684\u63a5\u6536\u7f13\u51b2\u533a\u8bfb\u53d6\u670d\u52a1\u7aef\u53d1\u56de\u6765\u7684\u56de\u663e\u6570\u636e&#xff1b; n &gt; 0 \u8868\u793a\u8bfb\u53d6\u6210\u529f&#xff0c;\u628a\u6570\u636e\u62f7\u8d1d\u5230 inbuffer&#xff0c;\u624b\u52a8\u52a0 \\\\0 \u8f6c\u6210\u5b57\u7b26\u4e32&#xff0c;\u6253\u5370\u5230\u7ec8\u7aef&#xff1b; n &#061;&#061; 0 \u8868\u793a\u670d\u52a1\u7aef\u6b63\u5e38\u65ad\u5f00\u8fde\u63a5&#xff0c;\u9000\u51fa\u5faa\u73af&#xff1b; n &lt; 0 \u8868\u793a\u8bfb\u53d6\u9519\u8bef&#xff08;\u6bd4\u5982\u7f51\u7edc\u5f02\u5e38&#xff09;&#xff0c;\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p>\u81f3\u6b64\u6574\u4e2a\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u7684\u4ee3\u7801\u903b\u8f91\u5c31\u8bb2\u5b8c\u4e86\u3002\u4e0b\u9762\u6211\u4eec\u770b\u8fd0\u884c\u7ed3\u679c:<\/p>\n<h3 id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A\">\u8fd0\u884c\u7ed3\u679c:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"192\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee998397e.png\" width=\"1238\" \/>\u670d\u52a1\u7aef\u542f\u52a8\u521d\u59cb\u5316\u6210\u529f&#xff0c;\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u548c\u76d1\u542c\u90fd\u5df2\u6210\u529f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"152\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee999f7cc.png\" width=\"1071\" \/>\u5ba2\u6237\u7aef\u542f\u52a8\u5e76\u6210\u529f\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u5ba2\u6237\u7aef connect() \u8c03\u7528\u6210\u529f&#xff0c;TCP \u8fde\u63a5\u5efa\u7acb\u3002please Enter#&#xff1a;\u5ba2\u6237\u7aef\u8fdb\u5165\u7b49\u5f85\u7528\u6237\u8f93\u5165\u7684\u72b6\u6001&#xff0c;\u51c6\u5907\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"266\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee99b7a2e.png\" width=\"1257\" \/>\u670d\u52a1\u7aef accept \u6210\u529f&#xff0c;\u62ff\u5230\u5ba2\u6237\u7aef\u8fde\u63a5\u3002accept() \u8c03\u7528\u6210\u529f\u540e &#xff0c;\u5185\u6838\u4e3a\u8fd9\u4e2a\u5ba2\u6237\u7aef\u521b\u5efa\u4e86\u4e13\u5c5e\u7684\u901a\u4fe1\u5957\u63a5\u5b57&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a 4\u3002\u670d\u52a1\u7aef\u540c\u65f6\u4e5f\u62ff\u5230\u4e86\u5ba2\u6237\u7aef\u7684 IP \u548c\u4e34\u65f6\u7aef\u53e3\u53f7&#xff0c;\u8bc1\u660e\u5ba2\u6237\u7aef\u8fde\u63a5\u5df2\u7ecf\u88ab\u670d\u52a1\u7aef\u63a5\u6536\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"345\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230041-6a63ee99db3d7.png\" width=\"2184\" \/>\u5ba2\u6237\u7aef\u8f93\u5165\u4e2d\u5348\u597d&#xff0c;\u53d1\u9001\u7ed9\u670d\u52a1\u7aef&#xff1b;\u6536\u5230\u670d\u52a1\u7aef\u56de\u663e server echo# \u4e2d\u5348\u597d\u3002\u8bc1\u660e\u6210\u529f\u6536\u5230\u4e86\u5ba2\u6237\u7aef\u7684\u6d88\u606f&#xff1b;\u5e76\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u4e86\u56de\u663e\u6570\u636e\u3002\u6b64\u65f6\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5b8c\u6210\u4e86\u53cc\u5411\u6570\u636e\u6536\u53d1&#xff0c;\u56de\u663e\u529f\u80fd\u6b63\u5e38&#xff0c;\u957f\u8fde\u63a5\u901a\u4fe1\u6d41\u7a0b\u8dd1\u901a\u3002<\/p>\n<h3 id=\"%E6%80%BB%E7%BB%93%3A\">\u603b\u7ed3:<\/h3>\n<p>\u5728\u6211\u4eec\u7684\u7b2c\u4e00\u9636\u6bb5\u5b9e\u73b0\u4e2d&#xff0c;\u5355\u8fdb\u7a0b\u548c\u5355\u7ebf\u7a0b\u662f\u7b49\u4ef7\u7684\u6982\u5ff5&#xff1a;\u6211\u4eec\u670d\u52a1\u7aef\u7684\u7a0b\u5e8f\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b&#xff0c;\u8fd9\u4e2a\u8fdb\u7a0b\u91cc\u4e5f\u53ea\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b&#xff0c;\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u8fd9\u4e2a\u670d\u52a1\u7aef\u6a21\u578b\u662f\u4e32\u884c\u6267\u884c\u7684&#xff1a;\u670d\u52a1\u7aef\u7684\u4e3b\u7ebf\u7a0b\u5fc5\u987b\u5b8c\u6574\u5904\u7406\u5b8c\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u6240\u6709\u6536\u53d1\u903b\u8f91(\u76f4\u5230\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#xff09;&#xff0c;\u624d\u80fd\u56de\u5230 accept() \u8c03\u7528&#xff0c;\u53bb\u63a5\u6536\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<p>\u8fd9\u5c31\u5bfc\u81f4\u4e86\u4e00\u4e2a\u76f4\u63a5\u7684\u7ed3\u679c&#xff1a;\u8fd9\u4e2a\u670d\u52a1\u7aef\u6ca1\u6709\u4efb\u4f55\u5e76\u53d1\u80fd\u529b\u3002\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u670d\u52a1\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u540e\u7eed\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u4f1a\u6392\u961f\u7b49\u5f85&#xff0c;\u751a\u81f3\u53ef\u80fd\u56e0\u4e3a\u7b49\u5f85\u8d85\u65f6\u800c\u5931\u8d25\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5c06\u8fdb\u5165\u7b2c\u4e8c\u9636\u6bb5\u7684\u6539\u9020&#xff1a;\u5f15\u5165\u591a\u8fdb\u7a0b\u6a21\u578b&#xff0c;\u4e3a\u6bcf\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u521b\u5efa\u72ec\u7acb\u7684\u5b50\u8fdb\u7a0b\u5904\u7406\u901a\u4fe1&#xff0c;\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u3002<\/p>\n<h2 id=\"%E5%9B%9B%E3%80%81%E9%98%B6%E6%AE%B5%E4%BA%8C%C2%A0%3A%20%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%89%88%E6%9C%AC\">\u56db\u3001\u9636\u6bb5\u4e8c\u00a0: \u591a\u8fdb\u7a0b\u7248\u672c<\/h2>\n<h3 id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A\">\u903b\u8f91\u68b3\u7406:<\/h3>\n<p>\u5728\u591a\u8fdb\u7a0b\u6539\u9020\u4e2d&#xff0c;\u6211\u4eec\u91c7\u7528<span style=\"color:#fe2c24\">\u4e3b\u8fdb\u7a0b\u8d1f\u8d23\u8fde\u63a5\u3001\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u901a\u4fe1<\/span>\u7684\u5206\u5de5\u6a21\u578b&#xff1a;\u670d\u52a1\u7aef\u4e3b\u8fdb\u7a0b\u7684\u804c\u8d23\u4ec5\u4fdd\u7559\u4e3a accept() \u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42&#xff1b;\u6bcf\u5f53\u6210\u529f\u5efa\u7acb\u4e00\u4e2a\u65b0\u8fde\u63a5&#xff0c;\u4e3b\u8fdb\u7a0b\u5c31\u901a\u8fc7 fork() \u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;\u5c06\u8be5\u5ba2\u6237\u7aef\u7684\u901a\u4fe1\u5957\u63a5\u5b57\u4ea4\u7ed9\u5b50\u8fdb\u7a0b\u5904\u7406&#xff0c;\u7531\u5b50\u8fdb\u7a0b\u6267\u884c serviceIO() \u51fd\u6570&#xff0c;\u5b8c\u6210\u4e0e\u5ba2\u6237\u7aef\u7684\u8bfb\u5199\u4ea4\u4e92\u4e0e\u56de\u663e\u3002\u4e3b\u8fdb\u7a0b\u5219\u7acb\u523b\u56de\u5230\u5faa\u73af&#xff0c;\u7ee7\u7eed\u76d1\u542c\u548c\u63a5\u6536\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u8fd9\u79cd\u5206\u5de5\u8ba9\u670d\u52a1\u7aef\u80fd\u591f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff0c;\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u80fd\u529b\u3002<\/p>\n<h3 id=\"%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98%3A\">\u5f15\u53d1\u7684\u95ee\u9898:<\/h3>\n<p>\u5f15\u5165\u591a\u8fdb\u7a0b\u6a21\u578b\u540e&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86 \u201c\u4e3b\u8fdb\u7a0b\u4e13\u5fc3 accept\u3001\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u901a\u4fe1\u201d \u7684\u5e76\u53d1\u5206\u5de5&#xff0c;\u4f46\u4e5f\u968f\u4e4b\u5e26\u6765\u4e86\u4e00\u4e2a\u65b0\u7684\u95ee\u9898&#xff1a;\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u4e3b\u52a8\u8c03\u7528 wait()\/waitpid() \u56de\u6536\u5b83\u7684\u9000\u51fa\u72b6\u6001&#xff0c;\u5b50\u8fdb\u7a0b\u5c31\u4f1a\u53d8\u6210 \u201c\u50f5\u5c38\u8fdb\u7a0b\u201d&#xff0c;\u957f\u671f\u5360\u7528\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<p>\u800c\u5982\u679c\u8ba9\u4e3b\u8fdb\u7a0b\u5728 accept() \u7684\u540c\u65f6&#xff0c;\u518d\u53bb\u963b\u585e\u7b49\u5f85\u5e76\u5904\u7406\u5b50\u8fdb\u7a0b\u56de\u6536&#xff0c;\u5c31\u4f1a\u7834\u574f \u201c\u4e3b\u8fdb\u7a0b\u53ea\u8d1f\u8d23\u63a5\u6536\u8fde\u63a5\u201d \u7684\u8bbe\u8ba1\u521d\u8877&#xff0c;\u8ba9\u4e3b\u8fdb\u7a0b\u518d\u6b21\u9677\u5165 \u201c\u65e2\u8981 accept \u53c8\u8981\u56de\u6536\u5b50\u8fdb\u7a0b\u201d \u7684\u53cc\u91cd\u804c\u8d23\u4e2d&#xff0c;\u5bfc\u81f4\u4e3b\u8fdb\u7a0b\u963b\u585e\u5728\u56de\u6536\u64cd\u4f5c\u4e0a&#xff0c;\u65e0\u6cd5\u53ca\u65f6\u54cd\u5e94\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5931\u53bb\u4e86\u591a\u8fdb\u7a0b\u5e76\u53d1\u7684\u610f\u4e49\u3002<\/p>\n<h3 id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%3A\">\u89e3\u51b3\u65b9\u6cd5:<\/h3>\n<p>\u90a3\u89e3\u51b3\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462? \u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5&#xff0c;\u6211\u4eec\u9010\u4e2a\u6765\u770b :<\/p>\n<h5 id=\"%E6%96%B9%E6%B3%95%E4%B8%80%20%3A%20%E5%BC%95%E5%85%A5%E5%AD%99%E5%AD%90%E8%BF%9B%E7%A8%8B\">\u65b9\u6cd5\u4e00 : \u5f15\u5165\u5b59\u5b50\u8fdb\u7a0b<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1329\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230042-6a63ee9a3f64a.png\" width=\"1536\" \/><\/p>\n<p>1. \u4e3b\u8fdb\u7a0b\u901a\u8fc7\u7b2c\u4e00\u6b21 fork() \u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u968f\u540e\u5728\u5b50\u8fdb\u7a0b\u5185\u90e8\u6267\u884c\u7b2c\u4e8c\u6b21 fork() \u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b&#xff0c;\u6b64\u65f6\u5b50\u8fdb\u7a0b\u76f4\u63a5\u8c03\u7528 exit(0) \u9000\u51fa\u3002\u6b64\u65f6&#xff0c;\u4e3b\u8fdb\u7a0b\u53ea\u9700\u8c03\u7528 waitpid() \u56de\u6536\u8fd9\u4e2a\u77ac\u95f4\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u800c\u7531\u4e8e\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa&#xff0c;waitpid()\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u963b\u585e&#xff0c;\u4e3b\u8fdb\u7a0b\u53ef\u4ee5\u7acb\u5373\u56de\u5230 accept() \u5faa\u73af&#xff0c;\u7ee7\u7eed\u63a5\u6536\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u540c\u65f6&#xff0c;\u88ab\u521b\u5efa\u51fa\u6765\u7684\u5b59\u5b50\u8fdb\u7a0b\u56e0\u4e3a\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa\u4e86&#xff0c;\u6240\u4ee5\u5c31\u4f1a\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u7531\u7cfb\u7edf init \u8fdb\u7a0b\u63a5\u7ba1&#xff0c;\u5b59\u5b50\u8fdb\u7a0b\u5c31\u5c06\u6267\u884c\u4e0e\u5ba2\u6237\u7aef\u7684 serviceIO \u901a\u4fe1\u903b\u8f91&#xff0c;\u901a\u4fe1\u7ed3\u675f\u540e\u9000\u51fa\u65f6\u4f1a\u88ab\u7cfb\u7edf\u81ea\u52a8\u56de\u6536&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4e5f\u65e0\u9700\u4e3b\u8fdb\u7a0b\u989d\u5916\u5904\u7406\u3002<\/p>\n<p>2. \u5728\u4e24\u6b21 fork \u7684\u5e76\u53d1\u6a21\u578b\u4e2d&#xff0c;\u4e09\u4e2a\u8fdb\u7a0b\u5404\u53f8\u5176\u804c&#xff0c;\u804c\u8d23\u5b8c\u5168\u5206\u79bb&#xff0c;\u4e0d\u5b58\u5728\u4efb\u4f55\u51b2\u7a81&#xff1a;<\/p>\n<p>\u4e3b\u8fdb\u7a0b&#xff1a;\u4ec5\u8d1f\u8d23\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u968f\u540e\u7acb\u5373\u8c03\u7528 waitpid() \u56de\u6536\u5b83&#xff1b;\u56de\u6536\u5b8c\u6210\u540e\u7acb\u523b\u56de\u5230\u5faa\u73af&#xff0c;\u7ee7\u7eed\u6267\u884c accept() \u63a5\u6536\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5168\u7a0b\u4e0d\u963b\u585e\u3001\u4e0d\u53c2\u4e0e\u4efb\u4f55\u4e1a\u52a1\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p>\u5b50\u8fdb\u7a0b&#xff1a;\u4ec5\u8d1f\u8d23\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b&#xff0c;\u521b\u5efa\u5b8c\u6210\u540e\u7acb\u523b\u8c03\u7528 exit(0) \u9000\u51fa&#xff0c;\u6210\u4e3a\u4e00\u4e2a\u4e00\u6b21\u6027\u7684 \u201c\u8fc7\u6e21\u8fdb\u7a0b\u201d&#xff0c;\u7531\u4e3b\u8fdb\u7a0b\u5feb\u901f\u56de\u6536&#xff0c;\u4e0d\u5360\u7528\u4efb\u4f55\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p>\u5b59\u5b50\u8fdb\u7a0b&#xff1a;\u88ab\u521b\u5efa\u540e\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u7531\u7cfb\u7edfinit\u8fdb\u7a0b\u63a5\u7ba1&#xff1b;\u5b83\u5c06\u6267\u884c\u4e0e\u5ba2\u6237\u7aef\u7684 serviceIO \u901a\u4fe1\u903b\u8f91&#xff0c;\u901a\u4fe1\u7ed3\u675f\u540e\u6b63\u5e38\u9000\u51fa&#xff0c;\u9000\u51fa\u72b6\u6001\u7531\u7cfb\u7edf\u81ea\u52a8\u56de\u6536&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4e5f\u65e0\u9700\u4e3b\u8fdb\u7a0b\u989d\u5916\u5904\u7406\u3002<\/p>\n<p>3. \u4ed4\u7ec6\u601d\u8003&#xff0c;\u6b64\u65f6\u4ecd\u7136\u5b58\u5728\u9690\u60a3\u95ee\u9898&#xff0c;<span style=\"color:#fe2c24\">\u5728 fork() \u521b\u5efa\u8fdb\u7a0b\u65f6&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u5b8c\u6574\u590d\u5236\u7236\u8fdb\u7a0b\u7684 task_struct \u8fdb\u7a0b\u7ed3\u6784\u4f53&#xff0c;\u5305\u62ec\u5b83\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868<\/span>\u3002\u56e0\u6b64&#xff0c;\u5f53\u4e3b\u8fdb\u7a0b accept() \u62ff\u5230\u5ba2\u6237\u7aef\u8fde\u63a5&#xff08;\u540c\u65f6\u6301\u6709\u76d1\u542c\u5957\u63a5\u5b57 _listensockfd \u548c\u901a\u4fe1\u5957\u63a5\u5b57\u00a0sockfd \u540e&#xff0c;fork() \u51fa\u6765\u7684\u5b50\u8fdb\u7a0b\u3001\u518d fork() \u51fa\u6765\u7684\u5b59\u5b50\u8fdb\u7a0b&#xff0c;\u5b83\u4eec\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u4e2d&#xff0c;\u90fd\u4f1a\u540c\u65f6\u5b58\u5728\u8fd9\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>4. \u4f46\u4e09\u4e2a\u8fdb\u7a0b\u7684\u804c\u8d23\u5b8c\u5168\u4e0d\u540c&#xff0c;<span style=\"color:#ff9900\">\u56e0\u6b64\u5fc5\u987b\u9488\u5bf9\u6027\u5730\u5173\u95ed\u81ea\u5df1\u4e0d\u9700\u8981\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u6587\u4ef6\u63cf\u8ff0\u7b26\u6cc4\u6f0f\u548c\u8fde\u63a5\u65e0\u6cd5\u6b63\u5e38\u5173\u95ed\u7684\u95ee\u9898&#xff1a;<\/span><\/p>\n<p>\u4e3b\u8fdb\u7a0b&#xff1a;\u5b83\u53ea\u8d1f\u8d23 accept \u65b0\u8fde\u63a5&#xff0c;\u4e0d\u9700\u8981\u548c\u5ba2\u6237\u7aef\u901a\u4fe1&#xff0c;\u56e0\u6b64\u5b83\u5fc5\u987b\u5173\u95ed\u81ea\u5df1\u7684\u901a\u4fe1\u5957\u63a5\u5b57 sockfd\u3002<\/p>\n<\/p>\n<p>\u5b59\u5b50\u8fdb\u7a0b&#xff1a;\u5b83\u53ea\u8d1f\u8d23\u548c\u5ba2\u6237\u7aef\u8fdb\u884c serviceIO \u901a\u4fe1&#xff0c;\u4e0d\u518d\u9700\u8981\u76d1\u542c\u65b0\u8fde\u63a5&#xff0c;\u56e0\u6b64\u5b83\u5fc5\u987b\u5173\u95ed\u81ea\u5df1\u7684\u76d1\u542c\u5957\u63a5\u5b57 _listensockfd\u3002<\/p>\n<\/p>\n<p>\u5b50\u8fdb\u7a0b&#xff1a;\u5b83\u53ea\u662f\u4e00\u4e2a \u201c\u8fc7\u6e21\u8fdb\u7a0b\u201d&#xff0c;\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b\u540e\u4f1a\u7acb\u523b exit() \u9000\u51fa&#xff0c;\u8fdb\u7a0b\u9000\u51fa\u65f6\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5173\u95ed\u5b83\u6301\u6709\u7684\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u56e0\u6b64\u5b83\u4e0d\u9700\u8981\u624b\u52a8\u5173\u95ed\u4efb\u4f55 fd\u3002<\/p>\n<p>5.\u00a0\u901a\u8fc7\u8fd9\u6837\u7684\u5206\u5de5\u548c\u5173\u95ed\u64cd\u4f5c&#xff0c;\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u53ea\u4fdd\u7559\u81ea\u5df1\u4e1a\u52a1\u6240\u9700\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u65e2\u907f\u514d\u4e86\u8d44\u6e90\u6cc4\u6f0f&#xff0c;\u4e5f\u4fdd\u8bc1\u4e86 TCP \u8fde\u63a5\u7684\u6b63\u5e38\u751f\u547d\u5468\u671f\u7ba1\u7406\u3002<\/p>\n<h5 id=\"%E6%96%B9%E6%B3%95%E4%BA%8C%20%3A%20%E5%BC%95%E5%85%A5%E4%BF%A1%E5%8F%B7\">\u65b9\u6cd5\u4e8c : \u5f15\u5165\u4fe1\u53f7<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"93\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230042-6a63ee9aab76c.png\" width=\"1028\" \/>\u8fd9\u4e2a\u4fe1\u53f7\u5982\u4e0a&#xff0c;\u5f53\u7236\u8fdb\u7a0b\u5728 fork() \u4e4b\u524d\u6267\u884c\u8fd9\u4e2a\u4fe1\u53f7\u540e\u5c31\u4ee3\u8868\u7740\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u6240\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u7531\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u81ea\u52a8\u56de\u6536&#xff0c;\u7236\u8fdb\u7a0b\u6b64\u65f6\u4e0d\u4f1a\u7ba1\u7406\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u6536\u5230\u8fd9\u4e2a\u6307\u4ee4\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6\u5c31\u4f1a\u88ab\u7cfb\u7edf\u81ea\u52a8\u6e05\u7406&#xff0c;\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u7236\u8fdb\u7a0b\u5c31\u4e0d\u7528\u5199 wait() \/ waitpid()&#xff0c;\u4e5f\u4e0d\u7528\u64cd\u5fc3\u4efb\u4f55\u56de\u6536\u3002<\/p>\n<p>\u539f\u7406\u5c31\u662f\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4f1a\u7ed9\u7236\u8fdb\u7a0b\u53d1\u4e00\u4e2a\u4fe1\u53f7&#xff1a;SIGCHLD\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u7236\u8fdb\u7a0b\u5fc5\u987b\u8c03\u7528 wait\/waitpid \u53bb\u56de\u6536&#xff0c;\u5426\u5219\u5b50\u8fdb\u7a0b\u53d8\u50f5\u5c38\u3002\u4f46\u662f\u5f53\u6211\u4eec\u6267\u884c\u4e86\u8fd9\u4e2a signal(SIGCHLD, SIG_IGN); \u4ee3\u7801\u540e&#xff0c;\u5c31\u610f\u5473\u7740\u7236\u8fdb\u7a0b&#xff0c;\u9009\u62e9\u3010\u5ffd\u7565\u3011\u6240\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u53f7\u3002\u8fd9\u4e2a\u65f6\u5019\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e0a\u8ff0\u7684\u6240\u6709\u95ee\u9898&#xff0c;\u6b64\u65f6\u7236\u5b50\u8fdb\u7a0b\u5c31\u53ef\u4ee5\u5404\u505a\u5404\u7684\u4e8b\u3002<\/p>\n<p>\u6240\u4ee5\u65b9\u6848\u4e8c\u662f\u6bd4\u65b9\u6848\u4e00\u66f4\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4f46\u662f\u8fd9\u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5\u90fd\u53ef\u4ee5\u3002\u8fd9\u91cc\u6211\u4eec\u5c31\u91c7\u53d6\u7b2c\u4e00\u79cd\u65b9\u6cd5\u4e86\u3002<\/p>\n<h3 id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A\">\u8fd0\u884c\u7ed3\u679c:<\/h3>\n<p>\u6b64\u65f6\u6211\u4eec\u5c31\u5c06\u670d\u52a1\u7aef\u6210\u529f\u7684\u6539\u9020\u4e3a\u591a\u8fdb\u7a0b\u7684\u5e76\u53d1\u6a21\u578b\u3001\u6211\u4eec\u53ea\u5728\u670d\u52a1\u7aef accept \u6210\u529f\u4e4b\u540e&#xff0c;\u65b0\u589e\u4e24\u5c42 fork \u7684\u76f8\u5173\u903b\u8f91&#xff0c;\u5176\u4f59\u7684\u6240\u6709\u4ee3\u7801\u90fd\u4e0d\u7528\u6539\u52a8\u3002\u4e0b\u9762\u6211\u4eec\u770b\u8fd0\u884c\u7ed3\u679c :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1055\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230042-6a63ee9abc10a.png\" width=\"2433\" \/> \u521d\u59cb\u5316\u9636\u6bb5\u65e5\u5fd7\u663e\u793a\u670d\u52a1\u7aef\u6210\u529f\u5b8c\u6210 socket \u521b\u5efa\u3001bind \u7ed1\u5b9a\u3001listen \u76d1\u542c\u4e09\u6b65\u521d\u59cb\u5316&#xff0c;\u7aef\u53e3 8080 \u5904\u4e8e\u6b63\u5e38\u76d1\u542c\u72b6\u6001\u3002\u4e09\u6b21\u8fde\u63a5\u5efa\u7acb\u65e5\u5fd7\u91cc\u51fa\u73b0\u4e86\u4e09\u6b21 accept success, sockfd: 4&#xff0c;\u8bf4\u660e\u6709\u4e09\u4e2a\u5ba2\u6237\u7aef\u6210\u529f\u7684\u8fde\u63a5\u4e0a\u4e86\u670d\u52a1\u7aef&#xff0c;\u4e3b\u8fdb\u7a0b\u4e09\u6b21\u6210\u529f\u63a5\u6536\u4e86\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e09\u6b21 accept \u90fd\u8fd4\u56de sockfd: 4&#xff0c;\u4e0d\u662f\u56e0\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\u51b2\u7a81&#xff0c;\u800c\u662f\u56e0\u4e3a\u4e3b\u8fdb\u7a0b\u6bcf\u6b21 accept \u540e\u90fd\u4f1a\u7acb\u523b close \u901a\u4fe1\u5957\u63a5\u5b57&#xff0c;\u8ba9 4 \u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u56de\u5230\u7a7a\u95f2\u72b6\u6001&#xff0c;\u5185\u6838\u4f1a\u5c06\u5b83\u91cd\u65b0\u5206\u914d\u7ed9\u4e0b\u4e00\u6b21 accept\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"958\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230043-6a63ee9bbae51.png\" width=\"2379\" \/>\u4e09\u4e2a\u5ba2\u6237\u7aef\u7a97\u53e3\u7684\u8868\u73b0\u4e00\u81f4&#xff1a;\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u8f93\u51fa\u4e86 connect to [124.222.191.171:80 80] success!&#xff0c;\u8bf4\u660e\u8fde\u63a5\u5efa\u7acb\u6b63\u5e38\u3002\u6d88\u606f\u6536\u53d1\u6b63\u5e38\u3002 \u5ba2\u6237\u7aef 1 \u8f93\u5165 \u201c\u6211\u662f\u5ba2\u6237\u7aef 1\u201d&#xff0c;\u6536\u5230\u670d\u52a1\u7aef\u56de\u663e server echo# \u6211\u662f\u5ba2\u6237\u7aef1 \u5ba2\u6237\u7aef 2 \u8f93\u5165 \u201c\u6211\u662f\u5ba2\u6237\u7aef 2\u201d&#xff0c;\u6536\u5230\u670d\u52a1\u7aef\u56de\u663e server echo# \u6211\u662f\u5ba2\u6237\u7aef2 \u5ba2\u6237\u7aef 3 \u8f93\u5165 \u201c\u6211\u662f\u5ba2\u6237\u7aef 3\u201d&#xff0c;\u6536\u5230\u670d\u52a1\u7aef\u56de\u663e server echo# \u6211\u662f\u5ba2\u6237\u7aef3<\/p>\n<p>\u5176\u5b9e\u5728\u5f53\u524d\u7684\u7ec8\u7aef\u754c\u9762\u4e0a&#xff0c;\u6211\u4eec\u662f\u65e0\u6cd5\u76f4\u89c2\u770b\u5230\u4e09\u4e2a\u8fdb\u7a0b\u662f \u201c\u540c\u65f6\u201d \u8fd0\u884c\u7684&#xff0c;\u56e0\u4e3a\u4e09\u4e2a\u5ba2\u6237\u7aef\u7684\u4ea4\u4e92\u7a97\u53e3\u662f\u5206\u5f00\u7684&#xff0c;\u6211\u4eec\u53ea\u80fd\u9010\u4e2a\u64cd\u4f5c\u3001\u9010\u4e2a\u770b\u56de\u663e&#xff0c;\u6ca1\u6cd5\u540c\u65f6\u770b\u5230\u4e09\u4e2a\u8fdb\u7a0b\u7684\u6267\u884c\u72b6\u6001\u3002\u4f46\u4ee3\u7801\u5c42\u9762&#xff0c;\u5b83\u4eec\u662f\u5e76\u53d1\u6267\u884c\u7684&#xff1a;\u4e09\u4e2a\u5b59\u5b50\u8fdb\u7a0b\u5404\u81ea\u72ec\u7acb\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bfb\u5199&#xff0c;\u4e3b\u8fdb\u7a0b\u4e5f\u5728\u540c\u65f6 accept \u65b0\u8fde\u63a5&#xff0c;\u5185\u6838\u4f1a\u8c03\u5ea6\u4e09\u4e2a\u8fdb\u7a0b\u4ea4\u66ff\u5360\u7528 CPU \u65f6\u95f4\u7247&#xff0c;\u5b9e\u73b0\u771f\u6b63\u7684\u5e76\u53d1\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u95ee\u9898\u5c31\u662f\u73b0\u5728\u8fd9\u4e2a\u8fd8\u4e0d\u5c5e\u4e8e\u804a\u5929\u5ba4\u9879\u76ee&#xff0c;\u5f53\u524d\u662f\u591a\u8fdb\u7a0b\u56de\u663e\u670d\u52a1\u5668&#xff0c;\u53ea\u662f \u201c\u5ba2\u6237\u7aef \u2194 \u670d\u52a1\u7aef\u201d \u7684\u4e00\u5bf9\u4e00\u901a\u4fe1\u6a21\u578b&#xff1a;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7684\u6d88\u606f\u4e0d\u4f1a\u8f6c\u53d1\u7ed9\u5176\u4ed6\u5ba2\u6237\u7aef\u3002\u804a\u5929\u5ba4\u9879\u76ee\u9700\u8981\u7684\u662f\u5e7f\u64ad\u6a21\u578b&#xff1a;\u670d\u52a1\u7aef\u9700\u8981\u628a\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u6d88\u606f&#xff0c;\u8f6c\u53d1\u7ed9\u6240\u6709\u5176\u4ed6\u5728\u7ebf\u7684\u5ba2\u6237\u7aef&#xff0c;\u8fd9\u9700\u8981\u989d\u5916\u7684 \u201c\u5ba2\u6237\u7aef\u5217\u8868\u7ba1\u7406 &#043; \u6d88\u606f\u5e7f\u64ad\u201d \u903b\u8f91\u3002<\/p>\n<h3 id=\"%E4%BB%A3%E7%A0%81%3A\" style=\"background-color:transparent\">\u4ee3\u7801:<\/h3>\n<p>EchoTcpServer.hpp :<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Logger.hpp&#034;<\/p>\n<p>using namespace NS_LOG_MODULE;<\/p>\n<p>enum<br \/>\n{<br \/>\n    SUCCESS &#061; 0,<br \/>\n    USAGE_ERR,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR,<br \/>\n    LISTEN_ERR,<br \/>\n    FORK_ERR<br \/>\n};<\/p>\n<p>static const int gbacklog &#061; 16;<br \/>\nstatic const uint16_t gport &#061; 8888;<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\npublic:<br \/>\n    TcpServer(uint16_t port &#061; gport) : _port(port)<br \/>\n    {<br \/>\n    }<br \/>\n    void InitServer()<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efasocket<br \/>\n        _listensockfd &#061; socket(AF_INET, SOCK_STREAM, 0); \/\/ TCP<br \/>\n        if (_listensockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;create socket error&#034;;<br \/>\n            exit(SOCKET_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;create socket success: &#034; &lt;&lt; _listensockfd;<br \/>\n        \/\/ 2. \u586b\u5145\u672c\u5730socket\u4fe1\u606f<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_port);<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u4efb\u610f\u5730\u5740bind<\/p>\n<p>        \/\/ 3. bind<br \/>\n        int n &#061; bind(_listensockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n            exit(BIND_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;bind socket success&#034;;<\/p>\n<p>        \/\/ 4. tcp\u662f\u9762\u5411\u8fde\u63a5\u7684&#xff0c;\u6240\u4ee5&#xff0c;TCP\u670d\u52a1\u5668\u8981\u5904\u4e8e\u4e00\u79cd\u53eb\u505alisten&#xff0c;\u76d1\u542c\u72b6\u6001\u3002<br \/>\n        n &#061; listen(_listensockfd, gbacklog);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;listen error&#034;;<br \/>\n            exit(LISTEN_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;listen socket success&#034;;<br \/>\n    }<br \/>\n    void serviceIO(int sockfd, InetAddr address)<br \/>\n    {<br \/>\n        \/\/ tcp sockfd, \u5168\u53cc\u5de5<br \/>\n        \/\/ \u957f\u670d\u52a1&#xff0c;\u957f\u8fde\u63a5<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;clinet info is : &#034; &lt;&lt; address.ToString();<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char inbuffer[1024] &#061; {0};<br \/>\n            \/\/ \u8bfb<br \/>\n            ssize_t n &#061; read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                inbuffer[n] &#061; 0;<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; address.ToString() &lt;&lt; &#034; say# &#034; &lt;&lt; inbuffer;<\/p>\n<p>                \/\/ \u5199<br \/>\n                std::string echo_string &#061; &#034;server echo# &#034;;<br \/>\n                echo_string &#043;&#061; inbuffer;<\/p>\n<p>                write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; &#034;client quit, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                LOG(LogLevel::ERROR) &lt;&lt; &#034;client read error, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        signal(SIGCHLD, SIG_IGN); \/\/ \u6700\u4f73\u5b9e\u8df5<br \/>\n        while (true)<br \/>\n        {<br \/>\n            struct sockaddr_in clientaddr;<br \/>\n            socklen_t len &#061; sizeof(clientaddr);<br \/>\n            \/\/ 5. \u83b7\u53d6\u8fde\u63a5<br \/>\n            int sockfd &#061; accept(_listensockfd, (struct sockaddr *)&amp;clientaddr, &amp;len);<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::WARNING) &lt;&lt; &#034;accept errr!&#034;;<br \/>\n                continue;<br \/>\n            }<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;accept success, sockfd: &#034; &lt;&lt; sockfd;<br \/>\n            \/\/ version 2: \u591a\u8fdb\u7a0b\u7248\u672c<br \/>\n            pid_t id &#061; fork();<br \/>\n            if (id &lt; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::FATAL) &lt;&lt; &#034;fork errr!&#034;;<br \/>\n                exit(FORK_ERR);<br \/>\n            }<br \/>\n            else if (id &#061;&#061; 0)<br \/>\n            {<br \/>\n                \/\/ \u5173\u95ed\u6389\u81ea\u5df1\u4e0d\u9700\u8981\u7684sockfd<br \/>\n                close(_listensockfd);<br \/>\n                \/\/ \u5b50\u8fdb\u7a0b<br \/>\n                if (fork() &gt; 0)<br \/>\n                    exit(0); \/\/ \u5b50\u8fdb\u7a0b\u76f4\u63a5\u9000\u51fa<\/p>\n<p>                \/\/ \u5b59\u5b50\u8fdb\u7a0b &#8211; \u5b64\u513f\u8fdb\u7a0b<br \/>\n                InetAddr clientaddress(clientaddr);<br \/>\n                serviceIO(sockfd, clientaddress);<br \/>\n                close(sockfd);<br \/>\n                exit(0); \/\/ \u91cd\u8981<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ \u7236\u8fdb\u7a0b<br \/>\n                \/\/ wait(id); waitpid: WNOHANG<br \/>\n                \/\/ \u5173\u95ed\u6389\u81ea\u5df1\u4e0d\u9700\u8981\u7684sockfd<br \/>\n                close(sockfd);<br \/>\n                pid_t rid &#061; waitpid(id, nullptr, 0);<br \/>\n                (void)rid;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    ~TcpServer()<br \/>\n    {<br \/>\n        close(_listensockfd);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t _port;<br \/>\n    \/\/ \u4e0d\u9700\u8981\u663e\u793a\u5305\u542bip<br \/>\n    int _listensockfd;<br \/>\n};<\/p>\n<p>EchoServerTcpMain.cc :<\/p>\n<p>#include &#034;EchoTcpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>static void Usage(const std::string &amp;process)<br \/>\n{<br \/>\n    std::cerr &lt;&lt; &#034;Usage:\\\\n\\\\t&#034;;<br \/>\n    std::cerr &lt;&lt; process &lt;&lt; &#034; local_port&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 2)<br \/>\n    {<br \/>\n        Usage(argv[0]);<br \/>\n        exit(USAGE_ERR);<br \/>\n    }<br \/>\n    ENABLE_CONSOLE_LOG_STRATEGY();<br \/>\n    uint16_t server_port &#061; std::stoi(argv[1]);<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;(server_port);<br \/>\n    tsvr-&gt;InitServer();<br \/>\n    tsvr-&gt;Start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>EchoTcpClient.cc :<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &#034;InetAddr.hpp&#034;<\/p>\n<p>static void Usage(const std::string &amp;name)<br \/>\n{<br \/>\n    std::cerr &lt;&lt; &#034;Usage:\\\\n\\\\t&#034;;<br \/>\n    std::cerr &lt;&lt; name &lt;&lt; &#034; server_ip server_port&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/client_tcp server_ip server_port<br \/>\nint 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    }<br \/>\n    std::string server_ip &#061; argv[1];<br \/>\n    uint16_t server_port &#061; std::stoi(argv[2]);<br \/>\n    \/\/ 1. \u521b\u5efatcpsocket\u5957\u63a5\u5b57<br \/>\n    int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sockfd &lt; 0)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;socket error&#034; &lt;&lt; std::endl;<br \/>\n        exit(2);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u8981\u4e0d\u8981\u8fdb\u884cbind&#xff1f;\u9700\u8981<br \/>\n    \/\/ \u8981\u4e0d\u8981\u663e\u793a\u7684bind\u672c\u5730IP\u548c\u7aef\u53e3&#xff1f;\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u663e\u793a\u7684\u8fdb\u884cbind&#xff0c;OS\u4f1a\u81ea\u52a8bind\u7684&#xff01;<br \/>\n    \/\/ OS \u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u968f\u673a\u7aef\u53e3&#xff0c;\u9632\u6b62\u7aef\u53e3\u53f7\u51b2\u7a81<br \/>\n    \/\/ 2. \u5e94\u8be5\u5e72\u4ec0\u4e48&#xff1f;bind\u7aef\u53e3\u53f7ip&#xff0c;\u53d1\u8d77\u5efa\u7acb\u8fde\u63a5\u7684\u5de5\u4f5c<br \/>\n    \/\/ {client ip, client port} &lt;-&gt; {server ip, server port}<br \/>\n    InetAddr serveraddress(server_port, server_ip);<br \/>\n    int n &#061; connect(sockfd, (struct sockaddr *)serveraddress.GetNetAddress(), serveraddress.Len());<br \/>\n    if (n &lt; 0)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;connect to &#034; &lt;&lt; serveraddress.ToString() &lt;&lt; &#034; failed!&#034;&lt;&lt; std::endl;<br \/>\n        exit(3);<br \/>\n    }<br \/>\n    std::cerr &lt;&lt; &#034;connect to &#034; &lt;&lt; serveraddress.ToString() &lt;&lt; &#034; success!&#034; &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 3. \u901a\u4fe1<br \/>\n    while(true)<br \/>\n    {<br \/>\n        std::string line;<br \/>\n        std::cout &lt;&lt; &#034;please Enter# &#034;;<br \/>\n        std::getline(std::cin, line);<\/p>\n<p>        write(sockfd, line.c_str(), line.size());<\/p>\n<p>        char inbuffer[1024];<br \/>\n        ssize_t n &#061; read(sockfd, inbuffer, sizeof(inbuffer));<br \/>\n        if(n &gt; 0)<br \/>\n        {<br \/>\n            inbuffer[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; inbuffer &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else if(n &#061;&#061; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;read enf of file!&#034; &lt;&lt; std::endl;<br \/>\n            break;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            std::cerr &lt;&lt; &#034;read error!&#034; &lt;&lt; std::endl;<br \/>\n            break;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h2 id=\"telnet\">telnet<\/h2>\n<p>\u4e0b\u9762\u5411\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e2a\u5de5\u5177 telnet&#xff0c;telnet \u662f\u4e00\u4e2a\u7ecf\u5178\u7684 TCP \u5ba2\u6237\u7aef\u5de5\u5177&#xff0c;<span style=\"color:#fe2c24\">\u53ef\u4ee5\u7528\u6765\u76f4\u63a5\u548c TCP \u670d\u52a1\u7aef\u5efa\u7acb\u8fde\u63a5\u3001\u624b\u52a8\u6536\u53d1\u6570\u636e<\/span>&#xff0c;\u5e38\u7528\u6765\u6d4b\u8bd5\u6211\u4eec\u670d\u52a1\u5668\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u5b83\u53ef\u4ee5\u5feb\u901f\u9a8c\u8bc1\u6211\u4eec\u7684 TCP \u670d\u52a1\u7aef&#xff08;\u6bd4\u5982\u6211\u4eec\u5199\u7684 8080 \u7aef\u53e3\u670d\u52a1&#xff09;\u662f\u5426\u80fd\u6b63\u5e38\u63a5\u6536\u8fde\u63a5&#xff0c;\u8fd8\u80fd\u76f4\u63a5\u548c\u670d\u52a1\u7aef\u7684\u7aef\u53e3\u5bf9\u8bdd&#xff0c;\u624b\u52a8\u53d1\u9001\u6570\u636e\u3001\u770b\u670d\u52a1\u7aef\u7684\u54cd\u5e94\u3002\u76f8\u5f53\u4e8e\u4e00\u4e2a \u201c\u6781\u7b80\u7248\u7684 TCP \u5ba2\u6237\u7aef\u201d&#xff0c;\u4e0d\u7528\u6211\u4eec\u81ea\u5df1\u5199\u5ba2\u6237\u7aef\u4ee3\u7801\u5c31\u80fd\u6d4b\u8bd5\u670d\u52a1\u7aef\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u8bd5\u9a8c\u4e00\u4e0b :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"210\" src=\"2026-07-25i1un01t053k.png\" width=\"1062\" \/> \u6211\u4eec\u76f4\u63a5\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 telnet 124.222.191.171 8080&#xff0c;\u5982\u679c\u8f93\u51fa Connected to 124.222.191.171. Escape character is &#039;^]&#039;.&#xff0c;\u8bf4\u660e\u8fde\u63a5\u6210\u529f&#xff0c;\u670d\u52a1\u7aef\u6b63\u5e38\u3002<\/p>\n<p>\u6b64\u65f6\u6211\u4eec\u6309\u4e0b\u00a0ctrl &#043; ] \u7136\u540e\u518d\u6309\u4e0b\u56de\u8f66\u540e\u624d\u80fd\u8f93\u5165\u3002<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"323\" src=\"2026-07-25pbxbfjyxehu.png\" width=\"1168\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"306\" src=\"2026-07-25eu20vcbuaj2.png\" width=\"1233\" \/>\u6b64\u65f6\u6211\u4eec\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u76f4\u63a5\u53d1\u9001\u7ed9\u670d\u52a1\u7aef&#xff0c;\u670d\u52a1\u7aef\u7684\u56de\u663e\u4e5f\u4f1a\u76f4\u63a5\u6253\u5370\u5728\u7ec8\u7aef\u4e0a&#xff0c;\u548c\u81ea\u5df1\u5199\u7684\u5ba2\u6237\u7aef\u6548\u679c\u4e00\u6837\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"334\" src=\"2026-07-251tbmana3rqq.png\" width=\"903\" \/>\u9000\u51fa\u65f6\u6211\u4eec\u897f\u836f\u518d\u6309 Ctrl &#043; ]&#xff0c;\u518d\u8f93\u5165 quit \u5373\u53ef\u3002<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"103\" src=\"2026-07-25a43t0rojmjz.png\" width=\"1237\" \/>\u6b64\u65f6\u670d\u52a1\u7aef\u4e5f\u4f1a\u663e\u793a\u9000\u51fa\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f telnet \u672c\u8eab\u4e0d\u52a0\u5bc6&#xff0c;\u4f20\u8f93\u7684\u6240\u6709\u6570\u636e\u90fd\u662f\u660e\u6587\u7684&#xff0c;\u6240\u4ee5\u73b0\u5728\u5f88\u5c11\u7528\u6765\u767b\u5f55\u8fdc\u7a0b\u670d\u52a1\u5668&#xff0c;\u66f4\u591a\u662f\u7528\u6765\u4e34\u65f6\u6d4b\u8bd5 TCP \u7aef\u53e3\u548c\u670d\u52a1\u3002\u751f\u4ea7\u73af\u5883\u7684\u8fdc\u7a0b\u767b\u5f55&#xff0c;\u4e00\u822c\u4f1a\u7528\u66f4\u5b89\u5168\u7684 ssh \u66ff\u4ee3\u3002<\/p>\n<h2 id=\"netstat%20-natp\">netstat -natp<\/h2>\n<p>\u6211\u4eec\u518d\u4ecb\u7ecd\u4e00\u4e2a\u547d\u4ee4&#xff0c;\u662f Linux \u91cc\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u7f51\u7edc\u8c03\u8bd5\u547d\u4ee4&#xff0c;\u7528\u6765\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u6240\u6709\u7684 TCP \u7f51\u7edc\u8fde\u63a5\u72b6\u6001\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"30\" src=\"2026-07-25idnl10fgzwj.png\" width=\"1164\" \/>n \u662f\u4ee5\u6570\u5b57\u5f62\u5f0f\u663e\u793a IP \u548c\u7aef\u53e3\u3002a \u663e\u793a\u6240\u6709\u8fde\u63a5&#xff0c;\u5305\u62ec\u76d1\u542c\u548c\u5df2\u5efa\u7acb\u7684\u3002t \u53ea\u663e\u793a TCP \u534f\u8bae\u7684\u8fde\u63a5\u3002p \u663e\u793a\u6bcf\u4e2a\u8fde\u63a5\u5bf9\u5e94\u7684\u8fdb\u7a0b PID \u548c\u7a0b\u5e8f\u540d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"129\" src=\"2026-07-25sh1hgmnmz31.png\" width=\"1236\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"206\" src=\"2026-07-25r3bux0rc2uk.png\" width=\"1224\" \/>\u8fd9\u4e24\u6761\u8bb0\u5f55&#xff0c;\u5c31\u662f\u6211\u4eec\u00a0telnet \u8fde\u63a5\u5230 server_tcp \u670d\u52a1\u7aef\u540e&#xff0c;\u7cfb\u7edf\u91cc\u7684\u4e00\u5bf9 TCP \u8fde\u63a5&#xff0c;\u5b83\u4eec\u662f\u53cc\u5411\u5bf9\u5e94\u7684&#xff1a;<\/p>\n<\/p>\n<p>\u4e0a\u9762\u4e00\u6761 Local Address: 10.0.16.7:44682 \u662f\u6211\u4eec\u00a0telnet \u5ba2\u6237\u7aef\u7528\u672c\u5730\u7aef\u53e3 44682 \u53d1\u8d77\u8fde\u63a5&#xff0c;Foreign Address: 124.222.191.171:8080 \u76ee\u6807\u662f\u6211\u4eec\u7684\u670d\u52a1\u7aef\u7684 8080 \u7aef\u53e3&#xff0c;State: ESTABLISHED \u8868\u793a\u8fde\u63a5\u5df2\u7ecf\u6210\u529f\u5efa\u7acb\u3002<\/p>\n<\/p>\n<p>\u4e0b\u9762\u4e00\u6761\u4e5f\u540c\u7406&#xff0c;Local Address: 10.0.16.7:8080 \u662f\u670d\u52a1\u7aef\u6b63\u5728\u76d1\u542c\u7684 8080 \u7aef\u53e3&#xff0c;Foreign Address: 124.222.191.171:44682 \u662f\u5bf9\u65b9\u5ba2\u6237\u7aef\u7684 IP \u548c\u4e34\u65f6\u7aef\u53e3&#xff0c;\u6b63\u597d\u5bf9\u5e94\u4e0a\u9762 telnet \u7684 44682&#xff0c;State: ESTABLISHED \u8868\u793a\u670d\u52a1\u7aef\u4e5f\u6210\u529f\u5efa\u7acb\u4e86\u8fde\u63a5\u3002<\/p>\n<p>\u4e00\u4e2a TCP \u8fde\u63a5\u5728\u4e00\u53f0\u673a\u5668\u4e0a&#xff0c;\u4e00\u822c\u53ea\u4f1a\u663e\u793a\u4e00\u6761\u8bb0\u5f55\u3002\u800c\u6211\u4eec\u73b0\u5728\u770b\u5230\u7684\u4e24\u6761&#xff0c;\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u90fd\u5728\u540c\u4e00\u53f0 Linux \u673a\u5668\u4e0a&#xff0c;\u8fd9\u53f0\u673a\u5668\u540c\u65f6\u62e5\u6709\u8fde\u63a5\u7684\u4e24\u4e2a\u7aef\u70b9&#xff0c;\u6240\u4ee5 netstat \u4f1a\u628a\u8fd9\u4e24\u4e2a\u7aef\u70b9\u7684\u8bb0\u5f55\u90fd\u5217\u51fa\u6765\u3002<\/p>\n<h2 id=\"%E4%BA%94%E3%80%81%E9%98%B6%E6%AE%B5%E4%B8%89%3A%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC\" style=\"background-color:transparent\">\u4e94\u3001\u9636\u6bb5\u4e09: \u591a\u7ebf\u7a0b\u7248\u672c<\/h2>\n<p>\u5728\u5b8c\u6210\u591a\u8fdb\u7a0b\u5e76\u53d1\u6a21\u578b\u7684\u6539\u9020\u540e&#xff0c;\u6211\u4eec\u8fce\u6765\u4e86\u7b2c\u4e09\u9636\u6bb5\u7684\u5347\u7ea7&#xff1a;\u591a\u7ebf\u7a0b\u7248\u672c\u7684 TCP \u5e76\u53d1\u670d\u52a1\u5668\u3002\u76f8\u8f83\u4e8e\u591a\u8fdb\u7a0b\u65b9\u6848&#xff0c;\u591a\u7ebf\u7a0b\u6a21\u578b\u4ee5\u66f4\u4f4e\u7684\u7cfb\u7edf\u5f00\u9500\u3001\u66f4\u7b80\u5355\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;\u5b9e\u73b0\u4e86\u76f8\u540c\u7684\u5e76\u53d1\u80fd\u529b\u3002\u4e3b\u7ebf\u7a0b\u4e13\u6ce8 accept \u63a5\u6536\u65b0\u8fde\u63a5&#xff0c;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7531\u72ec\u7acb\u7684\u5b50\u7ebf\u7a0b\u5904\u7406&#xff0c;\u65e2\u907f\u514d\u4e86\u8fdb\u7a0b\u521b\u5efa\u7684\u9ad8\u6210\u672c&#xff0c;\u4e5f\u89e3\u51b3\u4e86\u8fdb\u7a0b\u95f4\u901a\u4fe1\u590d\u6742\u7684\u95ee\u9898&#xff0c;\u662f Linux \u4e0b\u66f4\u8f7b\u91cf\u3001\u66f4\u9ad8\u6548\u7684\u5e76\u53d1\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u591a\u7ebf\u7a0b vs \u591a\u8fdb\u7a0b\u7684\u5fc3\u5dee\u5f02<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"471\" src=\"2026-07-25b2ba4olpkub.png\" width=\"1172\" \/><\/p>\n<h3 id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A\">\u903b\u8f91\u68b3\u7406:<\/h3>\n<p>\u5176\u5b9e\u591a\u7ebf\u7a0b\u7684\u5e76\u53d1\u601d\u8def\u548c\u591a\u8fdb\u7a0b\u7684\u4e00\u6837&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u5728 while(true) \u5faa\u73af\u91cc\u8c03\u7528 accept()&#xff0c;\u6536\u5230\u65b0\u8fde\u63a5\u540e&#xff0c;\u7acb\u523b\u521b\u5efa\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u5904\u7406\u8fd9\u4e2a\u8fde\u63a5&#xff0c;\u7136\u540e\u9a6c\u4e0a\u56de\u5230\u5faa\u73af\u7ee7\u7eed\u7b49\u5f85\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002\u5b50\u7ebf\u7a0b\u6bcf\u4e2a\u8fde\u63a5\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7ebf\u7a0b&#xff0c;\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570 thread_routine \u4f1a\u8c03\u7528 serviceIO \u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bfb\u5199\u4ea4\u4e92&#xff0c;\u7ebf\u7a0b\u7ed3\u675f\u524d\u4f1a\u81ea\u52a8\u5173\u95ed\u901a\u4fe1\u5957\u63a5\u5b57\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p>1. \u9996\u5148&#xff0c;\u6211\u4eec\u7684\u76ee\u6807\u662f\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7248\u672c\u7684 TCP \u670d\u52a1\u5668&#xff0c;\u4e3b\u7ebf\u7a0b\u8d1f\u8d23 accept \u63a5\u6536\u8fde\u63a5&#xff0c;\u6bcf\u6765\u4e00\u4e2a\u5ba2\u6237\u7aef\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u4e13\u95e8\u5904\u7406\u5b83\u7684 IO&#xff0c;\u6240\u4ee5\u5f53\u4e3b\u7ebf\u7a0b\u8c03\u7528 pthread_create \u521b\u5efa\u7ebf\u7a0b\u65f6&#xff0c;\u5fc5\u987b\u7ed9\u5b83\u4e00\u4e2a\u7b26\u5408\u683c\u5f0f\u8981\u6c42\u7684\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570 thread_routine()&#xff0c;\u4e5f\u5c31\u662f\u5fc5\u987b\u662f\u8fd4\u56de\u503c\u4e3a void*\u3001\u53c2\u6570\u4e3a void\u7684\u51fd\u6570\u3002<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"75\" src=\"2026-07-25zcmk24e1tod.png\" width=\"930\" \/><\/p>\n<p>2. \u800c\u5728 C&#043;&#043; \u91cc&#xff0c;\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u662f\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u7ebf\u7a0b\u5165\u53e3\u7684&#xff0c;\u56e0\u4e3a\u5b83\u5728\u5e95\u5c42\u4f1a\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u52a0\u4e0a\u4e00\u4e2a\u9690\u85cf\u7684 this \u6307\u9488\u53c2\u6570&#xff0c;\u7528\u6765\u6307\u5411\u5f53\u524d\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u4e00\u6765&#xff0c;\u666e\u901a\u6210\u5458\u51fd\u6570\u7684\u7b7e\u540d\u5c31\u53d8\u6210\u4e86 void thread_routine (TcpServer* this, void* args)&#xff0c;\u548c pthread_create \u8981\u6c42\u7684\u683c\u5f0f\u4e0d\u5339\u914d&#xff0c;\u6240\u4ee5\u5fc5\u987b\u628a thread_routine \u58f0\u660e\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570&#xff0c;\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u5bf9\u8c61&#xff0c;\u6ca1\u6709\u9690\u85cf\u7684 this \u6307\u9488&#xff0c;\u5b83\u7684\u7b7e\u540d\u81ea\u7136\u5c31\u662f void* thread_routine (void* args)&#xff0c;\u521a\u597d\u7b26\u5408 pthread_create \u7684\u8981\u6c42\u3002<\/p>\n<p>3. \u4f46\u95ee\u9898\u662f&#xff0c;<span style=\"color:#fe2c24\">\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u975e\u9759\u6001\u7684\u6210\u5458\u51fd\u6570<\/span>&#xff0c;\u6bd4\u5982\u6211\u4eec\u7684 serviceIO \u51fd\u6570&#xff0c;\u800c serviceIO \u5c31\u662f TcpServer \u7c7b\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u9759\u6001\u6210\u5458\u51fd\u6570\u8981\u8c03\u7528\u5fc5\u987b\u901a\u8fc7\u7c7b\u5bf9\u8c61\u6216\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\u6765\u8c03\u7528&#xff0c;\u6240\u4ee5\u6211\u4eec\u9700\u8981\u628a\u5f53\u524d TcpServer \u5bf9\u8c61\u7684\u5730\u5740&#xff0c;\u4e5f\u5c31\u662f EchoTcpServerMain.cc \u6587\u4ef6\u91cc\u7684\u5b9a\u4e49\u51fa\u6765\u7684\u00a0TcpServer \u7c7b\u5bf9\u8c61\u7684 this \u6307\u9488&#xff0c;\u4f20\u9012\u7ed9\u9759\u6001\u7ebf\u7a0b\u51fd\u6570\u3002<\/p>\n<p>4. \u53ef pthread_create \u53c8\u53ea\u80fd\u4f20\u4e00\u4e2a void \u7c7b\u578b\u7684\u53c2\u6570\u7ed9 thread_routine \u7ebf\u7a0b\u5165\u53e3\u51fd\u6570&#xff0c;\u800c\u6211\u4eec\u4e0d\u4ec5\u8981\u4f20 this\u6307\u9488&#xff0c;\u8fd8\u8981\u4f20\u5ba2\u6237\u7aef\u7684\u901a\u4fe1\u5957\u63a5\u5b57 sockfd \u548c\u5ba2\u6237\u7aef\u5730\u5740\u4fe1\u606f&#xff0c;\u6240\u4ee5\u5c31\u9700\u8981\u4e00\u4e2a\u5bb9\u5668\u628a\u8fd9\u4e09\u6837\u4e1c\u897f\u6253\u5305\u8d77\u6765&#xff0c;\u56e0\u6b64\u6211\u4eec\u5728 TcpServer \u7c7b\u91cc\u9762\u53c8\u5199\u4e86\u4e2a\u5185\u90e8\u7c7b\u00a0ThreadData&#xff0c;\u5b83\u5c31\u50cf\u4e00\u4e2a\u5feb\u9012\u76d2&#xff0c;\u628a\u4e3b\u7ebf\u7a0b\u91cc\u7684 this\u6307\u9488\u3001sockfd \u548c clientaddress \u5168\u90e8\u88c5\u8fdb\u53bb&#xff0c;\u4e3b\u7ebf\u7a0b accept \u5230\u8fde\u63a5\u540e&#xff0c;new \u4e00\u4e2a ThreadData \u5bf9\u8c61&#xff0c;\u628a\u8fd9\u4e09\u4e2a\u53c2\u6570\u90fd\u4f20\u8fdb\u53bb&#xff0c;\u7136\u540e\u628a\u8fd9\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u4f5c\u4e3a void \u53c2\u6570\u4f20\u7ed9 pthread_create&#xff0c;\u5728\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570 thread_routine \u91cc&#xff0c;\u6211\u4eec\u518d\u628a\u4f20\u8fdb\u6765\u7684 void \u53c2\u6570\u5f3a\u8f6c\u56de ThreadData \u7c7b\u578b&#xff0c;\u5c31\u80fd\u62ff\u5230\u91cc\u9762\u5b58\u597d\u7684 this \u6307\u9488\u3001sockfd \u548c\u5730\u5740&#xff0c;\u518d\u901a\u8fc7 this \u6307\u9488\u8c03\u7528 serviceIO \u6210\u5458\u51fd\u6570&#xff0c;\u8fd9\u6837\u65e2\u6ee1\u8db3\u4e86 pthread_create \u5bf9\u5165\u53e3\u51fd\u6570\u683c\u5f0f\u7684\u8981\u6c42&#xff0c;\u53c8\u80fd\u8ba9\u9759\u6001\u51fd\u6570\u6210\u529f\u8c03\u7528\u975e\u9759\u6001\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u540c\u65f6\u8fd8\u80fd\u628a\u7ebf\u7a0b\u9700\u8981\u7684\u6240\u6709\u53c2\u6570\u4e00\u6b21\u6027\u4f20\u9012\u8fc7\u53bb&#xff0c;\u800c ThreadData \u7684\u6790\u6784\u51fd\u6570\u91cc\u8fd8\u5199\u4e86 close (_sockfd)&#xff0c;\u5728\u7ebf\u7a0b\u51fd\u6570\u6267\u884c\u5b8c delete td \u65f6&#xff0c;\u4f1a\u81ea\u52a8\u5173\u95ed\u901a\u4fe1\u5957\u63a5\u5b57&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002\u53ef\u8c13\u5341\u5206\u4f18\u96c5!<\/p>\n<p>5. \u6574\u4e2a\u6d41\u7a0b\u91cc&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u7ebf\u7a0b\u51fd\u6570\u62ff\u5230\u6253\u5305\u597d\u7684\u53c2\u6570\u540e&#xff0c;\u5c31\u80fd\u72ec\u7acb\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bfb\u5199&#xff0c;\u548c\u4e3b\u7ebf\u7a0b\u4e92\u4e0d\u5e72\u6270&#xff0c;\u5b9e\u73b0\u4e86\u5e76\u53d1\u7684\u6548\u679c\u3002\u53ef\u8c13\u5341\u5206\u4f18\u96c5!<\/p>\n<p>6. \u6539\u8fdb\u7684\u7ebf\u7a0b\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"752\" src=\"2026-07-25reohae4hupn.png\" width=\"1410\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"612\" src=\"2026-07-25fjck3bgejcb.png\" width=\"1117\" \/><\/p>\n<p>7. \u6211\u4eec\u53ea\u9700\u8981\u5728 Start() \u51fd\u6570\u91cc\u65b0\u589e\u7ea2\u6846\u91cc\u8fd9\u51e0\u884c\u4ee3\u7801&#xff0c;\u518d\u914d\u5408\u4e0a\u9762\u7684 ThreadData \u5185\u90e8\u7c7b\u548c thread_routine \u9759\u6001\u51fd\u6570&#xff0c;\u5c31\u5b8c\u6210\u4e86\u591a\u7ebf\u7a0b\u7248\u672c\u7684\u6539\u9020&#xff0c;\u5176\u4ed6\u5730\u65b9\u7684\u4ee3\u7801\u90fd\u4e0d\u7528\u52a8\u3002<\/p>\n<h3 id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%20%3A\">\u8fd0\u884c\u7ed3\u679c :<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"228\" src=\"2026-07-25zkszspyfjm5.png\" width=\"1008\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"184\" src=\"2026-07-250ukncmjfgeu.png\" width=\"1015\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"808\" src=\"2026-07-255w5jrp4tz32.png\" width=\"2301\" \/> \u5728\u591a\u7ebf\u7a0b\u7248\u672c\u91cc&#xff0c;\u4e3b\u7ebf\u7a0b accept \u62ff\u5230 sockfd&#061;4 \u540e&#xff0c;\u4f1a\u6253\u5305\u4ea4\u7ed9\u00a0ThreadData \u4f20\u7ed9\u5b50\u7ebf\u7a0b&#xff0c;\u4e3b\u7ebf\u7a0b\u6ca1\u6709\u5173\u95ed\u5b83&#xff0c;\u4e3b\u7ebf\u7a0b\u9a6c\u4e0a\u56de\u5230\u5faa\u73af&#xff0c;\u518d\u6b21 accept&#xff0c;\u5185\u6838\u5206\u914d\u4e0b\u4e00\u4e2a\u53ef\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4e5f\u5c31\u662f 5 \u6240\u4ee5\u65e5\u5fd7\u91cc\u4f1a\u51fa\u73b0 4 \u548c 5&#xff0c;\u5b83\u4eec\u5206\u522b\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5404\u81ea\u88ab\u5bf9\u5e94\u7684\u5b50\u7ebf\u7a0b\u6301\u6709&#xff0c;\u4e3b\u7ebf\u7a0b\u8fd9\u8fb9\u7684\u5f15\u7528\u6ca1\u6709\u88ab\u5173\u95ed&#xff0c;\u5185\u6838\u5c31\u4e0d\u4f1a\u56de\u6536\u8fd9\u4e9b fd \u7f16\u53f7&#xff0c;\u53ea\u80fd\u6309\u987a\u5e8f\u9012\u589e\u5206\u914d\u3002<\/p>\n<p>\u800c\u5728\u524d\u9762\u7684\u591a\u8fdb\u7a0b\u7248\u672c\u4e2d&#xff0c;\u4e3b\u7ebf\u7a0b accept \u62ff\u5230 sockfd&#061;4 \u540e&#xff0c;\u4f1a\u7acb\u523b\u6267\u884c close(sockfd)&#xff0c;\u628a\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5173\u95ed\u3002\u8fd9\u6837&#xff0c;4 \u53f7 fd \u5c31\u56de\u5230\u4e86\u7a7a\u95f2\u72b6\u6001&#xff0c;\u4e0b\u4e00\u6b21 accept \u5c31\u4f1a\u518d\u6b21\u62ff\u5230 4&#xff0c;\u6240\u4ee5\u65e5\u5fd7\u91cc\u4e00\u76f4\u662f 4\u3002<\/p>\n<p>\u53c8\u56e0\u4e3a\u5728 ThreadData \u7c7b\u7684\u6790\u6784\u51fd\u6570\u91cc\u5199\u4e86 close(_sockfd); \u6240\u4ee5\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c serviceIO() \u540e&#xff0c;\u4f1a delete td&#xff0c;\u89e6\u53d1\u6790\u6784\u51fd\u6570&#xff0c;\u81ea\u52a8\u5173\u95ed sockfd&#xff0c;\u5185\u6838\u4e5f\u4f1a\u81ea\u52a8\u56de\u6536\u7ebf\u7a0b\u6301\u6709\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"849\" src=\"2026-07-25uj2emcgsf1f.png\" width=\"2283\" \/>\u6d88\u606f\u6536\u53d1\u6210\u529f!<\/p>\n<h3 id=\"%E4%BB%A3%E7%A0%81%3A\" style=\"background-color:transparent\">\u4ee3\u7801:<\/h3>\n<p>EchoTcpServer.hpp :<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &#034;Thread.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Logger.hpp&#034;<\/p>\n<p>using namespace NS_LOG_MODULE;<\/p>\n<p>enum<br \/>\n{<br \/>\n    SUCCESS &#061; 0,<br \/>\n    USAGE_ERR,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR,<br \/>\n    LISTEN_ERR,<br \/>\n    FORK_ERR<br \/>\n};<\/p>\n<p>static const int gbacklog &#061; 16;<br \/>\nstatic const uint16_t gport &#061; 8888;<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\npublic:<br \/>\n    TcpServer(uint16_t port &#061; gport) : _port(port)<br \/>\n    {<br \/>\n    }<br \/>\n    void InitServer()<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efasocket<br \/>\n        _listensockfd &#061; socket(AF_INET, SOCK_STREAM, 0); \/\/ TCP<br \/>\n        if (_listensockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;create socket error&#034;;<br \/>\n            exit(SOCKET_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;create socket success: &#034; &lt;&lt; _listensockfd;<br \/>\n        \/\/ 2. \u586b\u5145\u672c\u5730socket\u4fe1\u606f<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_port);<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u4efb\u610f\u5730\u5740bind<\/p>\n<p>        \/\/ 3. bind<br \/>\n        int n &#061; bind(_listensockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n            exit(BIND_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;bind socket success&#034;;<\/p>\n<p>        \/\/ 4. tcp\u662f\u9762\u5411\u8fde\u63a5\u7684&#xff0c;\u6240\u4ee5&#xff0c;TCP\u670d\u52a1\u5668\u8981\u5904\u4e8e\u4e00\u79cd\u53eb\u505alisten&#xff0c;\u76d1\u542c\u72b6\u6001\u3002<br \/>\n        n &#061; listen(_listensockfd, gbacklog);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;listen error&#034;;<br \/>\n            exit(LISTEN_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;listen socket success&#034;;<br \/>\n    }<br \/>\n    void serviceIO(int sockfd, InetAddr address)<br \/>\n    {<br \/>\n        \/\/ tcp sockfd, \u5168\u53cc\u5de5<br \/>\n        \/\/ \u957f\u670d\u52a1&#xff0c;\u957f\u8fde\u63a5<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;clinet info is : &#034; &lt;&lt; address.ToString();<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char inbuffer[1024] &#061; {0};<br \/>\n            \/\/ \u8bfb<br \/>\n            ssize_t n &#061; read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                inbuffer[n] &#061; 0;<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; address.ToString() &lt;&lt; &#034; say# &#034; &lt;&lt; inbuffer;<\/p>\n<p>                \/\/ \u5199<br \/>\n                std::string echo_string &#061; &#034;server echo# &#034;;<br \/>\n                echo_string &#043;&#061; inbuffer;<\/p>\n<p>                write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; &#034;client quit, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                LOG(LogLevel::ERROR) &lt;&lt; &#034;client read error, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    class ThreadData  \/\/\u5185\u90e8\u7c7b<br \/>\n    {<br \/>\n    public:<br \/>\n        ThreadData(TcpServer *ts, int sockfd, const InetAddr &amp;addr)<br \/>\n            : _this(ts), _sockfd(sockfd), _addr(addr)<br \/>\n        {<br \/>\n        }<br \/>\n        ~ThreadData()<br \/>\n        {<br \/>\n            close(_sockfd);<br \/>\n        }<\/p>\n<p>    public:<br \/>\n        TcpServer *_this;<br \/>\n        int _sockfd;<br \/>\n        InetAddr _addr;<br \/>\n    };<\/p>\n<p>    static void *thread_routine(void *args)<br \/>\n    {<br \/>\n        ThreadData *td &#061; static_cast&lt;ThreadData *&gt;(args);<br \/>\n        pthread_detach(pthread_self());<br \/>\n        td-&gt;_this-&gt;serviceIO(td-&gt;_sockfd, td-&gt;_addr);<br \/>\n        delete td;<br \/>\n        return nullptr;<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        while (true)<br \/>\n        {<br \/>\n            struct sockaddr_in clientaddr;<br \/>\n            socklen_t len &#061; sizeof(clientaddr);<br \/>\n            \/\/ 5. \u83b7\u53d6\u8fde\u63a5<br \/>\n            int sockfd &#061; accept(_listensockfd, (struct sockaddr *)&amp;clientaddr, &amp;len);<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::WARNING) &lt;&lt; &#034;accept errr!&#034;;<br \/>\n                continue;<br \/>\n            }<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;accept success, sockfd: &#034; &lt;&lt; sockfd;<br \/>\n            \/\/ version 3: \u591a\u7ebf\u7a0b\u7248\u672c<br \/>\n            pthread_t tid;<br \/>\n            InetAddr clientaddress(clientaddr);<br \/>\n            ThreadData *td &#061; new ThreadData(this, sockfd, clientaddress);<br \/>\n            pthread_create(&amp;tid, nullptr, thread_routine, (void *)td);<br \/>\n        }<br \/>\n    }<br \/>\n    ~TcpServer()<br \/>\n    {<br \/>\n        close(_listensockfd);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t _port;<br \/>\n    \/\/ \u4e0d\u9700\u8981\u663e\u793a\u5305\u542bip<br \/>\n    int _listensockfd;<br \/>\n};<\/p>\n<p>\u56e0\u4e3a EchoTcpServerMain.cc \u548c EchoTcpClient.cc \u6587\u4ef6\u91cc\u7684\u4ee3\u7801\u90fd\u6ca1\u6709\u6539\u52a8&#xff0c;\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4e86\u3002\u00a0<\/p>\n<h2 id=\"%E5%85%AD%E3%80%81%E9%98%B6%E6%AE%B5%E5%9B%9B%3A%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%89%88%E6%9C%AC\" style=\"background-color:transparent\">\u516d\u3001\u9636\u6bb5\u56db: \u7ebf\u7a0b\u6c60\u7248\u672c<\/h2>\n<p>\u5728\u524d\u9762\u7684\u7248\u672c\u4e2d&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86 \u201c\u4e3b\u7ebf\u7a0b\u6bcf accept \u6210\u529f\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c IO\u201d \u7684\u591a\u7ebf\u7a0b TCP \u670d\u52a1\u5668&#xff0c;\u89e3\u51b3\u4e86\u5355\u8fdb\u7a0b\u963b\u585e\u548c\u591a\u8fdb\u7a0b\u5f00\u9500\u5927\u7684\u95ee\u9898\u3002\u4f46\u968f\u7740\u5e76\u53d1\u91cf\u7684\u63d0\u5347&#xff0c;\u201c\u6765\u4e00\u4e2a\u8fde\u63a5\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u201d \u7684\u6a21\u5f0f&#xff0c;\u4e5f\u4f1a\u66b4\u9732\u51fa\u4e86\u4e24\u4e2a\u6838\u5fc3\u7f3a\u9677&#xff1a;<\/p>\n<p>1. \u9891\u7e41\u521b\u5efa \/ \u9500\u6bc1\u7ebf\u7a0b\u7684\u7cfb\u7edf\u5f00\u9500\u5927&#xff0c;\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u6027\u80fd\u635f\u8017\u660e\u663e<\/p>\n<p>2. \u7ebf\u7a0b\u6570\u91cf\u968f\u5ba2\u6237\u7aef\u6570\u65e0\u9650\u5236\u589e\u957f&#xff0c;\u53ef\u80fd\u8017\u5c3d\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u5f15\u53d1\u670d\u52a1\u5d29\u6e83<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898&#xff0c;\u6211\u4eec\u5f15\u5165\u4e86\u7ebf\u7a0b\u6c60\u6a21\u578b&#xff0c;\u8fd9\u4e5f\u662f\u751f\u4ea7\u73af\u5883\u4e2d\u670d\u52a1\u5668\u5e76\u53d1\u6a21\u578b\u7684\u4e3b\u6d41\u5b9e\u73b0\u3002<\/p>\n<h3 id=\"%E9%80%BB%E8%BE%91%E6%A2%B3%E7%90%86%3A\">\u903b\u8f91\u68b3\u7406:<\/h3>\n<p>\u5f15\u5165\u7ebf\u7a0b\u6c60\u4e4b\u540e&#xff0c;\u6574\u4e2a\u670d\u52a1\u7684\u5e76\u53d1\u6a21\u578b\u53d8\u6210\u4e86\u300c\u4e3b\u7ebf\u7a0b &#043; \u56fa\u5b9a\u6570\u91cf\u5de5\u4f5c\u7ebf\u7a0b\u300d\u7684\u534f\u4f5c\u6a21\u5f0f&#xff0c;\u670d\u52a1\u542f\u52a8\u65f6&#xff0c;\u6211\u4eec\u5148\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u5355\u4f8b\u5b9e\u4f8b&#xff0c;\u5b83\u4f1a\u4e00\u6b21\u6027\u521b\u5efa\u597d\u9884\u8bbe\u6570\u91cf\u7684\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u8fd9\u4e9b\u7ebf\u7a0b\u4e00\u521b\u5efa\u5c31\u4f1a\u8fdb\u5165\u963b\u585e\u72b6\u6001&#xff0c;\u7b49\u5f85\u4efb\u52a1\u961f\u5217\u91cc\u6709\u65b0\u4efb\u52a1\u5230\u6765\u3002<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u5219\u5728 while(true) \u5faa\u73af\u91cc&#xff0c;\u53ea\u8d1f\u8d23\u4e00\u4ef6\u4e8b&#xff1a;\u8c03\u7528 accept() \u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u4e00\u65e6\u6536\u5230\u65b0\u8fde\u63a5&#xff0c;\u4e3b\u7ebf\u7a0b\u4f1a\u628a\u5904\u7406\u8fd9\u4e2a\u5ba2\u6237\u7aef IO\u7684\u903b\u8f91\u5c01\u88c5\u6210\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u8c03\u7528 Enqueue() \u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u961f\u5217\u91cc\u3002\u63d0\u4ea4\u4efb\u52a1\u7684\u64cd\u4f5c\u662f\u975e\u963b\u585e\u7684&#xff0c;\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u7b49\u4efb\u52a1\u6267\u884c\u5b8c&#xff0c;\u800c\u662f\u7acb\u523b\u56de\u5230\u5faa\u73af\u5f00\u5934&#xff0c;\u7ee7\u7eed\u4e0b\u4e00\u6b21 accept()\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u5185\u90e8&#xff0c;\u5de5\u4f5c\u7ebf\u7a0b\u4f1a\u6301\u7eed\u4ece\u4efb\u52a1\u961f\u5217\u91cc\u53d6\u4efb\u52a1&#xff1a;\u961f\u5217\u91cc\u6709\u4efb\u52a1\u65f6&#xff0c;\u5c31\u53d6\u51fa\u5e76\u6267\u884c serviceIO \u5904\u7406\u5ba2\u6237\u7aef\u8bfb\u5199&#xff1b;\u961f\u5217\u4e3a\u7a7a\u65f6&#xff0c;\u5c31\u963b\u585e\u7b49\u5f85&#xff0c;\u4e0d\u6d88\u8017 CPU \u8d44\u6e90\u3002\u8fd9\u6837\u4e00\u6765&#xff0c;\u6240\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u7684 IO \u5904\u7406&#xff0c;\u90fd\u7531\u7ebf\u7a0b\u6c60\u91cc\u7684\u56fa\u5b9a\u7ebf\u7a0b\u590d\u7528\u6267\u884c&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u4e13\u6ce8\u63a5\u6536\u8fde\u63a5&#xff0c;\u4e24\u8005\u4e92\u4e0d\u963b\u585e\u3002<\/p>\n<p>\u548c\u4e4b\u524d \u201c\u6bcf\u8fde\u63a5\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u201d \u7684\u6a21\u5f0f\u76f8\u6bd4&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u65e2\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500&#xff0c;\u53c8\u901a\u8fc7\u7ebf\u7a0b\u6c60\u5927\u5c0f\u9650\u5236\u4e86\u6700\u5927\u5e76\u53d1\u6570&#xff0c;\u4e0d\u4f1a\u56e0\u4e3a\u5ba2\u6237\u7aef\u66b4\u589e\u800c\u8017\u5c3d\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u662f\u670d\u52a1\u5668\u5f00\u53d1\u4e2d\u66f4\u7a33\u5b9a\u3001\u66f4\u9ad8\u6548\u7684\u5e76\u53d1\u65b9\u6848\u3002<\/p>\n<h3 id=\"%E6%94%B9%E5%8A%A8%E7%9A%84%E5%9C%B0%E6%96%B9%3A\">\u6539\u52a8\u7684\u5730\u65b9:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"33\" src=\"2026-07-25shfhieb43b0.png\" width=\"921\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"42\" src=\"2026-07-25buymopycfkn.png\" width=\"1124\" \/>\u8fd9\u4e00\u6b65\u662f\u5f15\u5165\u6211\u4eec\u5c01\u88c5\u597d\u7684\u7ebf\u7a0b\u6c60\u6a21\u5757&#xff0c;\u5b83\u63d0\u4f9b\u4e86\u4efb\u52a1\u961f\u5217\u3001\u7ebf\u7a0b\u590d\u7528\u548c\u5e76\u53d1\u63a7\u5236\u7684\u6838\u5fc3\u80fd\u529b&#xff0c;\u662f\u6574\u4e2a\u4f18\u5316\u7684\u57fa\u7840\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"47\" src=\"2026-07-25zdkvuss5ybv.png\" width=\"902\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"604\" src=\"2026-07-25dphfrgfrb52.png\" width=\"1261\" \/>1. \u6211\u4eec\u5148\u7528 using task_t &#061; std::function&lt;void()&gt;&#xff0c;\u7edf\u4e00\u89c4\u5b9a\u4e86\u4efb\u52a1\u6807\u51c6&#xff1a;\u5fc5\u987b\u662f\u65e0\u53c2\u3001\u65e0\u8fd4\u56de\u503c\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002Lambda \u8868\u8fbe\u5f0f\u867d\u7136\u5185\u90e8\u8981\u4f7f\u7528\u00a0TcpServer \u5bf9\u8c61\u7684 this \u6307\u9488&#xff0c;\u5ba2\u6237\u7aef\u901a\u4fe1\u5957\u63a5\u5b57(sockfd&#xff09;\u548c\u5ba2\u6237\u7aef\u5730\u5740\u4fe1\u606f&#xff08;clientaddress&#xff09;\u7b49\u53d8\u91cf&#xff0c;\u4f46\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u901a\u8fc7\u6355\u83b7\u5217\u8868\u63d0\u524d\u4fdd\u5b58\u7684&#xff0c;\u4e0d\u5c5e\u4e8e\u51fd\u6570\u8c03\u7528\u65f6\u9700\u8981\u4f20\u9012\u7684\u5f62\u53c2&#xff0c;Lambda \u81ea\u8eab\u7684\u6267\u884c\u62ec\u53f7\u4e3a\u7a7a\u3001\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c&#xff0c;\u5bf9\u5916\u5b8c\u5168\u7b26\u5408\u65e0\u53c2\u65e0\u8fd4\u56de\u503c\u7684\u683c\u5f0f&#xff0c;\u6b63\u597d\u548c std::function&lt;void()&gt; \u5b8c\u7f8e\u5339\u914d\u3002\u6700\u7ec8\u6574\u4e2a Lambda \u4f1a\u88ab\u9690\u5f0f\u8f6c\u4e3a task_t \u4efb\u52a1\u7c7b\u578b&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u5305\u542b\u4e1a\u52a1\u903b\u8f91\u548c\u6240\u9700\u5168\u90e8\u6570\u636e\u7684 Lambda&#xff0c;\u76f4\u63a5\u5c01\u88c5\u6210\u4e00\u4e2a\u6807\u51c6\u4efb\u52a1&#xff0c;\u6295\u9012\u5230\u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u8ba9\u7ebf\u7a0b\u6c60\u7684\u5de5\u4f5c\u7ebf\u7a0b\u81ea\u52a8\u8c03\u5ea6\u6267\u884c\u3002\u4e3b\u7ebf\u7a0b\u5b8c\u5168\u4e0d\u7528\u7ba1\u4efb\u52a1\u600e\u4e48\u8dd1&#xff0c;\u53ea\u8d1f\u8d23\u4e0d\u505c accept \u63a5\u6536\u65b0\u8fde\u63a5\u3001\u6301\u7eed\u5f80\u7ebf\u7a0b\u6c60\u629b\u4efb\u52a1\u5c31\u884c\u3002<\/p>\n<p>2. \u7ebf\u7a0b\u6c60\u672c\u8eab\u662f\u4e00\u4e2a\u91cd\u91cf\u7ea7\u5bf9\u8c61&#xff0c;\u63d0\u524d\u521b\u5efa\u56fa\u5b9a\u6570\u91cf\u7684\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u662f\u4e3a\u4e86\u907f\u514d\u9891\u7e41\u521b\u5efa \/ \u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002\u5982\u679c\u4e3b\u7ebf\u7a0b\u5728 while(true) \u5faa\u73af\u91cc\u6bcf\u6b21 accept \u5230\u65b0\u8fde\u63a5\u65f6&#xff0c;\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6c60&#xff0c;\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u6c60\u5b9e\u4f8b\u65e0\u9650\u589e\u591a&#xff0c;\u5b8c\u5168\u5931\u53bb\u590d\u7528\u7ebf\u7a0b\u7684\u610f\u4e49&#xff0c;\u751a\u81f3\u76f4\u63a5\u8017\u5c3d\u7cfb\u7edf\u8d44\u6e90\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u6211\u4eec\u628a<span style=\"color:#fe2c24\">\u7ebf\u7a0b\u6c60\u8bbe\u8ba1\u6210\u5355\u4f8b\u6a21\u5f0f<\/span>&#xff1a;\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4&#xff0c;\u5168\u5c40\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5b9e\u4f8b&#xff1b;\u7b2c\u4e00\u6b21\u8c03\u7528 ThreadPool&lt;task_t&gt;::Instance() \u65f6&#xff0c;\u624d\u4f1a\u771f\u6b63\u521b\u5efa\u7ebf\u7a0b\u6c60&#xff0c;\u5e76\u521d\u59cb\u5316\u6240\u6709\u5de5\u4f5c\u7ebf\u7a0b&#xff1b;\u540e\u7eed\u6bcf\u6b21\u8c03\u7528 Instance()&#xff0c;\u90fd\u4f1a\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u7ebf\u7a0b\u6c60\u5bf9\u8c61&#xff0c;\u4e0d\u518d\u91cd\u590d\u521b\u5efa\u3002\u8fd9\u6837\u4e00\u6765&#xff0c;\u4e3b\u7ebf\u7a0b\u5728 while(true) \u5faa\u73af\u91cc&#xff0c;\u867d\u7136\u6bcf\u6b21\u90fd\u4f1a\u6267\u884c ThreadPool&lt;task_t&gt;::Instance()-&gt;Enqueue(&#8230;) \u8fd9\u884c\u4ee3\u7801&#xff0c;\u4f46\u672c\u8d28\u4e0a\u53ea\u662f\u5f80\u540c\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u961f\u5217\u91cc&#xff0c;\u4e0d\u65ad\u63d0\u4ea4\u65b0\u7684\u5ba2\u6237\u7aef\u5904\u7406\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u5185\u90e8\u7684\u5de5\u4f5c\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c&#xff0c;\u65e2\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u6c60\u53ea\u521b\u5efa\u4e00\u6b21\u3001\u7ebf\u7a0b\u590d\u7528&#xff0c;\u53c8\u4e0d\u5f71\u54cd\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u63a5\u6536\u65b0\u8fde\u63a5&#xff0c;\u5b9e\u73b0\u4e86\u9ad8\u6548\u3001\u53ef\u63a7\u7684\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<h3 id=\"%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C%3A\">\u8fd0\u884c\u7ed3\u679c:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"212\" src=\"2026-07-25k0vemkyvd5q.png\" width=\"1071\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"143\" src=\"2026-07-25qilphkzlsb2.png\" width=\"993\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"754\" src=\"2026-07-25anzqphhezvs.png\" width=\"2061\" \/>\u4e24\u4e2a\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u90fd\u88ab\u4e3b\u7ebf\u7a0b\u6210\u529f\u63a5\u6536\u3002\u7ebf\u7a0b\u6c60\u5355\u4f8b\u88ab\u6210\u529f\u521b\u5efa\u3002\u7ebf\u7a0b\u6c60\u91cc\u7684\u4e24\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u5df2\u7ecf\u542f\u52a8&#xff0c;\u6b63\u5904\u4e8e\u7b49\u5f85\u4efb\u52a1\u7684\u72b6\u6001\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"885\" src=\"2026-07-25qyko2p25pvx.png\" width=\"1883\" \/>\u6d88\u606f\u6536\u53d1\u4e0e\u56de\u663e\u529f\u80fd\u6b63\u5e38\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u540e&#xff0c;\u670d\u52a1\u5668\u7684 serviceIO \u7ebf\u7a0b\u6210\u529f\u8bfb\u53d6\u4e86\u6d88\u606f\u5e76\u539f\u6837\u56de\u663e&#xff1a;\u5ba2\u6237\u7aef 1 \u53d1\u9001 aaa&#xff0c;\u6536\u5230 server echo# aaa\u3002\u5ba2\u6237\u7aef 2 \u53d1\u9001 hhh&#xff0c;\u6536\u5230 server echo# hhh\u3002 \u8fd9\u8bc1\u660e\u4e24\u4e2a\u7ebf\u7a0b\u6c60\u7684\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u90fd\u6210\u529f\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4e86\u4efb\u52a1&#xff0c;\u5e76\u5728\u540e\u53f0\u6267\u884c\u4e86 serviceIO \u51fd\u6570&#xff0c;\u903b\u8f91\u6b63\u5e38\u3002<\/p>\n<h3 id=\"%E9%95%BF%E8%BF%9E%E6%8E%A5%E5%92%8C%E7%9F%AD%E8%BF%9E%E6%8E%A5\">\u957f\u8fde\u63a5\u548c\u77ed\u8fde\u63a5<\/h3>\n<p>\u5728\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u7248\u672c\u7684\u56de\u663e\u670d\u52a1\u5668\u65f6&#xff0c;\u6211\u4eec\u901a\u5e38\u4f1a\u9047\u5230\u4e86\u4e00\u4e2a\u5178\u578b\u7684\u8bbe\u8ba1\u9009\u62e9&#xff1a;\u957f\u8fde\u63a5\u4e0e\u77ed\u8fde\u63a5\u6a21\u578b\u3002\u8fd9\u4e24\u79cd\u6a21\u578b\u7684\u6838\u5fc3\u533a\u522b&#xff0c;\u76f4\u63a5\u4f53\u73b0\u5728 serviceIO \u51fd\u6570\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e0a&#xff0c;\u4e5f\u5f71\u54cd\u4e86\u7ebf\u7a0b\u6c60\u7684\u5de5\u4f5c\u8868\u73b0\u3002<\/p>\n<p>\u5728\u5f53\u524d\u7684\u5b9e\u73b0\u4e2d&#xff0c;serviceIO \u91c7\u7528\u4e86\u957f\u8fde\u63a5\u6a21\u578b&#xff1a;\u51fd\u6570\u5185\u90e8\u901a\u8fc7 while(true) \u5faa\u73af&#xff0c;\u6301\u7eed\u4e3a\u540c\u4e00\u4e2a\u5ba2\u6237\u7aef\u670d\u52a1&#xff0c;\u76f4\u5230\u5ba2\u6237\u7aef\u4e3b\u52a8\u65ad\u5f00\u6216\u53d1\u751f\u9519\u8bef\u3002\u8fd9\u79cd\u8bbe\u8ba1\u4e0b&#xff0c;\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u4f1a\u88ab\u4e00\u4e2a\u5ba2\u6237\u7aef\u957f\u671f\u5360\u7528&#xff0c;\u540c\u6837\u5c31\u4e0d\u80fd\u88ab\u5206\u914d\u7ed9\u5176\u4ed6\u5ba2\u6237\u7aef&#xff0c;\u8fd9\u662f\u957f\u8fde\u63a5\u7684\u7279\u6027\u3002<\/p>\n<p>\u5982\u679c\u6539\u4e3a\u77ed\u8fde\u63a5\u6a21\u578b&#xff0c;\u53ea\u9700\u79fb\u9664 while(true) \u5faa\u73af&#xff0c;\u8ba9 serviceIO \u6267\u884c\u4e00\u6b21\u8bfb\u5199\u56de\u663e\u540e&#xff0c;\u4e3b\u52a8\u8c03\u7528 close(sockfd) \u5173\u95ed\u8fde\u63a5\u3002\u8fd9\u6837\u4e00\u6765&#xff0c;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\u7ebf\u7a0b\u4f1a\u7acb\u5373\u91ca\u653e&#xff0c;\u8fd4\u56de\u7ebf\u7a0b\u6c60\u7b49\u5f85\u4e0b\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u5c31\u80fd\u770b\u5230 \u201c\u4efb\u52a1\u5b8c\u6210\u201d \u7684\u65e5\u5fd7&#xff0c;\u7ebf\u7a0b\u4e5f\u80fd\u88ab\u9ad8\u6548\u590d\u7528\u3002<\/p>\n<p>\u4e24\u79cd\u6a21\u578b\u5404\u6709\u9002\u7528\u573a\u666f&#xff1a;\u957f\u8fde\u63a5\u9002\u5408\u804a\u5929\u3001\u6e38\u620f\u8fd9\u7c7b\u9700\u8981\u9891\u7e41\u4ea4\u4e92\u7684\u573a\u666f&#xff0c;\u80fd\u51cf\u5c11\u8fde\u63a5\u5efa\u7acb\u7684\u5f00\u9500&#xff1b;\u77ed\u8fde\u63a5\u5219\u66f4\u9002\u5408 HTTP \u8fd9\u7c7b\u5355\u6b21\u8bf7\u6c42 &#8211; \u54cd\u5e94\u7684\u573a\u666f&#xff0c;\u80fd\u8ba9\u7ebf\u7a0b\u8d44\u6e90\u88ab\u66f4\u9ad8\u6548\u5730\u8c03\u5ea6\u3002<\/p>\n<h3 id=\"%E4%BB%A3%E7%A0%81%3A\" style=\"background-color:transparent\">\u4ee3\u7801:<\/h3>\n<p>EchoTcpServer.hpp :<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &#034;ThreadPool.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Logger.hpp&#034;<\/p>\n<p>using namespace NS_LOG_MODULE;<br \/>\nusing namespace NS_THREAD_POOL_MODULE;<\/p>\n<p>using task_t &#061; std::function&lt;void()&gt;;<\/p>\n<p>enum<br \/>\n{<br \/>\n    SUCCESS &#061; 0,<br \/>\n    USAGE_ERR,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR,<br \/>\n    LISTEN_ERR,<br \/>\n    FORK_ERR<br \/>\n};<\/p>\n<p>static const int gbacklog &#061; 16;<br \/>\nstatic const uint16_t gport &#061; 8888;<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\npublic:<br \/>\n    TcpServer(uint16_t port &#061; gport) : _port(port)<br \/>\n    {<br \/>\n    }<br \/>\n    void InitServer()<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efasocket<br \/>\n        _listensockfd &#061; socket(AF_INET, SOCK_STREAM, 0); \/\/ TCP<br \/>\n        if (_listensockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;create socket error&#034;;<br \/>\n            exit(SOCKET_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;create socket success: &#034; &lt;&lt; _listensockfd;<br \/>\n        \/\/ 2. \u586b\u5145\u672c\u5730socket\u4fe1\u606f<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_port);<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u4efb\u610f\u5730\u5740bind<\/p>\n<p>        \/\/ 3. bind<br \/>\n        int n &#061; bind(_listensockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n            exit(BIND_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;bind socket success&#034;;<\/p>\n<p>        \/\/ 4. tcp\u662f\u9762\u5411\u8fde\u63a5\u7684&#xff0c;\u6240\u4ee5&#xff0c;TCP\u670d\u52a1\u5668\u8981\u5904\u4e8e\u4e00\u79cd\u53eb\u505alisten&#xff0c;\u76d1\u542c\u72b6\u6001\u3002<br \/>\n        n &#061; listen(_listensockfd, gbacklog);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;listen error&#034;;<br \/>\n            exit(LISTEN_ERR);<br \/>\n        }<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;listen socket success&#034;;<br \/>\n    }<br \/>\n    void serviceIO(int sockfd, InetAddr address)<br \/>\n    {<br \/>\n        \/\/ tcp sockfd, \u5168\u53cc\u5de5<br \/>\n        \/\/ \u957f\u670d\u52a1&#xff0c;\u957f\u8fde\u63a5<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; &#034;clinet info is : &#034; &lt;&lt; address.ToString();<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char inbuffer[1024] &#061; {0};<br \/>\n            \/\/ \u8bfb<br \/>\n            ssize_t n &#061; read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                inbuffer[n] &#061; 0;<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; address.ToString() &lt;&lt; &#034; say# &#034; &lt;&lt; inbuffer;<\/p>\n<p>                \/\/ \u5199<br \/>\n                std::string echo_string &#061; &#034;server echo# &#034;;<br \/>\n                echo_string &#043;&#061; inbuffer;<\/p>\n<p>                write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; &#034;client quit, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                LOG(LogLevel::ERROR) &lt;&lt; &#034;client read error, address: &#034; &lt;&lt; address.ToString();<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    class ThreadData  \/\/\u5185\u90e8\u7c7b<br \/>\n    {<br \/>\n    public:<br \/>\n        ThreadData(TcpServer *ts, int sockfd, const InetAddr &amp;addr)<br \/>\n            : _this(ts), _sockfd(sockfd), _addr(addr)<br \/>\n        {<br \/>\n        }<br \/>\n        ~ThreadData()<br \/>\n        {<br \/>\n            close(_sockfd);<br \/>\n        }<\/p>\n<p>    public:<br \/>\n        TcpServer *_this;<br \/>\n        int _sockfd;<br \/>\n        InetAddr _addr;<br \/>\n    };<\/p>\n<p>    static void *thread_routine(void *args)<br \/>\n    {<br \/>\n        ThreadData *td &#061; static_cast&lt;ThreadData *&gt;(args);<br \/>\n        pthread_detach(pthread_self());<br \/>\n        td-&gt;_this-&gt;serviceIO(td-&gt;_sockfd, td-&gt;_addr);<br \/>\n        delete td;<br \/>\n        return nullptr;<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        while (true)<br \/>\n        {<br \/>\n            struct sockaddr_in clientaddr;<br \/>\n            socklen_t len &#061; sizeof(clientaddr);<br \/>\n            \/\/ 5. \u83b7\u53d6\u8fde\u63a5<br \/>\n            int sockfd &#061; accept(_listensockfd, (struct sockaddr *)&amp;clientaddr, &amp;len);<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LogLevel::WARNING) &lt;&lt; &#034;accept errr!&#034;;<br \/>\n                continue;<br \/>\n            }<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;accept success, sockfd: &#034; &lt;&lt; sockfd;<br \/>\n            \/\/ version 4: \u63a5\u5165\u7ebf\u7a0b\u6c60<br \/>\n            InetAddr clientaddress(clientaddr);<br \/>\n            ThreadPool&lt;task_t&gt;::Instance()-&gt;Enqueue([this, sockfd, clientaddress]()-&gt;void{<br \/>\n                this-&gt;serviceIO(sockfd, clientaddress);<br \/>\n            });<br \/>\n        }<br \/>\n    }<br \/>\n    ~TcpServer()<br \/>\n    {<br \/>\n        close(_listensockfd);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t _port;<br \/>\n    \/\/ \u4e0d\u9700\u8981\u663e\u793a\u5305\u542bip<br \/>\n    int _listensockfd;<br \/>\n};<\/p>\n<p>\u5ba2\u6237\u7aef\u4ee3\u7801\u3001\u670d\u52a1\u7aef\u4e3b\u51fd\u6570\u3001\u7f51\u7edc\u8fde\u63a5\u903b\u8f91\u548c\u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO \u5747\u65e0\u9700\u4fee\u6539&#xff0c;\u4ec5\u9700\u5728\u670d\u52a1\u7aef\u5934\u6587\u4ef6\u4e2d\u5f15\u5165 ThreadPool.hpp \u5373\u53ef\u3002<\/p>\n<h2 id=\"%E4%B8%83%E3%80%81%E6%80%BB%E7%BB%93\" style=\"background-color:transparent\">\u4e03\u3001\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4eceUDP\u8fc7\u6e21\u5230TCP\u534f\u8bae\u7684\u5b66\u4e60\u8fc7\u7a0b&#xff0c;\u5bf9\u6bd4\u4e86\u4e24\u79cd\u534f\u8bae\u7684\u6838\u5fc3\u5dee\u5f02\u3002TCP\u534f\u8bae\u5177\u6709\u8fde\u63a5\u53ef\u9760\u3001\u6709\u5e8f\u4f20\u8f93\u7b49\u7279\u70b9&#xff0c;\u9002\u5408\u6b63\u5f0f\u4e1a\u52a1\u573a\u666f\u3002\u6587\u7ae0\u901a\u8fc7\u56db\u4e2a\u9636\u6bb5\u9010\u6b65\u5b9e\u73b0TCP\u670d\u52a1\u7aef&#xff1a;\u5355\u7ebf\u7a0b\u7248\u672c\u68b3\u7406\u57fa\u7840\u6d41\u7a0b&#xff1b;\u591a\u8fdb\u7a0b\u7248\u672c\u5f15\u5165\u5e76\u53d1\u5904\u7406&#xff1b;\u591a\u7ebf\u7a0b\u7248\u672c\u4f18\u5316\u8d44\u6e90\u5f00\u9500&#xff1b;\u6700\u7ec8\u91c7\u7528\u7ebf\u7a0b\u6c60\u6a21\u578b\u5b9e\u73b0\u9ad8\u6548\u53ef\u63a7\u7684\u5e76\u53d1\u3002\u6bcf\u4e2a\u9636\u6bb5\u90fd\u5305\u542b\u4ee3\u7801\u5b9e\u73b0\u548c\u8fd0\u884c\u9a8c\u8bc1&#xff0c;\u5b8c\u6574\u5c55\u73b0\u4e86TCP\u670d\u52a1\u5668\u7684\u5f00\u53d1\u6f14\u8fdb\u8def\u5f84&#xff0c;\u4e3a\u7f51\u7edc\u7f16\u7a0b\u63d0\u4f9b\u4e86\u5b9e\u7528\u53c2\u8003\u3002<\/p>\n<p>\u8c22\u8c22\u5927\u5bb6\u7684\u89c2\u770b!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n\u4e00\u3001\u4ece UDP \u8fc7\u6e21\u5230 TCP \u534f\u8bae<br \/>\n\u4e8c\u3001TCP \u7f16\u7801\u73af\u8282<br \/>\n\u4e09\u3001\u9636\u6bb5\u4e00 : \u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c<br \/>\n\u94fa\u57ab<br \/>\n\u601d\u8def\u603b\u7ed3<br \/>\nEchoTcpServer.hpp \u670d\u52a1\u7aef:<br \/>\n\u6784\u9020\u51fd\u6570<br \/>\n\u521d\u59cb\u5316\u51fd\u6570 InitServer()<br \/>\nlisten()<br \/>\n\u542f\u52a8\u4e3b\u5faa\u73af Start() \u51fd\u6570<br \/>\naccept()<br \/>\n\u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO()<br \/>\nEchoTcpServerMain.cc \u670d\u52a1\u7aef:<br \/>\nEchoTcpClient.cc \u5ba2\u6237\u7aef:<br \/>\nconnect()<br \/>\n\u8fd0\u884c\u7ed3\u679c: <\/p>\n","protected":false},"author":2,"featured_media":82176,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,205,190,78],"topic":[],"class_list":["post-82196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-tcp-ip","tag-190","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \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\/82196.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u4e00\u3001\u4ece UDP \u8fc7\u6e21\u5230 TCP \u534f\u8bae \u4e8c\u3001TCP \u7f16\u7801\u73af\u8282 \u4e09\u3001\u9636\u6bb5\u4e00 : \u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c \u94fa\u57ab \u601d\u8def\u603b\u7ed3 EchoTcpServer.hpp \u670d\u52a1\u7aef: \u6784\u9020\u51fd\u6570 \u521d\u59cb\u5316\u51fd\u6570 InitServer() listen() \u542f\u52a8\u4e3b\u5faa\u73af Start() \u51fd\u6570 accept() \u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO() EchoTcpServerMain.cc \u670d\u52a1\u7aef: EchoTcpClient.cc \u5ba2\u6237\u7aef: connect() \u8fd0\u884c\u7ed3\u679c:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82196.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:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230036-6a63ee947c2f6.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=\"20 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82196.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82196.html\",\"name\":\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T23:00:45+00:00\",\"dateModified\":\"2026-07-24T23:00:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82196.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82196.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82196.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \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\/82196.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u4e00\u3001\u4ece UDP \u8fc7\u6e21\u5230 TCP \u534f\u8bae \u4e8c\u3001TCP \u7f16\u7801\u73af\u8282 \u4e09\u3001\u9636\u6bb5\u4e00 : \u5355\u8fdb\u7a0b\/\u5355\u7ebf\u7a0b\u7248\u672c \u94fa\u57ab \u601d\u8def\u603b\u7ed3 EchoTcpServer.hpp \u670d\u52a1\u7aef: \u6784\u9020\u51fd\u6570 \u521d\u59cb\u5316\u51fd\u6570 InitServer() listen() \u542f\u52a8\u4e3b\u5faa\u73af Start() \u51fd\u6570 accept() \u4e1a\u52a1\u5904\u7406\u51fd\u6570 serviceIO() EchoTcpServerMain.cc \u670d\u52a1\u7aef: EchoTcpClient.cc \u5ba2\u6237\u7aef: connect() \u8fd0\u884c\u7ed3\u679c:","og_url":"https:\/\/www.wsisp.com\/helps\/82196.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T23:00:45+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724230036-6a63ee947c2f6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"20 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/82196.html","url":"https:\/\/www.wsisp.com\/helps\/82196.html","name":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T23:00:45+00:00","dateModified":"2026-07-24T23:00:45+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82196.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82196.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82196.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux \u7f51\u7edc\u5957\u63a5\u5b57\u7f16\u7a0b\uff08\u4e94\uff09TCP \u56de\u58f0\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff08\u5355\u8fdb\u7a0b(\u5355\u7ebf\u7a0b)\/\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\/\u7ebf\u7a0b\u6c60\u56db\u4e2a\u7248\u672c\uff09"}]},{"@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\/82196","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=82196"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82196\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/82176"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82196"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}