{"id":31939,"date":"2025-04-22T16:06:18","date_gmt":"2025-04-22T08:06:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/31939.html"},"modified":"2025-04-22T16:06:18","modified_gmt":"2025-04-22T08:06:18","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e3%80%91%e6%9e%84%e5%bb%baudp%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e5%ad%97%e5%85%b8%e7%bf%bb%e8%af%91%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/31939.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf"},"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\/20250422080616-68074df859fdd.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\/20250422080616-68074df87d41b.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\u3001\u670d\u52a1\u7aef\u66f4\u65b0<\/li>\n<li>\n<ul>\n<li>1.1 \u51fd\u6570\u5bf9\u8c61\u58f0\u660e<\/li>\n<li>1.2 UdpServer \u7c7b\u6210\u5458\u66f4\u65b0<\/li>\n<li>1.3 \u6784\u9020\u51fd\u6570\u66f4\u65b0<\/li>\n<li>1.4 \u5f00\u59cb &#8211; Start() \u66f4\u65b0<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001Dictionary \u5b57\u5178\u7c7b\u8bbe\u8ba1<\/li>\n<li>\n<ul>\n<li>2.1 \u57fa\u672c\u7ed3\u6784<\/li>\n<li>2.2 \u52a0\u8f7d\u5b57\u5178\u6587\u4ef6 &#8211; LoadDictionary(const std::string&amp; path)<\/li>\n<li>2.3 \u6784\u9020\u51fd\u6570<\/li>\n<li>2.4 \u7ffb\u8bd1\u51fd\u6570<\/li>\n<li>2.5 \u670d\u52a1\u7aef\u8fd0\u884c\u66f4\u65b0<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001\u6574\u4f53\u4ee3\u7801<\/li>\n<li>&#x1f465;\u603b\u7ed3<\/li>\n<\/ul>\n<hr \/>\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d&#xff0c;\u6211\u4eec<font color=\"#8F5019\">\u5b9e\u73b0\u4e86\u56de\u663e\u5ba2\u6237\u7aef\u8f93\u5165\u7684\u529f\u80fd<\/font>&#xff0c;\u8fd9\u529f\u80fd\u5f80\u5f80\u662f\u4e0d\u591f\u7684&#xff0c;\u4e3a\u4e86\u66f4\u597d\u5730\u6a21\u62df\u73b0\u5b9e\u9700\u6c42&#xff0c;\u6211\u4eec\u73b0\u5728\u591a\u589e\u52a0\u4e00\u4e2a\u529f\u80fd &#8211; <font color=\"#EA3680\">\u5b57\u5178\u7ffb\u8bd1\u529f\u80fd<\/font><\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e00\u3001\u670d\u52a1\u7aef\u66f4\u65b0<\/h2>\n<h3>1.1 \u51fd\u6570\u5bf9\u8c61\u58f0\u660e<\/h3>\n<p>\u522b\u7684\u529f\u80fd\u3001\u6210\u5458\u540d\u4fdd\u6301\u4e0d\u53d8&#xff0c;<font color=\"#7E84F7\">\u4e3a\u4e86\u65b0\u589e\u5b57\u5178\u7ffb\u8bd1\u529f\u80fd&#xff0c;\u6211\u4eec\u9700\u8981\u5f15\u5165\u51fd\u6570\u5bf9\u8c61\u7c7b\u578b<\/font><\/p>\n<p>\/\/ \u56de\u8c03\u51fd\u6570\u5bf9\u8c61\u58f0\u660e<br \/>\nusing func_t &#061; std::function&lt;std::string(std::string)&gt;;<\/p>\n<h3>1.2 UdpServer \u7c7b\u6210\u5458\u66f4\u65b0<\/h3>\n<p>class UdpServer : public nocopy{<br \/>\n    public:<br \/>\nUdpServer(func_t func,uint16_t localport &#061; glocalport);<br \/>\n    void InitServer();<br \/>\n    void Start();<br \/>\n    ~UdpServer();<br \/>\n    private:<br \/>\n        int _sockfd;            \/\/ \u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n        uint16_t _localport;    \/\/ \u7aef\u53e3\u53f7<br \/>\n        std::string _localip;   \/\/ \u672c\u5730IP\u5730\u5740<br \/>\n        bool _isrunning;        \/\/ \u8fd0\u884c\u72b6\u6001<\/p>\n<p>        func_t _func;           \/\/ \u56de\u8c03\u51fd\u6570<br \/>\n};<\/p>\n<h3>1.3 \u6784\u9020\u51fd\u6570\u66f4\u65b0<\/h3>\n<ul>\n<li>\u6784\u9020\u51fd\u6570\u53ea\u9700<font color=\"#7E84F7\">\u589e\u52a0\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u53c2\u6570<\/font>&#xff0c;\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u53d8\u91cf\u5373\u53ef&#xff01;&#xff01;&#xff01;<\/li>\n<\/ul>\n<p>UdpServer(uint16_t localport &#061; gdefaultport, func_t func &#061; nullptr)<br \/>\n    : _sockfd(gsockfd), _localport(localport), _isrunning(false), _func(func) {}<\/p>\n<h3>1.4 \u5f00\u59cb &#8211; Start() \u66f4\u65b0<\/h3>\n<ul>\n<li>\u4e4b\u524d\u53ea\u9700\u8981\u56de\u663e\u7684\u65f6\u5019&#xff0c;<font color=\"#F09B59\">\u6211\u4eec\u76f4\u63a5\u63a5\u6536\u5ba2\u6237\u7aef\u4fe1\u606f&#xff0c;\u5c06\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u5ba2\u6237\u7aefip\u548c\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u4e3b\u673a\u5b57\u8282\u5e8f&#xff0c;\u518d\u8fd4\u56de\u5c31\u884c\u4e86<\/font><\/li>\n<li>\u73b0\u5728\u6211\u4eec\u8981\u5728\u8fd9\u4e4b\u95f4\u6dfb\u52a0\u4e00\u4e2a\u73af\u8282&#xff0c;<font color=\"#7E84F7\">\u4f7f\u6536\u5230\u7684\u5ba2\u6237\u7aef\u4fe1\u606f&#xff0c;\u5148\u901a\u8fc7\u5b57\u5178\u7ffb\u8bd1\u56de\u8c03\u51fd\u6570&#xff0c;\u5c06\u5904\u7406\u540e\u7684\u503c\u4f20\u56de\u53bb<\/font><\/li>\n<\/ul>\n<p>void Start() {<br \/>\n    _isrunning &#061; true;<br \/>\n    while (true) {<br \/>\n        char inbuffer[1024];              \/\/ \u63a5\u6536\u7f13\u51b2\u533a<br \/>\n        struct sockaddr_in peer;          \/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u5730\u5740<br \/>\n        socklen_t peerlen &#061; sizeof(peer); \/\/ \u8ba1\u7b97\u63a5\u6536\u7684\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6<\/p>\n<p>        \/\/ \u63a5\u6536\u6570\u636e\u62a5<br \/>\n        \/\/ recvfrom(int sockfd, void* buf, size_t len, int flags, struct<br \/>\n        \/\/ sockaddr* src_addr, socklen_t* addrlen)<br \/>\n        \/\/ \u4ece\u5957\u63a5\u5b57\u63a5\u6536\u6570\u636e&#xff0c;\u5e76\u5b58\u5165buf\u6307\u5411\u7684\u7f13\u51b2\u533a\u4e2d&#xff0c;\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u7684\u5b57\u8282\u6570<br \/>\n        \/\/ \u53c2\u6570sockfd&#xff1a;\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n        \/\/ \u53c2\u6570buf&#xff1a;\u6307\u5411\u63a5\u6536\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;c_str()\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3achar*&#xff0c;\u4ee5\u4fbf\u5b58\u5165\u7f13\u51b2\u533a<br \/>\n        \/\/ \u53c2\u6570len&#xff1a;\u63a5\u6536\u7f13\u51b2\u533a\u7684\u957f\u5ea6<br \/>\n        \/\/ \u53c2\u6570flags&#xff1a;\u63a5\u6536\u6807\u5fd7&#xff0c;\u4e00\u822c\u8bbe\u4e3a0<br \/>\n        \/\/ \u53c2\u6570src_addr&#xff1a;\u6307\u5411\u5ba2\u6237\u7aef\u5730\u5740\u7684\u6307\u9488&#xff0c;\u82e5\u4e0d\u4e3aNULL&#xff0c;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u8be5\u6307\u9488\u6307\u5411\u5ba2\u6237\u7aef\u7684\u5730\u5740&#xff0c;\u662f\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n        \/\/ \u53c2\u6570addrlen&#xff1a;\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6\u7684\u6307\u9488&#xff0c;\u82e5\u4e0d\u4e3aNULL&#xff0c;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u8be5\u6307\u9488\u6307\u5411\u5b9e\u9645\u7684\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6<br \/>\n        ssize_t n &#061; ::recvfrom(_sockfd, inbuffer, sizeof(inbuffer) &#8211; 1, 0,<br \/>\n                               CONV(&amp;peer), &amp;peerlen);<br \/>\n        if (n &gt; 0) {<br \/>\n            \/\/ \u5c06\u82f1\u6587\u5355\u8bcd \u8f6c\u6362\u4e3a \u4e2d\u6587<br \/>\n            std::string result &#061; _func(inbuffer);<br \/>\n            ::sendto(_sockfd, result.c_str(), result.size(), 0, CONV(&amp;peer),<br \/>\n                     peerlen);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e8c\u3001Dictionary \u5b57\u5178\u7c7b\u8bbe\u8ba1<\/h2>\n<p>\u5b57\u5178\u7c7b\u6267\u884c<font color=\"#EB3324\">\u52a0\u8f7d\u5b57\u5178\u6587\u4ef6 \u548c \u6267\u884c\u7ffb\u8bd1<\/font>\u7684\u529f\u80fd<\/p>\n<h3>2.1 \u57fa\u672c\u7ed3\u6784<\/h3>\n<p>class Dictionary{<br \/>\n    private:<br \/>\n        \/\/ \u52a0\u8f7d\u5b57\u5178\u6587\u4ef6<br \/>\n        void LoadDictionary(const std::string&amp; path);<br \/>\n    public:<br \/>\n        \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        Dictionary(const std::string&amp; path);<br \/>\n        \/\/ \u7ffb\u8bd1<br \/>\n        std::string Translate(const std::string&amp; word);<br \/>\n        \/\/ \u6790\u6784\u51fd\u6570<br \/>\n        ~Dictionary();<br \/>\n    private:<br \/>\n        std::unordered_map&lt;std::string, std::string&gt; _dict;     \/\/ \u5b57\u5178\u7ed3\u6784<br \/>\n        std::string _dict_path;                                 \/\/ \u6587\u4ef6\u8def\u5f84<br \/>\n};<\/p>\n<h3>2.2 \u52a0\u8f7d\u5b57\u5178\u6587\u4ef6 &#8211; LoadDictionary(const std::string&amp; path)<\/h3>\n<p>\u6211\u4eec\u4ee5 &#034;: &#034; \u4e00\u4e2a\u5192\u53f7\u52a0\u4e00\u4e2a\u7a7a\u683c\u7684\u5f62\u5f0f&#xff0c;\u8fdb\u884c\u7ffb\u8bd1<\/p>\n<ul>\n<li>\n<p>\u52a0\u8f7d\u5b57\u5178\u6587\u4ef6\u7684\u672c\u8d28\u662f<font color=\"#EA3680\">\u4ee5KV\u7684\u5f62\u5f0f\u5c06\u82f1\u6587\u5355\u8bcd\u548c\u4e2d\u6587\u7ffb\u8bd1\u63d2\u5165\u5230_dict\u54c8\u5e0c\u8868\u4e2d&#xff01;<\/font><\/p>\n<\/li>\n<li>\n<p>\u52a0\u8f7d\u6587\u4ef6\u5305\u542b3\u4e2a\u5927\u7684\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6<\/li>\n<li>\u6309\u884c\u8bfb\u53d6\u5185\u5bb9[\u9700\u8981\u8003\u8651\u4e2d\u95f4\u6709\u7a7a\u683c\u60c5\u51b5&#xff0c;\u4e00\u884c\u4e2d\u6ca1\u627e\u5230\u5206\u9694\u7b26\u60c5\u51b5]<\/li>\n<li>\u5173\u95ed\u6587\u4ef6<\/li>\n<\/li>\n<\/ul>\n<p>\/\/ \u52a0\u8f7d\u5b57\u5178\u6587\u4ef6<br \/>\nvoid LoadDictionary(const std::string&amp; path) {<br \/>\n    \/\/ 1. \u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6<br \/>\n    std::ifstream in(path);<br \/>\n    if (!in.is_open()) {<br \/>\n        LOG(LogLevel::FATAL) &lt;&lt; &#034;open &#034; &lt;&lt; path.c_str() &lt;&lt; &#034; failed&#034;;<br \/>\n        Die(1);<br \/>\n    }<br \/>\n    std::string line;<\/p>\n<p>    \/\/ 2. \u6309\u884c\u8bfb\u53d6\u5185\u5bb9<br \/>\n    while (std::getline(in, line)) {<br \/>\n        LOG(LogLevel::DEBUG) &lt;&lt; line.c_str() &lt;&lt; &#034;load success&#034;;<br \/>\n        if (line.empty())<br \/>\n            continue; \/\/ \u4e2d\u95f4\u6709\u7a7a\u683c\u60c5\u51b5<\/p>\n<p>        auto pos &#061; line.find(sep); \/\/ \u4f7f\u7528find\u627e\u5230\u5206\u9694\u7b26\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u8fed\u4ee3\u5668\u4f4d\u7f6e<br \/>\n        if (pos &#061;&#061; std::string::npos)<br \/>\n            continue; \/\/ \u627e\u4e0d\u5230\u5206\u9694\u7b26&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>        std::string key &#061; line.substr(0, pos); \/\/ \u524d\u95ed\u540e\u5f00<br \/>\n        if (key.empty())<br \/>\n            continue; \/\/ \u952e\u4e3a\u7a7a&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>        std::string value &#061; line.substr(pos &#043; sep.size());<br \/>\n        if (value.empty())<br \/>\n            continue; \/\/ \u503c\u4e3a\u7a7a&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>        _dict.insert(std::make_pair(key, value));<br \/>\n    }<br \/>\n    LOG(LogLevel::INFO) &lt;&lt; path.c_str() &lt;&lt; &#034; load success&#034;;<\/p>\n<p>    \/\/ 3. \u5173\u95ed\u6587\u4ef6<br \/>\n    in.close();<br \/>\n}<\/p>\n<h3>2.3 \u6784\u9020\u51fd\u6570<\/h3>\n<p>\u521d\u59cb\u5316\u5b57\u5178\u6587\u4ef6&#xff0c;\u5e76\u5c06\u952e\u503c\u5bf9\u52a0\u8f7d\u5230\u672c\u5730\u4fdd\u5b58<\/p>\n<p>\/\/ \u6784\u9020\u51fd\u6570<br \/>\nDictionary(const std::string&amp; path &#061; gpath &#043; gdictname) { LoadDictionary(path); }<\/p>\n<h3>2.4 \u7ffb\u8bd1\u51fd\u6570<\/h3>\n<p>\u5728\u952e\u503c\u5bf9\u4e2d\u67e5\u627e\u662f\u5426\u6709\u8be5\u5355\u8bcd&#xff0c;\u6709\u5355\u8bcd\u5c31\u8fd4\u56de\u503c&#xff0c;\u6ca1\u6709\u8fd4\u56deNone<\/p>\n<p>\/\/ \u7ffb\u8bd1<br \/>\nstd::string Translate(const std::string&amp; word) {<br \/>\n    auto iter &#061; _dict.find(word);<br \/>\n    if (iter &#061;&#061; _dict.end())<br \/>\n        return &#034;None&#034;;<br \/>\n    return iter-&gt;second;<br \/>\n}<\/p>\n<h3>2.5 \u670d\u52a1\u7aef\u8fd0\u884c\u66f4\u65b0<\/h3>\n<p>\u56e0\u4e3a\u6211\u4eec\u73b0\u5728\u9700\u8981\u5c06<font color=\"#7E84F7\">\u5b57\u5178\u7c7b\u7684\u67e5\u627e\u65b9\u6cd5 \u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u4f20\u7ed9\u670d\u52a1\u7aef<\/font> &#xff0c;\u6240\u4ee5\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u53d8\u5316<\/p>\n<p>#include &#034;UdpServer.hpp&#034;<br \/>\n#include &#034;Dictionary.hpp&#034;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 2){<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; localport&#034; &lt;&lt; std::endl;<br \/>\n        Die(1);<br \/>\n    }<\/p>\n<p>    uint16_t port &#061; std::stoi(argv[1]);<\/p>\n<p>    ENABLE_CONSOLE_LOG();   \/\/ \u65e5\u671f\u7c7b\u65b9\u6cd5&#xff0c;\u4f7f\u65e5\u5fd7\u5728\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n<p>    std::shared_ptr&lt;Dictionary&gt; dict_ptr &#061; std::make_shared&lt;Dictionary&gt;();<\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(port, [&amp;dict_ptr]( const std::string&amp; word){<br \/>\n        std::cout &lt;&lt; &#034;|&#034; &lt;&lt; word &lt;&lt; &#034;|&#034; &lt;&lt; std::endl;<br \/>\n        return dict_ptr-&gt;Translate(word);<br \/>\n    });<\/p>\n<p>    usvr-&gt;InitServer(); \/\/ \u521d\u59cb\u5316\u670d\u52a1\u7aef<br \/>\n    usvr-&gt;Start();      \/\/ \u542f\u52a8\u670d\u52a1\u7aef<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250422080616-68074df8a3aff.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>&#x1f3f3;\ufe0f\u200d&#x1f308;\u4e09\u3001\u6574\u4f53\u4ee3\u7801<\/h2>\n<p>UdpServer.hpp<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;cerrno&gt;   \/\/ \u8fd9\u4e2a\u5934\u6587\u4ef6\u5305\u542b\u4e86errno\u5b9a\u4e49&#xff0c;\u7528\u4e8e\u5b58\u653e\u7cfb\u7edf\u8c03\u7528\u7684\u8fd4\u56de\u503c<br \/>\n#include &lt;strings.h&gt;    \/\/ \u5c5e\u4e8ePOSIX\u6269\u5c55\u200b&#xff08;\u975e\u6807\u51c6C\/C&#043;&#043;&#xff09;&#xff0c;\u5e38\u89c1\u4e8eUnix\/Linux\u7cfb\u7edf&#xff0c;\u63d0\u4f9b\u989d\u5916\u5b57\u7b26\u4e32\u51fd\u6570&#xff08;\u5982 bcopy, bzero&#xff09;<\/p>\n<p>#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;<\/p>\n<p>#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>const static int gsockfd &#061; -1;<br \/>\nconst static std::string gdefaultip &#061; &#034;127.0.0.1&#034;; \/\/ \u8868\u793a\u672c\u5730\u4e3b\u673a<br \/>\nconst static uint16_t gdefaultport &#061; 8080;<\/p>\n<p>\/\/ \u56de\u8c03\u51fd\u6570\u5bf9\u8c61\u58f0\u660e<br \/>\nusing func_t &#061; std::function&lt;std::string(std::string)&gt;;<\/p>\n<p>class nocopy{<br \/>\n    public:<br \/>\n        nocopy(){}<br \/>\n        ~nocopy(){}<br \/>\n        nocopy(const nocopy&amp;) &#061; delete;     \/\/ \u7981\u6b62\u62f7\u8d1d\u6784\u9020\u51fd\u6570<br \/>\n        const nocopy&amp; operator&#061;(const nocopy&amp;) &#061; delete;   \/\/ \u7981\u6b62\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26<br \/>\n};<\/p>\n<p>class UdpServer : public nocopy{<br \/>\n    public:<br \/>\n        UdpServer(uint16_t localport &#061; gdefaultport, func_t func &#061; nullptr)<br \/>\n            : _sockfd(gsockfd),<br \/>\n              _localport(localport),<br \/>\n              _isrunning(false),<br \/>\n              _func(func)<br \/>\n        {}<br \/>\n        void InitServer(){<br \/>\n            \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n            \/\/ socket(int domain, int type, int protocol)<br \/>\n            \/\/ \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6216\u8005\u5728\u51fa\u9519\u65f6\u8fd4\u56de-1<br \/>\n            \/\/ \u53c2\u6570domain&#xff1a;\u534f\u8bae\u65cf&#xff0c;AF_INET&#xff0c;\u8868\u793aIPv4\u534f\u8bae\u65cf<br \/>\n            \/\/ \u53c2\u6570type&#xff1a;\u5957\u63a5\u5b57\u7c7b\u578b&#xff0c;SOCK_DGRAM&#xff0c;\u8868\u793aUDP\u5957\u63a5\u5b57<br \/>\n            \/\/ \u53c2\u6570protocol&#xff1a;\u534f\u8bae&#xff0c;0&#xff0c;\u8868\u793a\u9ed8\u8ba4\u534f\u8bae<br \/>\n            _sockfd &#061; ::socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n            if(_sockfd &lt; 0){<br \/>\n                LOG(LogLevel::FATAL) &lt;&lt; &#034;socket: &#034; &lt;&lt; strerror(errno);<br \/>\n                \/\/ exit(SOCKET_ERR) \u8868\u793a\u7a0b\u5e8f\u8fd0\u884c\u5931\u8d25&#xff0c;\u5e76\u8fd4\u56de\u6307\u5b9a\u7684\u9519\u8bef\u7801<br \/>\n                exit(SOCKET_ERR);<br \/>\n            }<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;socket success, sockfd is: &#034; &lt;&lt; _sockfd;<\/p>\n<p>            \/\/ 2. bind<br \/>\n            \/\/ sockaddr_in<br \/>\n            struct sockaddr_in local;<br \/>\n            \/\/ \u5c06local\u5168\u90e8\u7f6e\u96f6&#xff0c;\u4ee5\u4fbf\u540e\u9762\u8bbe\u7f6e<br \/>\n            memset(&amp;local, 0, sizeof(local));<br \/>\n            local.sin_family &#061; AF_INET; \/\/ IPv4\u534f\u8bae\u65cf<br \/>\n            local.sin_port &#061; htons(_localport); \/\/ \u7aef\u53e3\u53f7&#xff0c;\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n            local.sin_addr.s_addr &#061; htonl(INADDR_ANY); \/\/ \u672c\u5730IP\u5730\u5740&#xff0c;\u7f51\u7edc\u5b57\u8282\u5e8f<\/p>\n<p>            \/\/ \u5c06\u5957\u63a5\u5b57\u7ed1\u5b9a\u5230\u672c\u5730\u5730\u5740<br \/>\n            \/\/ bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen)<br \/>\n            \/\/ \u7ed1\u5b9a\u4e00\u4e2a\u5957\u63a5\u5b57\u5230\u4e00\u4e2a\u5730\u5740&#xff0c;\u4f7f\u5f97\u5957\u63a5\u5b57\u53ef\u4ee5\u63a5\u6536\u6765\u81ea\u8be5\u5730\u5740\u7684\u6570\u636e\u62a5<br \/>\n            \/\/ \u53c2\u6570sockfd&#xff1a;\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n            \/\/ \u53c2\u6570addr&#xff1a;\u6307\u5411sockaddr_in\u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u8868\u793a\u8981\u7ed1\u5b9a\u7684\u5730\u5740<br \/>\n            \/\/ \u53c2\u6570addrlen&#xff1a;\u5730\u5740\u957f\u5ea6&#xff0c;\u5373sizeof(sockaddr_in)<br \/>\n            \/\/ \u8fd4\u56de0\u8868\u793a\u6210\u529f&#xff0c;-1\u8868\u793a\u51fa\u9519<br \/>\n            int n &#061; ::bind(_sockfd, (struct sockaddr* )&amp;local, sizeof(local));<br \/>\n            if(n &lt; 0){<br \/>\n                LOG(LogLevel::FATAL) &lt;&lt; &#034;bind: &#034; &lt;&lt; strerror(errno);<br \/>\n                exit(BIND_ERR);<br \/>\n            }<br \/>\n            LOG(LogLevel::DEBUG) &lt;&lt; &#034;bind success&#034;;<br \/>\n        }<br \/>\n        void Start(){<br \/>\n            _isrunning &#061; true;<br \/>\n            while(true){<br \/>\n                char inbuffer[1024];                \/\/ \u63a5\u6536\u7f13\u51b2\u533a<br \/>\n                struct sockaddr_in peer;            \/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u5730\u5740<br \/>\n                socklen_t peerlen &#061; sizeof(peer);   \/\/ \u8ba1\u7b97\u63a5\u6536\u7684\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6<\/p>\n<p>                \/\/ \u63a5\u6536\u6570\u636e\u62a5<br \/>\n                \/\/ recvfrom(int sockfd, void* buf, size_t len, int flags, struct sockaddr* src_addr, socklen_t* addrlen)<br \/>\n                \/\/ \u4ece\u5957\u63a5\u5b57\u63a5\u6536\u6570\u636e&#xff0c;\u5e76\u5b58\u5165buf\u6307\u5411\u7684\u7f13\u51b2\u533a\u4e2d&#xff0c;\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u7684\u5b57\u8282\u6570<br \/>\n                \/\/ \u53c2\u6570sockfd&#xff1a;\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n                \/\/ \u53c2\u6570buf&#xff1a;\u6307\u5411\u63a5\u6536\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;c_str()\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3achar*&#xff0c;\u4ee5\u4fbf\u5b58\u5165\u7f13\u51b2\u533a<br \/>\n                \/\/ \u53c2\u6570len&#xff1a;\u63a5\u6536\u7f13\u51b2\u533a\u7684\u957f\u5ea6<br \/>\n                \/\/ \u53c2\u6570flags&#xff1a;\u63a5\u6536\u6807\u5fd7&#xff0c;\u4e00\u822c\u8bbe\u4e3a0<br \/>\n                \/\/ \u53c2\u6570src_addr&#xff1a;\u6307\u5411\u5ba2\u6237\u7aef\u5730\u5740\u7684\u6307\u9488&#xff0c;\u82e5\u4e0d\u4e3aNULL&#xff0c;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u8be5\u6307\u9488\u6307\u5411\u5ba2\u6237\u7aef\u7684\u5730\u5740&#xff0c;\u662f\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n                \/\/ \u53c2\u6570addrlen&#xff1a;\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6\u7684\u6307\u9488&#xff0c;\u82e5\u4e0d\u4e3aNULL&#xff0c;\u51fd\u6570\u8fd4\u56de\u65f6&#xff0c;\u8be5\u6307\u9488\u6307\u5411\u5b9e\u9645\u7684\u5ba2\u6237\u7aef\u5730\u5740\u957f\u5ea6<br \/>\n                ssize_t n &#061; ::recvfrom(_sockfd, inbuffer, sizeof(inbuffer) &#8211; 1, 0, CONV(&amp;peer), &amp;peerlen);<br \/>\n                if(n &gt; 0){<br \/>\n                    \/\/ \u5c06\u82f1\u6587\u5355\u8bcd \u8f6c\u6362\u4e3a \u4e2d\u6587<br \/>\n                    std::string result &#061; _func(inbuffer);<br \/>\n                    ::sendto(_sockfd, result.c_str(), result.size(), 0, CONV(&amp;peer), peerlen);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        ~UdpServer(){<br \/>\n            \/\/ \u5224\u65ad _sockfd \u662f\u5426\u662f\u4e00\u4e2a\u6709\u6548\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n            \/\/ \u6709\u6548\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982\u5957\u63a5\u5b57\u3001\u6253\u5f00\u7684\u6587\u4ef6\u7b49&#xff09;\u662f\u975e\u8d1f\u6574\u6570\u200b&#xff08;&gt;&#061; 0&#xff09;<br \/>\n            if(_sockfd &gt; -1) ::close(_sockfd);<br \/>\n        }<br \/>\n    private:<br \/>\n        int _sockfd;            \/\/ \u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n        uint16_t _localport;    \/\/ \u7aef\u53e3\u53f7<br \/>\n        std::string _localip;   \/\/ \u672c\u5730IP\u5730\u5740<br \/>\n        bool _isrunning;        \/\/ \u8fd0\u884c\u72b6\u6001<\/p>\n<p>        func_t _func;           \/\/ \u56de\u8c03\u51fd\u6570<br \/>\n};<\/p>\n<p>UdpServer.cpp<\/p>\n<p>#include &#034;UdpServer.hpp&#034;<br \/>\n#include &#034;Dictionary.hpp&#034;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 2){<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; localport&#034; &lt;&lt; std::endl;<br \/>\n        Die(1);<br \/>\n    }<\/p>\n<p>    uint16_t port &#061; std::stoi(argv[1]);<\/p>\n<p>    ENABLE_CONSOLE_LOG();   \/\/ \u65e5\u671f\u7c7b\u65b9\u6cd5&#xff0c;\u4f7f\u65e5\u5fd7\u5728\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n<p>    std::shared_ptr&lt;Dictionary&gt; dict_ptr &#061; std::make_shared&lt;Dictionary&gt;();<\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(port, [&amp;dict_ptr]( const std::string&amp; word){<br \/>\n        std::cout &lt;&lt; &#034;|&#034; &lt;&lt; word &lt;&lt; &#034;|&#034; &lt;&lt; std::endl;<br \/>\n        return dict_ptr-&gt;Translate(word);<br \/>\n    });<\/p>\n<p>    usvr-&gt;InitServer(); \/\/ \u521d\u59cb\u5316\u670d\u52a1\u7aef<br \/>\n    usvr-&gt;Start();      \/\/ \u542f\u52a8\u670d\u52a1\u7aef<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>UdpClient.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &#034;Common.hpp&#034;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstdlib&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;<\/p>\n<p>UdpClient.cpp<\/p>\n<p>#include &#034;UdpClient.hpp&#034;<\/p>\n<p>int main(int argc, char* argv[]){<br \/>\n    if(argc !&#061; 3){<br \/>\n        std::cerr &lt;&lt; argv[0] &lt;&lt; &#034; serverip server&#034; &lt;&lt; std::endl;<br \/>\n        Die(USAGE_ERR);<br \/>\n    }<br \/>\n    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    int sockfd &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n    if(sockfd &lt; 0){<br \/>\n        std::cerr &lt;&lt; &#034;create socket error&#034; &lt;&lt; std::endl;<br \/>\n        Die(SOCKET_ERR);<br \/>\n    }<\/p>\n<p>    \/\/ 1. \u586b\u5145 server \u4fe1\u606f<br \/>\n    struct sockaddr_in server;<br \/>\n    memset(&amp;server, 0, sizeof(server));<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_port &#061; ::htons(serverport);<br \/>\n    server.sin_addr.s_addr &#061; ::inet_addr(serverip.c_str());<\/p>\n<p>    \/\/ 2. \u53d1\u9001\u6570\u636e<br \/>\n    while(true){<br \/>\n        std::cout &lt;&lt; &#034;Please Enter# &#034;;<br \/>\n        std::string msg;<br \/>\n        std::getline(std::cin, msg);<\/p>\n<p>        \/\/ client \u5fc5\u987b\u81ea\u5df1\u7684ip\u548c\u7aef\u53e3\u3002\u4f46\u662f\u5ba2\u6237\u7aef&#xff0c;\u4e0d\u9700\u8981\u663e\u793a\u8c03\u7528bind<br \/>\n        \/\/ \u5ba2\u6237\u7aef\u9996\u6b21 sendto \u6d88\u606f\u7684\u65f6\u5019&#xff0c;\u7531OS\u81ea\u52a8bind<br \/>\n        \/\/ 1. \u5982\u4f55\u7406\u89e3 client \u81ea\u52a8\u968f\u673abind\u7aef\u53e3\u53f7&#xff1f; \u4e00\u4e2a\u7aef\u53e3\u53f7&#xff0c;\u53ea\u80fd\u8bfb\u4e00\u4e2a\u8fdb\u7a0bbind<br \/>\n        \/\/ 2. \u5982\u4f55\u7406\u89e3 server \u8981\u663e\u793a\u5730bind&#xff1f; \u5fc5\u987b\u7a33\u5b9a&#xff01;\u5fc5\u987b\u662f\u4f17\u6240\u5468\u77e5\u4e14\u4e0d\u80fd\u8f7b\u6613\u6539\u53d8\u7684<br \/>\n        int n &#061; ::sendto(sockfd, msg.c_str(), msg.size(), 0, CONV(&amp;server), sizeof(server));<br \/>\n        (void)n;<\/p>\n<p>        struct sockaddr_in temp;<br \/>\n        socklen_t len &#061; sizeof(temp);<br \/>\n        char buffer[1024];<br \/>\n        n &#061; ::recvfrom(sockfd, buffer,sizeof(buffer) &#8211; 1, 0, CONV(&amp;temp), &amp;len);<br \/>\n        if(n &gt; 0){<br \/>\n            buffer[n] &#061; 0;<br \/>\n            std::cout &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>Common.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>#define Die(code)   \\\\<br \/>\n    do              \\\\<br \/>\n    {               \\\\<br \/>\n        exit(code); \\\\<br \/>\n    } while (0)<\/p>\n<p>#define CONV(v) (struct sockaddr *)(v)<\/p>\n<p>enum<br \/>\n{<br \/>\n    USAGE_ERR &#061; 1,<br \/>\n    SOCKET_ERR,<br \/>\n    BIND_ERR<br \/>\n};<\/p>\n<p>InetAddr.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&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 &#034;Common.hpp&#034;<\/p>\n<p>class InetAddr<br \/>\n{<br \/>\nprivate:<br \/>\n    void PortNet2Host()<br \/>\n    {<br \/>\n        _port &#061; ::ntohs(_net_addr.sin_port);<br \/>\n    }<br \/>\n    void IpNet2Host()<br \/>\n    {<br \/>\n        char ipbuffer[64];<br \/>\n        const char *ip &#061; ::inet_ntop(AF_INET, &amp;_net_addr.sin_addr, ipbuffer, sizeof(ipbuffer));<br \/>\n        (void)ip;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    InetAddr(){}<br \/>\n    InetAddr(const struct sockaddr_in &amp;addr) : _net_addr(addr)<br \/>\n    {<br \/>\n        PortNet2Host();<br \/>\n        IpNet2Host();<br \/>\n    }<br \/>\n    InetAddr(uint16_t port) : _port(port), _ip(&#034;&#034;)<br \/>\n    {<br \/>\n        _net_addr.sin_family &#061; AF_INET;<br \/>\n        _net_addr.sin_port &#061; htons(_port);<br \/>\n        _net_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    }<br \/>\n    struct sockaddr *NetAddr() { return CONV(&amp;_net_addr); }<br \/>\n    socklen_t NetAddrLen() { return sizeof(_net_addr); }<br \/>\n    std::string Ip() { return _ip; }<br \/>\n    uint16_t Port() { return _port; }<br \/>\n    ~InetAddr()<br \/>\n    {<br \/>\n    }<\/p>\n<p>private:<br \/>\n    struct sockaddr_in _net_addr;<br \/>\n    std::string _ip;<br \/>\n    uint16_t _port;<br \/>\n};<\/p>\n<p>Dictionary.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>const static std::string sep &#061; &#034;: &#034;;<br \/>\nconst static std::string gpath &#061; &#034;.\/&#034;;<br \/>\nconst static std::string gdictname &#061; &#034;dict.txt&#034;;<\/p>\n<p>class Dictionary{<br \/>\n    private:<br \/>\n        \/\/ \u52a0\u8f7d\u5b57\u5178\u6587\u4ef6<br \/>\n        void LoadDictionary(const std::string&amp; path){<br \/>\n            \/\/ 1. \u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6<br \/>\n            std::ifstream in(path);<br \/>\n            if(!in.is_open()){<br \/>\n                LOG(LogLevel::FATAL) &lt;&lt; &#034;open &#034; &lt;&lt; path.c_str() &lt;&lt; &#034; failed&#034;;<br \/>\n                Die(1);<br \/>\n            }<br \/>\n            std::string line;<\/p>\n<p>            \/\/ 2. \u6309\u884c\u8bfb\u53d6\u5185\u5bb9<br \/>\n            while(std::getline(in, line)){<br \/>\n                LOG(LogLevel::DEBUG) &lt;&lt; line.c_str() &lt;&lt; &#034;load success&#034;;<br \/>\n                if(line.empty())<br \/>\n                    continue; \/\/ \u4e2d\u95f4\u6709\u7a7a\u683c\u60c5\u51b5<\/p>\n<p>                auto pos &#061; line.find(sep);  \/\/ \u4f7f\u7528find\u627e\u5230\u5206\u9694\u7b26\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u8fed\u4ee3\u5668\u4f4d\u7f6e<br \/>\n                if(pos &#061;&#061; std::string::npos)<br \/>\n                    continue; \/\/ \u627e\u4e0d\u5230\u5206\u9694\u7b26&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>                std::string key &#061; line.substr(0, pos); \/\/ \u524d\u95ed\u540e\u5f00<br \/>\n                if(key.empty())<br \/>\n                    continue; \/\/ \u952e\u4e3a\u7a7a&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>                std::string value &#061; line.substr(pos &#043; sep.size());<br \/>\n                if(value.empty())<br \/>\n                    continue; \/\/ \u503c\u4e3a\u7a7a&#xff0c;\u8df3\u8fc7\u8be5\u884c<\/p>\n<p>                _dict.insert(std::make_pair(key, value));<br \/>\n            }<br \/>\n            LOG(LogLevel::INFO) &lt;&lt; path.c_str() &lt;&lt; &#034; load success&#034;;<\/p>\n<p>            \/\/ 3. \u5173\u95ed\u6587\u4ef6<br \/>\n            in.close();<br \/>\n        }<br \/>\n    public:<br \/>\n        \/\/ \u6784\u9020\u51fd\u6570<br \/>\n        Dictionary(const std::string&amp; path &#061; gpath &#043; gdictname){<br \/>\n            LoadDictionary(path);<br \/>\n        }<br \/>\n        \/\/ \u7ffb\u8bd1<br \/>\n        std::string Translate(const std::string&amp; word){<br \/>\n            auto iter &#061; _dict.find(word);<br \/>\n            if(iter &#061;&#061; _dict.end()) return &#034;None&#034;;<br \/>\n            return iter-&gt;second;<br \/>\n        }<br \/>\n        \/\/ \u6790\u6784\u51fd\u6570<br \/>\n        ~Dictionary(){}<br \/>\n    private:<br \/>\n        std::unordered_map&lt;std::string, std::string&gt; _dict;     \/\/ \u5b57\u5178\u7ed3\u6784<br \/>\n        std::string _dict_path;                                 \/\/ \u6587\u4ef6\u8def\u5f84<br \/>\n};<\/p>\n<p>Log.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;sstream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;filesystem&gt; \/\/C&#043;&#043;17<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;time.h&gt;<br \/>\n#include &#034;Mutex.hpp&#034;<\/p>\n<p>namespace LogModule<br \/>\n{<br \/>\n    using namespace LockModule;<\/p>\n<p>    \/\/ \u83b7\u53d6\u4e00\u4e0b\u5f53\u524d\u7cfb\u7edf\u7684\u65f6\u95f4<br \/>\n    std::string CurrentTime()<br \/>\n    {<br \/>\n        time_t time_stamp &#061; ::time(nullptr);<br \/>\n        struct tm curr;<br \/>\n        localtime_r(&amp;time_stamp, &amp;curr); \/\/ \u65f6\u95f4\u6233&#xff0c;\u83b7\u53d6\u53ef\u8bfb\u6027\u8f83\u5f3a\u7684\u65f6\u95f4\u4fe1\u606f5<\/p>\n<p>        char buffer[1024];<br \/>\n        \/\/ bug<br \/>\n        snprintf(buffer, sizeof(buffer), &#034;%4d-%02d-%02d %02d:%02d:%02d&#034;,<br \/>\n                 curr.tm_year &#043; 1900,<br \/>\n                 curr.tm_mon &#043; 1,<br \/>\n                 curr.tm_mday,<br \/>\n                 curr.tm_hour,<br \/>\n                 curr.tm_min,<br \/>\n                 curr.tm_sec);<\/p>\n<p>        return buffer;<br \/>\n    }<\/p>\n<p>    \/\/ \u6784\u6210: 1. \u6784\u5efa\u65e5\u5fd7\u5b57\u7b26\u4e32 2. \u5237\u65b0\u843d\u76d8(screen, file)<br \/>\n    \/\/  1. \u65e5\u5fd7\u6587\u4ef6\u7684\u9ed8\u8ba4\u8def\u5f84\u548c\u6587\u4ef6\u540d<br \/>\n    const std::string defaultlogpath &#061; &#034;.\/log\/&#034;;<br \/>\n    const std::string defaultlogname &#061; &#034;log.txt&#034;;<\/p>\n<p>    \/\/ 2. \u65e5\u5fd7\u7b49\u7ea7<br \/>\n    enum class LogLevel<br \/>\n    {<br \/>\n        DEBUG &#061; 1,<br \/>\n        INFO,<br \/>\n        WARNING,<br \/>\n        ERROR,<br \/>\n        FATAL<br \/>\n    };<\/p>\n<p>    std::string Level2String(LogLevel level)<br \/>\n    {<br \/>\n        switch (level)<br \/>\n        {<br \/>\n        case LogLevel::DEBUG:<br \/>\n            return &#034;DEBUG&#034;;<br \/>\n        case LogLevel::INFO:<br \/>\n            return &#034;INFO&#034;;<br \/>\n        case LogLevel::WARNING:<br \/>\n            return &#034;WARNING&#034;;<br \/>\n        case LogLevel::ERROR:<br \/>\n            return &#034;ERROR&#034;;<br \/>\n        case LogLevel::FATAL:<br \/>\n            return &#034;FATAL&#034;;<br \/>\n        default:<br \/>\n            return &#034;None&#034;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u5237\u65b0\u7b56\u7565.<br \/>\n    class LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        virtual ~LogStrategy() &#061; default;<br \/>\n        virtual void SyncLog(const std::string &amp;message) &#061; 0;<br \/>\n    };<\/p>\n<p>    \/\/ 3.1 \u63a7\u5236\u53f0\u7b56\u7565<br \/>\n    class ConsoleLogStrategy : public LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        ConsoleLogStrategy()<br \/>\n        {<br \/>\n        }<br \/>\n        ~ConsoleLogStrategy()<br \/>\n        {<br \/>\n        }<br \/>\n        void SyncLog(const std::string &amp;message)<br \/>\n        {<br \/>\n            LockGuard lockguard(_lock);<br \/>\n            std::cout &lt;&lt; message &lt;&lt; std::endl;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        Mutex _lock;<br \/>\n    };<\/p>\n<p>    \/\/ 3.2 \u6587\u4ef6\u7ea7(\u78c1\u76d8)\u7b56\u7565<br \/>\n    class FileLogStrategy : public LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        FileLogStrategy(const std::string &amp;logpath &#061; defaultlogpath, const std::string &amp;logname &#061; defaultlogname)<br \/>\n            : _logpath(logpath),<br \/>\n              _logname(logname)<br \/>\n        {<br \/>\n            \/\/ \u786e\u8ba4_logpath\u662f\u5b58\u5728\u7684.<br \/>\n            LockGuard lockguard(_lock);<\/p>\n<p>            if (std::filesystem::exists(_logpath))<br \/>\n            {<br \/>\n                return;<br \/>\n            }<br \/>\n            try<br \/>\n            {<br \/>\n                std::filesystem::create_directories(_logpath);<br \/>\n            }<br \/>\n            catch (std::filesystem::filesystem_error &amp;e)<br \/>\n            {<br \/>\n                std::cerr &lt;&lt; e.what() &lt;&lt; &#034;\\\\n&#034;;<br \/>\n            }<br \/>\n        }<br \/>\n        ~FileLogStrategy()<br \/>\n        {<br \/>\n        }<br \/>\n        void SyncLog(const std::string &amp;message)<br \/>\n        {<br \/>\n            LockGuard lockguard(_lock);<br \/>\n            std::string log &#061; _logpath &#043; _logname; \/\/ .\/log\/log.txt<br \/>\n            std::ofstream out(log, std::ios::app); \/\/ \u65e5\u5fd7\u5199\u5165&#xff0c;\u4e00\u5b9a\u662f\u8ffd\u52a0<br \/>\n            if (!out.is_open())<br \/>\n            {<br \/>\n                return;<br \/>\n            }<br \/>\n            out &lt;&lt; message &lt;&lt; &#034;\\\\n&#034;;<br \/>\n            out.close();<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::string _logpath;<br \/>\n        std::string _logname;<\/p>\n<p>        \/\/ \u9501<br \/>\n        Mutex _lock;<br \/>\n    };<\/p>\n<p>    \/\/ \u65e5\u5fd7\u7c7b: \u6784\u5efa\u65e5\u5fd7\u5b57\u7b26\u4e32, \u6839\u636e\u7b56\u7565&#xff0c;\u8fdb\u884c\u5237\u65b0<br \/>\n    class Logger<br \/>\n    {<br \/>\n    public:<br \/>\n        Logger()<br \/>\n        {<br \/>\n            \/\/ \u9ed8\u8ba4\u91c7\u7528ConsoleLogStrategy\u7b56\u7565<br \/>\n            _strategy &#061; std::make_shared&lt;ConsoleLogStrategy&gt;();<br \/>\n        }<br \/>\n        void EnableConsoleLog()<br \/>\n        {<br \/>\n            _strategy &#061; std::make_shared&lt;ConsoleLogStrategy&gt;();<br \/>\n        }<br \/>\n        void EnableFileLog()<br \/>\n        {<br \/>\n            _strategy &#061; std::make_shared&lt;FileLogStrategy&gt;();<br \/>\n        }<br \/>\n        ~Logger() {}<br \/>\n        \/\/ \u4e00\u6761\u5b8c\u6574\u7684\u4fe1\u606f: [2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [16] &#043; \u65e5\u5fd7\u7684\u53ef\u53d8\u90e8\u5206(&lt;&lt; &#034;hello world&#034; &lt;&lt; 3.14 &lt;&lt; a &lt;&lt; b;)<br \/>\n        class LogMessage<br \/>\n        {<br \/>\n        public:<br \/>\n            LogMessage(LogLevel level, const std::string &amp;filename, int line, Logger &amp;logger)<br \/>\n                : _currtime(CurrentTime()),<br \/>\n                  _level(level),<br \/>\n                  _pid(::getpid()),<br \/>\n                  _filename(filename),<br \/>\n                  _line(line),<br \/>\n                  _logger(logger)<br \/>\n            {<br \/>\n                std::stringstream ssbuffer;<br \/>\n                ssbuffer &lt;&lt; &#034;[&#034; &lt;&lt; _currtime &lt;&lt; &#034;] &#034;<br \/>\n                         &lt;&lt; &#034;[&#034; &lt;&lt; Level2String(_level) &lt;&lt; &#034;] &#034;<br \/>\n                         &lt;&lt; &#034;[&#034; &lt;&lt; _pid &lt;&lt; &#034;] &#034;<br \/>\n                         &lt;&lt; &#034;[&#034; &lt;&lt; _filename &lt;&lt; &#034;] &#034;<br \/>\n                         &lt;&lt; &#034;[&#034; &lt;&lt; _line &lt;&lt; &#034;] &#8211; &#034;;<br \/>\n                _loginfo &#061; ssbuffer.str();<br \/>\n            }<br \/>\n            template &lt;typename T&gt;<br \/>\n            LogMessage &amp;operator&lt;&lt;(const T &amp;info)<br \/>\n            {<br \/>\n                std::stringstream ss;<br \/>\n                ss &lt;&lt; info;<br \/>\n                _loginfo &#043;&#061; ss.str();<br \/>\n                return *this;<br \/>\n            }<\/p>\n<p>            ~LogMessage()<br \/>\n            {<br \/>\n                if (_logger._strategy)<br \/>\n                {<br \/>\n                    _logger._strategy-&gt;SyncLog(_loginfo);<br \/>\n                }<br \/>\n            }<\/p>\n<p>        private:<br \/>\n            std::string _currtime; \/\/ \u5f53\u524d\u65e5\u5fd7\u7684\u65f6\u95f4<br \/>\n            LogLevel _level;       \/\/ \u65e5\u5fd7\u7b49\u7ea7<br \/>\n            pid_t _pid;            \/\/ \u8fdb\u7a0bpid<br \/>\n            std::string _filename; \/\/ \u6e90\u6587\u4ef6\u540d\u79f0<br \/>\n            int _line;             \/\/ \u65e5\u5fd7\u6240\u5728\u7684\u884c\u53f7<br \/>\n            Logger &amp;_logger;       \/\/ \u8d1f\u8d23\u6839\u636e\u4e0d\u540c\u7684\u7b56\u7565\u8fdb\u884c\u5237\u65b0<br \/>\n            std::string _loginfo;  \/\/ \u4e00\u6761\u5b8c\u6574\u7684\u65e5\u5fd7\u8bb0\u5f55<br \/>\n        };<\/p>\n<p>        \/\/ \u5c31\u662f\u8981\u62f7\u8d1d,\u6545\u610f\u7684\u62f7\u8d1d<br \/>\n        LogMessage operator()(LogLevel level, const std::string &amp;filename, int line)<br \/>\n        {<br \/>\n            return LogMessage(level, filename, line, *this);<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::shared_ptr&lt;LogStrategy&gt; _strategy; \/\/ \u65e5\u5fd7\u5237\u65b0\u7684\u7b56\u7565\u65b9\u6848<br \/>\n    };<\/p>\n<p>    Logger logger;<\/p>\n<p>#define LOG(Level) logger(Level, __FILE__, __LINE__)<br \/>\n#define ENABLE_CONSOLE_LOG() logger.EnableConsoleLog()<br \/>\n#define ENABLE_FILE_LOG() logger.EnableFileLog()<br \/>\n}<\/p>\n<p>Mutex.hpp<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>namespace LockModule<br \/>\n{<br \/>\n    class Mutex<br \/>\n    {<br \/>\n    public:<br \/>\n        Mutex(const Mutex&amp;) &#061; delete;<br \/>\n        const Mutex&amp; operator &#061; (const Mutex&amp;) &#061; delete;<br \/>\n        Mutex()<br \/>\n        {<br \/>\n            int n &#061; ::pthread_mutex_init(&amp;_lock, nullptr);<br \/>\n            (void)n;<br \/>\n        }<br \/>\n        ~Mutex()<br \/>\n        {<br \/>\n            int n &#061; ::pthread_mutex_destroy(&amp;_lock);<br \/>\n            (void)n;<br \/>\n        }<br \/>\n        void Lock()<br \/>\n        {<br \/>\n            int n &#061; ::pthread_mutex_lock(&amp;_lock);<br \/>\n            (void)n;<br \/>\n        }<br \/>\n        pthread_mutex_t *LockPtr()<br \/>\n        {<br \/>\n            return &amp;_lock;<br \/>\n        }<br \/>\n        void Unlock()<br \/>\n        {<br \/>\n            int n &#061; ::pthread_mutex_unlock(&amp;_lock);<br \/>\n            (void)n;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        pthread_mutex_t _lock;<br \/>\n    };<\/p>\n<p>    class LockGuard<br \/>\n    {<br \/>\n    public:<br \/>\n        LockGuard(Mutex &amp;mtx):_mtx(mtx)<br \/>\n        {<br \/>\n            _mtx.Lock();<br \/>\n        }<br \/>\n        ~LockGuard()<br \/>\n        {<br \/>\n            _mtx.Unlock();<br \/>\n        }<br \/>\n    private:<br \/>\n        Mutex &amp;_mtx;<br \/>\n    };<br \/>\n}<\/p>\n<p>Makefile<\/p>\n<p>.PHONY: all<br \/>\nall:server_udp client_udp<\/p>\n<p>server_udp:UdpServer.cpp<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17<\/p>\n<p>client_udp:UdpClient.cpp<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17<\/p>\n<p>.PHONY: clean<br \/>\nclean:<br \/>\nrm -f server_udp client_udp<\/p>\n<hr \/>\n<h2>&#x1f465;\u603b\u7ed3<\/h2>\n<p>\u672c\u7bc7\u535a\u6587\u5bf9 \u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUdp\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf \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\u8bfb768\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUdp\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf\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":31936,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,205,193,43,78],"topic":[],"class_list":["post-31939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-tcp-ip","tag-udp","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\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \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\/31939.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\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb768\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUdp\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf\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\/31939.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-22T08:06:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250422080616-68074df859fdd.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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/31939.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/31939.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-22T08:06:18+00:00\",\"dateModified\":\"2025-04-22T08:06:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/31939.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/31939.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/31939.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\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf\"}]},{\"@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\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \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\/31939.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb768\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u672c\u7bc7\u535a\u6587\u5bf9\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUdp\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf\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\/31939.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-22T08:06:18+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250422080616-68074df859fdd.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/31939.html","url":"https:\/\/www.wsisp.com\/helps\/31939.html","name":"\u3010Linux\u7f51\u7edc\u3011\u6784\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-22T08:06:18+00:00","dateModified":"2025-04-22T08:06:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/31939.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/31939.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/31939.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\u5efaUDP\u670d\u52a1\u5668\u4e0e\u5b57\u5178\u7ffb\u8bd1\u7cfb\u7edf"}]},{"@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\/31939","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=31939"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/31939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/31936"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=31939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=31939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=31939"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=31939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}