{"id":15279,"date":"2025-04-18T21:52:26","date_gmt":"2025-04-18T13:52:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/15279.html"},"modified":"2025-04-18T21:52:26","modified_gmt":"2025-04-18T13:52:26","slug":"c%e9%a1%b9%e7%9b%ae-%e9%9b%86%e7%be%a4%e8%81%8a%e5%a4%a9%e6%9c%8d%e5%8a%a1%e5%99%a8-%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%9b%86%e7%be%a4","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/15279.html","title":{"rendered":"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4"},"content":{"rendered":"<h2>C&#043;&#043;\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4<\/h2>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>C&#043;&#043;\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4<\/li>\n<li>\n<ul>\n<li>1.\u8d1f\u8f7d\u5747\u8861\u5b58\u5728\u7684\u610f\u4e49<\/li>\n<li>2.\u670d\u52a1\u5668\u96c6\u7fa4<\/li>\n<li>\n<ul>\n<li>\u8d1f\u8f7d\u5747\u8861\u5668 &#8211; \u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5<\/li>\n<li>nginx\u914d\u7f6etcp\u8d1f\u8f7d\u5747\u8861<\/li>\n<li>\u6d4b\u8bd5<\/li>\n<\/ul>\n<\/li>\n<li>3.\u670d\u52a1\u5668\u4e2d\u95f4\u4ef6-\u57fa\u4e8e\u53d1\u5e03-\u8ba2\u9605\u7684Redis<\/li>\n<li>\n<ul>\n<li>\u96c6\u7fa4\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u901a\u4fe1\u8bbe\u8ba1<\/li>\n<li>redis\u53d1\u5e03-\u8ba2\u9605\u7684\u5ba2\u6237\u7aef\u7f16\u7a0b<\/li>\n<li>\u4ee3\u7801\u65b9\u9762<\/li>\n<\/ul>\n<\/li>\n<li>4.\u96c6\u7fa4\u540e\u7684\u6d4b\u8bd5<\/li>\n<li>\n<ul>\n<li>1.\u4e24\u53f0\u670d\u52a1\u5668\u8fdb\u884c\u96c6\u7fa4<\/li>\n<li>2.\u767b\u5f55\u4e24\u4e2a\u5ba2\u6237\u7aef<\/li>\n<li>3.\u5206\u522b\u8fdb\u884c\u4e00\u5bf9\u4e00\u804a\u5929\u548c\u7fa4\u804a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>1.\u8d1f\u8f7d\u5747\u8861\u5b58\u5728\u7684\u610f\u4e49<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u8981\u6709\u8d1f\u8f7d\u5747\u8861\u5668&#xff1f;<\/p>\n<p>\u4e00\u53f0\u673a\u5668\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6709\u9650\u7684&#xff0c;\u4e4b\u524d\u7684linux\u7cfb\u7edf\u6700\u5927\u4e5f\u5c311024&#xff0c;\u90a3\u8bf4\u660e\u4e5f\u5c31\u540c\u65f6\u6700\u591a\u53ef\u4ee5\u63d0\u4f9b1024\u4e2a\u4eba\u7684\u670d\u52a1\u804a\u5929\u670d\u52a1\u8fd8\u662f\u957f\u8fde\u63a5&#xff0c;\u9700\u8981\u4e00\u76f4\u4fdd\u6301\u548c\u670d\u52a1\u5668\u7684\u8fde\u63a5\u624d\u884c&#xff0c;\u90a3\u4e48\u968f\u7740\u7528\u6237\u89c4\u6a21\u7684\u6269\u5927&#xff0c;\u5c31\u9700\u8981\u66f4\u591a\u7684\u670d\u52a1\u5668&#xff0c;\u800c\u8d1f\u8f7d\u5747\u8861\u5668\u5c31\u662f\u8d1f\u8d23\u628a\u8fd9\u4e9b\u8bf7\u6c42\u5206\u53d1\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\u9762\u53bb\u3002\u4e0d\u53ef\u80fd\u8bf4\u662f\u7ed9\u7528\u6237\u4e09\u53f0\u670d\u52a1\u5668&#xff0c;\u8ba9\u7528\u6237\u81ea\u5df1\u9009\u8981\u7528\u54ea\u4e00\u53f0\u63d0\u4f9b\u670d\u52a1<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-680259175269b.png\" alt=\"image-20250217174257075\" \/><\/p>\n<p>\u7b2c\u4e00\u70b9\u662f\u57fa\u672c\u529f\u80fd<\/p>\n<p>\u7b2c\u4e8c\u70b9&#xff0c;\u4fdd\u6301\u5fc3\u8df3\u662f\u4ee5\u9632\u6709\u7684\u4e3b\u673a\u53d1\u751f\u4e86\u6545\u969c&#xff0c;\u4e0d\u80fd\u591f\u7ee7\u7eed\u5de5\u4f5c&#xff0c;\u90a3\u4e48\u8d1f\u8f7d\u5747\u8861\u5668\u4e0d\u53ef\u4ee5\u5f80\u8fd9\u4e2a\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u670d\u52a1\u7684\u8bf7\u6c42<\/p>\n<p>\u7b2c\u4e09\u70b9&#xff0c;\u4e00\u822c\u670d\u52a1\u5668\u90fd\u662f24\u5c0f\u65f6\u4e0d\u5173\u7684&#xff0c;\u56e0\u4e3a\u53ef\u80fd24\u5c0f\u65f6\u90fd\u6709\u4eba\u8bf7\u6c42\u670d\u52a1&#xff0c;\u4e0d\u53ef\u80fd\u8bf4\u662f\u4e3a\u4e86\u6dfb\u52a0\u65b0\u7684\u673a\u5668\u5c31\u628a\u670d\u52a1\u7ed9\u505c\u6389&#xff0c;\u8fd9\u6837\u7684\u8bdd\u7528\u6237\u4f53\u9a8c\u4f1a\u5f88\u5dee&#xff0c;\u6240\u4ee5\u8981\u6709\u7b2c\u4e09\u70b9<\/p>\n<h3>2.\u670d\u52a1\u5668\u96c6\u7fa4<\/h3>\n<h4>\u8d1f\u8f7d\u5747\u8861\u5668 &#8211; \u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5<\/h4>\n<p>\u5355\u53f0\u670d\u52a1\u5668\u53d7\u9650\u4e8e\u786c\u4ef6\u8d44\u6e90&#xff0c;\u5176\u6027\u80fd\u662f\u6709\u4e0a\u9650\u7684&#xff0c;\u5f53\u5355\u53f0\u670d\u52a1\u5668\u4e0d\u80fd\u6ee1\u8db3\u5e94\u7528\u573a\u666f\u7684\u5e76\u53d1\u9700\u6c42\u91cf\u65f6&#xff0c;\u5c31\u9700\u8981\u8003\u8651\u90e8\u7f72\u591a\u4e2a\u670d\u52a1\u5668\u5171\u540c\u5904\u7406\u5ba2\u6237\u7aef\u7684\u5e76\u53d1\u8bf7\u6c42&#xff0c;\u4f46\u662f\u5ba2\u6237\u7aef\u600e\u4e48\u77e5\u9053\u53bb\u8fde\u63a5\u5177\u4f53\u54ea\u53f0\u670d\u52a1\u5668\u5462&#xff1f;\u6b64\u65f6\u5c31\u9700\u8981\u4e00\u53f0\u8d1f\u8f7d\u5747\u8861\u5668&#xff0c;\u901a\u8fc7\u9884\u8bbe\u7684\u8d1f\u8f7d\u7b97\u6cd5&#xff0c;\u6307\u5bfc\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u5668\u3002<\/p>\n<p>\u8d1f\u8f7d\u5747\u8861\u5668\u6709\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<p>\u666e\u901a\u7684\u57fa\u4e8e\u54c8\u5e0c\u7684\u8d1f\u8f7d\u7b97\u6cd5&#xff0c;\u5e76\u4e0d\u80fd\u6ee1\u8db3\u8d1f\u8f7d\u5747\u8861\u6240\u8981\u6c42\u7684\u5355\u8c03\u6027\u548c\u5e73\u8861\u6027&#xff0c;\u4f46\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u975e\u5e38\u597d\u7684\u4fdd\u6301\u4e86\u8fd9\u4e24\u79cd\u7279\u6027&#xff0c;\u6240\u4ee5\u7ecf\u5e38\u7528\u5728\u9700\u8981\u8bbe\u8ba1\u8d1f\u8f7d\u7b97\u6cd5\u7684\u5e94\u7528\u573a\u666f\u5f53\u4e2d\u3002<\/p>\n<h4>nginx\u914d\u7f6etcp\u8d1f\u8f7d\u5747\u8861<\/h4>\n<p>\u5728\u670d\u52a1\u5668\u5feb\u901f\u96c6\u7fa4\u73af\u5883\u642d\u5efa\u4e2d&#xff0c;\u90fd\u8feb\u5207\u9700\u8981\u4e00\u4e2a\u80fd\u62ff\u6765\u5373\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u5668&#xff0c;nginx\u57281.9\u7248\u672c\u4e4b\u524d&#xff0c;\u53ea\u652f\u6301http\u534f\u8baeweb\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u5747\u8861&#xff0c;\u4ece1.9\u7248\u672c\u5f00\u59cb\u4ee5\u540e&#xff0c;nginx\u5f00\u59cb\u652f\u6301tcp\u7684\u957f\u8fde\u63a5\u8d1f\u8f7d\u5747\u8861&#xff0c;\u4f46\u662fnginx\u9ed8\u8ba4\u5e76\u6ca1\u6709\u7f16\u8bd1tcp\u8d1f\u8f7d\u5747\u8861\u6a21\u5757&#xff0c;\u7f16\u5199\u5b83\u65f6&#xff0c;\u9700\u8981\u52a0\u5165\u2013with-stream\u53c2\u6570\u6765\u6fc0\u6d3b\u8fd9\u4e2a\u6a21\u5757\u3002<\/p>\n<p>nginx\u7f16\u8bd1\u52a0\u5165&#8211;with-stream\u53c2\u6570\u6fc0\u6d3btcp\u8d1f\u8f7d\u5747\u8861\u6a21\u5757<\/p>\n<p>nginx\u7f16\u8bd1\u5b89\u88c5\u9700\u8981\u5148\u5b89\u88c5pcre\u3001openssl\u3001zlib\u7b49\u5e93&#xff0c;\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7f16\u8bd1\u6267\u884c\u4e0b\u9762\u7684configure\u547d\u4ee4&#xff0c;\u6839<\/p>\n<p>\u636e\u9519\u8bef\u63d0\u793a\u4fe1\u606f&#xff0c;\u5b89\u88c5\u76f8\u5e94\u7f3a\u5c11\u7684\u5e93\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-6802591793238.png\" alt=\"image-20250217193805023\" \/><\/p>\n<p>\u4e0b\u9762\u7684make\u547d\u4ee4\u4f1a\u5411\u7cfb\u7edf\u8def\u5f84\u62f7\u8d1d\u6587\u4ef6&#xff0c;\u9700\u8981\u5728root\u7528\u6237\u4e0b\u6267\u884c<\/p>\n<p>.\/configure &#8211;with-stream<br \/>\nmake &amp;&amp; make install<\/p>\n<p>\u7f16\u8bd1\u5b8c\u6210\u540e&#xff0c;\u9ed8\u8ba4\u5b89\u88c5\u5728\u4e86\/usr\/local\/nginx\u76ee\u5f55\u3002<\/p>\n<p>cd \/usr\/local\/nginx\/<br \/>\nls<\/p>\n<p>\u53ef\u6267\u884c\u6587\u4ef6\u5728sbin\u76ee\u5f55\u91cc\u9762&#xff0c;\u914d\u7f6e\u6587\u4ef6\u5728conf\u76ee\u5f55\u91cc\u9762\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-68025917a44d1.png\" alt=\"image-20250217182431927\" \/><\/p>\n<p>nginx -s reload \u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u542f\u52a8<br \/>\nnginx -s stop \u505c\u6b62nginx\u670d\u52a1<\/p>\n<p>\u4e3b\u8981\u5728conf\u76ee\u5f55\u91cc\u9762\u914d\u7f6enginx.conf\u6587\u4ef6&#xff0c;\u914d\u7f6e\u5982\u4e0b&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-68025917c47d2.png\" alt=\"\" \/><\/p>\n<p>\u5f88\u591a\u4e1c\u897f\u4e4b\u524d\u90fd\u6709&#xff0c;\u628a\u4e0b\u9762\u7684\u90e8\u5206\u52a0\u4e0a\u5c31\u662f\u4e86<\/p>\n<p>stream <span class=\"token punctuation\">{<\/span><br \/>\nupstream MyServer <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">#weight\u662f\u6743\u91cd max_fails\u8868\u793a\u5fc3\u8df3&#xff0c;\u53ea\u8981\u76f8\u5e94\u8d85\u8fc730\u79d2\u7684\u6b21\u6570\u8d85\u8fc73\u6b21&#xff0c;\u90a3\u8fd9\u4e2a\u670d\u52a1\u5668\u5c31\u7b97\u662f\u6302\u6389\u4e86<\/span><br \/>\nserver <span class=\"token number\">127.0<\/span>.0.1:6000 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span> <span class=\"token assign-left variable\">max_fails<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">3<\/span> <span class=\"token assign-left variable\">fail_timeout<\/span><span class=\"token operator\">&#061;<\/span>30s<span class=\"token punctuation\">;<\/span><br \/>\nserver <span class=\"token number\">127.0<\/span>.0.1:6002 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span> <span class=\"token assign-left variable\">max_fails<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">3<\/span> <span class=\"token assign-left variable\">fail_timeout<\/span><span class=\"token operator\">&#061;<\/span>30s<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>server <span class=\"token punctuation\">{<\/span><br \/>\nproxy_connect_timeout 1s<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">#proxy_timeout 3s;<\/span><br \/>\n<span class=\"token comment\">#ngxin\u76d1\u542c\u7684\u7aef\u53e3\u53f7&#xff0c;\u5ba2\u6237\u7aef\u90fd\u8fde\u63a5\u8fd9\u4e2a\u7aef\u53e3\u53f7&#xff0c;\u7136\u540engxin\u5206\u53d1\u5230\u5176\u4ed6\u670d\u52a1\u5668<\/span><br \/>\nlisten <span class=\"token number\">8000<\/span><span class=\"token punctuation\">;<\/span><br \/>\nproxy_pass MyServer<span class=\"token punctuation\">;<\/span><br \/>\ntcp nodelay on<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u914d\u7f6e\u5b8c\u6210\u540e&#xff0c;.\/nginx -s reload\u5e73\u6ed1\u91cd\u542f\u3002<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p><span class=\"token comment\">\/\/   src\/server\/main.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&#034;chatserver.hpp&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&#034;chatservice.hpp&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;signal.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">using<\/span> <span class=\"token keyword\">namespace<\/span> std<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u5904\u7406\u5668ctrl &#043; c\u7ed3\u675f\u540e&#xff0c;\u91cd\u7f6euser \u7684\u72b6\u6001\u4fe1\u606f&#xff0c;\u628a\u767b\u5f55\u53d8\u6210\u672a\u767b\u5f55<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">resetHandler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">ChatService<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">instance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">-&gt;<\/span><span class=\"token function\">reset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> argc<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token operator\">*<\/span>argv<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>argc<span class=\"token operator\">&lt;<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;\u53c2\u6570\u8fc7\u5c11!example:.\/ChatServer 127.0.0.1 6000&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u89e3\u6790\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u7684ip\u548cport<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>ip<span class=\"token operator\">&#061;<\/span>argv<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">uint16_t<\/span> port<span class=\"token operator\">&#061;<\/span><span class=\"token function\">atoi<\/span><span class=\"token punctuation\">(<\/span>argv<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">signal<\/span><span class=\"token punctuation\">(<\/span>SIGINT<span class=\"token punctuation\">,<\/span>resetHandler<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    EventLoop loop<span class=\"token punctuation\">;<\/span><br \/>\n    InetAddress <span class=\"token function\">addr<\/span><span class=\"token punctuation\">(<\/span>ip<span class=\"token punctuation\">,<\/span>port<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    ChatServer <span class=\"token function\">server<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>loop<span class=\"token punctuation\">,<\/span>addr<span class=\"token punctuation\">,<\/span><span class=\"token string\">&#034;ChatServer&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    server<span class=\"token punctuation\">.<\/span><span class=\"token function\">start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    loop<span class=\"token punctuation\">.<\/span><span class=\"token function\">loop<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>1.\u542f\u52a8\u4e24\u53f0\u670d\u52a1\u5668<\/p>\n<p>2.\u542f\u52a8\u4e24\u4e2a\u5ba2\u6237\u7aef<\/p>\n<p>\u5ba2\u6237\u7aef\u90fd\u662f8000\u7aef\u53e3&#xff0c;\u5c31\u662fnginx\u76d1\u542c\u7684\u7aef\u53e3<\/p>\n<p>\u4e24\u4e2a\u5ba2\u6237\u7aef\u90fd\u53ef\u4ee5\u767b\u5f55&#xff0c;\u6ce8\u518c\u4e4b\u7c7b\u7684\u64cd\u4f5c<\/p>\n<p>\u4f46\u804a\u5929\u73b0\u5728\u8fd8\u4e0d\u884c&#xff0c;\u56e0\u4e3a_userConnMap\u5e76\u4e0d\u76f8\u901a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135224-680259183e2d4.png\" alt=\"image-20250217210150481\" \/><\/p>\n<h3>3.\u670d\u52a1\u5668\u4e2d\u95f4\u4ef6-\u57fa\u4e8e\u53d1\u5e03-\u8ba2\u9605\u7684Redis<\/h3>\n<h4>\u96c6\u7fa4\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u901a\u4fe1\u8bbe\u8ba1<\/h4>\n<p>\u5f53ChatServer\u96c6\u7fa4\u90e8\u7f72\u591a\u53f0\u670d\u52a1\u5668\u4ee5\u540e&#xff0c;\u5f53\u767b\u5f55\u5728\u4e0d\u540c\u670d\u52a1\u5668\u4e0a\u7684\u7528\u6237\u8fdb\u884c\u901a\u4fe1\u65f6&#xff0c;\u8be5\u600e\u4e48\u8bbe\u8ba1&#xff01;\u5982\u4e0b\u8bbe\u8ba1\u597d\u5417&#xff1f;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135224-6802591882e52.png\" alt=\"image-20250217182514434\" \/><\/p>\n<p>\u4e0a\u9762\u7684\u8bbe\u8ba1&#xff0c;\u8ba9\u5404\u4e2aChatServer\u670d\u52a1\u5668\u4e92\u76f8\u4e4b\u95f4\u76f4\u63a5\u5efa\u7acbTCP\u8fde\u63a5\u8fdb\u884c\u901a\u4fe1&#xff0c;\u76f8\u5f53\u4e8e\u5728\u670d\u52a1\u5668\u7f51\u7edc\u4e4b\u95f4\u8fdb\u884c\u5e7f\u64ad\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u670d\u52a1\u5668\u4e4b\u95f4\u8026\u5408\u5ea6\u592a\u9ad8&#xff0c;\u4e0d\u5229\u4e8e\u7cfb\u7edf\u6269\u5c55&#xff0c;\u5e76\u4e14\u4f1a\u5360\u7528\u7cfb\u7edf\u5927\u91cf\u7684socket\u8d44\u6e90&#xff0c;\u5404\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5e26\u5bbd\u538b\u529b\u5f88\u5927&#xff0c;\u4e0d\u80fd\u591f\u8282\u7701\u8d44\u6e90\u7ed9\u66f4\u591a\u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1&#xff0c;\u56e0\u6b64\u7edd\u5bf9\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002<\/p>\n<p>\u96c6\u7fa4\u90e8\u7f72\u7684\u670d\u52a1\u5668\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1&#xff0c;\u6700\u597d\u7684\u65b9\u5f0f\u5c31\u662f\u5f15\u5165\u4e2d\u95f4\u4ef6\u6d88\u606f\u961f\u5217&#xff0c;\u89e3\u8026\u5404\u4e2a\u670d\u52a1\u5668&#xff0c;\u4f7f\u6574\u4e2a\u7cfb\u7edf\u677e\u8026\u5408&#xff0c;\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u54cd\u5e94\u80fd\u529b&#xff0c;\u8282\u7701\u670d\u52a1\u5668\u7684\u5e26\u5bbd\u8d44\u6e90&#xff0c;\u5982\u4e0b\u56fe\u6240\u793a&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135224-68025918be470.png\" alt=\"image-20250217182540935\" \/><\/p>\n<p>\u5728\u96c6\u7fa4\u5206\u5e03\u5f0f\u73af\u5883\u4e2d&#xff0c;\u7ecf\u5e38\u4f7f\u7528\u7684\u4e2d\u95f4\u4ef6\u6d88\u606f\u961f\u5217\u6709ActiveMQ\u3001RabbitMQ\u3001Kafka\u7b49&#xff0c;\u90fd\u662f\u5e94\u7528\u573a\u666f\u5e7f\u6cdb\u5e76\u4e14\u6027\u80fd\u5f88\u597d\u7684\u6d88\u606f\u961f\u5217&#xff0c;\u4f9b\u96c6\u7fa4\u670d\u52a1\u5668\u4e4b\u95f4&#xff0c;\u5206\u5e03\u5f0f\u670d\u52a1\u4e4b\u95f4\u8fdb\u884c\u6d88\u606f\u901a\u4fe1\u3002\u9650\u4e8e\u6211\u4eec\u7684\u9879\u76ee\u4e1a\u52a1\u7c7b\u578b\u5e76\u4e0d\u662f\u975e\u5e38\u590d\u6742&#xff0c;\u5bf9\u5e76\u53d1\u8bf7\u6c42\u91cf\u4e5f\u6ca1\u6709\u592a\u9ad8\u7684\u8981\u6c42&#xff0c;\u56e0\u6b64\u6211\u4eec\u7684\u4e2d\u95f4\u4ef6\u6d88\u606f\u961f\u5217\u9009\u578b\u7684\u662f-\u57fa\u4e8e\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u7684redis\u3002<\/p>\n<h4>redis\u53d1\u5e03-\u8ba2\u9605\u7684\u5ba2\u6237\u7aef\u7f16\u7a0b<\/h4>\n<p>redis\u652f\u6301\u591a\u79cd\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u7f16\u7a0b\u8bed\u8a00&#xff0c;\u4f8b\u5982Java\u5bf9\u5e94jedis\u3001php\u5bf9\u5e94phpredis\u3001C&#043;&#043;\u5bf9\u5e94\u7684\u5219\u662f<\/p>\n<p>hiredis\u3002\u4e0b\u9762\u662f\u5b89\u88c5hiredis\u7684\u6b65\u9aa4&#xff1a;<\/p>\n<p>1.git clone https:\/\/github.com\/redis\/hiredis \u4ecegithub\u4e0a\u4e0b\u8f7dhiredis\u5ba2\u6237\u7aef&#xff0c;\u8fdb\u884c\u6e90\u7801<\/p>\n<p>\u7f16\u8bd1\u5b89\u88c5<\/p>\n<p>tony&#064;tony-virtual-machine:~\/github$ git clone<br \/>\nhttps:\/\/github.com\/redis\/hiredis<br \/>\n\u6b63\u514b\u9686\u5230 &#039;hiredis&#039;&#8230;<br \/>\nremote: Enumerating objects: 3261 , done.<br \/>\n^C\u6536\u5bf9\u8c61\u4e2d: 83 % (2707\/3261), 876 .01 KiB | 59 .00 KiB\/s<\/p>\n<p>2.cd hiredis<\/p>\n<p>3.make<\/p>\n<p>tony&#064;tony-virtual-machine:~\/github\/hiredis$ make<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb net.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb hiredis.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb sds.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb async.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb read.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb sockcompat.c<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb sslio.c<br \/>\ncc -shared -Wl,-soname,libhiredis.so.0.14 -o libhiredis.so net.o hiredis.o<br \/>\nsds.o async.o read.o sockcompat.o sslio.o<br \/>\nar rcs libhiredis.a net.o hiredis.o sds.o async.o read.o sockcompat.o<br \/>\nsslio.o<br \/>\ncc -std&#061;c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-<br \/>\nstrings -Wno-missing-field-initializers -g -ggdb test.c<br \/>\ncc -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-<br \/>\nfield-initializers -g -ggdb -o hiredis-test test.o libhiredis.a<br \/>\nGenerating hiredis.pc for pkgconfig&#8230;<br \/>\ntony&#064;tony-virtual-machine:~\/github\/hiredis$<\/p>\n<p>\u7f16\u8bd1\u6210\u529f&#xff01;<\/p>\n<p>4.sudo make install<\/p>\n<p>tony&#064;tony-virtual-machine:~\/github\/hiredis$ sudo make install<br \/>\n[sudo] tony \u7684\u5bc6\u7801&#xff1a;<br \/>\nmkdir -p \/usr\/local\/include\/hiredis \/usr\/local\/include\/hiredis\/adapters<br \/>\n\/usr\/local\/lib<br \/>\ncp -pPR hiredis.h async.h read.h sds.h sslio.h \/usr\/local\/include\/hiredis<br \/>\ncp -pPR adapters\/*.h \/usr\/local\/include\/hiredis\/adapters<br \/>\ncp -pPR libhiredis.so \/usr\/local\/lib\/libhiredis.so.0.<br \/>\ncd \/usr\/local\/lib &amp;&amp; ln -sf libhiredis.so.0.14 libhiredis.so<br \/>\ncp -pPR libhiredis.a \/usr\/local\/lib<br \/>\nmkdir -p \/usr\/local\/lib\/pkgconfig<br \/>\ncp -pPR hiredis.pc \/usr\/local\/lib\/pkgconfig<\/p>\n<p>\u62f7\u8d1d\u751f\u6210\u7684\u52a8\u6001\u5e93\u5230\/usr\/local\/lib\u76ee\u5f55\u4e0b&#xff01;<\/p>\n<p>5.sudo ldconfig \/usr\/local\/lib<\/p>\n<h4>\u4ee3\u7801\u65b9\u9762<\/h4>\n<p><span class=\"token comment\">\/\/redis.hpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">ifndef<\/span> <span class=\"token expression\">REDIS_H<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">REDIS_H<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;hiredis\/hiredis.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;thread&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;functional&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">using<\/span> <span class=\"token keyword\">namespace<\/span> std<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">using<\/span> redis_handler <span class=\"token operator\">&#061;<\/span> function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">,<\/span>string<span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Redis<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">Redis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">Redis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8fde\u63a5Redis\u670d\u52a1\u5668<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">connect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5411Redis\u6307\u5b9a\u7684\u901a\u9053channel\u53d1\u5e03\u6d88\u606f<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">publish<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">,<\/span> string message<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5411Redis\u6307\u5b9a\u7684\u901a\u9053subscribe\u8ba2\u9605\u6d88\u606f<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">subscribe<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u53d6\u6d88\u8ba2\u9605<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">unsubscribe<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u72ec\u7acb\u7ebf\u7a0b\u4e2d\u63a5\u6536\u8ba2\u9605\u901a\u9053\u7684\u6d88\u606f<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">observer_channel_message<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u521d\u59cb\u5316\u4e1a\u52a1\u5c42\u4e0a\u62a5\u901a\u9053\u6d88\u606f\u7684\u56de\u8c03\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">init_notify_handler<\/span><span class=\"token punctuation\">(<\/span>redis_handler handler<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/hiredis\u540c\u6b65\u4e0a\u4e0b\u6587\u5bf9\u8c61&#xff0c;\u8d1f\u8d23publish\u6d88\u606f<\/span><br \/>\n    redisContext <span class=\"token operator\">*<\/span>publish_context_<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8d1f\u8d23subscribe\u6d88\u606f<\/span><br \/>\n    redisContext <span class=\"token operator\">*<\/span>subcribe_context_<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u56de\u8c03\u64cd\u4f5c&#xff0c;\u6536\u5230\u6d88\u606f\u7ed9service\u4e0a\u62a5<\/span><br \/>\n    redis_handler notify_message_handler_<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">endif<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/redis.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;redis.hpp&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><\/p>\n<p><span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">Redis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">:<\/span> <span class=\"token function\">publish_context_<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">subcribe_context_<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token operator\">~<\/span><span class=\"token function\">Redis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>publish_context_ <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">redisFree<\/span><span class=\"token punctuation\">(<\/span>publish_context_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>subcribe_context_ <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">redisFree<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u8fde\u63a5Redis\u670d\u52a1\u5668<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">connect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    publish_context_ <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">redisConnect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">6379<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>publish_context_ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;connect redis failed!&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    subcribe_context_ <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">redisConnect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">6379<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>subcribe_context_ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;connect redis failed!&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u72ec\u7acb\u7ebf\u7a0b\u4e2d\u63a5\u6536\u8ba2\u9605\u901a\u9053\u7684\u6d88\u606f<\/span><br \/>\n    thread <span class=\"token function\">t<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">observer_channel_message<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    t<span class=\"token punctuation\">.<\/span><span class=\"token function\">detach<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;connect redis-server success!&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u5411Redis\u6307\u5b9a\u7684\u901a\u9053channel\u53d1\u5e03\u6d88\u606f<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">publish<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">,<\/span> string message<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u76f8\u5f53\u4e8epublish \u952e \u503c<\/span><br \/>\n    <span class=\"token comment\">\/\/ redis 127.0.0.1:6379&gt; PUBLISH runoobChat &#034;Redis PUBLISH test&#034;<\/span><br \/>\n    redisReply <span class=\"token operator\">*<\/span>reply <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>redisReply <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token function\">redisCommand<\/span><span class=\"token punctuation\">(<\/span>publish_context_<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;PUBLISH %d %s&#034;<\/span><span class=\"token punctuation\">,<\/span> channel<span class=\"token punctuation\">,<\/span> message<span class=\"token punctuation\">.<\/span><span class=\"token function\">c_str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>reply <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;publish command failed!&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u91ca\u653e\u8d44\u6e90<\/span><br \/>\n    <span class=\"token function\">freeReplyObject<\/span><span class=\"token punctuation\">(<\/span>reply<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5411Redis\u6307\u5b9a\u7684\u901a\u9053subscribe\u8ba2\u9605\u6d88\u606f<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">subscribe<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ redisCommand \u4f1a\u5148\u628a\u547d\u4ee4\u7f13\u5b58\u5230context\u4e2d&#xff0c;\u7136\u540e\u8c03\u7528RedisAppendCommand\u53d1\u9001\u7ed9redis<\/span><br \/>\n    <span class=\"token comment\">\/\/ redis\u6267\u884csubscribe\u662f\u963b\u585e&#xff0c;\u4e0d\u4f1a\u54cd\u5e94&#xff0c;\u4e0d\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2areply<\/span><br \/>\n    <span class=\"token comment\">\/\/ redis 127.0.0.1:6379&gt; SUBSCRIBE runoobChat<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>REDIS_ERR <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token function\">redisAppendCommand<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;SUBSCRIBE %d&#034;<\/span><span class=\"token punctuation\">,<\/span> channel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;subscibe command failed&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> done <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>done<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>REDIS_ERR <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token function\">redisBufferWrite<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>done<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;subscribe command failed&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u53d6\u6d88\u8ba2\u9605<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">unsubscribe<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> channel<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/redisCommand \u4f1a\u5148\u628a\u547d\u4ee4\u7f13\u5b58\u5230context\u4e2d&#xff0c;\u7136\u540e\u8c03\u7528RedisAppendCommand\u53d1\u9001\u7ed9redis<\/span><br \/>\n    <span class=\"token comment\">\/\/redis\u6267\u884csubscribe\u662f\u963b\u585e&#xff0c;\u4e0d\u4f1a\u54cd\u5e94&#xff0c;\u4e0d\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2areply<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>REDIS_ERR <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token function\">redisAppendCommand<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;UNSUBSCRIBE %d&#034;<\/span><span class=\"token punctuation\">,<\/span> channel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;subscibe command failed&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> done <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>done<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>REDIS_ERR <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token function\">redisBufferWrite<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>done<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;subscribe command failed&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u72ec\u7acb\u7ebf\u7a0b\u4e2d\u63a5\u6536\u8ba2\u9605\u901a\u9053\u7684\u6d88\u606f<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">observer_channel_message<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    redisReply <span class=\"token operator\">*<\/span>reply <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span>REDIS_OK <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token function\">redisGetReply<\/span><span class=\"token punctuation\">(<\/span>subcribe_context_<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>reply<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/reply\u91cc\u9762\u662f\u8fd4\u56de\u7684\u6570\u636e\u6709\u4e09\u4e2a&#xff0c;0. message , 1.\u901a\u9053\u53f7&#xff0c;2.\u6d88\u606f<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>reply <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span> <span class=\"token operator\">&amp;&amp;<\/span> reply<span class=\"token operator\">-&gt;<\/span>element<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span> <span class=\"token operator\">&amp;&amp;<\/span> reply<span class=\"token operator\">-&gt;<\/span>element<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">-&gt;<\/span>str <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/\u7ed9\u4e1a\u52a1\u5c42\u4e0a\u62a5\u6d88\u606f<\/span><br \/>\n            <span class=\"token function\">notify_message_handler_<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">atoi<\/span><span class=\"token punctuation\">(<\/span>reply<span class=\"token operator\">-&gt;<\/span>element<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">-&gt;<\/span>str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> reply<span class=\"token operator\">-&gt;<\/span>element<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">-&gt;<\/span>str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token function\">freeReplyObject<\/span><span class=\"token punctuation\">(<\/span>reply<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; oberver_channel_message quit&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u521d\u59cb\u5316\u4e1a\u52a1\u5c42\u4e0a\u62a5\u901a\u9053\u6d88\u606f\u7684\u56de\u8c03\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">Redis<\/span><span class=\"token double-colon punctuation\">::<\/span><span class=\"token function\">init_notify_handler<\/span><span class=\"token punctuation\">(<\/span>redis_handler handler<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    notify_message_handler_ <span class=\"token operator\">&#061;<\/span> handler<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>cahtService.hpp\u548ccpp\u7684\u4fee\u6539\u89c1github\u5730\u5740<\/p>\n<h3>4.\u96c6\u7fa4\u540e\u7684\u6d4b\u8bd5<\/h3>\n<h4>1.\u4e24\u53f0\u670d\u52a1\u5668\u8fdb\u884c\u96c6\u7fa4<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135225-6802591905495.png\" alt=\"image-20250218100733663\" \/><\/p>\n<h4>2.\u767b\u5f55\u4e24\u4e2a\u5ba2\u6237\u7aef<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135225-680259194afa2.png\" alt=\"image-20250218100827724\" \/><\/p>\n<h4>3.\u5206\u522b\u8fdb\u884c\u4e00\u5bf9\u4e00\u804a\u5929\u548c\u7fa4\u804a<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135225-68025919a2c55.png\" alt=\"image-20250218100856663\" \/><\/p>\n<p>\u6d4b\u8bd5\u6210\u529f&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u4e00\u53f0\u673a\u5668\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6709\u9650\u7684\uff0c\u4e4b\u524d\u7684linux\u7cfb\u7edf\u6700\u5927\u4e5f\u5c311024\uff0c\u90a3\u8bf4\u660e\u4e5f\u5c31\u540c\u65f6\u6700\u591a\u53ef\u4ee5\u63d0\u4f9b1024\u4e2a\u4eba\u7684\u670d\u52a1\u804a\u5929\u670d\u52a1\u8fd8\u662f\u957f\u8fde\u63a5\uff0c\u9700\u8981\u4e00\u76f4\u4fdd\u6301\u548c\u670d\u52a1\u5668\u7684\u8fde\u63a5\u624d\u884c\uff0c\u90a3\u4e48\u968f\u7740\u7528\u6237\u89c4\u6a21\u7684\u6269\u5927\uff0c\u5c31\u9700\u8981\u66f4\u591a\u7684\u670d\u52a1\u5668\uff0c\u800c\u8d1f\u8f7d\u5747\u8861\u5668\u5c31\u662f\u8d1f\u8d23\u628a\u8fd9\u4e9b\u8bf7\u6c42\u5206\u53d1\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\u9762\u53bb\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u670d\u52a1\u5668\u4e4b\u95f4\u8026\u5408\u5ea6\u592a\u9ad8\uff0c\u4e0d\u5229\u4e8e\u7cfb\u7edf\u6269\u5c55\uff0c\u5e76\u4e14\u4f1a\u5360\u7528\u7cfb\u7edf\u5927\u91cf\u7684socket\u8d44\u6e90\uff0c\u5404\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5e26\u5bbd\u538b\u529b\u5f88\u5927\uff0c\u4e0d\u80fd\u591f\u8282\u7701\u8d44\u6e90\u7ed9\u66f4\u591a\u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\uff0c\u56e0\u6b64\u7edd\u5bf9\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002\u8d1f\u8f7d\u5747\u8861\u5668\u6709\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u5747\u8861\u3002_c++\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668<\/p>\n","protected":false},"author":2,"featured_media":15269,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190,43,185],"topic":[],"class_list":["post-15279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-190","tag-43","tag-185"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \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\/15279.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u4e00\u53f0\u673a\u5668\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6709\u9650\u7684\uff0c\u4e4b\u524d\u7684linux\u7cfb\u7edf\u6700\u5927\u4e5f\u5c311024\uff0c\u90a3\u8bf4\u660e\u4e5f\u5c31\u540c\u65f6\u6700\u591a\u53ef\u4ee5\u63d0\u4f9b1024\u4e2a\u4eba\u7684\u670d\u52a1\u804a\u5929\u670d\u52a1\u8fd8\u662f\u957f\u8fde\u63a5\uff0c\u9700\u8981\u4e00\u76f4\u4fdd\u6301\u548c\u670d\u52a1\u5668\u7684\u8fde\u63a5\u624d\u884c\uff0c\u90a3\u4e48\u968f\u7740\u7528\u6237\u89c4\u6a21\u7684\u6269\u5927\uff0c\u5c31\u9700\u8981\u66f4\u591a\u7684\u670d\u52a1\u5668\uff0c\u800c\u8d1f\u8f7d\u5747\u8861\u5668\u5c31\u662f\u8d1f\u8d23\u628a\u8fd9\u4e9b\u8bf7\u6c42\u5206\u53d1\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\u9762\u53bb\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u670d\u52a1\u5668\u4e4b\u95f4\u8026\u5408\u5ea6\u592a\u9ad8\uff0c\u4e0d\u5229\u4e8e\u7cfb\u7edf\u6269\u5c55\uff0c\u5e76\u4e14\u4f1a\u5360\u7528\u7cfb\u7edf\u5927\u91cf\u7684socket\u8d44\u6e90\uff0c\u5404\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5e26\u5bbd\u538b\u529b\u5f88\u5927\uff0c\u4e0d\u80fd\u591f\u8282\u7701\u8d44\u6e90\u7ed9\u66f4\u591a\u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\uff0c\u56e0\u6b64\u7edd\u5bf9\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002\u8d1f\u8f7d\u5747\u8861\u5668\u6709\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u5747\u8861\u3002_c++\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/15279.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-18T13:52:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-680259175269b.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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15279.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/15279.html\",\"name\":\"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:52:26+00:00\",\"dateModified\":\"2025-04-18T13:52:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/15279.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/15279.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15279.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4\"}]},{\"@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":"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \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\/15279.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u4e00\u53f0\u673a\u5668\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6709\u9650\u7684\uff0c\u4e4b\u524d\u7684linux\u7cfb\u7edf\u6700\u5927\u4e5f\u5c311024\uff0c\u90a3\u8bf4\u660e\u4e5f\u5c31\u540c\u65f6\u6700\u591a\u53ef\u4ee5\u63d0\u4f9b1024\u4e2a\u4eba\u7684\u670d\u52a1\u804a\u5929\u670d\u52a1\u8fd8\u662f\u957f\u8fde\u63a5\uff0c\u9700\u8981\u4e00\u76f4\u4fdd\u6301\u548c\u670d\u52a1\u5668\u7684\u8fde\u63a5\u624d\u884c\uff0c\u90a3\u4e48\u968f\u7740\u7528\u6237\u89c4\u6a21\u7684\u6269\u5927\uff0c\u5c31\u9700\u8981\u66f4\u591a\u7684\u670d\u52a1\u5668\uff0c\u800c\u8d1f\u8f7d\u5747\u8861\u5668\u5c31\u662f\u8d1f\u8d23\u628a\u8fd9\u4e9b\u8bf7\u6c42\u5206\u53d1\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\u9762\u53bb\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u670d\u52a1\u5668\u4e4b\u95f4\u8026\u5408\u5ea6\u592a\u9ad8\uff0c\u4e0d\u5229\u4e8e\u7cfb\u7edf\u6269\u5c55\uff0c\u5e76\u4e14\u4f1a\u5360\u7528\u7cfb\u7edf\u5927\u91cf\u7684socket\u8d44\u6e90\uff0c\u5404\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5e26\u5bbd\u538b\u529b\u5f88\u5927\uff0c\u4e0d\u80fd\u591f\u8282\u7701\u8d44\u6e90\u7ed9\u66f4\u591a\u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\uff0c\u56e0\u6b64\u7edd\u5bf9\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002\u8d1f\u8f7d\u5747\u8861\u5668\u6709\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u5747\u8861\u3002_c++\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668","og_url":"https:\/\/www.wsisp.com\/helps\/15279.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:52:26+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135223-680259175269b.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/15279.html","url":"https:\/\/www.wsisp.com\/helps\/15279.html","name":"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:52:26+00:00","dateModified":"2025-04-18T13:52:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/15279.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/15279.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/15279.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u9879\u76ee | \u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668 | \u670d\u52a1\u5668\u96c6\u7fa4"}]},{"@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\/15279","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=15279"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/15279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/15269"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=15279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=15279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=15279"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=15279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}