{"id":32413,"date":"2025-04-24T08:11:13","date_gmt":"2025-04-24T00:11:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/32413.html"},"modified":"2025-04-24T08:11:13","modified_gmt":"2025-04-24T00:11:13","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e3%80%91%e5%90%84%e7%89%88%e6%9c%actcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%9e%84%e5%bb%ba-%e4%bb%8e%e7%90%86%e8%a7%a3%e5%88%b0%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/32413.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0"},"content":{"rendered":"<p>&#x1f4e2;\u535a\u5ba2\u4e3b\u9875&#xff1a;https:\/\/blog.csdn.net\/2301_779549673 &#x1f4e2;\u535a\u5ba2\u4ed3\u5e93&#xff1a;https:\/\/gitee.com\/JohnKingW\/linux_test\/tree\/master\/lesson &#x1f4e2;\u6b22\u8fce\u70b9\u8d5e &#x1f44d; \u6536\u85cf \u2b50\u7559\u8a00 &#x1f4dd; \u5982\u6709\u9519\u8bef\u656c\u8bf7\u6307\u6b63&#xff01; &#x1f4e2;\u672c\u6587\u7531 JohnKi \u539f\u521b&#xff0c;\u9996\u53d1\u4e8e CSDN&#x1f649; &#x1f4e2;\u672a\u6765\u5f88\u957f&#xff0c;\u503c\u5f97\u6211\u4eec\u5168\u529b\u5954\u8d74\u66f4\u7f8e\u597d\u7684\u751f\u6d3b\u2728<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819f02006.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819f26257.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e00\u3001TcpServer.cpp<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001TcpServer.hpp<\/li>\n<li>\n<ul>\n<li>2.1 \u679a\u4e3e\u9519\u8bef\u60c5\u51b5<\/li>\n<li>2.2 \u57fa\u672c\u7ed3\u6784<\/li>\n<li>2.3 \u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570<\/li>\n<li>2.4 \u521d\u59cb\u5316\u65b9\u6cd5<\/li>\n<li>2.5 \u5faa\u73af\u76d1\u542c<\/li>\n<li>\n<ul>\n<li>2.5.1 server 0 &#8211; \u5355\u6267\u884c\u6d41\u7248\u672c<\/li>\n<li>2.5.2 server 1 &#8211; \u591a\u8fdb\u7a0b\u7248\u672c<\/li>\n<li>2.5.3 server 2 &#8211; \u591a\u7ebf\u7a0b\u7248\u672c<\/li>\n<li>2.5.4 server 3 &#8211; \u5185\u5b58\u6c60<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001TcpClient.cpp<\/li>\n<li>&#x1f465;\u603b\u7ed3<\/li>\n<\/ul>\n<hr \/>\n<p><font color=\"#F09B59\">\u524d\u9762\u51e0\u7bc7\u6587\u7ae0\u4e2d\u4f7f\u7528UDP\u534f\u8bae\u5b9e\u73b0\u4e86\u76f8\u5173\u529f\u80fd<\/font> &#xff0c;<font color=\"#EB3324\">\u8fd9\u7bc7\u4f7f\u7528TCP\u534f\u8bae\u5b9e\u73b0\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u901a\u4fe1<\/font><\/p>\n<p>\u76f8\u6bd4\u4e0eUDP\u534f\u8bae&#xff0c;TCP\u534f\u8bae\u66f4\u52a0\u53ef\u9760&#xff0c;\u4e5f\u66f4\u52a0\u590d\u6742&#xff01;\u4e0eUDP\u7c7b\u4f3c&#xff0c;\u6211\u4eec\u5148\u5199\u4e3b\u51fd\u6570&#xff0c;\u7136\u540e\u5b9e\u73b0\u76f8\u5173\u51fd\u6570&#xff01;<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e00\u3001TcpServer.cpp<\/h2>\n<p><font color=\"#7E84F7\">\u670d\u52a1\u7aef\u4e3b\u51fd\u6570\u4f7f\u7528\u667a\u80fd\u6307\u9488\u6784\u9020Server\u5bf9\u8c61&#xff0c;\u7136\u540e\u8c03\u7528\u521d\u59cb\u5316\u4e0e\u6267\u884c\u51fd\u6570&#xff0c;\u8c03\u7528\u4e3b\u51fd\u6570\u4f7f\u7528\u8be5\u53ef\u6267\u884c\u7a0b\u5e8f &#043; \u7aef\u53e3\u53f7&#xff01;<\/font><\/p>\n<p>#include &#034;TcpServer.hpp&#034;<\/p>\n<p>int main(int argc, char* argv[]){<br \/>\n    if(argc !&#061; 2){<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; port&#034; &lt;&lt; std::endl;<br \/>\n        Die(1);<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;(port);<\/p>\n<p>    tsvr-&gt;InitServer();<br \/>\n    tsvr-&gt;Loop();<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001TcpServer.hpp<\/h2>\n<h3>2.1 \u679a\u4e3e\u9519\u8bef\u60c5\u51b5<\/h3>\n<p><font color=\"#7E84F7\">\u4e0e UDP \u540c\u6837\u7684&#xff0c;\u6211\u4eec\u5148\u679a\u4e3e\u9519\u8bef\u60c5\u51b5<\/font>&#xff0c;\u5c06\u5176\u653e\u5728 common.hpp \u4e2d<\/p>\n<p>enum {<br \/>\n    USAGE_ERR &#061; 1,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR,<br \/>\n    LISTEN_ERR<br \/>\n};<\/p>\n<h3>2.2 \u57fa\u672c\u7ed3\u6784<\/h3>\n<p><font color=\"#F09B59\">\u6211\u4eec\u8fd9\u91cc\u5148\u5b9e\u73b0\u6700\u57fa\u672c\u7684 TCP \u670d\u52a1\u5668&#xff0c;<\/font><font color=\"#7E84F7\">\u57fa\u672c\u6210\u5458\u6709\u7aef\u53e3\u53f7&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4e0e\u8fd0\u884c\u72b6\u6001<\/font><\/p>\n<p>class TcpServer{<br \/>\n    public:<br \/>\n        TcpServer(){}<br \/>\n        void InitServer(){}<br \/>\n        void Loop(int sockfd){}<br \/>\n        ~TcpServer(){}<\/p>\n<p>    private:<br \/>\n        int _listensockfd; \/\/ \u76d1\u542csocket<br \/>\n        uint16_t _port;<br \/>\n        bool _isrunning;<br \/>\n};<\/p>\n<h3>2.3 \u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570<\/h3>\n<p><font color=\"#7E84F7\">\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf&#xff0c;\u6790\u6784\u51fd\u6570\u65e0\u9700\u5904\u7406&#xff01;<\/font><\/p>\n<ul>\n<li>\u8fd9\u91cc\u9700\u8981\u4e3a\u7aef\u53e3\u53f7\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c &#8211; 8080<\/li>\n<\/ul>\n<p>static const uint16_t gport &#061; 8080;<br \/>\n\/\/ \u6784\u9020\u51fd\u6570<br \/>\nTcpServer(uint16_t port &#061; gport)<br \/>\n    :_port(port),_sockfd(gsockfd),_isrunning(false){}<br \/>\n\/\/ \u6790\u6784\u51fd\u6570<br \/>\n~TcpServer(){}<\/p>\n<h3>2.4 \u521d\u59cb\u5316\u65b9\u6cd5<\/h3>\n<p>\u521d\u59cb\u5316\u51fd\u6570\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65&#xff1a;<\/p>\n<li>\u521b\u5efa socket (\u7c7b\u578b\u4e0eUDP\u4e0d\u540c)<\/li>\n<li>bind sockfd \u548c socket addr<\/li>\n<li>\u83b7\u53d6\u8fde\u63a5(\u4e0eUDP\u4e0d\u540c)<\/li>\n<p>\u83b7\u53d6\u8fde\u63a5\u9700\u8981\u4f7f\u7528 listen \u51fd\u6570(<font color=\"#EA3680\">\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u76d1\u542c\u6a21\u5f0f&#xff0c;\u4ee5\u4fbf\u80fd\u591f\u63a5\u53d7\u8fdb\u5165\u7684\u8fde\u63a5\u8bf7\u6c42<\/font>)<\/p>\n<p>\u00b7listen\u00b7 <font color=\"#F09B59\">\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u961f\u5217&#xff0c;\u7528\u6765\u4fdd\u5b58\u7b49\u5f85\u8fde\u63a5\u5730\u5ba2\u6237\u7aef<\/font>&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4e8b\u5148\u8bbe\u7f6e\u4e00\u4e2a #define BACKLOG 8 \u6765\u5b9a\u4e49\u8fd9\u4e2a\u961f\u5217\u957f\u5ea6<\/p>\n<p>void InitServer() {<br \/>\n    \/\/ 1. \u521b\u5efa socket<br \/>\n    _listensockfd &#061; ::socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (_listensockfd &lt; 0) {<br \/>\n        LOG(LogLevel::ERROR) &lt;&lt; &#034;create socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n        Die(2);<br \/>\n    }<br \/>\n    LOG(LogLevel::INFO) &lt;&lt; &#034;create sockfd success: &#034; &lt;&lt; _listensockfd;<\/p>\n<p>    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; htonl(INADDR_ANY);<\/p>\n<p>    \/\/ 2. \u7ed1\u5b9a socket<br \/>\n    if (::bind(_listensockfd, CONV(&amp;local), sizeof(local)) &lt; 0) {<br \/>\n        LOG(LogLevel::ERROR) &lt;&lt; &#034;bind socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n        Die(3);<br \/>\n    }<br \/>\n    LOG(LogLevel::INFO) &lt;&lt; &#034;bind sockfd success: &#034; &lt;&lt; _listensockfd;<\/p>\n<p>    \/\/ 3. \u56e0\u4e3a tcp \u662f\u9762\u5411\u8fde\u63a5\u7684&#xff0c;tcp\u9700\u8981\u672a\u6765\u4e0d\u65ad\u5730\u83b7\u53d6\u8fde\u63a5<br \/>\n    \/\/ listen \u5c31\u662f\u76d1\u542c\u8fde\u63a5\u7684\u610f\u601d&#xff0c;\u6240\u4ee5\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u961f\u5217&#xff0c;\u6765\u4fdd\u5b58\u7b49\u5f85\u8fde\u63a5\u7684\u5ba2\u6237\u7aef<br \/>\n    \/\/ \u961f\u5217\u7684\u957f\u5ea6\u4e3a 8&#xff0c;\u8868\u793a\u6700\u591a\u53ef\u4ee5\u6709 8 \u4e2a\u5ba2\u6237\u7aef\u7b49\u5f85\u8fde\u63a5<br \/>\n    \/\/ listen(int sockfd, int backlog)<br \/>\n    \/\/ sockfd \u5c31\u662f\u4e4b\u524d\u521b\u5efa\u7684 socket \u53e5\u67c4<br \/>\n    \/\/ backlog \u5c31\u662f\u961f\u5217\u7684\u957f\u5ea6<br \/>\n    \/\/ \u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff0c;\u5931\u8d25\u8fd4\u56de -1<br \/>\n    if (::listen(_listensockfd, BACKLOG) &lt; 0) {<br \/>\n        LOG(LogLevel::ERROR) &lt;&lt; &#034;listen socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n        Die(4);<br \/>\n    }<br \/>\n    LOG(LogLevel::INFO) &lt;&lt; &#034;listen sockfd success: &#034; &lt;&lt; _listensockfd;<br \/>\n}<\/p>\n<p><font color=\"#EA3680\">\u6211\u4eec\u5148\u5c06 Loop() \u51fd\u6570\u8bbe\u8ba1\u6210\u6b7b\u5faa\u73af&#xff0c;\u9a8c\u8bc1\u4e00\u4e0b \u521d\u59cb\u5316\u51fd\u6570 \u7684\u6b63\u786e\u6027<\/font><\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nvoid Loop() {<br \/>\n    _isrunning &#061; true;<br \/>\n    while (_isrunning) {<br \/>\n        sleep(1);<br \/>\n    }<br \/>\n    _isrunning &#061; false;<br \/>\n}<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819f4d83c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>2.5 \u5faa\u73af\u76d1\u542c<\/h3>\n<p>\u6267\u884c\u670d\u52a1\u51fd\u6570\u4e3b\u8981\u5206\u4e3a\u4e24\u6b65&#xff1a;<\/p>\n<li><font color=\"#7E84F7\">\u83b7\u53d6\u65b0\u8fde\u63a5<\/font> ( accept\u51fd\u6570 [<font color=\"#F09B59\">\u4ece\u5df2\u5b8c\u6210\u8fde\u63a5\u961f\u5217\u7684\u5934\u90e8\u8fd4\u56de\u4e0b\u4e00\u4e2a\u5df2\u5b8c\u6210\u8fde\u63a5&#xff0c;\u5982\u679c\u961f\u5217\u4e3a\u7a7a&#xff0c;\u5219\u963b\u585e\u8c03\u7528\u8fdb\u7a0b<\/font>])<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819fb0382.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> 2. <font color=\"#7E84F7\">\u6267\u884c\u670d\u52a1<\/font> (\u524d\u63d0\u662f\u83b7\u53d6\u5230\u65b0\u8fde\u63a5)<\/p>\n<h4>2.5.1 server 0 &#8211; \u5355\u6267\u884c\u6d41\u7248\u672c<\/h4>\n<p>\u5de5\u4f5c\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li><font color=\"#7E84F7\">\u5355\u7ebf\u7a0b\u901a\u8fc7while(1)\u5faa\u73af\u4f9d\u6b21\u5904\u7406\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff0c;\u5904\u7406\u5b8c\u4e00\u4e2a\u8bf7\u6c42\u540e\u624d\u80fd\u5904\u7406\u4e0b\u4e00\u4e2a\u3002<\/font><\/li>\n<\/ul>\n<p><font color=\"#EB3324\">\u6ce8\u610f&#xff1a;tcp\u534f\u8bae\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528read&#xff0c;write\u51fd\u6570\u8bfb\u5199\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5185\u5bb9(\u56e0\u4e3atcp\u662f\u9762\u5411\u5b57\u8282\u6d41\u7684)&#xff01;<\/font><\/p>\n<p>\/\/ server &#8211; 0 \u5355\u6267\u884c\u6d41\u7248\u672c<br \/>\n\/\/ \u200b\u5de5\u4f5c\u673a\u5236&#xff1a;\u5355\u7ebf\u7a0b\u901a\u8fc7while(1)\u5faa\u73af\u4f9d\u6b21\u5904\u7406\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff0c;\u5904\u7406\u5b8c\u4e00\u4e2a\u8bf7\u6c42\u540e\u624d\u80fd\u5904\u7406\u4e0b\u4e00\u4e2a\u3002<br \/>\nvoid Loop() {<br \/>\n    _isrunning &#061; true;<br \/>\n    while (_isrunning) {<br \/>\n        struct sockaddr_in client;<br \/>\n        socklen_t len &#061; sizeof(client);<\/p>\n<p>        \/\/ 1. \u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n        int sockfd &#061; ::accept(_listensockfd, CONV(&amp;client), &amp;len);<br \/>\n        if (sockfd &lt; 0) {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;accept socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            continue;<br \/>\n        }<br \/>\n        InetAddr cli(client);<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;accept new connection from &#034; &lt;&lt; cli.AddrStr()<br \/>\n                            &lt;&lt; &#034; sockfd: &#034; &lt;&lt; sockfd;<\/p>\n<p>        \/\/ \u83b7\u53d6\u6210\u529f<br \/>\n        Server(sockfd, cli);<br \/>\n    }<br \/>\n}<br \/>\nvoid Server(int sockfd, InetAddr&amp; cli) {<br \/>\n    \/\/ \u957f\u670d\u52a1<br \/>\n    while (true) {<br \/>\n        char inbuffer[1024]; \/\/ \u5f53\u4f5c\u5b57\u7b26\u4e32<br \/>\n        \/\/ 1. \u8bfb\u6587\u4ef6<br \/>\n        \/\/ read<br \/>\n        \/\/ \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7f13\u51b2\u533a&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7684\u5b57\u8282\u6570<br \/>\n        \/\/ \u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u8bfb\u5165\u7684\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de -1<br \/>\n        ssize_t n &#061; ::read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n        if (n &gt; 0) {<br \/>\n            inbuffer[n] &#061; 0;<br \/>\n            LOG(LogLevel::INFO)<br \/>\n                &lt;&lt; &#034;get msg from &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; msg: &#034; &lt;&lt; inbuffer;<br \/>\n            std::string echo_string &#061; &#034;[server echo]# &#034;;<br \/>\n            echo_string &#043;&#061; inbuffer;<\/p>\n<p>            \/\/ 2. \u5199\u6587\u4ef6<br \/>\n            write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n        } else if (n &#061;&#061; 0) {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; closed&#034;;<br \/>\n            break;<br \/>\n        } else {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;read socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    ::close(sockfd);<br \/>\n}<\/p>\n<h4>2.5.2 server 1 &#8211; \u591a\u8fdb\u7a0b\u7248\u672c<\/h4>\n<p>\u200b\u5de5\u4f5c\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li><font color=\"#7E84F7\">\u7236\u8fdb\u7a0b\u901a\u8fc7fork()\u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u5904\u7406\u5b8c\u8bf7\u6c42\u540e\u9000\u51fa\u3002<\/font><\/li>\n<li><font color=\"#7E84F7\">\u4f46\u662f\u8fdb\u7a0b\u521b\u5efa\/\u9500\u6bc1\u5f00\u9500\u5927&#xff0c;\u200b\u9ad8\u5e76\u53d1\u65f6\u8d44\u6e90\u8017\u5c3d\u200b<\/font><\/li>\n<\/ul>\n<p>void Loop() {<br \/>\n    _isrunning &#061; true;<br \/>\n    while (_isrunning) {<br \/>\n        struct sockaddr_in client;<br \/>\n        socklen_t len &#061; sizeof(client);<\/p>\n<p>        \/\/ 1. \u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n        int sockfd &#061; ::accept(_listensockfd, CONV(&amp;client), &amp;len);<br \/>\n        if (sockfd &lt; 0) {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;accept socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            continue;<br \/>\n        }<br \/>\n        InetAddr cli(client);<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;accept new connection from &#034; &lt;&lt; cli.AddrStr()<br \/>\n                            &lt;&lt; &#034; sockfd: &#034; &lt;&lt; sockfd;<\/p>\n<p>        \/\/ \u83b7\u53d6\u6210\u529f<br \/>\n        pid_t id &#061; fork();<br \/>\n        if (id &#061;&#061; 0) {              \/\/ \u5b50\u8fdb\u7a0b<br \/>\n            ::close(_listensockfd); \/\/ \u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57&#xff08;\u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542c&#xff09;<\/p>\n<p>            \/\/ \u5b59\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa<br \/>\n            if (fork() &gt; 0)<br \/>\n                exit(0); \/\/ \u7236\u8fdb\u7a0b&#xff08;\u5b50\u8fdb\u7a0b&#xff09;\u9000\u51fa&#xff0c;\u5b59\u5b50\u8fdb\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/p>\n<p>            \/\/ \u5b59\u5b50\u8fdb\u7a0b\u6267\u884c\u5b9e\u9645\u670d\u52a1\u903b\u8f91<br \/>\n            Server(sockfd, cli);<br \/>\n            exit(0);<br \/>\n        }<br \/>\n        \/\/ \u7236\u8fdb\u7a0b&#xff08;\u4e3b\u8fdb\u7a0b&#xff09;<br \/>\n        ::close(sockfd); \/\/ \u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57&#xff08;\u7236\u8fdb\u7a0b\u4e0d\u5904\u7406\u5177\u4f53\u4e1a\u52a1&#xff09;<br \/>\n        int n &#061; waitpid(id, nullptr, 0); \/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u9000\u51fa<br \/>\n        if (n &gt; 0) {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;wait chid success&#034;;<br \/>\n        }<br \/>\n    }<br \/>\n    _isrunning &#061; false;<br \/>\n}<br \/>\nvoid Server(int sockfd, InetAddr&amp; cli) {<br \/>\n    \/\/ \u957f\u670d\u52a1<br \/>\n    while (true) {<br \/>\n        char inbuffer[1024]; \/\/ \u5f53\u4f5c\u5b57\u7b26\u4e32<br \/>\n        \/\/ 1. \u8bfb\u6587\u4ef6<br \/>\n        \/\/ read<br \/>\n        \/\/ \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7f13\u51b2\u533a&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7684\u5b57\u8282\u6570<br \/>\n        \/\/ \u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u8bfb\u5165\u7684\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de -1<br \/>\n        ssize_t n &#061; ::read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n        if (n &gt; 0) {<br \/>\n            inbuffer[n] &#061; 0;<br \/>\n            LOG(LogLevel::INFO)<br \/>\n                &lt;&lt; &#034;get msg from &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; msg: &#034; &lt;&lt; inbuffer;<br \/>\n            std::string echo_string &#061; &#034;[server echo]# &#034;;<br \/>\n            echo_string &#043;&#061; inbuffer;<\/p>\n<p>            \/\/ 2. \u5199\u6587\u4ef6<br \/>\n            write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n        } else if (n &#061;&#061; 0) {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; closed&#034;;<br \/>\n            break;<br \/>\n        } else {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;read socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    ::close(sockfd);<br \/>\n}<\/p>\n<p>\u53cc\u91cd fork() \u7684\u6838\u5fc3\u76ee\u7684<\/p>\n<li>\u200b\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u200b\n<ul>\n<li><font color=\"#7E84F7\">\u82e5\u5b50\u8fdb\u7a0b\u76f4\u63a5\u5904\u7406\u8bf7\u6c42\u5e76\u9000\u51fa&#xff0c;\u7236\u8fdb\u7a0b\u9700\u901a\u8fc7 waitpid \u56de\u6536\u8d44\u6e90&#xff0c;\u5426\u5219\u5b50\u8fdb\u7a0b\u4f1a\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b\u3002<\/font><\/li>\n<li><font color=\"#7E84F7\">\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b&#xff0c;\u7236\u8fdb\u7a0b\u53ef\u80fd\u56e0\u9891\u7e41\u8c03\u7528 waitpid \u800c\u963b\u585e&#xff0c;\u65e0\u6cd5\u53ca\u65f6\u5904\u7406\u65b0\u8fde\u63a5\u3002<\/font><\/li>\n<\/ul>\n<\/li>\n<ul>\n<li>\u200b\u89e3\u51b3\u65b9\u6848&#xff1a;\n<ul>\n<li><font color=\"#F09B59\">\u5b50\u8fdb\u7a0b\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b\u540e\u7acb\u5373\u9000\u51fa&#xff0c;\u5b59\u5b50\u8fdb\u7a0b\u6210\u4e3a \u200b\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u7531 init \u8fdb\u7a0b&#xff08;PID&#061;1&#xff09;\u63a5\u7ba1\u5e76\u81ea\u52a8\u56de\u6536\u8d44\u6e90\u3002<\/font><\/li>\n<li><font color=\"#F09B59\">\u7236\u8fdb\u7a0b\u53ea\u9700\u7b49\u5f85\u5b50\u8fdb\u7a0b&#xff08;\u77ed\u6682\u5b58\u5728&#xff09;\u9000\u51fa&#xff0c;\u907f\u514d\u963b\u585e\u3002<\/font><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>\u7236\u8fdb\u7a0b\u5feb\u901f\u56de\u5230\u4e3b\u5faa\u73af\n<ul>\n<li><font color=\"#7E84F7\">\u7236\u8fdb\u7a0b\u7684 waitpid \u4ec5\u9700\u7b49\u5f85\u5b50\u8fdb\u7a0b&#xff08;\u800c\u975e\u5b59\u5b50\u8fdb\u7a0b&#xff09;&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa\u901f\u5ea6\u6781\u5feb&#xff08;\u4ec5\u6267\u884c fork() \u548c exit()&#xff09;&#xff0c;\u7236\u8fdb\u7a0b\u8fc5\u901f\u8fd4\u56de accept \u5faa\u73af\u3002<\/font><\/li>\n<li><font color=\"#7E84F7\">\u63d0\u5347\u670d\u52a1\u5668\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/font><\/li>\n<\/ul>\n<\/li>\n<h4>2.5.3 server 2 &#8211; \u591a\u7ebf\u7a0b\u7248\u672c<\/h4>\n<p>\u200b\u5de5\u4f5c\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li><font color=\"#7E84F7\">\u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u5206\u914d\u72ec\u7acb\u7684\u7ebf\u7a0b\u5904\u7406\u4e1a\u52a1\u903b\u8f91<\/font><\/li>\n<\/ul>\n<li>\u4e3b\u7ebf\u7a0b&#xff08;Loop \u51fd\u6570&#xff09;\u200b\u901a\u8fc7 accept \u5faa\u73af\u76d1\u542c\u65b0\u8fde\u63a5&#xff0c;\u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u521b\u5efa\u7ebf\u7a0b&#xff08;pthread_create&#xff09;\u4f20\u9012\u8fde\u63a5\u4fe1\u606f\u7ed9\u5b50\u7ebf\u7a0b&#xff08;\u901a\u8fc7 ThreadDate \u7ed3\u6784\u4f53&#xff09;<\/li>\n<li>\u5b50\u7ebf\u7a0b&#xff08;Execute \u51fd\u6570&#xff09;\u200b\u200b\u8c03\u7528 pthread_detach \u5206\u79bb\u81ea\u8eab&#xff08;\u907f\u514d\u4e3b\u7ebf\u7a0b\u8c03\u7528 pthread_join&#xff09;\u6267\u884c Server \u51fd\u6570\u5904\u7406\u5177\u4f53\u4e1a\u52a1\u903b\u8f91\u7ebf\u7a0b\u7ed3\u675f\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90&#xff08;\u901a\u8fc7 delete td&#xff09;<\/li>\n<li>\u4e1a\u52a1\u5904\u7406&#xff08;Server \u51fd\u6570&#xff09;\u200b\u5faa\u73af\u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e&#xff08;read&#xff09;&#xff0c;\u5904\u7406\u4e1a\u52a1\u903b\u8f91&#xff08;\u793a\u4f8b\u4e2d\u7684\u56de\u663e\u670d\u52a1&#xff09;&#xff0c;\u53d1\u9001\u54cd\u5e94&#xff08;write&#xff09;\u5173\u95ed\u8fde\u63a5&#xff08;close(sockfd)&#xff09;<\/li>\n<p>        \/\/ server &#8211; 2 \u591a\u7ebf\u7a0b\u7248\u672c<br \/>\n        \/\/ \u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u5206\u914d\u72ec\u7acb\u7684\u7ebf\u7a0b\u5904\u7406\u4e1a\u52a1\u903b\u8f91<br \/>\n        \/\/ &#xff08;Loop \u51fd\u6570&#xff09;\u901a\u8fc7 accept \u5faa\u73af\u76d1\u542c\u65b0\u8fde\u63a5&#xff0c;\u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u521b\u5efa\u7ebf\u7a0b&#xff08;pthread_create&#xff09;\u4f20\u9012\u8fde\u63a5\u4fe1\u606f\u7ed9\u5b50\u7ebf\u7a0b&#xff08;\u901a\u8fc7 ThreadDate \u7ed3\u6784\u4f53&#xff09;<br \/>\n        \/\/ &#xff08;Execute \u51fd\u6570&#xff09;\u200b\u200b\u8c03\u7528 pthread_detach \u5206\u79bb\u81ea\u8eab&#xff08;\u907f\u514d\u4e3b\u7ebf\u7a0b\u8c03\u7528 pthread_join&#xff09;\u6267\u884c Server \u51fd\u6570\u5904\u7406\u5177\u4f53\u4e1a\u52a1\u903b\u8f91\u7ebf\u7a0b\u7ed3\u675f\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90&#xff08;\u901a\u8fc7 delete td&#xff09;<br \/>\n        \/\/ &#xff08;Server \u51fd\u6570&#xff09;\u200b\u5faa\u73af\u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e&#xff08;read&#xff09;&#xff0c;\u5904\u7406\u4e1a\u52a1\u903b\u8f91&#xff08;\u793a\u4f8b\u4e2d\u7684\u56de\u663e\u670d\u52a1&#xff09;&#xff0c;\u53d1\u9001\u54cd\u5e94&#xff08;write&#xff09;\u5173\u95ed\u8fde\u63a5&#xff08;close(sockfd)&#xff09;<br \/>\n        void Loop(){<br \/>\n            _isrunning &#061; true;<br \/>\n            while(_isrunning){<br \/>\n                struct sockaddr_in client;<br \/>\n                socklen_t len &#061; sizeof(client);<\/p>\n<p>                \/\/ 1. \u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n                int sockfd &#061; ::accept(_listensockfd, CONV(&amp;client), &amp;len);<br \/>\n                if(sockfd &lt; 0){<br \/>\n                    LOG(LogLevel::ERROR) &lt;&lt; &#034;accept socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n                    continue;<br \/>\n                }<br \/>\n                InetAddr cli(client);<br \/>\n                LOG(LogLevel::INFO) &lt;&lt; &#034;accept new connection from &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; sockfd: &#034; &lt;&lt; sockfd;<\/p>\n<p>                \/\/ \u83b7\u53d6\u6210\u529f<br \/>\n                pthread_t tid;<br \/>\n                ThreadDate* td &#061; new ThreadDate(sockfd, this, cli);<br \/>\n                \/\/ pthread_create \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0bid&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u5c5e\u6027&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u51fd\u6570&#xff0c;\u7b2c\u56db\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u51fd\u6570\u53c2\u6570<br \/>\n                pthread_create(&amp;tid, nullptr, Execute, td);<br \/>\n            }<br \/>\n            _isrunning &#061; false;<br \/>\n        }<br \/>\n        \/\/ \u7ebf\u7a0b\u51fd\u6570\u53c2\u6570\u5bf9\u8c61<br \/>\n        class ThreadDate{<br \/>\n            public:<br \/>\n                int _sockfd;<br \/>\n                TcpServer* _self;<br \/>\n                InetAddr _addr;<br \/>\n            public:<br \/>\n                ThreadDate(int sockfd, TcpServer* self, const InetAddr&amp; addr)<br \/>\n                    : _sockfd(sockfd), _self(self), _addr(addr)<br \/>\n                {}<br \/>\n        };<br \/>\n        \/\/ \u7ebf\u7a0b\u51fd\u6570<br \/>\n        static void* Execute(void* args){<br \/>\n            ThreadDate* td &#061; static_cast&lt;ThreadDate*&gt;(args);<br \/>\n            \/\/ \u5b50\u7ebf\u7a0b\u7ed3\u675f\u540e\u7531\u7cfb\u7edf\u81ea\u52a8\u56de\u6536\u8d44\u6e90&#xff0c;\u65e0\u9700\u4e3b\u7ebf\u7a0b\u8c03\u7528 pthread_join<br \/>\n            pthread_detach(pthread_self()); \/\/ \u5206\u79bb\u65b0\u7ebf\u7a0b&#xff0c;\u65e0\u9700\u4e3b\u7ebf\u7a0b\u56de\u6536<br \/>\n            td-&gt;_self-&gt;Server(td-&gt;_sockfd, td-&gt;_addr);<br \/>\n            delete td;<br \/>\n            return nullptr;<br \/>\n        }<br \/>\n        void Server(int sockfd, InetAddr&amp; cli){<br \/>\n            \/\/ \u957f\u670d\u52a1<br \/>\n            while(true){<br \/>\n                char inbuffer[1024]; \/\/ \u5f53\u4f5c\u5b57\u7b26\u4e32<br \/>\n                \/\/ 1. \u8bfb\u6587\u4ef6<br \/>\n                \/\/ read \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7f13\u51b2\u533a&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7684\u5b57\u8282\u6570<br \/>\n                \/\/ \u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u8bfb\u5165\u7684\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de -1<br \/>\n                ssize_t n &#061; ::read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n                if(n &gt; 0){<br \/>\n                    inbuffer[n] &#061; 0;<br \/>\n                    LOG(LogLevel::INFO) &lt;&lt; &#034;get msg from &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; msg: &#034; &lt;&lt; inbuffer;<br \/>\n                    std::string echo_string &#061; &#034;[server echo]# &#034;;<br \/>\n                    echo_string &#043;&#061; inbuffer;<\/p>\n<p>                    \/\/ 2. \u5199\u6587\u4ef6<br \/>\n                    write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n                }<br \/>\n                else if(n &#061;&#061; 0){<br \/>\n                    LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; closed&#034;;<br \/>\n                    break;<br \/>\n                }<br \/>\n                else{<br \/>\n                    LOG(LogLevel::ERROR) &lt;&lt; &#034;read socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            ::close(sockfd);<br \/>\n        }<\/p>\n<h4>2.5.4 server 3 &#8211; \u5185\u5b58\u6c60<\/h4>\n<p>\u5c06\u6267\u884c\u670d\u52a1\u7684\u51fd\u6570\u5165\u7ebf\u7a0b\u6c60\u961f\u5217&#xff0c;\u8be5\u51fd\u6570\u9700\u8981\u662f\u53c2\u6570\u4e3a\u7a7a\u548c\u8fd4\u56de\u503c\u4e3avoid\u7684\u51fd\u6570&#xff0c;\u56e0\u6b64\u9700\u8981bind\u7ed1\u5b9a\u51fd\u6570&#xff01;<\/p>\n<p>using task_t &#061; std::function&lt;void(int sockfd, InetAddr&amp; cli)&gt;;<\/p>\n<p>\/\/ server &#8211; 3 \u5185\u5b58\u6c60\u7248\u672c<br \/>\nvoid Loop() {<br \/>\n    _isrunning &#061; true;<br \/>\n    while (_isrunning) {<br \/>\n        struct sockaddr_in client;<br \/>\n        socklen_t len &#061; sizeof(client);<\/p>\n<p>        \/\/ 1. \u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n        int sockfd &#061; ::accept(_listensockfd, CONV(&amp;client), &amp;len);<br \/>\n        if (sockfd &lt; 0) {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;accept socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            continue;<br \/>\n        }<br \/>\n        InetAddr cli(client);<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;accept new connection from &#034; &lt;&lt; cli.AddrStr()<br \/>\n                            &lt;&lt; &#034; sockfd: &#034; &lt;&lt; sockfd;<\/p>\n<p>        \/\/ \u83b7\u53d6\u6210\u529f<br \/>\n        task_t task &#061; std::bind(&amp;TcpServer::Server, this, sockfd, cli);<br \/>\n        ThreadPool&lt;task_t&gt;::getInstance()-&gt;Equeue(task);<br \/>\n    }<br \/>\n    _isrunning &#061; false;<br \/>\n}<br \/>\nvoid Server(int sockfd, InetAddr&amp; cli) {<br \/>\n    \/\/ \u957f\u670d\u52a1<br \/>\n    while (true) {<br \/>\n        char inbuffer[1024]; \/\/ \u5f53\u4f5c\u5b57\u7b26\u4e32<br \/>\n        \/\/ 1. \u8bfb\u6587\u4ef6<br \/>\n        \/\/ read<br \/>\n        \/\/ \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7f13\u51b2\u533a&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8bfb\u5165\u7684\u5b57\u8282\u6570<br \/>\n        \/\/ \u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u8bfb\u5165\u7684\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de -1<br \/>\n        ssize_t n &#061; ::read(sockfd, inbuffer, sizeof(inbuffer) &#8211; 1);<br \/>\n        if (n &gt; 0) {<br \/>\n            inbuffer[n] &#061; 0;<br \/>\n            LOG(LogLevel::INFO)<br \/>\n                &lt;&lt; &#034;get msg from &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; msg: &#034; &lt;&lt; inbuffer;<br \/>\n            std::string echo_string &#061; &#034;[server echo]# &#034;;<br \/>\n            echo_string &#043;&#061; inbuffer;<\/p>\n<p>            \/\/ 2. \u5199\u6587\u4ef6<br \/>\n            write(sockfd, echo_string.c_str(), echo_string.size());<br \/>\n        } else if (n &#061;&#061; 0) {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; cli.AddrStr() &lt;&lt; &#034; closed&#034;;<br \/>\n            break;<br \/>\n        } else {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;read socket error: &#034; &lt;&lt; strerror(errno);<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    ::close(sockfd);<br \/>\n}<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001TcpClient.cpp<\/h2>\n<li>\u200b\u521b\u5efaSocket,\u8c03\u7528 socket(AF_INET, SOCK_STREAM, 0) \u521b\u5efaTCP\u5957\u63a5\u5b57\u3002\u5931\u8d25\u65f6\u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa\u3002<\/li>\n<li>\u200b\u8bbe\u7f6e\u670d\u52a1\u5668\u5730\u5740&#xff0c;\u901a\u8fc7 sockaddr_in \u7ed3\u6784\u6307\u5b9a\u670d\u52a1\u5668\u7684IP\u548c\u7aef\u53e3\u3002inet_pton \u5c06\u5b57\u7b26\u4e32IP\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u3002<\/li>\n<li>\u5efa\u7acb\u8fde\u63a5&#xff0c;\u8c03\u7528 connect \u4e3b\u52a8\u8fde\u63a5\u670d\u52a1\u5668\u3002\u5931\u8d25\u65f6\u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa\u3002<\/li>\n<li>\u200b\u4ea4\u4e92\u5f0f\u901a\u4fe1&#xff0c;\u5faa\u73af\u8bfb\u53d6\u7528\u6237\u8f93\u5165&#xff08;\u5982 hello&#xff09;\u3002\u901a\u8fc7 write \u53d1\u9001\u6d88\u606f\u5230\u670d\u52a1\u5668\u3002\u901a\u8fc7 read \u8bfb\u53d6\u670d\u52a1\u5668\u56de\u663e\u7684\u6d88\u606f\u5e76\u663e\u793a\u3002\u82e5\u670d\u52a1\u5668\u65ad\u5f00\u6216\u8bfb\u5931\u8d25&#xff0c;\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li>\u200b\u5173\u95ed\u8fde\u63a5&#xff0c;\u8c03\u7528 close \u5173\u95ed\u5957\u63a5\u5b57\u3002<\/li>\n<p>#include &#034;TcpClient.hpp&#034;<\/p>\n<p>int main(int argc, char* argv[]){<br \/>\n    if(argc !&#061; 3){<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; &lt;server_ip&gt; &lt;server_port&gt;&#034; &lt;&lt; std::endl;<br \/>\n        Die(1);<br \/>\n    }<\/p>\n<p>    std::string serverip &#061; argv[1];<br \/>\n    uint16_t serverport &#061; std::stoi(argv[2]);<\/p>\n<p>    \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        \/\/ AF_INET: IPv4\u534f\u8bae<br \/>\n        \/\/ SOCK_STREAM: TCP\u534f\u8bae<br \/>\n        \/\/ 0: \u8868\u793a\u9ed8\u8ba4\u534f\u8bae<br \/>\n    int sockfd &#061; ::socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if(sockfd &lt; 0){<br \/>\n        std::cerr &lt;&lt; &#034;Create socket error: &#034; &lt;&lt; std::strerror(errno) &lt;&lt; std::endl;<br \/>\n        Die(2);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u8bbe\u7f6e\u670d\u52a1\u5668\u5730\u5740<br \/>\n    struct sockaddr_in server;<br \/>\n    memset(&amp;server, 0, sizeof(server));<br \/>\n    server.sin_family &#061; AF_INET;    \/\/ IPv4\u534f\u8bae<br \/>\n    server.sin_port &#061; htons(serverport); \/\/ \u7aef\u53e3\u53f7<br \/>\n    \/\/ \u8fd9\u53e5\u8bdd\u8868\u793a\u5c06\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684IP\u5730\u5740\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684IP\u5730\u5740<br \/>\n    \/\/ inet_pton\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u70b9\u5206\u5341\u8fdb\u5236\u7684IP\u5730\u5740\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684IP\u5730\u5740<br \/>\n    \/\/ \u8fd9\u91cc\u7684AF_INET\u8868\u793aIPv4\u534f\u8bae<br \/>\n    \/\/ \u8fd9\u91cc\u7684serverip.c_str()\u8868\u793aIP\u5730\u5740\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f<br \/>\n    \/\/ &amp;server.sin_addr\u8868\u793a\u5c06IP\u5730\u5740\u5b58\u50a8\u5230sin_addr\u6210\u5458\u53d8\u91cf\u4e2d<br \/>\n    ::inet_pton(AF_INET, serverip.c_str(), &amp;server.sin_addr);   \/\/ IP\u5730\u5740<\/p>\n<p>    \/\/ 3. \u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5<br \/>\n    int n &#061; ::connect(sockfd, CONV(&amp;server), sizeof(server));<br \/>\n    if(n &lt; 0){<br \/>\n        std::cerr &lt;&lt; &#034;Connect error: &#034; &lt;&lt; std::strerror(errno) &lt;&lt; std::endl;<br \/>\n        Die(5);<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u53d1\u9001\u6d88\u606f<br \/>\n    while(true){<br \/>\n        std::string mag;<br \/>\n        std::cout &lt;&lt; &#034;Enter# &#034;;<br \/>\n        std::getline(std::cin, mag);<\/p>\n<p>        write(sockfd, mag.c_str(), mag.size());<br \/>\n        char echo_buf[1024];<br \/>\n        n &#061; read(sockfd, echo_buf, sizeof(echo_buf));<br \/>\n        if(n &gt; 0){<br \/>\n            echo_buf[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; &#034;Echo: &#034; &lt;&lt; echo_buf &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else{<br \/>\n            break;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    ::close(sockfd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h2>&#x1f465;\u603b\u7ed3<\/h2>\n<p>\u672c\u7bc7\u535a\u6587\u5bf9 \u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa &#8211; \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 \u505a\u4e86\u4e00\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd&#xff0c;\u4e0d\u77e5\u9053\u5bf9\u4f60\u6709\u6ca1\u6709\u5e2e\u52a9\u5462<\/p>\n<p>\u89c9\u5f97\u535a\u4e3b\u5199\u5f97\u8fd8\u4e0d\u9519\u7684\u4e09\u8fde\u652f\u6301\u4e0b\u5427&#xff01;\u4f1a\u7ee7\u7eed\u52aa\u529b\u7684~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb455\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf35\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa &#8211; \u4ece\u7406\u89e3\u5230\u5b9e\u73b0\u505a\u4e86\u4e00\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u4e0d\u77e5\u9053\u5bf9\u4f60\u6709\u6ca1\u6709\u5e2e\u52a9\u5462\u89c9\u5f97\u535a\u4e3b\u5199\u5f97\u8fd8\u4e0d\u9519\u7684\u4e09\u8fde\u652f\u6301\u4e0b\u5427\uff01\u4f1a\u7ee7\u7eed\u52aa\u529b\u7684~<\/p>\n","protected":false},"author":2,"featured_media":32409,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,78],"topic":[],"class_list":["post-32413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \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\/32413.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb455\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf35\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0\u505a\u4e86\u4e00\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u4e0d\u77e5\u9053\u5bf9\u4f60\u6709\u6ca1\u6709\u5e2e\u52a9\u5462\u89c9\u5f97\u535a\u4e3b\u5199\u5f97\u8fd8\u4e0d\u9519\u7684\u4e09\u8fde\u652f\u6301\u4e0b\u5427\uff01\u4f1a\u7ee7\u7eed\u52aa\u529b\u7684~\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/32413.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-24T00:11:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819f02006.gif\" \/>\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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/32413.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/32413.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-24T00:11:13+00:00\",\"dateModified\":\"2025-04-24T00:11:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/32413.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/32413.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/32413.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0\"}]},{\"@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":"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \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\/32413.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb455\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf35\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0\u505a\u4e86\u4e00\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u4e0d\u77e5\u9053\u5bf9\u4f60\u6709\u6ca1\u6709\u5e2e\u52a9\u5462\u89c9\u5f97\u535a\u4e3b\u5199\u5f97\u8fd8\u4e0d\u9519\u7684\u4e09\u8fde\u652f\u6301\u4e0b\u5427\uff01\u4f1a\u7ee7\u7eed\u52aa\u529b\u7684~","og_url":"https:\/\/www.wsisp.com\/helps\/32413.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-24T00:11:13+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250424001111-6809819f02006.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/32413.html","url":"https:\/\/www.wsisp.com\/helps\/32413.html","name":"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-24T00:11:13+00:00","dateModified":"2025-04-24T00:11:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/32413.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/32413.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/32413.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7f51\u7edc\u3011\u5404\u7248\u672cTCP\u670d\u52a1\u5668\u6784\u5efa - \u4ece\u7406\u89e3\u5230\u5b9e\u73b0"}]},{"@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\/32413","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=32413"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/32413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/32409"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=32413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=32413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=32413"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=32413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}