{"id":106331,"date":"2026-09-17T07:33:18","date_gmt":"2026-09-16T23:33:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/106331.html"},"modified":"2026-09-17T07:33:18","modified_gmt":"2026-09-16T23:33:18","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e5%8a%a0%e9%a4%90%ef%bc%88%e4%b8%89%ef%bc%89%e3%80%91%e5%ad%a6%e4%b9%a0%e5%85%a8%e8%ae%b0%e5%bd%95%ef%bc%9audp-%e5%9b%9e%e5%a3%b0%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/106331.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757"},"content":{"rendered":"<p id=\"lesson-51-\u5b66\u4e60\u5168\u8bb0\u5f55\uff1audp-\u56de\u58f0\u670d\u52a1\u5668-+-\u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757\uff08\u542b\u5b8c\u6574\u8fed\u4ee3\u8fc7\u7a0b\u4e0e\u8e29\u5751\u5b9e\u5f55\uff09\">UDP socket \u7f16\u7a0b\u3001sockaddr_in \u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u3001INADDR_ANY\u3001pthread \u4e92\u65a5\u9501\u4e0e RAII\u3001\u7b56\u7565\u6a21\u5f0f\u3001std::function \u56de\u8c03\u3001\u6d41\u5f0f\u65e5\u5fd7\u7684\u5b9e\u73b0\u539f\u7406\u3002<\/p>\n<h3 id=\"1.-\u6700\u7ec8\u6210\u679c\uff1a\u9879\u76ee\u91cc\u6709\u4ec0\u4e48\">1. \u9879\u76ee\u91cc\u6709\u4ec0\u4e48<\/h3>\n<p>\u5305\u542b 6 \u4e2a\u6e90\u6587\u4ef6&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<p>\u6587\u4ef6<\/p>\n<p>\u804c\u8d23<\/p>\n<\/tr>\n<tr>\n<td>\n<p>Mutex.hpp<\/p>\n<\/td>\n<td>\n<p>\u5bf9 pthread \u4e92\u65a5\u9501\u7684 C&#043;&#043; \u5c01\u88c5&#xff0c;\u5916\u52a0 RAII \u9501\u5b88\u536b LockGuard<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Log.hpp<\/p>\n<\/td>\n<td>\n<p>\u65e5\u5fd7\u6a21\u5757&#xff1a;\u65e5\u5fd7\u7b49\u7ea7\u3001\u65f6\u95f4\u6233\u3001\u6d41\u5f0f\u8f93\u51fa\u3001\u5c4f\u5e55\/\u6587\u4ef6\u4e24\u79cd\u5237\u65b0\u7b56\u7565<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UdpServer.hpp<\/p>\n<\/td>\n<td>\n<p>UDP \u670d\u52a1\u5668\u4e3b\u4f53&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u3001\u6536\u53d1\u5faa\u73af\u3001\u8c03\u7528\u56de\u8c03\u5904\u7406\u4e1a\u52a1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UdpServer.cc<\/p>\n<\/td>\n<td>\n<p>\u670d\u52a1\u5668 main&#xff1a;\u89e3\u6790\u547d\u4ee4\u884c\u7aef\u53e3\u53f7\u3001\u6ce8\u5165\u4e1a\u52a1\u56de\u8c03\u51fd\u6570\u3001\u542f\u52a8\u670d\u52a1\u5668<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UdpClient.cc<\/p>\n<\/td>\n<td>\n<p>UDP \u5ba2\u6237\u7aef&#xff1a;\u4ece\u952e\u76d8\u8bfb\u6d88\u606f\u53d1\u7ed9\u670d\u52a1\u5668&#xff0c;\u963b\u585e\u7b49\u5f85\u5e76\u6253\u5370\u670d\u52a1\u5668\u7684\u56de\u590d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Makefile<\/p>\n<\/td>\n<td>\n<p>\u4e00\u6761\u547d\u4ee4\u7f16\u8bd1\u4e24\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u7ec8\u7a0b\u5e8f\u7684\u7ed3\u679c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u670d\u52a1\u5668&#xff1a;.\/udpserver 8080&#xff0c;\u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361\u7684 8080 \u7aef\u53e3&#xff0c;\u6536\u5230\u6d88\u606f\u540e\u4ea4\u7ed9\u4e00\u4e2a\u53ef\u66ff\u6362\u7684\u56de\u8c03\u51fd\u6570\u5904\u7406&#xff0c;\u518d\u628a\u5904\u7406\u7ed3\u679c\u53d1\u56de\u5ba2\u6237\u7aef\u3002<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef&#xff1a;.\/udpclient 127.0.0.1 8080&#xff0c;\u952e\u76d8\u8f93\u5165\u4e00\u884c\u5c31\u53d1\u4e00\u884c&#xff0c;\u6536\u5230\u56de\u590d\u540e\u6253\u5370\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5929\u7684\u6d4b\u8bd5\u56de\u8c03\u7ed9\u6bcf\u6761\u6d88\u606f\u52a0\u4e0a hello,  \u524d\u7f00&#xff0c;\u4f8b\u5982\u53d1 world \u6536\u5230 hello, world\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"2.-\u7b2c\u4e00\u90e8\u5206\uff1a\u4e92\u65a5\u9501\u6a21\u5757-mutex.hpp\">2. \u7b2c\u4e00\u90e8\u5206&#xff1a;\u4e92\u65a5\u9501\u6a21\u5757 Mutex.hpp<\/h3>\n<p>\u8fd9\u662f\u6574\u4e2a\u9879\u76ee\u6700\u5e95\u5c42\u7684\u5de5\u5177&#xff0c;\u65e5\u5fd7\u6a21\u5757\u4f9d\u8d56\u5b83\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u4e0b\u8f93\u51fa\u4e0d\u4ea4\u53c9\u3002\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>namespace MutexModule<br \/>\n{<br \/>\n    class Mutex<br \/>\n    {<br \/>\n    public:<br \/>\n        Mutex()<br \/>\n        {<br \/>\n            pthread_mutex_init(&amp;_mutex, nullptr);<br \/>\n        }<br \/>\n        void Lock()<br \/>\n        {<br \/>\n            int n &#061; pthread_mutex_lock(&amp;_mutex);<br \/>\n            (void)n;<br \/>\n        }<br \/>\n        void Unlock()<br \/>\n        {<br \/>\n            int n &#061; pthread_mutex_unlock(&amp;_mutex);<br \/>\n            (void)n;<br \/>\n        }<br \/>\n        ~Mutex()<br \/>\n        {<br \/>\n            pthread_mutex_destroy(&amp;_mutex);<br \/>\n        }<br \/>\n    private:<br \/>\n        pthread_mutex_t _mutex;<br \/>\n    };<\/p>\n<p>    class LockGuard<br \/>\n    {<br \/>\n    public:<br \/>\n        LockGuard(Mutex &amp;mutex) : _mutex(mutex)<br \/>\n        {<br \/>\n            _mutex.Lock();<br \/>\n        }<br \/>\n        ~LockGuard()<br \/>\n        {<br \/>\n            _mutex.Unlock();<br \/>\n        }<br \/>\n    private:<br \/>\n        Mutex &amp;_mutex;<br \/>\n    };<br \/>\n}<\/p>\n<h4 id=\"2.1-\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83\">2.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5b83<\/h4>\n<p>\u8bbe\u60f3\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6253\u65e5\u5fd7&#xff0c;\u5982\u679c\u6ca1\u6709\u9501&#xff0c;\u4e24\u6761\u65e5\u5fd7\u7684\u5b57\u7b26\u53ef\u80fd\u4ea4\u9519\u5728\u4e00\u8d77&#xff0c;\u8f93\u51fa\u4e71\u6210\u4e00\u56e2\u3002\u56e0\u6b64\u6bcf\u6761\u65e5\u5fd7\u7684&#034;\u5199\u51fa\u52a8\u4f5c&#034;\u5fc5\u987b\u662f\u4e92\u65a5\u7684&#xff1a;\u540c\u4e00\u65f6\u523b\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u5199\u3002<\/p>\n<h4 id=\"2.2-\u9010\u6bb5\u89e3\u91ca\">2.2 \u9010\u6bb5\u89e3\u91ca<\/h4>\n<p>Mutex \u7c7b&#xff1a;\u5bf9 POSIX \u7ebf\u7a0b\u5e93\u56db\u4e2a\u539f\u751f API \u7684\u9762\u5411\u5bf9\u8c61\u5305\u88c5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6784\u9020\u51fd\u6570\u8c03 pthread_mutex_init \u521d\u59cb\u5316\u9501&#xff1b;<\/p>\n<\/li>\n<li>\n<p>Lock() \u8c03 pthread_mutex_lock \u52a0\u9501&#xff08;\u62ff\u4e0d\u5230\u9501\u5c31\u963b\u585e&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>Unlock() \u8c03 pthread_mutex_unlock \u89e3\u9501&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6790\u6784\u51fd\u6570\u8c03 pthread_mutex_destroy \u9500\u6bc1\u9501&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/li>\n<\/ul>\n<p>(void)n; \u7684\u4f5c\u7528\u662f&#034;\u663e\u5f0f\u5ffd\u7565\u8fd4\u56de\u503c&#034;&#xff0c;\u9632\u6b62\u7f16\u8bd1\u5668\u56e0\u53d8\u91cf\u672a\u4f7f\u7528\u800c\u544a\u8b66\u3002<\/p>\n<hr \/>\n<p>LockGuard \u7c7b&#xff1a;\u8fd9\u662f\u5178\u578b\u7684 RAII&#xff08;\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316&#xff09; \u624b\u6cd5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u5bf9\u8c61\u65f6\u81ea\u52a8\u52a0\u9501&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u79bb\u5f00\u4f5c\u7528\u57df\u65f6&#xff0c;\u5bf9\u8c61\u6790\u6784\u81ea\u52a8\u89e3\u9501\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u5373\u4f7f\u4e2d\u95f4\u4ee3\u7801\u629b\u5f02\u5e38\u6216\u63d0\u524d return&#xff0c;\u9501\u4e5f\u4e00\u5b9a\u4f1a\u88ab\u91ca\u653e&#xff0c;\u4ece\u6839\u4e0a\u675c\u7edd&#034;\u52a0\u4e86\u9501\u5fd8\u8bb0\u89e3&#034;\u7684\u95ee\u9898\u3002\u4f7f\u7528\u65f6\u53ea\u6709\u4e00\u884c&#xff1a;<\/p>\n<p>{<br \/>\n    LockGuard lockguard(_mutex); \/\/ \u8fdb\u5165\u4f5c\u7528\u57df&#xff0c;\u52a0\u9501<br \/>\n    \/\/ &#8230; \u4e34\u754c\u533a\u4ee3\u7801 &#8230;<br \/>\n}                                \/\/ \u79bb\u5f00\u4f5c\u7528\u57df&#xff0c;\u81ea\u52a8\u89e3\u9501<\/p>\n<p>\u6ce8\u610f LockGuard \u7684\u6210\u5458\u662f Mutex &amp;_mutex\u2014\u2014<span style=\"color:#fe2c24\">\u5f15\u7528\u7c7b\u578b<\/span>&#xff0c;\u5b83\u4e0d\u62e5\u6709\u9501&#xff0c;\u53ea\u662f\u7ed1\u5b9a\u5916\u90e8\u90a3\u628a\u9501&#xff0c;\u6240\u4ee5\u591a\u4e2a\u5b88\u536b\u53ef\u4ee5\u7ba1\u7406\u540c\u4e00\u628a\u9501\u3002<\/p>\n<hr \/>\n<h3 id=\"3.-\u7b2c\u4e8c\u90e8\u5206\uff1a\u65e5\u5fd7\u6a21\u5757-log.hpp\uff08\u7b56\u7565\u6a21\u5f0f-+-\u6d41\u5f0f\u8f93\u51fa\uff09\">3. \u7b2c\u4e8c\u90e8\u5206&#xff1a;\u65e5\u5fd7\u6a21\u5757 Log.hpp&#xff08;\u7b56\u7565\u6a21\u5f0f &#043; \u6d41\u5f0f\u8f93\u51fa&#xff09;<\/h3>\n<p>\u8fd9\u662f\u6700\u6838\u5fc3\u3001\u8e29\u5751\u6700\u591a\u7684\u6587\u4ef6\u3002\u5b83\u8981\u5b9e\u73b0\u4e24\u4e2a\u80fd\u529b&#xff1a;<\/p>\n<li>\n<p>\u4e00\u6761\u65e5\u5fd7\u5305\u542b\u65f6\u95f4\u3001\u7b49\u7ea7\u3001\u8fdb\u7a0b PID\u3001\u6e90\u6587\u4ef6\u540d\u3001\u884c\u53f7\u548c\u6b63\u6587&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u65e5\u5fd7\u53ef\u4ee5\u9009\u62e9\u6253\u5370\u5230\u5c4f\u5e55&#xff0c;\u4e5f\u53ef\u4ee5\u5199\u5165\u6587\u4ef6&#xff0c;\u672a\u6765\u8fd8\u80fd\u6269\u5c55\u5176\u4ed6\u65b9\u5f0f\u3002<\/p>\n<\/li>\n<h4 id=\"3.1-\u7b56\u7565\u6a21\u5f0f\uff1a\u62bd\u8c61\u57fa\u7c7b\u4e0e\u4e24\u4e2a\u5177\u4f53\u7b56\u7565\">3.1 \u7b56\u7565\u6a21\u5f0f&#xff1a;\u62bd\u8c61\u57fa\u7c7b\u4e0e\u4e24\u4e2a\u5177\u4f53\u7b56\u7565<\/h4>\n<p>namespace LogModule<br \/>\n{<br \/>\n    const std::string gsep &#061; &#034;\\\\r\\\\n&#034;;<\/p>\n<p>    class LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        ~LogStrategy() &#061; default;<br \/>\n        virtual void SyncLog(const std::string &amp;message) &#061; 0;<br \/>\n    };<\/p>\n<p>LogStrategy \u662f\u62bd\u8c61\u7b56\u7565&#xff1a;\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 SyncLog&#xff0c;\u542b\u4e49\u662f&#034;\u628a\u4e00\u6761\u5b8c\u6574\u65e5\u5fd7\u540c\u6b65\u5237\u51fa\u53bb&#034;\u3002\u57fa\u7c7b\u865a\u6790\u6784\u51fd\u6570\u7528 &#061; default&#xff0c;\u4fdd\u8bc1\u901a\u8fc7\u57fa\u7c7b\u6307\u9488\u9500\u6bc1\u5b50\u7c7b\u5bf9\u8c61\u65f6\u80fd\u6b63\u786e\u8c03\u7528\u5b50\u7c7b\u6790\u6784\u3002<\/p>\n<hr \/>\n<p>\u5177\u4f53\u7b56\u7565\u4e00&#xff1a;\u6253\u5370\u5230\u5c4f\u5e55<\/p>\n<p>    class ConsoleLogStrategy : public LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        ConsoleLogStrategy()<br \/>\n        {<br \/>\n        }<br \/>\n        void SyncLog(const std::string &amp;message) override<br \/>\n        {<br \/>\n            LockGuard lockguard(_mutex);<br \/>\n            std::cout &lt;&lt; message &lt;&lt; std::endl; \/\/ \u771f\u6b63\u8f93\u51fa\u5230\u663e\u793a\u5668<br \/>\n        }<br \/>\n        ~ConsoleLogStrategy()<br \/>\n        {<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        Mutex _mutex;<br \/>\n    };<\/p>\n<hr \/>\n<p>\u5177\u4f53\u7b56\u7565\u4e8c&#xff1a;\u5199\u5165\u6587\u4ef6<\/p>\n<p>    const std::string defaultpath &#061; &#034;.\/log&#034;;<br \/>\n    const std::string defaultfile &#061; &#034;my.log&#034;;<br \/>\n    class FileLogStrategy : public LogStrategy<br \/>\n    {<br \/>\n    public:<br \/>\n        FileLogStrategy(const std::string &amp;path &#061; defaultpath, const std::string &amp;file &#061; defaultfile)<br \/>\n            : _path(path),<br \/>\n              _file(file)<br \/>\n        {<br \/>\n            LockGuard lockguard(_mutex);<br \/>\n            if (std::filesystem::exists(_path))<br \/>\n            {<br \/>\n                return;<br \/>\n            }<br \/>\n            try<br \/>\n            {<br \/>\n                std::filesystem::create_directories(_path);<br \/>\n            }<br \/>\n            catch (const std::filesystem::filesystem_error &amp;e)<br \/>\n            {<br \/>\n                std::cerr &lt;&lt; e.what() &lt;&lt; &#039;\\\\n&#039;;<br \/>\n            }<br \/>\n        }<br \/>\n        void SyncLog(const std::string &amp;message) override<br \/>\n        {<br \/>\n            LockGuard lockguard(_mutex);<\/p>\n<p>            std::string filename &#061; _path &#043; (_path.back() &#061;&#061; &#039;\/&#039; ? &#034;&#034; : &#034;\/&#034;) &#043; _file;<br \/>\n            std::ofstream out(filename, std::ios::app);<br \/>\n            if (!out.is_open())<br \/>\n            {<br \/>\n                return;<br \/>\n            }<br \/>\n            out &lt;&lt; message &lt;&lt; gsep;<br \/>\n            out.close();<br \/>\n        }<br \/>\n        ~FileLogStrategy()<br \/>\n        {<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::string _path;<br \/>\n        std::string _file;<br \/>\n        Mutex _mutex;<br \/>\n    };<\/p>\n<p>\u8fd9\u91cc\u4e3b\u8981\u662f\u8bb0\u4f4f\u4ee5\u4e0b\u51e0\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u9ed8\u8ba4\u65e5\u5fd7\u8def\u5f84 .\/log\u3001\u6587\u4ef6\u540d my.log&#xff0c;\u90fd\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u53c2\u6570\u8986\u76d6&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6784\u9020\u65f6\u7528 C&#043;&#043;17 \u7684 std::filesystem \u5224\u65ad\u76ee\u5f55\u662f\u5426\u5b58\u5728&#xff0c;\u4e0d\u5b58\u5728\u5c31 create_directories \u9012\u5f52\u521b\u5efa&#xff0c;\u5e76\u7528 try\/catch \u6355\u83b7\u6587\u4ef6\u7cfb\u7edf\u5f02\u5e38&#xff08;\u6bd4\u5982\u6743\u9650\u4e0d\u8db3&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5199\u6587\u4ef6\u7528 std::ios::app \u8ffd\u52a0\u6a21\u5f0f&#xff0c;\u6bcf\u6b21\u8fd0\u884c\u4e0d\u4f1a\u8986\u76d6\u65e7\u65e5\u5fd7&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u62fc\u6587\u4ef6\u540d\u65f6\u7528\u4e09\u5143\u8868\u8fbe\u5f0f\u5904\u7406\u8def\u5f84\u672b\u5c3e\u6709\u6ca1\u6709 \/&#xff0c;\u6700\u7ec8\u62fc\u6210 .\/log\/my.log&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u91cc\u6362\u884c\u4e0d\u7528 std::endl&#xff0c;\u800c\u7528\u5168\u5c40\u5e38\u91cf gsep &#061; &#034;\\\\r\\\\n&#034;&#xff08;CRLF&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"3.2-\u65e5\u5fd7\u7b49\u7ea7\u4e0e\u65f6\u95f4\u6233\">3.2 \u65e5\u5fd7\u7b49\u7ea7\u4e0e\u65f6\u95f4\u6233<\/h4>\n<p>    enum class LogLevel<br \/>\n    {<br \/>\n        DEBUG,<br \/>\n        INFO,<br \/>\n        WARNING,<br \/>\n        ERROR,<br \/>\n        FATAL<br \/>\n    };<\/p>\n<p>enum class \u662f C&#043;&#043;11 \u7684\u5f3a\u7c7b\u578b\u679a\u4e3e&#xff0c;\u4e0d\u4f1a\u548c\u6574\u6570\u9690\u5f0f\u8f6c\u6362&#xff0c;\u4f7f\u7528\u65f6\u5fc5\u987b\u5199 LogLevel::INFO\u3002<\/p>\n<hr \/>\n<p>    std::string Level2Str(LogLevel level)<br \/>\n    {<br \/>\n        switch (level)<br \/>\n        {<br \/>\n        case LogLevel::DEBUG:   return &#034;DEBUG&#034;;<br \/>\n        case LogLevel::INFO:    return &#034;INFO&#034;;<br \/>\n        case LogLevel::WARNING: return &#034;WARNING&#034;;<br \/>\n        case LogLevel::ERROR:   return &#034;ERROR&#034;;<br \/>\n        case LogLevel::FATAL:   return &#034;FATAL&#034;;<br \/>\n        default:                return &#034;UNKNOWN&#034;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>\u679a\u4e3e\u503c\u65e0\u6cd5\u76f4\u63a5\u8f93\u51fa&#xff0c;\u7528 switch \u8f6c\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<hr \/>\n<p>    std::string GetTimeStamp()<br \/>\n    {<br \/>\n        time_t curr &#061; time(nullptr);<br \/>\n        struct tm curr_tm;<br \/>\n        localtime_r(&amp;curr, &amp;curr_tm);<br \/>\n        char timebuffer[128];<br \/>\n        snprintf(timebuffer, sizeof(timebuffer),&#034;%4d-%02d-%02d %02d:%02d:%02d&#034;,<br \/>\n            curr_tm.tm_year&#043;1900,<br \/>\n            curr_tm.tm_mon&#043;1,<br \/>\n            curr_tm.tm_mday,<br \/>\n            curr_tm.tm_hour,<br \/>\n            curr_tm.tm_min,<br \/>\n            curr_tm.tm_sec<br \/>\n        );<br \/>\n        return timebuffer;<br \/>\n    }<\/p>\n<p>\u4e3b\u8981\u8bb0\u4f4f&#xff1a;<\/p>\n<ul>\n<li>\n<p>time(nullptr) \u62ff\u5230\u4ece 1970 \u5e74\u81f3\u4eca\u7684\u79d2\u6570&#xff1b;<\/p>\n<\/li>\n<li>\n<p>localtime_r \u662f localtime \u7684\u7ebf\u7a0b\u5b89\u5168\u7248\u672c&#xff08;\u5e26 _r \u540e\u7f00&#xff0c;\u7ed3\u679c\u5199\u5165\u8c03\u7528\u8005\u63d0\u4f9b\u7684 curr_tm&#xff09;&#xff0c;\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5fc5\u987b\u7528\u8fd9\u4e2a&#xff1b;<\/p>\n<\/li>\n<li>\n<p>tm_year \u5b58\u7684\u662f&#034;\u8ddd 1900 \u5e74\u7684\u5e74\u6570&#034;&#xff0c;\u6240\u4ee5\u8981 &#043;1900&#xff1b;tm_mon \u4ece 0 \u5f00\u59cb&#xff0c;\u6240\u4ee5\u8981 &#043;1&#xff1b;<\/p>\n<\/li>\n<li>\n<p>%02d \u8868\u793a\u81f3\u5c11\u4e24\u4f4d\u3001\u4e0d\u8db3\u8865\u96f6&#xff0c;\u5f97\u5230 2026-09-13 12:56:46 \u8fd9\u6837\u7684\u5b9a\u957f\u65f6\u95f4\u4e32\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"3.3-logger-\u7c7b\u4e0e\u6d41\u5f0f\u65e5\u5fd7\u7684\u6838\u5fc3\u6280\u5de7\">3.3 Logger \u7c7b\u4e0e\u6d41\u5f0f\u65e5\u5fd7\u7684\u6838\u5fc3\u6280\u5de7<\/h4>\n<p>    class Logger<br \/>\n    {<br \/>\n    public:<br \/>\n        Logger()<br \/>\n        {<br \/>\n            EnableConsoleLogStrategy();<br \/>\n        }<br \/>\n        void EnableFileLogStrategy()<br \/>\n        {<br \/>\n            _fflush_strategy &#061; std::make_unique&lt;FileLogStrategy&gt;();<br \/>\n        }<br \/>\n        void EnableConsoleLogStrategy()<br \/>\n        {<br \/>\n            _fflush_strategy &#061; std::make_unique&lt;ConsoleLogStrategy&gt;();<br \/>\n        }<\/p>\n<p>Logger \u6301\u6709\u4e00\u4e2a std::unique_ptr&lt;LogStrategy&gt;&#xff0c;\u8fd9\u5c31\u662f\u7b56\u7565\u6a21\u5f0f\u7684&#034;\u6301\u6709\u4e00\u4e2a\u53ef\u66ff\u6362\u7684\u7b56\u7565&#034;\u3002\u60f3\u628a\u65e5\u5fd7\u4ece\u5c4f\u5e55\u5207\u5230\u6587\u4ef6&#xff0c;\u53ea\u9700\u8ba9\u6307\u9488\u6307\u5411\u53e6\u4e00\u4e2a\u5177\u4f53\u7b56\u7565\u5bf9\u8c61\u3002\u6784\u9020\u65f6\u9ed8\u8ba4\u4f7f\u7528\u5c4f\u5e55\u7b56\u7565\u3002<\/p>\n<hr \/>\n<p>\u5185\u90e8\u7c7b LogMessage&#xff1a;\u4e00\u6761&#034;\u5c1a\u672a\u5199\u5b8c&#034;\u7684\u65e5\u5fd7<\/p>\n<p>        class LogMessage<br \/>\n        {<br \/>\n        public:<br \/>\n            LogMessage(LogLevel &amp;level, std::string &amp;src_name, int line_number, Logger &amp;logger)<br \/>\n                : _curr_time(GetTimeStamp()),<br \/>\n                  _level(level),<br \/>\n                  _pid(getpid()),<br \/>\n                  _src_name(src_name),<br \/>\n                  _line_number(line_number),<br \/>\n                  _logger(logger)<br \/>\n            {<br \/>\n                std::stringstream ss;<br \/>\n                ss &lt;&lt; &#034;[&#034; &lt;&lt; _curr_time &lt;&lt; &#034;] &#034;<br \/>\n                   &lt;&lt; &#034;[&#034; &lt;&lt; Level2Str(_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; _src_name &lt;&lt; &#034;] &#034;<br \/>\n                   &lt;&lt; &#034;[&#034; &lt;&lt; _line_number &lt;&lt; &#034;] &#034;<br \/>\n                   &lt;&lt; &#034;- &#034;;<br \/>\n                _loginfo &#061; ss.str();<br \/>\n            }<\/p>\n<hr \/>\n<p>\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u62fc\u65e5\u5fd7\u7684\u5de6\u534a\u90e8\u5206&#xff08;\u56fa\u5b9a\u524d\u7f00&#xff09;&#xff1a;<\/p>\n<p>[2026-09-13 12:56:46] [INFO] [771141] [UdpServer.hpp] [40] &#8211;<\/p>\n<p>\u4f9d\u6b21\u662f\u65f6\u95f4\u3001\u7b49\u7ea7\u3001\u8fdb\u7a0b PID&#xff08;getpid()&#xff09;\u3001\u6e90\u6587\u4ef6\u540d\u3001\u884c\u53f7\u3002<\/p>\n<p>            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<hr \/>\n<p>\u6a21\u677f\u7248 operator&lt;&lt; \u8d1f\u8d23\u65e5\u5fd7\u7684\u53f3\u534a\u90e8\u5206&#xff08;\u53ef\u53d8\u6b63\u6587&#xff09;&#xff1a;\u65e0\u8bba\u4f20\u5165\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u90fd\u5148\u7528 stringstream \u8f6c\u6210\u5b57\u7b26\u4e32\u518d\u8ffd\u52a0\u3002\u8fd4\u56de\u81ea\u8eab\u5f15\u7528&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528 &lt;&lt; a &lt;&lt; b &lt;&lt; c\u3002<\/p>\n<p>            \/\/ \u4e13\u95e8\u63a5\u6536 std::endl \/ std::flush \u7b49\u6d41\u64cd\u7eb5\u7b26(\u51fd\u6570\u6a21\u677f\u65e0\u6cd5\u88ab\u4e0a\u9762\u7684\u6a21\u677f operator&lt;&lt; \u63a8\u5bfc)<br \/>\n            \/\/ \u6362\u884c\u7531 SyncLog \u7edf\u4e00\u8ffd\u52a0, \u8fd9\u91cc\u76f4\u63a5\u5ffd\u7565\u5373\u53ef<br \/>\n            LogMessage &amp;operator&lt;&lt;(std::ostream &amp;(*manip)(std::ostream &amp;))<br \/>\n            {<br \/>\n                (void)manip;<br \/>\n                return *this;<br \/>\n            }<\/p>\n<p>std::endl \u4e0d\u662f\u6570\u636e\u800c\u662f\u51fd\u6570&#xff0c;\u6a21\u677f operator&lt;&lt; \u63a8\u5bfc\u4e0d\u51fa\u5b83\u7684\u7c7b\u578b&#xff0c;\u5fc5\u987b\u7528\u4e00\u4e2a&#034;\u51fd\u6570\u6307\u9488\u53c2\u6570&#034;\u7684\u975e\u6a21\u677f\u91cd\u8f7d\u4e13\u95e8\u63a5\u4f4f\u5b83\u3002\u6362\u884c\u7531\u6700\u7ec8\u7684 SyncLog \u7edf\u4e00\u52a0\u3002<\/p>\n<hr \/>\n<p>            ~LogMessage()<br \/>\n            {<br \/>\n                if (_logger._fflush_strategy)<br \/>\n                {<br \/>\n                    _logger._fflush_strategy-&gt;SyncLog(_loginfo);<br \/>\n                }<br \/>\n            }<\/p>\n<p>        private:<br \/>\n            std::string _curr_time;<br \/>\n            LogLevel _level;<br \/>\n            pid_t _pid;<br \/>\n            std::string _src_name;<br \/>\n            int _line_number;<br \/>\n            std::string _loginfo;<br \/>\n            Logger &amp;_logger;<br \/>\n        };<\/p>\n<p>\u6790\u6784\u51fd\u6570\u662f\u6574\u4e2a\u6d41\u5f0f\u65e5\u5fd7\u7684&#034;\u6273\u673a&#034;&#xff1a;\u4e00\u6761\u65e5\u5fd7\u8bed\u53e5\u5199\u5b8c\u3001\u4e34\u65f6\u5bf9\u8c61\u6790\u6784\u7684\u77ac\u95f4&#xff0c;\u624d\u628a\u62fc\u597d\u7684\u5b8c\u6574\u5b57\u7b26\u4e32\u771f\u6b63\u4ea4\u7ed9\u7b56\u7565\u8f93\u51fa\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4e1a\u52a1\u4ee3\u7801\u91cc\u4e0d\u9700\u8981\u624b\u52a8\u8c03\u7528&#034;\u6253\u5370&#034;\u3002<\/p>\n<hr \/>\n<p>\u5916\u5c42\u5165\u53e3\u4e0e\u5b8f&#xff1a;<\/p>\n<p>        \/\/ \u8fd9\u91cc\u6545\u610f\u5199\u6210\u8fd4\u56de\u4e34\u65f6\u5bf9\u8c61<br \/>\n        LogMessage operator()(LogLevel level, std::string name, int line)<br \/>\n        {<br \/>\n            return LogMessage(level, name, line, *this);<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::unique_ptr&lt;LogStrategy&gt; _fflush_strategy;<br \/>\n    };<\/p>\n<p>    Logger logger;   \/\/ \u5168\u5c40\u65e5\u5fd7\u5bf9\u8c61<\/p>\n<p>    #define LOG(level) logger(level, __FILE__, __LINE__)<br \/>\n    #define Enable_Console_Log_Strategy() logger.EnableConsoleLogStrategy()<br \/>\n    #define Enable_File_Log_Strategy() logger.EnableFileLogStrategy()<br \/>\n}<\/p>\n<p>\u4ee5\u4ee3\u7801\u91cc\u7684\u8fd9\u53e5\u4e3a\u4f8b&#xff0c;\u8d70\u4e00\u904d\u5b8c\u6574\u751f\u547d\u5468\u671f&#xff1a;<\/p>\n<p>LOG(LogLevel::INFO) &lt;&lt; &#034;bind success, sockfd : &#034; &lt;&lt; _sockfd;<\/p>\n<li>\n<p>LOG(LogLevel::INFO) \u5b8f\u5c55\u5f00\u4e3a logger(INFO, __FILE__, __LINE__)&#xff1b;<\/p>\n<\/li>\n<li>\n<p>logger.operator()(&#8230;) \u8fd4\u56de\u4e00\u4e2a\u4e34\u65f6 LogMessage \u5bf9\u8c61&#xff0c;\u6784\u9020\u65f6\u62fc\u597d\u524d\u7f00&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u4e24\u4e2a &lt;&lt; \u628a\u6b63\u6587\u8ffd\u52a0\u8fdb _loginfo&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6574\u6761\u8bed\u53e5\u7ed3\u675f&#xff0c;\u4e34\u65f6\u5bf9\u8c61\u6790\u6784&#xff0c;\u8c03\u7528\u5f53\u524d\u7b56\u7565\u7684 SyncLog \u8f93\u51fa\u3002<\/p>\n<\/li>\n<p>__FILE__ \u548c __LINE__ \u662f\u7f16\u8bd1\u5668\u5185\u7f6e\u5b8f&#xff0c;\u81ea\u52a8\u5c55\u5f00\u6210\u5f53\u524d\u6587\u4ef6\u540d\u548c\u884c\u53f7&#xff0c;\u6240\u4ee5\u6bcf\u6761\u65e5\u5fd7\u80fd\u7cbe\u786e\u5b9a\u4f4d\u5230\u4ee3\u7801\u884c\u3002<\/p>\n<hr \/>\n<h3 id=\"4.-\u7b2c\u4e09\u90e8\u5206\uff1audp-\u670d\u52a1\u5668-udpserver.hpp\uff08\u6700\u7ec8\u56de\u8c03\u7248\uff09\">4. \u7b2c\u4e09\u90e8\u5206&#xff1a;UDP \u670d\u52a1\u5668 UdpServer.hpp&#xff08;\u56de\u8c03\u7248&#xff09;<\/h3>\n<h4 id=\"4.1-\u5934\u6587\u4ef6\u4e0e\u7c7b\u578b\u522b\u540d\">4.1 \u5934\u6587\u4ef6\u4e0e\u7c7b\u578b\u522b\u540d<\/h4>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;strings.h&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;Log.hpp&#034;<\/p>\n<p>using namespace LogModule;<\/p>\n<p>using func_t &#061; std::function&lt;std::string(const std::string&amp;)&gt;;<\/p>\n<p>const int defaultfd &#061; -1;<\/p>\n<ul>\n<li>\n<p>&lt;functional&gt;&#xff1a;\u4e3a\u4e86\u7528 std::function&#xff1b;<\/p>\n<\/li>\n<li>\n<p>&lt;strings.h&gt;&#xff1a;\u63d0\u4f9b bzero&#xff1b;<\/p>\n<\/li>\n<li>\n<p>&lt;sys\/socket.h&gt;\u3001&lt;netinet\/in.h&gt;\u3001&lt;arpa\/inet.h&gt;&#xff1a;socket API\u3001\u5730\u5740\u7ed3\u6784\u4f53\u3001\u5b57\u8282\u5e8f\/IP \u8f6c\u6362\u51fd\u6570&#xff1b;<\/p>\n<\/li>\n<li>\n<p>func_t \u662f\u56de\u8c03\u7c7b\u578b&#xff1a;\u5403\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff08;\u5ba2\u6237\u7aef\u6d88\u606f&#xff09;&#xff0c;\u5410\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff08;\u5904\u7406\u7ed3\u679c&#xff09;\u3002\u4efb\u4f55\u7b7e\u540d\u5339\u914d\u7684\u666e\u901a\u51fd\u6570\u3001lambda\u3001\u4eff\u51fd\u6570\u90fd\u80fd\u4f20\u8fdb\u6765&#xff1b;<\/p>\n<\/li>\n<li>\n<p>defaultfd &#061; -1&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u975e\u6cd5\u503c&#xff0c;\u6784\u9020\u65f6\u5148\u628a _sockfd \u7f6e\u4e3a -1\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"4.2-\u6784\u9020\u51fd\u6570\">4.2 \u6784\u9020\u51fd\u6570<\/h4>\n<p>class UdpServer<br \/>\n{<br \/>\npublic:<br \/>\n    UdpServer(uint16_t port, func_t func)<br \/>\n        : _sockfd(defaultfd),<br \/>\n        \/\/   _ip(ip),<br \/>\n          _port(port),<br \/>\n          _isrunning(false),<br \/>\n          _func(func)<br \/>\n    {<br \/>\n    }<\/p>\n<p>\u53ea\u63a5\u6536\u4e24\u4e2a\u53c2\u6570&#xff1a;\u7aef\u53e3\u53f7\u548c\u4e1a\u52a1\u56de\u8c03\u3002\u6ce8\u610f\u88ab\u6ce8\u91ca\u6389\u7684 _ip(ip)\u2014\u2014\u8fd9\u662f\u5f53\u5929\u6211\u8fed\u4ee3\u7684\u75d5\u8ff9&#xff1a;\u65e9\u671f\u7248\u672c\u6784\u9020\u51fd\u6570\u662f UdpServer(const std::string &amp;ip, uint16_t port)&#xff0c;\u540e\u6765\u6539\u7528 INADDR_ANY \u540e\u4e0d\u518d\u9700\u8981\u6307\u5b9a IP\u3002<\/p>\n<hr \/>\n<p>\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<p>private:<br \/>\n    int _sockfd;<br \/>\n    uint16_t _port;<br \/>\n    \/\/ std::string _ip;<br \/>\n    bool _isrunning;<\/p>\n<p>    func_t _func; \/\/ \u670d\u52a1\u5668\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u7528\u6765\u8fdb\u884c\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406<\/p>\n<hr \/>\n<h4 id=\"4.3-init()\uff1a\u521b\u5efa\u5957\u63a5\u5b57-+-\u7ed1\u5b9a\">4.3 Init()&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57 &#043; \u7ed1\u5b9a<\/h4>\n<p>    void Init()<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        _sockfd &#061; socket(AF_INET, SOCK_DGRAM, 0);<br \/>\n        if (_sockfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;socket error!&#034;;<br \/>\n            exit(1);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;socket success, sockfd : &#034; &lt;&lt; _sockfd;<\/p>\n<p>socket(AF_INET, SOCK_DGRAM, 0) \u4e09\u4e2a\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>AF_INET&#xff1a;IPv4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>SOCK_DGRAM&#xff1a;\u6570\u636e\u62a5\u5957\u63a5\u5b57&#xff0c;\u5373 UDP&#xff08;TCP \u5bf9\u5e94 SOCK_STREAM&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570 0&#xff1a;\u8ba9\u5185\u6838\u6309\u524d\u4e24\u4e2a\u53c2\u6570\u81ea\u52a8\u9009\u9ed8\u8ba4\u534f\u8bae\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6210\u529f\u8fd4\u56de\u975e\u8d1f\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5b9e\u6d4b\u4ece 3 \u5f00\u59cb&#xff0c;\u56e0\u4e3a 0\/1\/2 \u5df2\u88ab\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u9519\u8bef\u5360\u7528&#xff09;&#xff0c;\u5931\u8d25\u8fd4\u56de -1\u3002\u5931\u8d25\u5c5e\u81f4\u547d\u9519\u8bef&#xff0c;\u6253 FATAL \u65e5\u5fd7\u540e exit(1)\u3002<\/p>\n<hr \/>\n<p>        \/\/ 2. \u7ed1\u5b9asocket\u4fe1\u606f&#xff0c;ip\u548c\u7aef\u53e3<br \/>\n        struct sockaddr_in local;<br \/>\n        bzero(&amp;local, 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; inet_addr(_ip.c_str()); \/\/ TODO<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>        int n &#061; bind(_sockfd, (struct sockaddr *)&amp;local, sizeof(local));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(LogLevel::FATAL) &lt;&lt; &#034;bind error&#034;;<br \/>\n            exit(2);<br \/>\n        }<br \/>\n        LOG(LogLevel::INFO) &lt;&lt; &#034;bind success, sockfd : &#034; &lt;&lt; _sockfd;<br \/>\n    }<\/p>\n<p>\u5730\u5740\u7ed3\u6784\u4f53 sockaddr_in \u7684\u586b\u5145\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u529f&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<p>\u5b57\u6bb5<\/p>\n<p>\u586b\u4ec0\u4e48<\/p>\n<p>\u4e3a\u4ec0\u4e48<\/p>\n<\/tr>\n<tr>\n<td>\n<p>sin_family<\/p>\n<\/td>\n<td>\n<p>AF_INET<\/p>\n<\/td>\n<td>\n<p>\u5730\u5740\u65cf&#xff0c;\u548c socket() \u4fdd\u6301\u4e00\u81f4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>sin_port<\/p>\n<\/td>\n<td>\n<p>htons(_port)<\/p>\n<\/td>\n<td>\n<p>\u7aef\u53e3\u5fc5\u987b\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>sin_addr.s_addr<\/p>\n<\/td>\n<td>\n<p>INADDR_ANY<\/p>\n<\/td>\n<td>\n<p>\u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\n<p>bzero \u5148\u628a\u6574\u4e2a\u7ed3\u6784\u4f53\u6e05\u96f6&#xff0c;\u907f\u514d\u672a\u8d4b\u503c\u5b57\u6bb5\u5e26\u7740\u5783\u573e\u503c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>htons &#061; host to network short&#xff0c;\u628a\u7aef\u53e3\u4ece\u4e3b\u673a\u5b57\u8282\u5e8f\u8f6c\u7f51\u7edc\u5b57\u8282\u5e8f&#xff1b;<\/p>\n<\/li>\n<li>\n<p>INADDR_ANY \u7684\u503c\u5c31\u662f 0&#xff08;\u5373 0.0.0.0&#xff09;&#xff0c;htonl(0) \u4ecd\u662f 0&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u4e0d\u7528\u518d\u5305\u4e00\u5c42\u8f6c\u6362&#xff1b;<\/p>\n<\/li>\n<li>\n<p>bind \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7c7b\u578b\u662f\u901a\u7528\u7684 struct sockaddr*&#xff0c;\u5fc5\u987b\u5f3a\u8f6c&#xff1b;\u5931\u8d25\u65f6&#xff08;\u6700\u5e38\u89c1\u662f\u7aef\u53e3\u88ab\u5360\u7528&#xff09;exit(2)\u3002<\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u4e3a\u4ec0\u4e48\u670d\u52a1\u5668\u5fc5\u987b\u663e\u5f0f bind&#xff0c;\u800c\u5ba2\u6237\u7aef\u4e0d\u7528&#xff1f;<\/span><\/p>\n<p>\u56e0\u4e3a\u670d\u52a1\u5668\u7684 IP &#043; \u7aef\u53e3\u5fc5\u987b\u662f\u4f17\u6240\u5468\u77e5\u3001\u56fa\u5b9a\u4e0d\u53d8\u7684&#xff0c;\u5ba2\u6237\u7aef\u624d\u80fd\u627e\u5230\u5b83&#xff1b;\u5ba2\u6237\u7aef\u7aef\u53e3\u7531\u64cd\u4f5c\u7cfb\u7edf\u9996\u6b21\u53d1\u9001\u65f6\u968f\u673a\u5206\u914d\u5373\u53ef\u3002<\/p>\n<hr \/>\n<h4 id=\"4.4-start()\uff1a\u6536\u53d1\u5faa\u73af\">4.4 Start()&#xff1a;\u6536\u53d1\u5faa\u73af<\/h4>\n<p>    void Start()<br \/>\n    {<br \/>\n        _isrunning &#061; true;<br \/>\n        while (_isrunning)<br \/>\n        {<br \/>\n            char buffer[1024];<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<br \/>\n            \/\/ 1. \u6536\u6d88\u606f<br \/>\n            ssize_t s &#061; recvfrom(_sockfd, buffer, sizeof(buffer) &#8211; 1, 0, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (s &gt; 0)<br \/>\n            {<br \/>\n                int peer_port &#061; ntohs(peer.sin_port);<br \/>\n                std::string peer_ip &#061; inet_ntoa(peer.sin_addr);<\/p>\n<p>                buffer[s] &#061; 0;<\/p>\n<p>                std::string result &#061; _func(buffer);<\/p>\n<p>                \/\/ LOG(LogLevel::DEBUG) &lt;&lt; &#034;[&#034; &lt;&lt; peer_ip &lt;&lt; &#034;:&#034; &lt;&lt; peer_port&lt;&lt; &#034;]# &#034; &lt;&lt; buffer;<\/p>\n<p>                \/\/ 2. \u53d1\u6d88\u606f<br \/>\n                sendto(_sockfd, result.c_str(), result.size(), 0, (struct sockaddr*)&amp;peer, len);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>\u9010\u6b65\u89e3\u91ca&#xff1a;<\/p>\n<li>\n<p>recvfrom&#xff1a;UDP \u6536\u6570\u636e\u7684\u6838\u5fc3\u8c03\u7528\u3002\u5b83\u4e0d\u4ec5\u628a\u6570\u636e\u653e\u8fdb buffer&#xff0c;\u8fd8\u4f1a\u628a\u53d1\u9001\u65b9\u7684\u5b8c\u6574\u5730\u5740\u5199\u8fdb peer&#xff0c;\u628a\u5730\u5740\u957f\u5ea6\u5199\u8fdb len\u3002\u8fd4\u56de\u503c\u662f\u6536\u5230\u7684\u5b57\u8282\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u6536\u5305\u957f\u5ea6\u4e0a\u9650\u4f20 sizeof(buffer) &#8211; 1&#xff08;1023&#xff09;&#xff0c;\u6545\u610f\u7559\u4e00\u4e2a\u5b57\u8282\u3002<\/p>\n<\/li>\n<li>\n<p>if (s &gt; 0)&#xff1a;\u53ea\u6709\u786e\u5b9e\u6536\u5230\u6570\u636e\u624d\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bc6\u522b\u53d1\u9001\u65b9&#xff1a;peer.sin_port \u662f\u7f51\u7edc\u5b57\u8282\u5e8f&#xff0c;\u7528 ntohs&#xff08;network to host short&#xff09;\u8f6c\u56de\u4e3b\u673a\u5b57\u8282\u5e8f&#xff1b;peer.sin_addr \u662f 4 \u5b57\u8282\u7f51\u7edc\u5e8f IP&#xff0c;\u7528 inet_ntoa \u8f6c\u6210 &#034;127.0.0.1&#034; \u8fd9\u6837\u7684\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>buffer[s] &#061; 0&#xff1a;UDP \u5305\u4e0d\u662f C \u5b57\u7b26\u4e32&#xff0c;\u4e0d\u4f1a\u81ea\u52a8\u5e26 &#039;\\\\0&#039;\u3002\u624b\u52a8\u5728\u6709\u6548\u6570\u636e\u672b\u5c3e\u8865\u96f6&#xff0c;\u624d\u80fd\u5b89\u5168\u5730\u5f53\u5b57\u7b26\u4e32\u4f7f\u7528&#xff0c;\u5426\u5219\u4f1a\u8bfb\u51fa\u7f13\u51b2\u533a\u540e\u9762\u7684\u810f\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>std::string result &#061; _func(buffer);&#xff1a;\u628a\u6d88\u606f\u4ea4\u7ed9\u6ce8\u5165\u8fdb\u6765\u7684\u56de\u8c03\u51fd\u6570\u5904\u7406\u3002\u670d\u52a1\u5668\u672c\u8eab\u5b8c\u5168\u4e0d\u5173\u5fc3\u4e1a\u52a1<\/p>\n<\/li>\n<li>\n<p>sendto&#xff1a;\u628a\u56de\u8c03\u8fd4\u56de\u7684\u7ed3\u679c\u53d1\u56de peer\u3002\u56e0\u4e3a\u56de\u590d\u5730\u5740\u76f4\u63a5\u7528 recvfrom \u5e26\u56de\u7684 peer&#xff0c;\u670d\u52a1\u5668\u4e0d\u9700\u8981\u7ef4\u62a4\u4efb\u4f55\u5ba2\u6237\u7aef\u5217\u8868&#xff0c;\u5929\u7136\u652f\u6301\u591a\u4e2a\u5ba2\u6237\u7aef\u3002<\/p>\n<\/li>\n<p>UDP \u670d\u52a1\u5668\u6ca1\u6709 TCP \u7684 listen\u3001accept&#xff0c;\u6574\u4e2a\u751f\u547d\u5468\u671f\u5c31\u662f\u8fd9\u56db\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff1a;socket &#8211;&gt;\u00a0bind &#8211;&gt;\u00a0recvfrom &#8211;&gt;\u00a0sendto\u3002<\/p>\n<hr \/>\n<h4 id=\"4.5-\u6790\u6784\u51fd\u6570\">4.5 \u6790\u6784\u51fd\u6570<\/h4>\n<p>    ~UdpServer()<br \/>\n    {<br \/>\n    }<\/p>\n<p>\u5f53\u524d\u4e3a\u7a7a\u3002\u6587\u4ef6\u63cf\u8ff0\u7b26\u5728\u8fdb\u7a0b\u9000\u51fa\u65f6\u7531\u64cd\u4f5c\u7cfb\u7edf\u56de\u6536&#xff1b;\u5982\u679c\u4ee5\u540e\u8981\u505a\u4f18\u96c5\u9000\u51fa&#xff0c;\u53ef\u4ee5\u5728\u8fd9\u91cc close(_sockfd)\u3002<\/p>\n<hr \/>\n<h3 id=\"5.-\u7b2c\u56db\u90e8\u5206\uff1a\u670d\u52a1\u5668\u5165\u53e3-udpserver.cc\">5. \u7b2c\u56db\u90e8\u5206&#xff1a;\u670d\u52a1\u5668\u5165\u53e3 UdpServer.cc<\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &#034;UdpServer.hpp&#034;<\/p>\n<p>\/\/ \u4ec5\u4ec5\u662f\u7528\u6765\u8fdb\u884c\u6d4b\u8bd5\u7684<br \/>\nstd::string defaulthandler(const std::string &amp;message)<br \/>\n{<br \/>\n    std::string hello &#061; &#034;hello, &#034;;<br \/>\n    hello &#043;&#061; message;<br \/>\n    return hello;<br \/>\n}<\/p>\n<p>\/\/ .\/udpserver port<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 2)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; port&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<\/p>\n<p>    Enable_Console_Log_Strategy();<\/p>\n<p>    std::unique_ptr&lt;UdpServer&gt; usvr &#061; std::make_unique&lt;UdpServer&gt;(port, defaulthandler);<br \/>\n    usvr-&gt;Init();<br \/>\n    usvr-&gt;Start();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u4e3b\u8981\u8bb0\u4f4f&#xff1a;<\/p>\n<ul>\n<li>\n<p>defaulthandler \u5c31\u662f\u4e00\u4e2a\u7b26\u5408 func_t \u7b7e\u540d\u7684\u666e\u901a\u51fd\u6570&#xff1a;\u6536\u5230\u4ec0\u4e48\u90fd\u5728\u524d\u9762\u62fc hello, \u3002\u8fd9\u4e2a\u8bc1\u660e\u4e86\u56de\u8c03\u673a\u5236\u2014\u2014\u60f3\u6362\u6210&#034;\u7ffb\u8bd1&#034;&#034;\u5927\u5c0f\u5199\u8f6c\u6362&#034;&#034;\u8ba1\u7b97\u5668&#034;&#xff0c;\u53ea\u9700\u53e6\u5199\u4e00\u4e2a\u51fd\u6570\u4f20\u5165&#xff0c;UdpServer \u4e00\u884c\u90fd\u4e0d\u7528\u6539\u3002<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u884c\u53c2\u6570\u7ea6\u5b9a\u4e3a\u4e00\u4e2a\u7aef\u53e3\u53f7&#xff1a;argc !&#061; 2&#xff08;\u7a0b\u5e8f\u540d\u672c\u8eab\u7b97 argv[0]&#xff09;\u65f6\u6253\u5370\u7528\u6cd5\u5e76\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>std::stoi \u628a\u5b57\u7b26\u4e32\u8f6c\u6210\u6574\u6570&#xff0c;\u518d\u9690\u5f0f\u6536\u7a84\u4e3a uint16_t\u3002<\/p>\n<\/li>\n<li>\n<p>Enable_Console_Log_Strategy() \u662f\u5b8f&#xff0c;\u5c55\u5f00\u4e3a\u5bf9\u5168\u5c40 logger \u7684\u8c03\u7528&#xff0c;\u628a\u65e5\u5fd7\u8f93\u51fa\u5207\u5230\u5c4f\u5e55&#xff08;\u5176\u5b9e\u6784\u9020\u65f6\u9ed8\u8ba4\u5c31\u662f\u5c4f\u5e55\u7b56\u7565&#xff0c;\u8fd9\u91cc\u5c5e\u4e8e\u663e\u5f0f\u58f0\u660e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u7528 std::make_unique&lt;UdpServer&gt;(port, defaulthandler) \u521b\u5efa\u5bf9\u8c61&#xff1a;\u51fd\u6570\u540d\u4f5c\u4e3a\u51fd\u6570\u6307\u9488\u4f20\u5165&#xff0c;\u80fd\u9690\u5f0f\u6784\u9020 std::function\u3002<\/p>\n<\/li>\n<li>\n<p>\u542f\u52a8\u987a\u5e8f\u56fa\u5b9a&#xff1a;Init()&#xff08;\u521b\u5efa&#043;\u7ed1\u5b9a&#xff09;\u2192 Start()&#xff08;\u8fdb\u5165\u6b7b\u5faa\u73af&#xff0c;\u6b63\u5e38\u60c5\u51b5\u4e0b\u6c38\u4e0d\u8fd4\u56de&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"6.-\u7b2c\u4e94\u90e8\u5206\uff1audp-\u5ba2\u6237\u7aef-udpclient.cc\">6. \u7b2c\u4e94\u90e8\u5206&#xff1a;UDP \u5ba2\u6237\u7aef UdpClient.cc<\/h3>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;string&gt;<br \/>\n#include&lt;cstdint&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;cstring&gt;<br \/>\n\/\/.\/udpclient serverip &#043; severport<br \/>\nint main(int argc,char *argv[])<br \/>\n{<br \/>\n    if(argc!&#061;3)<br \/>\n    {<br \/>\n        std::cerr&lt;&lt;&#034;Usage: &#034;&lt;&lt;argv[0]&lt;&lt;&#034; server_ip server_port&#034;&lt;&lt;std::endl;<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    std::string server_ip &#061; argv[1];<br \/>\n    uint16_t server_port &#061; std::stoi(argv[2]);<br \/>\n    int sockfd &#061; socket(AF_INET,SOCK_DGRAM,0);<br \/>\n    if(sockfd&lt;0)<br \/>\n    {<br \/>\n        std::cerr&lt;&lt;&#034;socket error&#034;&lt;&lt;std::endl;<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>\u5ba2\u6237\u7aef\u540c\u6837\u5148\u521b\u5efa UDP \u5957\u63a5\u5b57&#xff0c;\u4f46<span style=\"color:#fe2c24\">\u53ea\u586b\u670d\u52a1\u5668\u5730\u5740<\/span>&#xff1a;<\/p>\n<p>    \/\/ client\u8981\u4e0d\u8981bind&#xff1f;\u9700\u8981bind\u3002<br \/>\n    \/\/ client\u8981\u4e0d\u8981\u663e\u793a\u7684bind&#xff1f;\u4e0d\u8981&#xff01;&#xff01;\u9996\u6b21\u53d1\u9001\u6d88\u606f&#xff0c;OS\u4f1a\u81ea\u52a8\u7ed9client\u8fdb\u884cbind&#xff0c;<br \/>\n    \/\/ OS\u77e5\u9053IP&#xff0c;\u7aef\u53e3\u53f7\u91c7\u7528\u968f\u673a\u7aef\u53e3\u53f7\u7684\u65b9\u5f0f<br \/>\n    \/\/ \u4e3a\u4ec0\u4e48&#xff1f;\u4e00\u4e2a\u7aef\u53e3\u53f7\u53ea\u80fd\u88ab\u4e00\u4e2a\u8fdb\u7a0bbind&#xff0c;\u4e3a\u4e86\u907f\u514dclient\u7aef\u53e3\u51b2\u7a81<br \/>\n    struct sockaddr_in server;<br \/>\n    server.sin_family &#061; AF_INET;<br \/>\n    server.sin_addr.s_addr &#061; inet_addr(server_ip.c_str());<br \/>\n    server.sin_port &#061; htons(server_port);<\/p>\n<p>\u8fd9\u91cc\u7684\u6ce8\u91ca\u8bb0\u5f55\u4e86\u4e00\u4e2a\u91cd\u8981\u77e5\u8bc6\u70b9&#xff1a;\u5ba2\u6237\u7aef\u4e0d\u663e\u5f0f bind\u3002\u9996\u6b21 sendto \u65f6\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u7ed1\u5b9a\u672c\u673a IP \u548c\u4e00\u4e2a\u968f\u673a\u7a7a\u95f2\u7aef\u53e3\u3002\u56e0\u4e3a\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u88ab\u522b\u4eba&#034;\u4e3b\u52a8\u627e\u5230&#034;&#xff0c;\u968f\u673a\u7aef\u53e3\u8fd8\u80fd\u907f\u514d\u591a\u5f00\u5ba2\u6237\u7aef\u65f6\u7684\u7aef\u53e3\u51b2\u7a81\u3002<\/p>\n<p>inet_addr \u628a &#034;127.0.0.1&#034; \u8fd9\u79cd\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6210\u7f51\u7edc\u5b57\u8282\u5e8f\u7684 4 \u5b57\u8282\u6574\u6570&#xff0c;\u6240\u4ee5\u4e0d\u9700\u8981\u518d\u5957 htonl\u3002<\/p>\n<hr \/>\n<p>\u6536\u53d1\u4e3b\u5faa\u73af&#xff1a;<\/p>\n<p>    while(true)<br \/>\n    {<br \/>\n        std::string input;<br \/>\n        std::cout&lt;&lt;&#034;Please Enter# &#034;&lt;&lt;std::flush;<br \/>\n        if(!std::getline(std::cin,input)) break;<br \/>\n        if(input.empty()) continue;<\/p>\n<p>        int n &#061; sendto(sockfd,input.c_str(),input.size(),0,(struct sockaddr*)&amp;server,sizeof(server));<br \/>\n        (void) n;<br \/>\n        char buffer[1024];<br \/>\n        struct sockaddr_in peer;<br \/>\n        socklen_t len &#061; sizeof(peer);<br \/>\n        int m &#061; recvfrom(sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&amp;peer,&amp;len);<br \/>\n        if(m&gt;0)<br \/>\n        {<br \/>\n            buffer[m] &#061; 0;<br \/>\n            std::cout&lt;&lt;buffer&lt;&lt;std::endl;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u9010\u884c\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>std::cout &lt;&lt; &#034;Please Enter# &#034; &lt;&lt; std::flush;&#xff1a;\u7528 flush \u800c\u4e0d\u662f endl&#xff0c;\u56e0\u4e3a\u53ea\u9700\u8981\u63d0\u793a\u7acb\u5373\u663e\u793a\u3001\u4e0d\u9700\u8981\u6362\u884c&#xff08;\u5149\u6807\u505c\u5728\u63d0\u793a\u540e\u9762\u65b9\u4fbf\u8f93\u5165&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>if(!std::getline(std::cin,input)) break;&#xff1a;\u8bfb\u53d6\u4e00\u6574\u884c\u3002\u5fc5\u987b\u68c0\u67e5\u8fd4\u56de\u503c\u2014\u2014\u5f53\u8f93\u5165\u7ed3\u675f&#xff08;Ctrl&#043;D \u89e6\u53d1 EOF&#xff09;\u65f6 getline \u5931\u8d25&#xff0c;\u82e5\u4e0d\u9000\u51fa\u4f1a\u9677\u5165\u6b7b\u5faa\u73af\u75af\u72c2\u6253\u5370\u63d0\u793a<\/p>\n<\/li>\n<li>\n<p>if(input.empty()) continue;&#xff1a;\u76f4\u63a5\u6309\u56de\u8f66\u4ea7\u751f\u7a7a\u884c\u65f6\u8df3\u8fc7\u53d1\u9001\u3002\u7a7a UDP \u5305\u53d1\u51fa\u540e\u670d\u52a1\u5668 s &gt; 0 \u4e0d\u6210\u7acb\u3001\u4e0d\u56de\u590d&#xff0c;\u5ba2\u6237\u7aef\u4f1a\u6c38\u8fdc\u963b\u585e\u5728 recvfrom\u3002<\/p>\n<\/li>\n<li>\n<p>sendto&#xff1a;\u628a\u6574\u884c\u5b57\u7b26\u4e32\u53d1\u7ed9 server&#xff0c;\u957f\u5ea6\u7528 input.size()&#xff08;\u4e0d\u542b C \u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684 &#039;\\\\0&#039;&#xff0c;\u7f51\u7edc\u4e0a\u672c\u6765\u4e5f\u4e0d\u8be5\u53d1\u5b83&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u968f\u540e\u963b\u585e\u5728 recvfrom \u7b49\u670d\u52a1\u5668\u56de\u590d&#xff0c;\u6536\u5230\u540e\u8865 &#039;\\\\0&#039; \u5e76\u6253\u5370\u3002\u8fd9\u5c31\u662f&#034;\u53d1\u4e00\u53e5\u3001\u7b49\u4e00\u53e5&#034;\u7684\u95ee\u7b54\u6a21\u5f0f\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"7.-\u7b2c\u516d\u90e8\u5206\uff1amakefile-\u4e0e\u7f16\u8bd1\u65b9\u5f0f\">7. \u7b2c\u516d\u90e8\u5206&#xff1a;Makefile \u4e0e\u7f16\u8bd1\u65b9\u5f0f<\/h3>\n<p>.PHONY:all<br \/>\nall:udpclient udpserver<\/p>\n<p>udpclient:UdpClient.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17 -pthread<br \/>\nudpserver:UdpServer.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;17 -pthread<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -f udpclient udpserver<\/p>\n<ul>\n<li>\n<p>$&#064; \u4ee3\u8868\u76ee\u6807\u540d&#xff0c;$^ \u4ee3\u8868\u6240\u6709\u4f9d\u8d56&#xff0c;\u6240\u4ee5\u4e24\u6761\u89c4\u5219\u5206\u522b\u7b49\u4ef7\u4e8e g&#043;&#043; -o udpclient UdpClient.cc &#8230;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>-std&#061;c&#043;&#043;17&#xff1a;\u4ee3\u7801\u7528\u4e86 std::filesystem&#xff0c;\u5fc5\u987b C&#043;&#043;17&#xff1b;<\/p>\n<\/li>\n<li>\n<p>-pthread&#xff1a;\u4ee3\u7801\u7528\u4e86 pthread \u4e92\u65a5\u9501\u3002\u8fd9\u662f\u5f53\u5929\u8865\u4e0a\u7684\u9009\u9879&#xff0c;\u5b83\u540c\u65f6\u5904\u7406\u9884\u5904\u7406\u5b8f\u548c\u94fe\u63a5\u5e93&#xff0c;\u6bd4\u624b\u5199 -lpthread \u66f4\u89c4\u8303&#xff1b;<\/p>\n<\/li>\n<li>\n<p>.PHONY \u58f0\u660e all \u548c clean \u662f\u4f2a\u76ee\u6807&#xff0c;\u907f\u514d\u76ee\u5f55\u91cc\u6070\u597d\u6709\u540c\u540d\u6587\u4ef6\u65f6\u89c4\u5219\u5931\u6548\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7f16\u8bd1\u4e0e\u6e05\u7406&#xff1a;<\/p>\n<p>make        # \u7f16\u8bd1\u4e24\u4e2a\u7a0b\u5e8f<br \/>\nmake clean  # \u5220\u9664\u4e24\u4e2a\u53ef\u6267\u884c\u6587\u4ef6<\/p>\n<p>\u8fd0\u884c&#xff08;\u4e24\u4e2a\u7ec8\u7aef&#xff09;&#xff1a;<\/p>\n<p># \u7ec8\u7aef A&#xff1a;\u670d\u52a1\u5668<br \/>\n.\/udpserver 8080<\/p>\n<p># \u7ec8\u7aef B&#xff1a;\u5ba2\u6237\u7aef<br \/>\n.\/udpclient 127.0.0.1 8080<\/p>\n<hr \/>\n<h3 id=\"8.-\u7b2c\u4e03\u90e8\u5206\uff1a\u5b8c\u6574\u8fed\u4ee3\u8fc7\u7a0b\uff088-\u4e2a\u9636\u6bb5\uff0c\u542b\u771f\u5b9e\u62a5\u9519\uff09\">8. \u7b2c\u4e03\u90e8\u5206&#xff1a;\u8fed\u4ee3\u8fc7\u7a0b<\/h3>\n<h4>\u9636\u6bb5\u4e00&#xff1a;\u4ece\u7ed1\u5b9a\u6307\u5b9a IP \u6539\u4e3a INADDR_ANY<\/h4>\n<p>\u65e9\u671f\u670d\u52a1\u5668\u6784\u9020\u51fd\u6570\u63a5\u6536 (ip, port)&#xff0c;bind \u65f6\u5199&#xff1a;<\/p>\n<p>local.sin_addr.s_addr &#061; inet_addr(_ip.c_str());<\/p>\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u670d\u52a1\u5668\u53ea\u63a5\u6536\u53d1\u7ed9\u67d0\u4e00\u4e2a\u5177\u4f53 IP \u7684\u5305\u3002\u540e\u6765\u6539\u4e3a&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6784\u9020\u51fd\u6570\u53ea\u6536 port&#xff1b;<\/p>\n<\/li>\n<li>\n<p>local.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<\/li>\n<li>\n<p>\u6210\u5458 _ip \u6ce8\u91ca\u4fdd\u7559&#xff1b;<\/p>\n<\/li>\n<li>\n<p>main \u7684\u53c2\u6570\u6821\u9a8c\u4ece argc !&#061; 3 \u6539\u4e3a argc !&#061; 2&#xff0c;\u542f\u52a8\u65b9\u5f0f\u4ece .\/udpserver 127.0.0.1 8080 \u53d8\u4e3a .\/udpserver 8080\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"\u9636\u6bb5\u516d\uff1a\u5b9e\u6d4b-inaddr_any-\u7684\u542b\u4e49\">\u9636\u6bb5\u4e8c&#xff1a;\u5b9e\u6d4b INADDR_ANY \u7684\u542b\u4e49<\/h4>\n<p>\u6539\u5b8c\u540e\u7528\u7cfb\u7edf\u5de5\u5177\u548c\u4e24\u79cd\u5730\u5740\u5b9e\u6d4b&#xff1a;<\/p>\n<p>UNCONN 0  0  0.0.0.0:8080  0.0.0.0:*<\/p>\n<p>ss -uln \u663e\u793a\u76d1\u542c\u5728 0.0.0.0:8080&#xff08;-u UDP\u3001-l listening\u3001-n \u6570\u5b57\u683c\u5f0f&#xff09;&#xff0c;\u5373\u6240\u6709\u7f51\u5361\u3002\u5ba2\u6237\u7aef\u5206\u522b\u7528\u56de\u73af\u5730\u5740\u548c\u771f\u5b9e\u7f51\u5361 IP \u90fd\u80fd\u901a&#xff1a;<\/p>\n<p>&#061;&#061;&#061; \u7528\u56de\u73af\u5730\u5740 127.0.0.1 \u8bbf\u95ee &#061;&#061;&#061;<br \/>\nPlease Enter# server echo&#064;via loopback<br \/>\n&#061;&#061;&#061; \u7528\u771f\u5b9e\u7f51\u5361IP 10.0.0.2 \u8bbf\u95ee &#061;&#061;&#061;<br \/>\nPlease Enter# server echo&#064;via lan ip<\/p>\n<p>\u8fd9\u6f84\u6e05\u4e86&#034;\u4efb\u610f IP&#034;\u7684\u4e24\u5c42\u542b\u4e49&#xff1a;\u670d\u52a1\u5668\u7ed1\u5b9a\u672c\u673a\u6240\u6709\u7f51\u5361&#xff08;INADDR_ANY \u7684\u4f5c\u7528&#xff09;&#xff1b;UDP \u672c\u8eab\u4e0d\u6821\u9a8c\u5ba2\u6237\u7aef\u6765\u6e90&#xff0c;\u4efb\u4f55\u5730\u5740\u53d1\u6765\u7684\u5305\u90fd\u63a5\u6536\u3002\u4f46\u8de8\u673a\u5668\u80fd\u5426\u8fde\u901a\u8fd8\u53d6\u51b3\u4e8e\u8def\u7531\u53ef\u8fbe\u6027\u548c\u9632\u706b\u5899\/\u5b89\u5168\u7ec4\u662f\u5426\u653e\u884c UDP \u7aef\u53e3\u3002<\/p>\n<hr \/>\n<h4 id=\"\u9636\u6bb5\u4e03\uff1a\u8bc6\u522b\u53d1\u9001\u65b9-ip-\u548c\u7aef\u53e3\">\u9636\u6bb5\u4e09&#xff1a;\u8bc6\u522b\u53d1\u9001\u65b9 IP \u548c\u7aef\u53e3<\/h4>\n<p>\u5728 recvfrom \u4e4b\u540e\u65b0\u589e&#xff1a;<\/p>\n<p>int peer_port &#061; ntohs(peer.sin_port);<br \/>\nstd::string peer_ip &#061; inet_ntoa(peer.sin_addr);<\/p>\n<p>\u5e76\u6253\u5370 DEBUG \u65e5\u5fd7<\/p>\n<p>[127.0.0.1:37509]# hello world<br \/>\n[127.0.0.1:56592]# \u6211\u662f\u5ba2\u6237\u7aefB<br \/>\n[127.0.0.1:33066]# \u6211\u662f\u5ba2\u6237\u7aefA<br \/>\n[10.0.0.2:57611]# via lan ip<\/p>\n<p>\u65e5\u5fd7\u6e05\u695a\u663e\u793a&#xff1a;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7684\u6e90\u7aef\u53e3\u7531\u64cd\u4f5c\u7cfb\u7edf\u968f\u673a\u5206\u914d&#xff08;37509\u300156592\u300133066\u2026\u2026&#xff09;&#xff0c;\u670d\u52a1\u5668\u6b63\u662f\u9760 peer \u91cc\u7684\u8fd9\u4e2a\u5730\u5740\u628a\u56de\u590d\u51c6\u786e\u53d1\u56de\u5bf9\u5e94\u5ba2\u6237\u7aef\u3002<\/p>\n<hr \/>\n<h4 id=\"\u9636\u6bb5\u516b\uff1a\u91cd\u6784\u4e3a\u56de\u8c03\u6a21\u5f0f\uff08\u6700\u7ec8\u7248\uff09\">\u9636\u6bb5\u56db&#xff1a;\u91cd\u6784\u4e3a\u56de\u8c03\u6a21\u5f0f<\/h4>\n<p>\u6700\u540e\u4e00\u6b21\u91cd\u6784\u628a&#034;\u4e1a\u52a1\u5904\u7406&#034;\u4ece\u670d\u52a1\u5668\u91cc\u5265\u79bb&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u65b0\u589e using func_t &#061; std::function&lt;std::string(const std::string&amp;)&gt;;<\/p>\n<\/li>\n<li>\n<p>\u6784\u9020\u51fd\u6570\u589e\u52a0 func_t func \u53c2\u6570&#xff0c;\u4fdd\u5b58\u4e3a\u6210\u5458 _func&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6536\u53d1\u5faa\u73af\u4e2d\u5199\u6b7b\u7684 &#034;server echo&#064;&#034; \u62fc\u63a5\u88ab\u5220\u9664&#xff0c;\u6539\u4e3a std::string result &#061; _func(buffer); \u540e\u53d1\u56de result&#xff1b;<\/p>\n<\/li>\n<li>\n<p>main \u91cc\u5b9a\u4e49\u6d4b\u8bd5\u51fd\u6570 defaulthandler&#xff08;\u52a0 hello,  \u524d\u7f00&#xff09;\u5e76\u6ce8\u5165\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6700\u7ec8\u5b9e\u6d4b&#xff08;\u90e8\u5206\u8f93\u51fa&#xff09;&#xff1a;<\/p>\n<p>hello, world<br \/>\nhello, UDP\u670d\u52a1\u5668<br \/>\nhello, 3.14<br \/>\nhello, !&#064;#$%<\/p>\n<p>\u81f3\u6b64&#xff0c;\u6211\u4eec\u7684\u7f51\u7edc\u6846\u67b6&#xff08;UdpServer&#xff09;\u4e0e\u4e1a\u52a1\u903b\u8f91&#xff08;handler&#xff09;\u5f7b\u5e95\u89e3\u8026\u3002<\/p>\n<hr \/>\n<h3 id=\"9.-\u7b2c\u516b\u90e8\u5206\uff1a\u5b9e\u6d4b\u8bc1\u636e\u6c47\u603b\">9. \u7b2c\u516b\u90e8\u5206&#xff1a;\u5b9e\u6d4b\u6c47\u603b<\/h3>\n<p>\u7f16\u8bd1&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"40\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233310-6aab27363c7db.png\" width=\"509\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"54\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233310-6aab27369e284.png\" width=\"515\" \/><\/p>\n<p>\u670d\u52a1\u5668\u542f\u52a8\u65e5\u5fd7&#xff0c;\u591a\u7ec4\u6d4b\u8bd5\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"569\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233310-6aab2736af3e8.png\" width=\"1730\" \/><\/p>\n<hr \/>\n<h3 id=\"10.-\u7b2c\u4e5d\u90e8\u5206\uff1a\u77e5\u8bc6\u70b9\u6e05\u5355\u4e0e\u9057\u7559\u95ee\u9898\">10. \u7b2c\u4e5d\u90e8\u5206&#xff1a;\u77e5\u8bc6\u70b9\u56de\u987e<\/h3>\n<p>\u7f51\u7edc\u7f16\u7a0b<\/p>\n<li>\n<p>UDP \u56db\u4ef6\u5957&#xff1a;socket(AF_INET, SOCK_DGRAM, 0) \u2192 bind \u2192 recvfrom \u2192 sendto&#xff0c;\u65e0\u8fde\u63a5\u3001\u65e0 listen\/accept\u3002<\/p>\n<\/li>\n<li>\n<p>sockaddr_in \u4e09\u4e2a\u5fc5\u586b\u5b57\u6bb5&#xff1a;sin_family\u3001sin_port&#xff08;htons&#xff09;\u3001sin_addr.s_addr\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f51\u7edc\u5b57\u8282\u5e8f\u662f\u5927\u7aef&#xff1a;\u7aef\u53e3\u7528 htons\/ntohs&#xff1b;inet_addr \u548c inet_ntoa \u5185\u90e8\u5df2\u5904\u7406\u5b57\u8282\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>INADDR_ANY&#xff08;0.0.0.0&#xff09;\u76d1\u542c\u6240\u6709\u7f51\u5361&#xff1b;\u670d\u52a1\u5668\u5fc5\u987b\u663e\u5f0f bind&#xff0c;\u5ba2\u6237\u7aef\u7531 OS \u5728\u9996\u6b21\u53d1\u9001\u65f6\u81ea\u52a8 bind \u968f\u673a\u7aef\u53e3\u3002<\/p>\n<\/li>\n<li>\n<p>recvfrom \u540c\u65f6\u5e26\u56de\u5bf9\u7aef\u5730\u5740&#xff0c;UDP \u9760\u5b83\u652f\u6301\u591a\u5ba2\u6237\u7aef\u3001\u56de\u590d\u62a5\u6587\u3002<\/p>\n<\/li>\n<li>\n<p>UDP \u6570\u636e\u62a5\u4e0d\u5e26 &#039;\\\\0&#039;&#xff0c;\u6536\u5230\u540e\u5fc5\u987b\u624b\u52a8 buffer[s] &#061; 0 \u624d\u80fd\u5f53 C \u5b57\u7b26\u4e32\u7528\u3002<\/p>\n<\/li>\n<p>C&#043;&#043; \u4e0e\u5de5\u7a0b\u5316<\/p>\n<li>\n<p>RAII&#xff1a;LockGuard \u6784\u9020\u52a0\u9501\u3001\u6790\u6784\u89e3\u9501&#xff1b;unique_ptr \u7ba1\u7406\u7b56\u7565\u5bf9\u8c61\u548c\u670d\u52a1\u5668\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u7b56\u7565\u6a21\u5f0f&#xff1a;\u62bd\u8c61\u57fa\u7c7b &#043; \u4e24\u4e2a\u5177\u4f53\u7b56\u7565&#xff0c;\u8fd0\u884c\u671f\u66ff\u6362 _fflush_strategy\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d41\u5f0f\u65e5\u5fd7\u539f\u7406&#xff1a;\u4e34\u65f6\u5bf9\u8c61\u7528 operator&lt;&lt; \u7d2f\u79ef\u5b57\u7b26\u4e32&#xff0c;\u6790\u6784\u65f6\u7edf\u4e00\u5237\u51fa&#xff1b;__FILE__\/__LINE__ \u9760\u5b8f\u6ce8\u5165\u3002<\/p>\n<\/li>\n<li>\n<p>std::endl \u662f\u51fd\u6570\u6a21\u677f&#xff0c;\u81ea\u5b9a\u4e49\u6d41\u5f0f\u7c7b\u5fc5\u987b\u4e3a\u6d41\u64cd\u7eb5\u7b26\u5355\u72ec\u5199\u91cd\u8f7d\u3002<\/p>\n<\/li>\n<li>\n<p>std::function \u56de\u8c03\u5b9e\u73b0\u7f51\u7edc\u6846\u67b6\u4e0e\u4e1a\u52a1\u89e3\u8026\u3002<\/p>\n<\/li>\n<li>\n<p>std::filesystem::create_directories&#xff08;C&#043;&#043;17&#xff09;\u5efa\u76ee\u5f55&#xff1b;localtime_r \u662f\u7ebf\u7a0b\u5b89\u5168\u65f6\u95f4\u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f16\u8bd1\u9009\u9879&#xff1a;C&#043;&#043;17 \u914d -std&#061;c&#043;&#043;17&#xff0c;pthread \u914d -pthread\u3002<\/p>\n<\/li>\n<hr \/>\n<h3>11.\u6700\u7ec8\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"425\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233311-6aab27372a381.png\" width=\"971\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3194\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233311-6aab273756d27.png\" width=\"1035\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"6876\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233313-6aab27391ab70.png\" width=\"922\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2027\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233315-6aab273b5307f.png\" width=\"1012\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1119\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233316-6aab273c1f506.png\" width=\"1026\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1566\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233316-6aab273c8d09f.png\" width=\"871\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UDP socket \u7f16\u7a0b\u3001sockaddr_in \u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u3001INADDR_ANY\u3001pthread \u4e92\u65a5\u9501\u4e0e RAII\u3001\u7b56\u7565\u6a21\u5f0f\u3001std::function \u56de\u8c03\u3001\u6d41\u5f0f\u65e5\u5fd7\u7684\u5b9e\u73b0\u539f\u7406\u30021. \u9879\u76ee\u91cc\u6709\u4ec0\u4e48\u5305\u542b 6 \u4e2a\u6e90\u6587\u4ef6&#xff1a;\u6587\u4ef6\u804c\u8d23Mutex.hpp\u5bf9 pthread \u4e92\u65a5\u9501\u7684 C \u5c01\u88c5&#xff0c;\u5916\u52a0 RAII \u9501\u5b88\u536b LockGuardLog.hpp\u65e5\u5fd7\u6a21\u5757&#xff1a;\u65e5\u5fd7\u7b49\u7ea7\u3001\u65f6\u95f4\u6233\u3001\u6d41\u5f0f\u8f93\u51fa\u3001\u5c4f\u5e55\/\u6587\u4ef6\u4e24\u79cd\u5237\u65b0\u7b56\u7565UdpServer.hppUD<\/p>\n","protected":false},"author":2,"featured_media":106322,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12070,11653,193,43,3063,44],"topic":[],"class_list":["post-106331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-udp","tag-11653","tag-43","tag-3063","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \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\/106331.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\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"UDP socket \u7f16\u7a0b\u3001sockaddr_in \u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u3001INADDR_ANY\u3001pthread \u4e92\u65a5\u9501\u4e0e RAII\u3001\u7b56\u7565\u6a21\u5f0f\u3001std::function \u56de\u8c03\u3001\u6d41\u5f0f\u65e5\u5fd7\u7684\u5b9e\u73b0\u539f\u7406\u30021. \u9879\u76ee\u91cc\u6709\u4ec0\u4e48\u5305\u542b 6 \u4e2a\u6e90\u6587\u4ef6&#xff1a;\u6587\u4ef6\u804c\u8d23Mutex.hpp\u5bf9 pthread \u4e92\u65a5\u9501\u7684 C \u5c01\u88c5&#xff0c;\u5916\u52a0 RAII \u9501\u5b88\u536b LockGuardLog.hpp\u65e5\u5fd7\u6a21\u5757&#xff1a;\u65e5\u5fd7\u7b49\u7ea7\u3001\u65f6\u95f4\u6233\u3001\u6d41\u5f0f\u8f93\u51fa\u3001\u5c4f\u5e55\/\u6587\u4ef6\u4e24\u79cd\u5237\u65b0\u7b56\u7565UdpServer.hppUD\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/106331.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-16T23:33:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233310-6aab27363c7db.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106331.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/106331.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-16T23:33:18+00:00\",\"dateModified\":\"2026-09-16T23:33:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/106331.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/106331.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106331.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757\"}]},{\"@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\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \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\/106331.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"UDP socket \u7f16\u7a0b\u3001sockaddr_in \u4e0e\u7f51\u7edc\u5b57\u8282\u5e8f\u3001INADDR_ANY\u3001pthread \u4e92\u65a5\u9501\u4e0e RAII\u3001\u7b56\u7565\u6a21\u5f0f\u3001std::function \u56de\u8c03\u3001\u6d41\u5f0f\u65e5\u5fd7\u7684\u5b9e\u73b0\u539f\u7406\u30021. \u9879\u76ee\u91cc\u6709\u4ec0\u4e48\u5305\u542b 6 \u4e2a\u6e90\u6587\u4ef6&#xff1a;\u6587\u4ef6\u804c\u8d23Mutex.hpp\u5bf9 pthread \u4e92\u65a5\u9501\u7684 C \u5c01\u88c5&#xff0c;\u5916\u52a0 RAII \u9501\u5b88\u536b LockGuardLog.hpp\u65e5\u5fd7\u6a21\u5757&#xff1a;\u65e5\u5fd7\u7b49\u7ea7\u3001\u65f6\u95f4\u6233\u3001\u6d41\u5f0f\u8f93\u51fa\u3001\u5c4f\u5e55\/\u6587\u4ef6\u4e24\u79cd\u5237\u65b0\u7b56\u7565UdpServer.hppUD","og_url":"https:\/\/www.wsisp.com\/helps\/106331.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-16T23:33:18+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260916233310-6aab27363c7db.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/106331.html","url":"https:\/\/www.wsisp.com\/helps\/106331.html","name":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-16T23:33:18+00:00","dateModified":"2026-09-16T23:33:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/106331.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/106331.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/106331.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7f51\u7edc\u52a0\u9910\uff08\u4e09\uff09\u3011\u5b66\u4e60\u5168\u8bb0\u5f55\uff1aUDP \u56de\u58f0\u670d\u52a1\u5668 + \u7b56\u7565\u6a21\u5f0f\u65e5\u5fd7\u6a21\u5757"}]},{"@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\/106331","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=106331"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106331\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/106322"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=106331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=106331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=106331"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=106331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}