{"id":32710,"date":"2025-04-25T18:25:25","date_gmt":"2025-04-25T10:25:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/32710.html"},"modified":"2025-04-25T18:25:25","modified_gmt":"2025-04-25T10:25:25","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e3%80%91%e6%9e%84%e5%bb%ba%e7%b1%bb%e4%bc%bcxshell%e5%8a%9f%e8%83%bd%e7%9a%84tcp%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/32710.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668"},"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\/20250425102523-680b63131d3ed.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\/20250425102523-680b6313411ee.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.hpp<\/li>\n<li>\n<ul>\n<li>1.1 \u57fa\u672c\u7ed3\u6784<\/li>\n<li>1.2 \u591a\u7ebf\u7a0b\u5904\u7406\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001Common.hpp<\/li>\n<li>\n<ul>\n<li>2.1 \u57fa\u672c\u7ed3\u6784<\/li>\n<li>2.2 \u6784\u9020\u3001\u6790\u6784\u51fd\u6570<\/li>\n<li>2.3 SafeCheck()<\/li>\n<li>2.4 Excute()<\/li>\n<li>2.5 HandlerCommand()<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001TcpServer.cpp<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u56db\u3001\u6d4b\u8bd5<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e94\u3001\u6574\u4f53\u4ee3\u7801<\/li>\n<li>\n<ul>\n<li>5.1 TcpServer.hpp<\/li>\n<li>5.2 TcpServer.cpp<\/li>\n<li>5.3 Command.hpp<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f465;\u603b\u7ed3<\/li>\n<\/ul>\n<hr \/>\n<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff0c;\u7b14\u8005\u5e26\u5927\u5bb6<font color=\"#EA3680\">\u5b9e\u73b0\u4e86TCP\u670d\u52a1\u5668\u7684 4\u79cd \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u901a\u4fe1\u7684\u6a21\u5f0f<\/font> &#xff0c;\u5206\u522b\u662f<font color=\"#7E84F7\">\u5355\u6267\u884c\u6d41\u6a21\u5f0f\u3001\u591a\u8fdb\u7a0b\u6a21\u5f0f\u3001\u591a\u7ebf\u7a0b\u6a21\u5f0f\u3001\u4ee5\u53ca\u7ebf\u7a0b\u6c60\u6a21\u5f0f<\/font><\/p>\n<p>\u8fd9\u4e00\u7bc7&#xff0c;\u6211\u5c06\u5e26\u5927\u5bb6\u8fdb\u4e00\u6b65\u7406\u89e3TCP&#xff0c;\u5c31\u4ece<font color=\"#EB3324\">\u7528TCP\u5b9e\u73b0\u7c7bXShell\u529f\u80fd\u670d\u52a1\u5668<\/font> &#xff0c;\u91c7\u7528\u7684\u662f\u591a\u7ebf\u7a0b\u7248\u672c\u54e6<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e00\u3001TcpServer.hpp<\/h2>\n<p>\u5176\u4ed6\u90e8\u5206\u4fdd\u6301\u4e0d\u53d8&#xff0c;\u6211\u4eec\u6dfb\u52a0\u4e00\u4e2a\u5904\u7406\u56de\u8c03\u51fd\u6570&#xff0c;\u7528\u6765\u5224\u65ad\u548c\u6267\u884c\u76f8\u5e94\u7684 xshell \u547d\u4ee4<\/p>\n<h3>1.1 \u57fa\u672c\u7ed3\u6784<\/h3>\n<p>using handler_t &#061; std::function&lt;std::string(int sockfd, InetAddr addr)&gt;;<\/p>\n<p>class TcpServer{<br \/>\n    public:<br \/>\n        \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        TcpServer(handler_t handler,uint16_t port &#061; gport)<br \/>\n            :_handler(handler), _port(port), _isrunning(false){}<br \/>\n        \/\/ \u521d\u59cb\u5316<br \/>\n        void InitServer(){}<br \/>\n        \/\/ server &#8211; 2 \u591a\u7ebf\u7a0b\u7248\u672c<br \/>\n        void Loop(){}<br \/>\n        \/\/ \u6790\u6784\u51fd\u6570<br \/>\n        ~TcpServer(){}<\/p>\n<p>    private:<br \/>\n        int _listensockfd; \/\/ \u76d1\u542csocket<br \/>\n        uint16_t _port;<br \/>\n        bool _isrunning;<br \/>\n        handler_t _handler;<br \/>\n};<\/p>\n<h3>1.2 \u591a\u7ebf\u7a0b\u5904\u7406\u51fd\u6570<\/h3>\n<p>\u6211\u4eec\u4e4b\u524d\u4f7f\u7528 Server \u8fdb\u884c\u56de\u663e\u5904\u7406&#xff0c;<font color=\"#7E84F7\">\u8fd9\u91cc\u5c06 Execute \u51fd\u6570\u7684\u5904\u7406\u65b9\u6cd5\u4ece Server \u6539\u6210 _handler\u5c31\u884c\u4e86<\/font><\/p>\n<p>\/\/ \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;_handler(td-&gt;_sockfd, td-&gt;_addr);<br \/>\n            delete td;<br \/>\n            return nullptr;<br \/>\n        }<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001Common.hpp<\/h2>\n<h3>2.1 \u57fa\u672c\u7ed3\u6784<\/h3>\n<p>Command \u7c7b\u5b9e\u73b0\u7c7b\u4f3c\u4e8eXShell\u7684\u529f\u80fd&#xff0c;\u4f46\u662f\u9700\u8981\u6ce8\u610f<font color=\"#EA3680\">\u8981\u8ba9\u6240\u6709\u547d\u4ee4\u90fd\u53ef\u4ee5\u6267\u884c&#xff0c;\u56e0\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5220\u5e93\u7b49\u76f8\u5173\u7684\u95ee\u9898\u3002<\/font>\u4e0d<font color=\"#7E84F7\">\u6210\u5458\u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528set\u5bb9\u5668\u5b58\u50a8\u5141\u8bb8\u6267\u884c\u547d\u4ee4\u7684\u524d\u7f00&#xff01;<\/font><\/p>\n<p>class Command{<br \/>\n    public:<br \/>\n        Command(){}<br \/>\n        bool SafeCheck(const std::string&amp; cmdstr){}<br \/>\n        std::string Excute(const std::string&amp; cmdstr){}<br \/>\n        void HandlerCommand(int sockfd, InetAddr addr){}<br \/>\n        ~Command(){}<br \/>\n    private:<br \/>\n        std::set&lt;std::string&gt; _safe_command;  \/\/ \u5141\u8bb8\u6267\u884c\u7684\u547d\u4ee4<br \/>\n};<\/p>\n<h3>2.2 \u6784\u9020\u3001\u6790\u6784\u51fd\u6570<\/h3>\n<p>\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981&#xff0c;<font color=\"#7E84F7\">\u5728\u6784\u9020\u51fd\u6570\u79cd\u5c06\u5141\u8bb8\u4f7f\u7528\u7684\u547d\u4ee4\u63d2\u5165\u5230\u5bb9\u5668<\/font>&#xff0c;<font color=\"#F09B59\">\u6790\u6784\u51fd\u6570\u65e0\u9700\u5904\u7406&#xff01;<\/font><\/p>\n<p>Command() {<br \/>\n    \/\/ \u767d\u540d\u5355<br \/>\n    _safe_command.insert(&#034;ls&#034;);<br \/>\n    _safe_command.insert(&#034;pwd&#034;);<br \/>\n    _safe_command.insert(&#034;touch&#034;);<br \/>\n    _safe_command.insert(&#034;whoami&#034;);<br \/>\n    _safe_command.insert(&#034;which&#034;);<br \/>\n}<br \/>\n~Command(){}<\/p>\n<h3>2.3 SafeCheck()<\/h3>\n<p>\u68c0\u67e5\u5f53\u524d\u547d\u4ee4\u662f\u5426\u5728\u767d\u540d\u5355\u4e2d<\/p>\n<p>bool SafeCheck(const std::string&amp; cmdstr){<br \/>\n            for(auto&amp; cmd : _safe_command){<br \/>\n                \/\/ \u503c\u6bd4\u8f83\u547d\u4ee4\u5f00\u5934<br \/>\n                if(strncmp(cmd.c_str(), cmdstr.c_str(), cmd.size()) &#061;&#061; 0){<br \/>\n                    return true;<br \/>\n                }<br \/>\n            }<br \/>\n            return false;<br \/>\n        }<\/p>\n<h3>2.4 Excute()<\/h3>\n<p>\u5199\u8fd9\u90e8\u5206\u4ee3\u7801\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u7528\u5230\u4e00\u4e2a\u51fd\u6570 &#8211; popen<\/p>\n<p><font color=\"#7E84F7\">popen \u7684\u4f5c\u7528\u662f \u200b\u521b\u5efa\u5b50\u8fdb\u7a0b\u6267\u884c\u7cfb\u7edf\u547d\u4ee4&#xff0c;\u5e76\u901a\u8fc7\u7ba1\u9053&#xff08;Pipe&#xff09;\u4e0e\u5176\u901a\u4fe1&#xff0c;\u4ed6\u4f1a\u542f\u52a8\u5b50\u8fdb\u7a0b&#xff0c;\u8c03\u7528 \/bin\/sh&#xff08;\u6216\u5176\u4ed6\u9ed8\u8ba4 shell&#xff09;\u89e3\u6790\u5e76\u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4&#xff08;\u5982 ls -l\u3001grep \u201cerror\u201d \u7b49&#xff09;\u3002<\/font><\/p>\n<p>std::string Excute(const std::string&amp; cmdstr) {<br \/>\n    \/\/ \u68c0\u67e5\u662f\u5426\u5b89\u5168&#xff0c;\u4e0d\u5b89\u5168\u8fd4\u56de<br \/>\n    if (!SafeCheck(cmdstr)) {<br \/>\n        return &#034;unsafe&#034;;<br \/>\n    }<br \/>\n    std::string result;<br \/>\n    \/\/ popen \u521b\u5efa\u5b50\u8fdb\u7a0b\u6267\u884c\u7cfb\u7edf\u547d\u4ee4&#xff0c;\u5e76\u901a\u8fc7\u7ba1\u9053&#xff08;Pipe&#xff09;\u4e0e\u5176\u901a\u4fe1<br \/>\n    \/\/ popen(const char* command, const char* type)<br \/>\n    \/\/ command: \u547d\u4ee4\u5b57\u7b26\u4e32<br \/>\n    \/\/ type: \u7ba1\u9053\u7c7b\u578b&#xff0c;&#034;r&#034;\u8868\u793a\u8bfb&#xff0c;&#034;w&#034;\u8868\u793a\u5199&#xff0c;&#034;r&#043;&#034;\u8868\u793a\u8bfb\u5199<br \/>\n    \/\/ \u8fd4\u56de\u6587\u4ef6\u6307\u9488&#xff0c;\u5931\u8d25\u8fd4\u56deNULL<br \/>\n    FILE* fp &#061; popen(cmdstr.c_str(), &#034;r&#034;);<br \/>\n    if (fp) {<br \/>\n        \/\/ \u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa<br \/>\n        \/\/ \u4e00\u884c\u8bfb\u53d6<br \/>\n        char line[1024];<br \/>\n        while (fgets(line, sizeof(line), fp)) {<br \/>\n            result &#043;&#061; line;<br \/>\n        }<br \/>\n        return result.empty() ? &#034;success&#034; : result; \/\/ \u6709\u4e9b\u547d\u4ee4\u521b\u5efa\u65e0\u8fd4\u56de\u503c<br \/>\n    }<br \/>\n}<\/p>\n<h3>2.5 HandlerCommand()<\/h3>\n<p>\u547d\u4ee4\u5904\u7406\u51fd\u6570\u662f\u4e00\u4e2a\u957f\u670d\u52a1(\u6b7b\u5faa\u73af)&#xff0c;<font color=\"#7E84F7\">\u5148\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u4fe1\u606f&#xff0c;\u5982\u679c\u63a5\u6536\u6210\u529f\u5219\u5904\u7406\u6536\u5230\u7684\u6d88\u606f(\u547d\u4ee4)&#xff0c;\u5e76\u5c06\u5904\u7406\u7684\u7ed3\u679c\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef&#xff0c;\u5982\u679c\u8bfb\u5230\u6587\u4ef6\u7ed3\u5c3e\u6216\u8005\u63a5\u6536\u5931\u8d25\u5219\u9000\u51fa\u5faa\u73af&#xff01;<\/font><\/p>\n<p>void HandlerCommand(int sockfd, InetAddr addr) {<br \/>\n    \/\/ \u6211\u4eec\u628a\u5b83\u5f53\u4f5c\u4e00\u4e2a\u957f\u670d\u52a1<br \/>\n    while (true) {<br \/>\n        char commandbuffer[1024]; \/\/ \u63a5\u6536\u547d\u4ee4\u7684\u7f13\u51b2\u533a<br \/>\n        \/\/ 1. \u63a5\u6536\u6d88\u606f<br \/>\n        \/\/ recv(int sockfd, void* buf, size_t len, int flags)<br \/>\n        \/\/ sockfd: \u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n        \/\/ buf: \u63a5\u6536\u7f13\u51b2\u533a<br \/>\n        \/\/ len: \u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n        \/\/ flags: \u63a5\u6536\u6807\u5fd7  0\u8868\u793a\u963b\u585e&#xff0c;\u975e0\u8868\u793a\u975e\u963b\u585e<br \/>\n        ssize_t n &#061; ::recv(sockfd, commandbuffer, sizeof(commandbuffer) &#8211; 1, 0);<br \/>\n        if (n &gt; 0) {<br \/>\n            commandbuffer[n] &#061; 0;<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;get command from client&#034; &lt;&lt; addr.AddrStr()<br \/>\n                                &lt;&lt; &#034;:&#034; &lt;&lt; commandbuffer;<br \/>\n            std::string result &#061; Excute(commandbuffer);<\/p>\n<p>            \/\/ 2. \u53d1\u9001\u6d88\u606f<br \/>\n            \/\/ send(int sockfd, const void* buf, size_t len, int flags)<br \/>\n            \/\/ sockfd: \u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n            \/\/ buf: \u53d1\u9001\u7f13\u51b2\u533a<br \/>\n            \/\/ len: \u53d1\u9001\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n            \/\/ flags: \u53d1\u9001\u6807\u5fd7  0\u8868\u793a\u4e0d\u963b\u585e&#xff0c;\u975e0\u8868\u793a\u963b\u585e<br \/>\n            ::send(sockfd, result.c_str(), result.size(), 0);<br \/>\n        }<br \/>\n        \/\/ \u8bfb\u5230\u6587\u4ef6\u7ed3\u5c3e<br \/>\n        else if (n &#061;&#061; 0) {<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; addr.AddrStr() &lt;&lt; &#034; quit&#034;;<br \/>\n            break;<br \/>\n        } else {<br \/>\n            LOG(LogLevel::ERROR) &lt;&lt; &#034;read error from client &#034; &lt;&lt; addr.AddrStr();<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001TcpServer.cpp<\/h2>\n<p><font color=\"#EA3680\">\u670d\u52a1\u7aef\u4e3b\u51fd\u6570\u4f7f\u7528\u667a\u80fd\u6307\u9488\u6784\u9020Server\u5bf9\u8c61(\u53c2\u6570\u9700\u8981\u52a0\u6267\u884c\u65b9\u6cd5)<\/font> &#xff0c;<font color=\"#7E84F7\">\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><font color=\"#EB3324\">\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;Command::HandlerCommand \u662f \u200b\u975e\u9759\u6001\u6210\u5458\u51fd\u6570&#xff0c;\u8c03\u7528\u65f6\u5fc5\u987b\u901a\u8fc7 Command \u7c7b\u7684\u5b9e\u4f8b&#xff08;\u5982 cmdservice&#xff09;\u6765\u8bbf\u95ee\u3002<\/font><\/p>\n<p>#include &#034;TcpServer.hpp&#034;<br \/>\n#include &#034;Command.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>    Command cmdservice;<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;(<br \/>\n        \/\/ &amp;Command::HandlerCommand&#xff1a;\u6210\u5458\u51fd\u6570\u6307\u9488\u3002<br \/>\n        \/\/ &amp;cmdservice&#xff1a;Command \u5bf9\u8c61\u7684\u5b9e\u4f8b\u6307\u9488&#xff08;this \u6307\u9488&#xff09;<br \/>\n        \/\/ _1 \u548c _2&#xff1a;\u5360\u4f4d\u7b26&#xff0c;\u8868\u793a\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570&#xff08;int sockfd \u548c InetAddr addr&#xff09;<br \/>\n        std::bind(&amp;Command::HandlerCommand, &amp;cmdservice, std::placeholders::_1, std::placeholders::_2),<br \/>\n        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;\u56db\u3001\u6d4b\u8bd5<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250425102523-680b631367760.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e94\u3001\u6574\u4f53\u4ee3\u7801<\/h2>\n<h3>5.1 TcpServer.hpp<\/h3>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;signal.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;functional&gt;<\/p>\n<p>#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;ThreadPool.hpp&#034;<\/p>\n<p>#define BACKLOG 8<\/p>\n<p>using namespace LogModule;<br \/>\nusing namespace ThreadPoolModule;<\/p>\n<p>static const uint16_t gport &#061; 8080;<\/p>\n<p>using handler_t &#061; std::function&lt;void(int sockfd, InetAddr addr)&gt;;<\/p>\n<p>class TcpServer{<br \/>\n    public:<br \/>\n        \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        TcpServer(handler_t handler,uint16_t port &#061; gport)<br \/>\n            :_handler(handler), _port(port), _isrunning(false){}<\/p>\n<p>        \/\/ \u521d\u59cb\u5316<br \/>\n        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>        \/\/ 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;_handler(td-&gt;_sockfd, td-&gt;_addr);<br \/>\n            delete td;<br \/>\n            return nullptr;<br \/>\n        }<\/p>\n<p>        \/\/ \u6790\u6784\u51fd\u6570<br \/>\n        ~TcpServer(){}<\/p>\n<p>    private:<br \/>\n        int _listensockfd; \/\/ \u76d1\u542csocket<br \/>\n        uint16_t _port;<br \/>\n        bool _isrunning;<\/p>\n<p>        handler_t _handler;<br \/>\n};<\/p>\n<h3>5.2 TcpServer.cpp<\/h3>\n<p>#include &#034;TcpServer.hpp&#034;<br \/>\n#include &#034;Command.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>    Command cmdservice;<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;(<br \/>\n        \/\/ &amp;Command::HandlerCommand&#xff1a;\u6210\u5458\u51fd\u6570\u6307\u9488\u3002<br \/>\n        \/\/ &amp;cmdservice&#xff1a;Command \u5bf9\u8c61\u7684\u5b9e\u4f8b\u6307\u9488&#xff08;this \u6307\u9488&#xff09;<br \/>\n        \/\/ _1 \u548c _2&#xff1a;\u5360\u4f4d\u7b26&#xff0c;\u8868\u793a\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570&#xff08;int sockfd \u548c InetAddr addr&#xff09;<br \/>\n        std::bind(&amp;Command::HandlerCommand, &amp;cmdservice, std::placeholders::_1, std::placeholders::_2),<br \/>\n        port);<\/p>\n<p>    tsvr-&gt;InitServer();<br \/>\n    tsvr-&gt;Loop();<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>5.3 Command.hpp<\/h3>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;set&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<\/p>\n<p>#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>class Command{<br \/>\n    public:<br \/>\n        Command(){<br \/>\n            \/\/ \u767d\u540d\u5355<br \/>\n            _safe_command.insert(&#034;ls&#034;);<br \/>\n            _safe_command.insert(&#034;pwd&#034;);<br \/>\n            _safe_command.insert(&#034;touch&#034;);<br \/>\n            _safe_command.insert(&#034;whoami&#034;);<br \/>\n            _safe_command.insert(&#034;which&#034;);<br \/>\n        }<br \/>\n        bool SafeCheck(const std::string&amp; cmdstr){<br \/>\n            for(auto&amp; cmd : _safe_command){<br \/>\n                \/\/ \u503c\u6bd4\u8f83\u547d\u4ee4\u5f00\u5934<br \/>\n                if(strncmp(cmd.c_str(), cmdstr.c_str(), cmd.size()) &#061;&#061; 0){<br \/>\n                    return true;<br \/>\n                }<br \/>\n            }<br \/>\n            return false;<br \/>\n        }<br \/>\n        std::string Excute(const std::string&amp; cmdstr){<br \/>\n            \/\/ \u68c0\u67e5\u662f\u5426\u5b89\u5168&#xff0c;\u4e0d\u5b89\u5168\u8fd4\u56de<br \/>\n            if(!SafeCheck(cmdstr)){<br \/>\n                return &#034;unsafe&#034;;<br \/>\n            }<br \/>\n            std::string result;<br \/>\n            FILE* fp &#061; popen(cmdstr.c_str(), &#034;r&#034;);<br \/>\n            if(fp){<br \/>\n                char line[1024];<br \/>\n                while(fgets(line, sizeof(line), fp)){<br \/>\n                    result &#043;&#061; line;<br \/>\n                }<br \/>\n                return result.empty() ? &#034;success&#034; : result; \/\/ \u6709\u4e9b\u547d\u4ee4\u521b\u5efa\u65e0\u8fd4\u56de\u503c<br \/>\n            }<br \/>\n            return &#034;Execute error&#034;;<br \/>\n        }<br \/>\n        std::string Excute(std::string&amp; cmdstr){<br \/>\n            \/\/ \u68c0\u67e5\u662f\u5426\u5b89\u5168&#xff0c;\u4e0d\u5b89\u5168\u8fd4\u56de<br \/>\n            if(!SafeCheck(cmdstr)){<br \/>\n                return &#034;unsafe&#034;;<br \/>\n            }<br \/>\n            std::string result;<br \/>\n            \/\/ popen \u521b\u5efa\u5b50\u8fdb\u7a0b\u6267\u884c\u7cfb\u7edf\u547d\u4ee4&#xff0c;\u5e76\u901a\u8fc7\u7ba1\u9053&#xff08;Pipe&#xff09;\u4e0e\u5176\u901a\u4fe1<br \/>\n            \/\/ popen(const char* command, const char* type)<br \/>\n            \/\/ command: \u547d\u4ee4\u5b57\u7b26\u4e32<br \/>\n            \/\/ type: \u7ba1\u9053\u7c7b\u578b&#xff0c;&#034;r&#034;\u8868\u793a\u8bfb&#xff0c;&#034;w&#034;\u8868\u793a\u5199&#xff0c;&#034;r&#043;&#034;\u8868\u793a\u8bfb\u5199<br \/>\n            \/\/ \u8fd4\u56de\u6587\u4ef6\u6307\u9488&#xff0c;\u5931\u8d25\u8fd4\u56deNULL<br \/>\n            FILE* fp &#061; popen(cmdstr.c_str(), &#034;r&#034;);<br \/>\n            if(fp){<br \/>\n                \/\/ \u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa<br \/>\n                \/\/ \u4e00\u884c\u8bfb\u53d6<br \/>\n                char line[1024];<br \/>\n                while(fgets(line, sizeof(line), fp)){<br \/>\n                    result &#043;&#061; line;<br \/>\n                }<br \/>\n                return result.empty() ? &#034;success&#034; : result; \/\/ \u6709\u4e9b\u547d\u4ee4\u521b\u5efa\u65e0\u8fd4\u56de\u503c<br \/>\n            }<br \/>\n        }<br \/>\n        void HandlerCommand(int sockfd, InetAddr addr){<br \/>\n            \/\/ \u6211\u4eec\u628a\u5b83\u5f53\u4f5c\u4e00\u4e2a\u957f\u670d\u52a1<br \/>\n            while(true){<br \/>\n                char commandbuffer[1024];   \/\/ \u63a5\u6536\u547d\u4ee4\u7684\u7f13\u51b2\u533a<br \/>\n                \/\/ 1. \u63a5\u6536\u6d88\u606f<br \/>\n                \/\/ recv(int sockfd, void* buf, size_t len, int flags)<br \/>\n                \/\/ sockfd: \u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n                \/\/ buf: \u63a5\u6536\u7f13\u51b2\u533a<br \/>\n                \/\/ len: \u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n                \/\/ flags: \u63a5\u6536\u6807\u5fd7  0\u8868\u793a\u963b\u585e&#xff0c;\u975e0\u8868\u793a\u975e\u963b\u585e<br \/>\n                ssize_t n &#061; ::recv(sockfd, commandbuffer, sizeof(commandbuffer) &#8211; 1, 0);<br \/>\n                if(n &gt; 0){<br \/>\n                    commandbuffer[n] &#061; 0;<br \/>\n                    LOG(LogLevel::INFO) &lt;&lt; &#034;get command from client&#034; &lt;&lt; addr.AddrStr() &lt;&lt; &#034;:&#034; &lt;&lt; commandbuffer;<br \/>\n                    std::string result &#061; Excute(commandbuffer);<\/p>\n<p>                    \/\/ 2. \u53d1\u9001\u6d88\u606f<br \/>\n                    \/\/ send(int sockfd, const void* buf, size_t len, int flags)<br \/>\n                    \/\/ sockfd: \u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n                    \/\/ buf: \u53d1\u9001\u7f13\u51b2\u533a<br \/>\n                    \/\/ len: \u53d1\u9001\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n                    \/\/ flags: \u53d1\u9001\u6807\u5fd7  0\u8868\u793a\u4e0d\u963b\u585e&#xff0c;\u975e0\u8868\u793a\u963b\u585e<br \/>\n                    ::send(sockfd, result.c_str(), result.size(), 0);<br \/>\n                }<br \/>\n                \/\/ \u8bfb\u5230\u6587\u4ef6\u7ed3\u5c3e<br \/>\n                else if(n &#061;&#061; 0){<br \/>\n                    LOG(LogLevel::INFO) &lt;&lt; &#034;client &#034; &lt;&lt; addr.AddrStr() &lt;&lt; &#034; quit&#034;;<br \/>\n                    break;<br \/>\n                }<br \/>\n                else{<br \/>\n                    LOG(LogLevel::ERROR) &lt;&lt; &#034;read error from client &#034; &lt;&lt; addr.AddrStr();<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        ~Command(){}<br \/>\n    private:<br \/>\n        std::set&lt;std::string&gt; _safe_command;  \/\/ \u5141\u8bb8\u6267\u884c\u7684\u547d\u4ee4<br \/>\n};<\/p>\n<hr \/>\n<h2>&#x1f465;\u603b\u7ed3<\/h2>\n<p>\u672c\u7bc7\u535a\u6587\u5bf9 \u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 \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\u8bfb1k\u6b21\uff0c\u70b9\u8d5e38\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668\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":32707,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,78],"topic":[],"class_list":["post-32710","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\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \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\/32710.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\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e38\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668\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\/32710.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-25T10:25:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250425102523-680b63131d3ed.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\/32710.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/32710.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-25T10:25:25+00:00\",\"dateModified\":\"2025-04-25T10:25:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/32710.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/32710.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/32710.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\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668\"}]},{\"@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\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \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\/32710.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e38\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668\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\/32710.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-25T10:25:25+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250425102523-680b63131d3ed.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\/32710.html","url":"https:\/\/www.wsisp.com\/helps\/32710.html","name":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-25T10:25:25+00:00","dateModified":"2025-04-25T10:25:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/32710.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/32710.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/32710.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\u6784\u5efa\u7c7b\u4f3cXShell\u529f\u80fd\u7684TCP\u670d\u52a1\u5668"}]},{"@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\/32710","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=32710"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/32710\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/32707"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=32710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=32710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=32710"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=32710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}