{"id":115869,"date":"2026-10-20T13:41:17","date_gmt":"2026-10-20T05:41:17","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/115869.html"},"modified":"2026-10-20T13:41:17","modified_gmt":"2026-10-20T05:41:17","slug":"%e4%bb%8e%e9%9b%b6%e5%85%a5%e9%97%a8%e8%ae%a1%e7%ae%97%e6%9c%ba%e7%bd%91%e7%bb%9c%e7%b3%bb%e5%88%97%ef%bc%88%e4%b8%83%ef%bc%89%ef%bc%9a%e5%ba%94%e7%94%a8%e5%b1%82%e5%8d%8f%e8%ae%ae%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/115869.html","title":{"rendered":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668"},"content":{"rendered":"<p>\u4e0a\u4e24\u7bc7\u6211\u4eec\u8bb2\u4e86TCP\u4e0eUDP\u5957\u63a5\u5b57\u7684\u7f16\u7a0b\u57fa\u7840&#xff0c;\u4e5f\u5199\u4e86\u56db\u5957\u7b80\u5355\u7684Demo\u3002\u4ece\u672c\u6587\u5f00\u59cb&#xff0c;\u6211\u4eec\u6b63\u5f0f\u52a8\u624b\u8bbe\u8ba1\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u201c\u5e94\u7528\u5c42\u534f\u8bae\u201d&#xff0c;\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e00\u4e2a\u201c\u7f51\u7edc\u8ba1\u7b97\u5668\u201d\u3002\u597d&#xff0c;\u5e9f\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u76f4\u63a5\u5f00\u59cb\u3002<\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E6%88%91%E4%BB%AC%E6%8E%A5%E4%B8%8B%E6%9D%A5%E5%88%B0%E5%BA%95%E8%A6%81%E5%81%9A%E4%BB%80%E4%B9%88-toc\" style=\"margin-left:80px\">\u4e00\u3001\u4ece\u9700\u6c42\u51fa\u53d1&#xff1a;\u8bbe\u8ba1\u4e00\u4e2a\u7f51\u7edc\u7248\u8ba1\u7b97\u5668<\/p>\n<p id=\"1.1%20%E8%87%AA%E4%B8%BB%E8%AE%BE%E8%AE%A1%E7%9A%84%E5%BA%94%E7%94%A8%E5%B1%82%E5%8D%8F%E8%AE%AE%EF%BC%8C%E9%9C%80%E8%A6%81%E8%A7%A3%E5%86%B3%E4%B8%A4%E4%B8%AA%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:120px\">1.1 \u81ea\u4e3b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u9700\u8981\u89e3\u51b3\u54ea\u4e9b\u95ee\u9898<\/p>\n<p id=\"1.2%20%E5%86%8D%E8%B0%88%E2%80%9C%E5%8D%8F%E8%AE%AE%E2%80%9D%E2%80%94%E2%80%94%E5%88%B0%E5%BA%95%E6%98%AF%E4%BB%80%E4%B9%88-toc\" style=\"margin-left:120px\">1.2 \u518d\u8c08\u201c\u534f\u8bae\u201d&#xff1a;\u7f51\u7edc\u901a\u4fe1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ea6\u5b9a<\/p>\n<p id=\"1.3%20%E7%BD%91%E7%BB%9C%E7%89%88%E8%AE%A1%E7%AE%97%E5%99%A8-toc\" style=\"margin-left:120px\">1.3 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1<\/p>\n<p id=\"1.3.1%20%E7%BA%A6%E5%AE%9A%E6%96%B9%E6%A1%88%E4%B8%80-toc\" style=\"margin-left:160px\">1.3.1 \u65b9\u6848\u4e00&#xff1a;\u8bbe\u8ba1\u901a\u4fe1\u6570\u636e\u7684\u7ea6\u5b9a\u683c\u5f0f<\/p>\n<p id=\"1.3.2%20%E7%BA%A6%E5%AE%9A%E6%96%B9%E6%A1%88%E4%BA%8C-toc\" style=\"margin-left:160px\">1.3.2 \u65b9\u6848\u4e8c&#xff1a;\u8fdb\u4e00\u6b65\u5b8c\u5584\u534f\u8bae\u7ea6\u5b9a<\/p>\n<p id=\"1.3.3%20%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96-toc\" style=\"margin-left:160px\">1.3.3 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316&#xff1a;\u8ba9\u7ed3\u6784\u5316\u6570\u636e\u80fd\u591f\u4f20\u8f93<\/p>\n<p id=\"1.3.4%E6%88%91%E4%BB%AC%E8%A6%81%E5%86%99%E7%9A%84%E7%BD%91%E7%BB%9C%E8%AE%A1%E7%AE%97%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E6%9E%B6%E6%9E%84%E6%98%AF%E6%80%8E%E4%B9%88%E6%A0%B7%E7%9A%84-toc\" style=\"margin-left:160px\">1.3.4 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u6574\u4f53\u67b6\u6784<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C-toc\" style=\"margin-left:80px\">\u4e8c\u3001\u5f00\u53d1\u51c6\u5907&#xff1a;I\/O\u673a\u5236\u3001Socket\u5c01\u88c5\u4e0eJsonCpp<\/p>\n<p id=\"2.1%20%E9%87%8D%E6%96%B0%E7%90%86%E8%A7%A3%20read%E3%80%81write%E3%80%81recv%E3%80%81send-toc\" style=\"margin-left:120px\">2.1 \u91cd\u65b0\u7406\u89e3read\u3001write\u3001recv\u4e0esend<\/p>\n<p id=\"2.1.1%20%E4%B8%BA%E4%BB%80%E4%B9%88%20TCP%20%E6%94%AF%E6%8C%81%E5%85%A8%E5%8F%8C%E5%B7%A5-toc\" style=\"margin-left:160px\">2.1.1 TCP\u4e3a\u4ec0\u4e48\u652f\u6301\u5168\u53cc\u5de5\u901a\u4fe1<\/p>\n<p id=\"2.1.2%20%E9%87%8D%E6%96%B0%E7%90%86%E8%A7%A3%20I%2FO%20%E6%8E%A5%E5%8F%A3%EF%BC%9A%E5%86%99%E4%B8%8E%E8%AF%BB%E7%9A%84%E6%9C%AC%E8%B4%A8%EF%BC%8C%E6%98%AF%E6%95%B0%E6%8D%AE%E6%8B%B7%E8%B4%9D-toc\" style=\"margin-left:160px\">2.1.2 I\/O\u64cd\u4f5c\u7684\u672c\u8d28&#xff1a;\u7528\u6237\u6001\u4e0e\u5185\u6838\u6001\u4e4b\u95f4\u7684\u6570\u636e\u62f7\u8d1d<\/p>\n<p id=\"2.1.3%20%E5%86%85%E6%A0%B8%E4%B8%8E%E7%94%A8%E6%88%B7%E6%80%81%E4%B9%8B%E9%97%B4%E7%9A%84%E7%94%9F%E4%BA%A7%E8%80%85%E6%B6%88%E8%B4%B9%E8%80%85%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:160px\">2.1.3 \u4ece\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7406\u89e3\u6570\u636e\u6536\u53d1\u8fc7\u7a0b<\/p>\n<p id=\"2.2%20%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%20Socket%20%E5%B0%81%E8%A3%85-toc\" style=\"margin-left:120px\">2.2 \u5c01\u88c5Socket&#xff1a;\u7edf\u4e00\u7f51\u7edc\u901a\u4fe1\u63a5\u53e3<\/p>\n<p id=\"2.2.1%20%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E2%80%94%E2%80%94%E6%A8%A1%E6%9D%BF%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:160px\">2.2.1 \u8bbe\u8ba1\u6a21\u5f0f&#xff1a;\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f<\/p>\n<p id=\"2.2.2%20Socket.hpp%EF%BC%9A%E5%B0%81%E8%A3%85%E5%AE%9E%E7%8E%B0%E4%B8%8E%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90-toc\" style=\"margin-left:120px\">2.2.2 Socket.hpp&#xff1a;\u5c01\u88c5\u5b9e\u73b0\u4e0e\u6e90\u7801\u89e3\u6790<\/p>\n<p id=\"2.3%20%E7%90%86%E8%A7%A3%20Jsoncpp%E2%80%94%E2%80%94%E4%B8%80%E7%A7%8D%E8%BD%BB%E9%87%8F%E7%BA%A7%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%B7%A5%E5%85%B7-toc\" style=\"margin-left:120px\">2.3 \u8ba4\u8bc6JsonCpp&#xff1a;\u8f7b\u91cf\u7ea7JSON\u5904\u7406\u5de5\u5177<\/p>\n<p id=\"2.3.1%20Jsoncpp%20%E6%98%AF%E4%BB%80%E4%B9%88-toc\" style=\"margin-left:160px\">2.3.1 JsonCpp\u662f\u4ec0\u4e48<\/p>\n<p id=\"2.3.2%20Jsoncpp%20%E7%9A%84%E7%89%B9%E6%80%A7-toc\" style=\"margin-left:160px\">2.3.2 JsonCpp\u7684\u4e3b\u8981\u7279\u70b9<\/p>\n<p id=\"2.3.3%20%E5%AE%89%E8%A3%85JsonCpp-toc\" style=\"margin-left:120px\">2.3.3 \u5b89\u88c5JsonCpp<\/p>\n<p id=\"CentOS%EF%BC%9A-toc\" style=\"margin-left:160px\">CentOS&#xff1a;<\/p>\n<p id=\"2.3.4%20Jsoncpp%20%E5%BA%8F%E5%88%97%E5%8C%96-toc\" style=\"margin-left:160px\">2.3.4 JSON\u5e8f\u5217\u5316&#xff1a;\u5c06\u7ed3\u6784\u5316\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c<\/p>\n<p id=\"1.%20%E4%BD%BF%E7%94%A8Json%3A%3AValue%E7%9A%84toStyledString%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:200px\">2.3.4.1 \u4f7f\u7528Json::Value\u7684toStyledString()\u65b9\u6cd5<\/p>\n<p id=\"2.%20%E4%BD%BF%E7%94%A8Json%3A%3AStreamWriterBuilder%EF%BC%88%E6%8E%A8%E8%8D%90%EF%BC%89-toc\" style=\"margin-left:200px\">2.3.4.2 \u4f7f\u7528Json::StreamWriterBuilder\u4e0ewriteString()&#xff08;\u63a8\u8350&#xff09;<\/p>\n<p id=\"3.%20%E4%BD%BF%E7%94%A8%20Json%3A%3AFastWriter%20%E6%88%96%20Json%3A%3AStyledWriter-toc\" style=\"margin-left:200px\">2.3.4.3 FastWriter\u4e0eStyledWriter\u7b49\u65e7\u63a5\u53e3<\/p>\n<p id=\"2.3.5%20%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96-toc\" style=\"margin-left:160px\">2.3.5 JSON\u53cd\u5e8f\u5217\u5316&#xff1a;\u4ece\u6587\u672c\u8fd8\u539f\u7ed3\u6784\u5316\u6570\u636e<\/p>\n<p id=\"1.%20%E4%BD%BF%E7%94%A8%20Json%3A%3AReader-toc\" style=\"margin-left:200px\">2.3.5.1 \u4f7f\u7528Json::Reader\u65e7\u63a5\u53e3<\/p>\n<p id=\"2.%20%E4%BD%BF%E7%94%A8%20Json%3A%3ACharReader%20%E7%9A%84%E6%B4%BE%E7%94%9F%E7%B1%BB%EF%BC%88%E4%B8%8D%E6%8E%A8%E8%8D%90%EF%BC%89-toc\" style=\"margin-left:200px\">2.3.5.2 \u4f7f\u7528Json::CharReaderBuilder\u521b\u5efa\u89e3\u6790\u5668<\/p>\n<p id=\"2.3.6%20Json%3A%3AValue%E8%A1%A5%E5%85%85%EF%BC%9A%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C%E4%B8%8E%E6%95%B0%E6%8D%AE%E8%AE%BF%E9%97%AE%EF%BC%88%E9%80%89%E8%AF%BB%EF%BC%89-toc\" style=\"margin-left:160px\">2.3.6 Json::Value\u8865\u5145&#xff1a;\u5e38\u7528\u64cd\u4f5c\u4e0e\u6570\u636e\u8bbf\u95ee&#xff08;\u9009\u8bfb&#xff09;<\/p>\n<p id=\"1.%20%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:200px\">2.3.6.1 \u6784\u9020\u4e0e\u521d\u59cb\u5316<\/p>\n<p id=\"2.3.6.2%20%E5%85%83%E7%B4%A0%E8%AE%BF%E9%97%AE-toc\" style=\"margin-left:200px\">2.3.6.2 \u5143\u7d20\u8bbf\u95ee<\/p>\n<p id=\"3.%20%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%9F%A5-toc\" style=\"margin-left:200px\">2.3.6.3 \u7c7b\u578b\u68c0\u67e5<\/p>\n<p id=\"4.%20%E8%B5%8B%E5%80%BC%E5%92%8C%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-toc\" style=\"margin-left:200px\">2.3.6.4 \u8d4b\u503c\u4e0e\u7c7b\u578b\u8f6c\u6362<\/p>\n<p id=\"5.%20%E6%95%B0%E7%BB%84%E5%92%8C%E5%AF%B9%E8%B1%A1%E6%93%8D%E4%BD%9C-toc\" style=\"margin-left:200px\">2.3.6.5 JSON\u6570\u7ec4\u4e0e\u5bf9\u8c61\u64cd\u4f5c<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96-toc\" style=\"margin-left:80px\">\u4e09\u3001\u6df1\u5165\u7406\u89e3\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316&#xff1a;\u5e94\u7528\u5c42\u534f\u8bae\u7684\u6838\u5fc3<\/p>\n<p id=\"3.1%20%E5%BA%94%E7%94%A8%E5%B1%82%E5%8D%8F%E8%AE%AE%E4%B8%8E%E7%BB%93%E6%9E%84%E5%8C%96%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:120px\">3.1 \u5e94\u7528\u5c42\u534f\u8bae\u4e0e\u7ed3\u6784\u5316\u6570\u636e<\/p>\n<p id=\"3.1.1%20%E7%BB%93%E6%9E%84%E5%8C%96%E6%95%B0%E6%8D%AE%E4%B8%8E%E5%8D%8F%E8%AE%AE%E5%AE%9A%E4%B9%89-toc\" style=\"margin-left:160px\">3.1.1 \u7ed3\u6784\u5316\u6570\u636e\u4e0e\u534f\u8bae\u683c\u5f0f\u8bbe\u8ba1<\/p>\n<p id=\"3.1.2%20%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E5%8F%91%E9%80%81%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BB%93%E6%9E%84%E4%BD%93%EF%BC%9F-toc\" style=\"margin-left:160px\">3.1.2 \u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u53d1\u9001\u4e8c\u8fdb\u5236\u7ed3\u6784\u4f53<\/p>\n<p id=\"3.2%20%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%9A%84%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:120px\">3.2 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u7684\u57fa\u672c\u539f\u7406<\/p>\n<p id=\"3.2.1%20%E6%A6%82%E5%BF%B5%E5%AE%9A%E4%B9%89-toc\" style=\"margin-left:160px\">3.2.1 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u7684\u6982\u5ff5<\/p>\n<p id=\"3.2.2%20%E6%A0%B8%E5%BF%83%E4%BB%B7%E5%80%BC%E4%B8%8E%E4%BC%98%E5%8A%BF-toc\" style=\"margin-left:160px\">3.2.2 \u5e8f\u5217\u5316\u7684\u4f5c\u7528\u4e0e\u4f18\u52bf<\/p>\n<p id=\"3.3%20%E5%BA%94%E7%94%A8%E5%B1%82%E9%80%9A%E4%BF%A1%E7%9A%84%E6%A0%87%E5%87%86%E5%8C%96%E5%BB%BA%E8%AE%AE-toc\" style=\"margin-left:120px\">3.3 \u5e94\u7528\u5c42\u901a\u4fe1\u7684\u6807\u51c6\u5316\u5efa\u8bae<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E9%99%84%E4%B8%8A%E6%BA%90%E7%A0%81-toc\" style=\"margin-left:80px\">\u56db\u3001\u5b8c\u6574\u6e90\u7801\u4e0e\u8fd0\u884c\u6d4b\u8bd5<\/p>\n<p id=\"4.1%20Common.hpp%EF%BC%88%E5%85%AC%E5%85%B1%E5%A4%B4%E6%96%87%E4%BB%B6%EF%BC%89-toc\" style=\"margin-left:120px\">4.1 Common.hpp&#xff08;\u516c\u5171\u5934\u6587\u4ef6&#xff09;<\/p>\n<p id=\"4.2%20Log.hpp%EF%BC%88%E6%97%A5%E5%BF%97%EF%BC%89-toc\" style=\"margin-left:120px\">4.2 Log.hpp&#xff08;\u65e5\u5fd7&#xff09;<\/p>\n<p id=\"4.3%20InetAddr.hpp%EF%BC%88IP%20%E5%9C%B0%E5%9D%80%E5%B0%81%E8%A3%85%EF%BC%89-toc\" style=\"margin-left:120px\">4.3 InetAddr.hpp&#xff08;IP \u5730\u5740\u5c01\u88c5&#xff09;<\/p>\n<p id=\"4.4%20Socket.hpp%EF%BC%88Socket%20%E5%B0%81%E8%A3%85%EF%BC%89-toc\" style=\"margin-left:120px\">4.4 Socket.hpp&#xff08;Socket \u5c01\u88c5&#xff09;<\/p>\n<p id=\"4.5%20Protocol.hpp%EF%BC%88%E5%8D%8F%E8%AE%AE%E6%A8%A1%E5%9D%97%EF%BC%9A%E5%BA%8F%E5%88%97%E5%8C%96%E3%80%81%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E3%80%81%E7%BB%84%E5%B8%A7%EF%BC%89-toc\" style=\"margin-left:120px\">4.5 Protocol.hpp&#xff08;\u534f\u8bae\u6a21\u5757&#xff1a;\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316\u3001\u7ec4\u5e27&#xff09;<\/p>\n<p id=\"4.6%20TcpServer.hpp%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%B1%BB%EF%BC%89-toc\" style=\"margin-left:120px\">4.6 TcpServer.hpp&#xff08;\u670d\u52a1\u7aef\u7c7b&#xff09;<\/p>\n<p id=\"4.7%20TcpServer.cc%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89-toc\" style=\"margin-left:120px\">4.7 TcpServer.cc&#xff08;\u670d\u52a1\u7aef\u4e3b\u7a0b\u5e8f&#xff09;<\/p>\n<p id=\"4.8%20TcpClient.cc%EF%BC%88%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89-toc\" style=\"margin-left:120px\">4.8 TcpClient.cc&#xff08;\u5ba2\u6237\u7aef\u4e3b\u7a0b\u5e8f&#xff09;<\/p>\n<p id=\"4.9%20Makefile-toc\" style=\"margin-left:120px\">4.9 Makefile<\/p>\n<p id=\"4.10%20%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F-toc\" style=\"margin-left:120px\">4.10 \u8fd0\u884c\u65b9\u5f0f<\/p>\n<p id=\"4.11%20%E5%8D%8F%E8%AE%AE%E8%AE%BE%E8%AE%A1%E8%AF%B4%E6%98%8E-toc\" style=\"margin-left:120px\">4.11 \u534f\u8bae\u8bbe\u8ba1\u8bf4\u660e<\/p>\n<hr id=\"hr-toc\" \/>\n<h4 id=\"%E4%B8%80%E3%80%81%E6%88%91%E4%BB%AC%E6%8E%A5%E4%B8%8B%E6%9D%A5%E5%88%B0%E5%BA%95%E8%A6%81%E5%81%9A%E4%BB%80%E4%B9%88\">\u4e00\u3001\u4ece\u9700\u6c42\u51fa\u53d1&#xff1a;\u8bbe\u8ba1\u4e00\u4e2a\u7f51\u7edc\u7248\u8ba1\u7b97\u5668<\/h4>\n<h5 id=\"1.1%20%E8%87%AA%E4%B8%BB%E8%AE%BE%E8%AE%A1%E7%9A%84%E5%BA%94%E7%94%A8%E5%B1%82%E5%8D%8F%E8%AE%AE%EF%BC%8C%E9%9C%80%E8%A6%81%E8%A7%A3%E5%86%B3%E4%B8%A4%E4%B8%AA%E9%97%AE%E9%A2%98\">1.1 \u81ea\u4e3b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u9700\u8981\u89e3\u51b3\u54ea\u4e9b\u95ee\u9898<\/h5>\n<p>\u6211\u4eec\u5373\u5c06\u8bbe\u8ba1\u7684\u8fd9\u5957\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u5957\u7ed3\u6784\u5316\u5b57\u6bb5\u7684\u7f16\u7801\u7ea6\u5b9a\u3002\u5b83\u5f97\u8fbe\u6210\u4e24\u4e2a\u6838\u5fc3\u76ee\u6807&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63d0\u4f9b\u5b8c\u5907\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u673a\u5236&#xff0c;\u786e\u4fdd\u7ed3\u6784\u5316\u6570\u636e\u5728\u5b57\u8282\u6d41\u548c\u5185\u5b58\u5bf9\u8c61\u4e4b\u95f4\u65e0\u635f\u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e3\u51b3TCP\u5b57\u8282\u6d41\u6ca1\u6709\u8fb9\u754c\u6240\u5bfc\u81f4\u7684\u62a5\u6587\u8bfb\u53d6\u4e0d\u5b8c\u6574\u95ee\u9898\u3002<\/p>\n<\/li>\n<\/ul>\n<h5 id=\"1.2%20%E5%86%8D%E8%B0%88%E2%80%9C%E5%8D%8F%E8%AE%AE%E2%80%9D%E2%80%94%E2%80%94%E5%88%B0%E5%BA%95%E6%98%AF%E4%BB%80%E4%B9%88\">1.2 \u518d\u8c08\u201c\u534f\u8bae\u201d&#xff1a;\u7f51\u7edc\u901a\u4fe1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ea6\u5b9a<\/h5>\n<p>\u534f\u8bae&#xff0c;\u8bf4\u5230\u5e95\u5c31\u662f\u4e00\u79cd\u201c\u7ea6\u5b9a\u201d\u3002<\/p>\n<p>Socket API\u5728\u8bfb\u5199\u6570\u636e\u65f6&#xff0c;\u90fd\u662f\u6309\u201c\u5b57\u7b26\u4e32\u201d\u7684\u65b9\u5f0f\u6765\u53d1\u9001\u548c\u63a5\u6536\u7684\u3002\u53ef\u5982\u679c\u6211\u4eec\u8981\u4f20\u8f93\u4e00\u4e9b\u201c\u7ed3\u6784\u5316\u7684\u6570\u636e\u201d\u5462&#xff1f;<\/p>\n<p>\u7b54\u6848\u8fd8\u662f\u90a3\u4e24\u4e2a\u5b57&#xff1a;\u7ea6\u5b9a\u3002\u534f\u8bae&#xff0c;\u5c31\u662f\u53cc\u65b9\u4e8b\u5148\u7ea6\u5b9a\u597d\u7684\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<h5 id=\"1.3%20%E7%BD%91%E7%BB%9C%E7%89%88%E8%AE%A1%E7%AE%97%E5%99%A8\">1.3 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1<\/h5>\n<p>\u4e3e\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50&#xff1a;\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\u5668\u7248\u7684\u52a0\u6cd5\u5668\u3002\u5ba2\u6237\u7aef\u628a\u8981\u8ba1\u7b97\u7684\u4e24\u4e2a\u52a0\u6570\u53d1\u8fc7\u53bb&#xff0c;\u670d\u52a1\u5668\u7b97\u5b8c&#xff0c;\u518d\u628a\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<h6 id=\"1.3.1%20%E7%BA%A6%E5%AE%9A%E6%96%B9%E6%A1%88%E4%B8%80\">1.3.1 \u65b9\u6848\u4e00&#xff1a;\u8bbe\u8ba1\u901a\u4fe1\u6570\u636e\u7684\u7ea6\u5b9a\u683c\u5f0f<\/h6>\n<p>\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a\u5f62\u5982\u201c1&#043;2\u201d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u6709\u4e24\u4e2a\u64cd\u4f5c\u6570&#xff0c;\u90fd\u662f\u6574\u578b&#xff1b;\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u6709\u4e00\u4e2a\u5b57\u7b26\u662f\u8fd0\u7b97\u7b26&#xff0c;\u8fd0\u7b97\u7b26\u53ea\u80fd\u662f&#043;&#xff1b;\u6570\u5b57\u548c\u8fd0\u7b97\u7b26\u4e4b\u95f4\u6ca1\u6709\u7a7a\u683c\u3002<\/p>\n<h6 id=\"1.3.2%20%E7%BA%A6%E5%AE%9A%E6%96%B9%E6%A1%88%E4%BA%8C\">1.3.2 \u65b9\u6848\u4e8c&#xff1a;\u8fdb\u4e00\u6b65\u5b8c\u5584\u534f\u8bae\u7ea6\u5b9a<\/h6>\n<p>\u5b9a\u4e49\u7ed3\u6784\u4f53\u6765\u8868\u793a\u6211\u4eec\u9700\u8981\u4ea4\u4e92\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u628a\u8fd9\u4e2a\u7ed3\u6784\u4f53\u6309\u7167\u4e00\u4e2a\u89c4\u5219\u8f6c\u6362\u6210\u5b57\u7b26\u4e32&#xff1b;\u63a5\u6536\u5230\u6570\u636e\u65f6&#xff0c;\u518d\u6309\u7167\u76f8\u540c\u7684\u89c4\u5219\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u7ed3\u6784\u4f53\u3002<\/p>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u5c31\u53eb\u201c\u5e8f\u5217\u5316\u201d\u548c\u201c\u53cd\u5e8f\u5217\u5316\u201d\u3002<\/p>\n<h6 id=\"1.3.3%20%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\">1.3.3 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316&#xff1a;\u8ba9\u7ed3\u6784\u5316\u6570\u636e\u80fd\u591f\u4f20\u8f93<\/h6>\n<p>\u65e0\u8bba\u91c7\u7528\u65b9\u6848\u4e00\u3001\u65b9\u6848\u4e8c&#xff0c;\u8fd8\u662f\u5176\u4ed6\u4ec0\u4e48\u65b9\u6848&#xff0c;\u53ea\u8981\u4fdd\u8bc1\u4e00\u7aef\u53d1\u9001\u65f6\u6784\u9020\u7684\u6570\u636e&#xff0c;\u5728\u53e6\u4e00\u7aef\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790&#xff0c;\u5c31\u662fOK\u7684\u3002\u8fd9\u79cd\u7ea6\u5b9a&#xff0c;\u5c31\u662f\u5e94\u7528\u5c42\u534f\u8bae\u3002<\/p>\n<p>\u4e0d\u8fc7&#xff0c;\u4e3a\u4e86\u8ba9\u6211\u4eec\u6df1\u523b\u7406\u89e3\u534f\u8bae&#xff0c;\u8fd9\u6b21\u6253\u7b97\u81ea\u5b9a\u4e49\u5b9e\u73b0\u4e00\u904d\u534f\u8bae\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>\u6211\u4eec\u91c7\u7528\u65b9\u6848\u4e8c&#xff0c;\u540c\u65f6\u4e5f\u8981\u4f53\u73b0\u534f\u8bae\u5b9a\u5236\u7684\u7ec6\u8282\u3002<\/p>\n<ul>\n<li>\n<p>\u5f15\u5165\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002\u53ea\u4e0d\u8fc7\u8fd9\u6b21\u76f4\u63a5\u91c7\u7528\u73b0\u6210\u7684\u65b9\u6848&#xff0c;jsoncpp\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9socket\u8fdb\u884c\u5b57\u8282\u6d41\u7684\u8bfb\u53d6\u5904\u7406\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u6709\u4e2a\u5173\u952e\u70b9&#xff1a;\u9762\u5411\u5b57\u8282\u6d41\u7684\u65f6\u5019&#xff0c;\u53d1\u9001\u62a5\u6587\u7684\u6b21\u6570\u548c\u8bfb\u53d6\u62a5\u6587\u7684\u6b21\u6570&#xff0c;\u5e76\u4e0d\u662f\u4e25\u683c\u76841:1\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n<p>\u5728TCP\u4e2d&#xff0c;\u8981\u8bfb\u5230\u5b8c\u6574\u7684\u62a5\u6587&#xff0c;\u5f97\u7531\u63a5\u6536\u65b9\u5e94\u7528\u5c42\u81ea\u5df1\u786e\u5b9a\u8fb9\u754c\u3002\u53ea\u8bfb\u53d6\u534a\u4e2a\u62a5\u6587&#xff0c;\u6216\u8005\u4e00\u6b21\u591a\u8bfb\u4e86\u62a5\u6587&#xff0c;\u8fd9\u7c7b\u95ee\u9898\u6211\u4eec\u53eb\u5b83&#xff1a;\u6570\u636e\u62a5\u9ecf\u62a5\u95ee\u9898\u3002<\/p>\n<p>TCP\u662f\u9762\u5411\u5b57\u8282\u6d41\u7684&#xff0c;\u6240\u4ee5\u201c\u4ec0\u4e48\u65f6\u5019\u53d1\u3001\u53d1\u591a\u5c11\u3001\u51fa\u9519\u4e86\u600e\u4e48\u529e\u201d&#xff0c;\u5b8c\u5168\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ed3\u5408TCP\u534f\u8bae\u81ea\u4e3b\u51b3\u5b9a\u3002<\/p>\n<h6 id=\"1.3.4%E6%88%91%E4%BB%AC%E8%A6%81%E5%86%99%E7%9A%84%E7%BD%91%E7%BB%9C%E8%AE%A1%E7%AE%97%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E6%9E%B6%E6%9E%84%E6%98%AF%E6%80%8E%E4%B9%88%E6%A0%B7%E7%9A%84\">1.3.4 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u6574\u4f53\u67b6\u6784<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"541\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054113-6ad6fef97cbd5.png\" width=\"826\" \/><\/p>\n<h4 id=\"%E4%BA%8C%E3%80%81%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\">\u4e8c\u3001\u5f00\u53d1\u51c6\u5907&#xff1a;I\/O\u673a\u5236\u3001Socket\u5c01\u88c5\u4e0eJsonCpp<\/h4>\n<h5 id=\"2.1%20%E9%87%8D%E6%96%B0%E7%90%86%E8%A7%A3%20read%E3%80%81write%E3%80%81recv%E3%80%81send\">2.1 \u91cd\u65b0\u7406\u89e3read\u3001write\u3001recv\u4e0esend<\/h5>\n<h6 id=\"2.1.1%20%E4%B8%BA%E4%BB%80%E4%B9%88%20TCP%20%E6%94%AF%E6%8C%81%E5%85%A8%E5%8F%8C%E5%B7%A5\">2.1.1 TCP\u4e3a\u4ec0\u4e48\u652f\u6301\u5168\u53cc\u5de5\u901a\u4fe1<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"666\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054113-6ad6fef9aae58.png\" width=\"1306\" \/><\/p>\n<p>\u5728\u4efb\u4f55\u4e00\u53f0\u4e3b\u673a\u4e0a&#xff0c;\u4e00\u4e2aTCP\u8fde\u63a5\u90fd\u914d\u7740\u4e24\u5957\u7f13\u51b2\u533a&#xff1a;\u4e00\u5957\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u4e00\u5957\u63a5\u6536\u7f13\u51b2\u533a\u3002<\/p>\n<p>\u6b63\u56e0\u5982\u6b64&#xff0c;\u5185\u6838\u624d\u80fd\u5728\u53d1\u6d88\u606f\u7684\u540c\u65f6&#xff0c;\u4e5f\u6536\u6d88\u606f\u3002\u53d1\u4f60\u7684&#xff0c;\u6536\u6211\u7684&#xff0c;\u4e24\u6761\u901a\u9053\u5404\u8dd1\u5404\u7684&#xff0c;\u4e92\u4e0d\u5e72\u6270\u3002\u8fd9\u5c31\u662f\u5168\u53cc\u5de5\u3002<\/p>\n<p>\u4e5f\u6b63\u56e0\u4e3a\u8fd9\u4e2a\u8bbe\u8ba1&#xff0c;\u4e00\u4e2aTCP\u7684sockfd&#xff0c;\u65e2\u80fd\u8bfb&#xff0c;\u4e5f\u80fd\u5199&#xff0c;\u8bfb\u5199\u90fd\u9760\u5b83\u4e00\u4e2a\u3002\u4e0d\u9700\u8981\u50cfUDP\u90a3\u6837\u51c6\u5907\u4e24\u4e2a\u5957\u63a5\u5b57&#xff0c;\u4e00\u4e2aTCP\u5957\u63a5\u5b57\u5c31\u628a\u6536\u53d1\u4e24\u4ef6\u4e8b\u5168\u5305\u4e86\u3002<\/p>\n<p>\u81f3\u4e8e\u5b9e\u9645\u6570\u636e\u4ec0\u4e48\u65f6\u5019\u53d1\u3001\u53d1\u591a\u5c11\u3001\u51fa\u9519\u4e86\u600e\u4e48\u529e&#xff0c;\u7edf\u7edf\u7531TCP\u81ea\u5df1\u638c\u63a7\u3002\u5e94\u7528\u7a0b\u5e8f\u53ea\u7ba1\u628a\u6570\u636e\u585e\u8fdb\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u5269\u4e0b\u7684\u8def\u7531\u3001\u91cd\u4f20\u3001\u6d41\u91cf\u63a7\u5236&#xff0c;TCP\u4e00\u624b\u5305\u529e\u3002<\/p>\n<p>\u6240\u4ee5\u5b83\u624d\u53eb\u4f20\u8f93\u63a7\u5236\u534f\u8bae\u2014\u2014\u201c\u63a7\u5236\u201d\u4e24\u4e2a\u5b57&#xff0c;\u4e0d\u662f\u767d\u53eb\u7684\u3002<\/p>\n<h6 id=\"2.1.2%20%E9%87%8D%E6%96%B0%E7%90%86%E8%A7%A3%20I%2FO%20%E6%8E%A5%E5%8F%A3%EF%BC%9A%E5%86%99%E4%B8%8E%E8%AF%BB%E7%9A%84%E6%9C%AC%E8%B4%A8%EF%BC%8C%E6%98%AF%E6%95%B0%E6%8D%AE%E6%8B%B7%E8%B4%9D\" style=\"background-color:transparent\">2.1.2 I\/O\u64cd\u4f5c\u7684\u672c\u8d28&#xff1a;\u7528\u6237\u6001\u4e0e\u5185\u6838\u6001\u4e4b\u95f4\u7684\u6570\u636e\u62f7\u8d1d<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"838\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054114-6ad6fefa2ae99.png\" width=\"1877\" \/><\/p>\n<p>\u5199\u5957\u63a5\u5b57\u901a\u4fe1\u4ee3\u7801\u65f6&#xff0c;write\/send\u548cread\/recv\u5929\u5929\u89c1\u3002\u4f46\u522b\u88ab\u540d\u5b57\u9a97\u4e86&#xff0c;\u8fd9\u4e9b\u63a5\u53e3\u538b\u6839\u4e0d\u76f4\u63a5\u8ddf\u7f51\u7edc\u3001\u7f51\u5361\u6253\u4ea4\u9053\u3002\u5b83\u4eec\u7684\u6838\u5fc3\u52a8\u4f5c&#xff0c;\u5c31\u4e00\u4ef6\u4e8b&#xff1a;\u5728\u5e94\u7528\u5c42\u7f13\u51b2\u533a\u548c\u5185\u6838\u7f13\u51b2\u533a\u4e4b\u95f4&#xff0c;\u642c\u6570\u636e\u3002<\/p>\n<p>\u5e94\u7528\u5c42\u7f13\u51b2\u533a (User Space) \u00a0&lt;&#8212;&gt; \u00a0\u7cfb\u7edf\u8c03\u7528 (write\/read) \u00a0&lt;&#8212;&gt; \u00a0\u5185\u6838\u7f13\u51b2\u533a (OS Kernel)<\/p>\n<p>write\/send\u7684\u672c\u8d28&#xff1a;\u5185\u5b58\u62f7\u8d1d<\/p>\n<p>\u4e0a\u5c42\u8c03\u7528write(sockfd, buffer, len)&#xff0c;\u5b83\u5e72\u7684\u6d3b\u513f\u5f88\u5355\u7eaf&#xff0c;\u628a\u7528\u6237\u6001\u5e94\u7528\u5c42\u7f13\u51b2\u533a&#xff08;buffer&#xff09;\u91cc\u7684\u6570\u636e&#xff0c;\u62f7\u8d1d\u5230\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u5bf9\u5e94\u7684TCP\u53d1\u9001\u7f13\u51b2\u533a\u91cc\u3002<\/p>\n<p>\u6ce8\u610f&#xff0c;\u6570\u636e\u5e76\u6ca1\u6709\u76f4\u63a5\u53d1\u5230\u7f51\u7edc\u4e0a\u3002 write\u6210\u529f\u8fd4\u56de&#xff0c;\u53ea\u4ee3\u8868\u6570\u636e\u6210\u529f\u62f7\u8d1d\u8fdb\u4e86\u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u4e0d\u4ee3\u8868\u5bf9\u7aef\u5df2\u7ecf\u6536\u5230\u3002\u81f3\u4e8e\u4ec0\u4e48\u65f6\u5019\u53d1\u3001\u53d1\u591a\u5c11\u3001\u8d85\u65f6\u91cd\u4f20\u600e\u4e48\u5904\u7406&#xff0c;\u5168\u7531\u5185\u6838\u7684 TCP \u534f\u8bae\u6808\u81ea\u5df1\u62ff\u4e3b\u610f\u3002<\/p>\n<p>read\/recv\u7684\u672c\u8d28&#xff1a;\u5185\u5b58\u62f7\u8d1d\u4e0e\u540c\u6b65<\/p>\n<p>\u4e0a\u5c42\u8c03\u7528read(sockfd, buffer, len)&#xff0c;\u5b83\u5148\u7785\u4e00\u773cTCP\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u6709\u6ca1\u6709\u6570\u636e\u3002<\/p>\n<p>\u6709\u6570\u636e&#xff0c;\u5c31\u628a\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u7684\u5185\u5bb9&#xff0c;\u62f7\u8d1d\u5230\u7528\u6237\u6001\u7684\u5e94\u7528\u5c42\u7f13\u51b2\u533a&#xff08;buffer&#xff09;\u91cc\u3002\u6ca1\u6570\u636e&#xff0c;\u8c03\u7528\u901a\u5e38\u5c31\u963b\u585e\u5728\u90a3\u513f&#xff08;\u963b\u585e\u6a21\u5f0f\u4e0b&#xff09;&#xff0c;\u76f4\u5230\u7f51\u5361\u6536\u5230\u7f51\u7edc\u6570\u636e\u5305\u3001\u64cd\u4f5c\u7cfb\u7edf\u628a\u5b83\u5199\u8fdb\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u624d\u628a\u8c03\u7528\u5524\u9192\u3002<\/p>\n<p>\u901a\u4fe1\u5373\u62f7\u8d1d\u2014\u2014\u8ba1\u7b97\u673a\u4e16\u754c\u7684\u672c\u8d28<\/p>\n<p>\u4e24\u53f0\u4e3b\u673a\u4e4b\u95f4\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u8bf4\u5230\u5e95&#xff0c;\u5c31\u662f\u628a\u53d1\u9001\u65b9\u53d1\u9001\u7f13\u51b2\u533a\u91cc\u7684\u6570\u636e&#xff0c;\u8de8\u8fc7\u7f51\u7edc&#xff0c;\u62f7\u8d1d\u5230\u5bf9\u7aef\u7684\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u3002<\/p>\n<p>\u6574\u6761\u6570\u636e\u6d41\u8f6c\u94fe\u8def\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<p>\u5e94\u7528\u5c42\u2192 \u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a\u2192\u7f51\u5361 \u2192\u7f51\u7edc \u2192\u5bf9\u7aef\u7f51\u5361 \u2192\u5bf9\u7aef\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a \u2192\u5bf9\u7aef\u5e94\u7528\u5c42<\/p>\n<p>\u6bcf\u4e00\u6b65\u7684\u6838\u5fc3\u52a8\u4f5c&#xff0c;\u90fd\u662f\u6570\u636e\u62f7\u8d1d\u3002\u7f51\u7edc\u901a\u4fe1\u8fd9\u4ef6\u770b\u8d77\u6765\u65e0\u6bd4\u590d\u6742\u7684\u4e8b&#xff0c;\u62c6\u5230\u6700\u5e95\u5c42&#xff0c;\u4e0d\u8fc7\u662f\u4e00\u573a\u63a5\u529b\u5f0f\u7684\u6570\u636e\u642c\u8fd0\u3002<\/p>\n<h6 id=\"2.1.3%20%E5%86%85%E6%A0%B8%E4%B8%8E%E7%94%A8%E6%88%B7%E6%80%81%E4%B9%8B%E9%97%B4%E7%9A%84%E7%94%9F%E4%BA%A7%E8%80%85%E6%B6%88%E8%B4%B9%E8%80%85%E6%A8%A1%E5%9E%8B\">2.1.3 \u4ece\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7406\u89e3\u6570\u636e\u6536\u53d1\u8fc7\u7a0b<\/h6>\n<p>\u57fa\u4e8e\u5185\u6838\u7f13\u51b2\u533a\u4e0eI\/O\u7cfb\u7edf\u8c03\u7528\u7684\u5de5\u4f5c\u673a\u5236&#xff0c;\u7528\u6237\u8fdb\u7a0b\u548c\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u4e4b\u95f4&#xff0c;\u5176\u5b9e\u6784\u6210\u4e86\u4e00\u4e2a\u5178\u578b\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u3002<\/p>\n<p>[\u53d1\u9001\u7aef\u5e94\u7528\u5c42] &#8212;&gt; (\u751f\u4ea7\u8005: write) &#8212;&gt; [\u53d1\u9001\u7f13\u51b2\u533a] &#8212;&gt; (\u6d88\u8d39\u8005: TCP\u534f\u8bae\u6808)<br \/>\n                                                                 |<br \/>\n                                                             (\u7f51\u7edc\u4f20\u8f93)<br \/>\n                                                                 |<br \/>\n[\u63a5\u6536\u7aef\u5e94\u7528\u5c42] &lt;&#8212; (\u6d88\u8d39\u8005: read)  &lt;&#8212; [\u63a5\u6536\u7f13\u51b2\u533a] &lt;&#8212; (\u751f\u4ea7\u8005: \u7f51\u5361\/OS) <\/p>\n<p>\u5148\u770b\u53d1\u9001\u7aef\u3002<\/p>\n<p>\u5e94\u7528\u5c42\u662f\u751f\u4ea7\u8005&#xff1a;write\u4e00\u8c03&#xff0c;\u6570\u636e\u5c31\u88ab\u653e\u8fdb\u53d1\u9001\u7f13\u51b2\u533a\u3002\u5199\u7684\u4eba\u53ea\u7ba1\u5f80\u91cc\u585e&#xff0c;\u585e\u5b8c\u5c31\u8d70\u3002<\/p>\n<p>TCP\u534f\u8bae\u6808\u662f\u6d88\u8d39\u8005&#xff1a;\u5b83\u76ef\u7740\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u4ec0\u4e48\u65f6\u5019\u53d1\u3001\u53d1\u591a\u5c11&#xff0c;\u7531\u5b83\u81ea\u5df1\u8bf4\u4e86\u7b97\u3002\u7f13\u51b2\u533a\u91cc\u7684\u6570\u636e\u88ab\u53d6\u8d70\u3001\u6253\u5305\u3001\u9001\u4e0a\u7f51\u5361&#xff0c;\u751f\u4ea7\u51fa\u6765\u7684\u201c\u8d27\u201d\u624d\u7b97\u771f\u6b63\u51fa\u5e93\u3002<\/p>\n<p>\u518d\u770b\u63a5\u6536\u7aef&#xff0c;\u89d2\u8272\u6b63\u597d\u53cd\u8fc7\u6765\u3002<\/p>\n<p>\u7f51\u5361\u548c\u64cd\u4f5c\u7cfb\u7edf\u662f\u751f\u4ea7\u8005&#xff1a;\u7f51\u7edc\u6570\u636e\u5305\u4e00\u5230&#xff0c;\u7531\u7f51\u5361\u63a5\u6536\u3001\u64cd\u4f5c\u7cfb\u7edf\u63a5\u624b&#xff0c;\u5199\u8fdb\u63a5\u6536\u7f13\u51b2\u533a\u3002\u8d27\u5230\u4e86&#xff0c;\u5148\u5165\u5e93\u3002<\/p>\n<p>\u5e94\u7528\u5c42\u662f\u6d88\u8d39\u8005&#xff1a;read\u4e00\u8c03&#xff0c;\u5c31\u4ece\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u628a\u6570\u636e\u53d6\u8d70\u3002\u53d6\u591a\u5c11\u3001\u4ec0\u4e48\u65f6\u5019\u53d6&#xff0c;\u5e94\u7528\u5c42\u81ea\u5df1\u51b3\u5b9a\u3002<\/p>\n<p>\u4e00\u8fdb\u4e00\u51fa&#xff0c;\u4e00\u5b58\u4e00\u53d6\u3002\u53d1\u9001\u7aef\u662f\u201c\u5e94\u7528\u5c42\u751f\u4ea7\u3001TCP\u6d88\u8d39\u201d&#xff0c;\u63a5\u6536\u7aef\u662f\u201c\u5185\u6838\u751f\u4ea7\u3001\u5e94\u7528\u5c42\u6d88\u8d39\u201d\u3002\u4e24\u5957\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b&#xff0c;\u88ab\u7f51\u7edc\u8fd9\u6839\u7ebf\u4e32\u5728\u4e00\u8d77&#xff0c;\u5404\u81ea\u8f6c\u5404\u81ea\u7684\u5708&#xff0c;\u53c8\u5f7c\u6b64\u54ac\u5408\u3002<\/p>\n<p>\u7406\u89e3\u4e86\u8fd9\u5c42\u6a21\u578b&#xff0c;\u518d\u770bwrite\u4e3a\u4ec0\u4e48\u8fd4\u56de\u4e86\u6570\u636e\u5374\u6ca1\u5230\u5bf9\u7aef\u3001read\u4e3a\u4ec0\u4e48\u6ca1\u6570\u636e\u5c31\u4f1a\u963b\u585e&#xff0c;\u5c31\u90fd\u987a\u7406\u6210\u7ae0\u4e86\u3002\u7f13\u51b2\u533a\u5c31\u662f\u90a3\u4e2a\u67dc\u53f0&#xff0c;\u751f\u4ea7\u548c\u6d88\u8d39\u7684\u8282\u594f\u5bf9\u4e0d\u4e0a&#xff0c;\u67dc\u53f0\u5c31\u5f97\u627f\u62c5\u7f13\u51b2\u7684\u804c\u8d23&#xff0c;\u6ee1\u4e86\u8981\u7b49&#xff0c;\u7a7a\u4e86\u4e5f\u8981\u7b49\u3002<\/p>\n<h5 id=\"2.2%20%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%20Socket%20%E5%B0%81%E8%A3%85\">2.2 \u5c01\u88c5Socket&#xff1a;\u7edf\u4e00\u7f51\u7edc\u901a\u4fe1\u63a5\u53e3<\/h5>\n<p>\u628a\u5957\u63a5\u5b57\u7684\u5404\u79cd\u63a5\u53e3\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b&#xff0c;\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u5199\u8d77\u6765\u4f1a\u987a\u624b\u5f88\u591a\u3002<\/p>\n<h6 id=\"2.2.1%20%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E2%80%94%E2%80%94%E6%A8%A1%E6%9D%BF%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F\">2.2.1 \u8bbe\u8ba1\u6a21\u5f0f&#xff1a;\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f<\/h6>\n<p>\u57fa\u7c7b\u5b9a\u597d\u4e00\u5957\u6a21\u677f&#xff0c;\u540c\u65f6\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u7ec4\u5408\u8c03\u7528\u63a5\u53e3&#xff0c;\u5177\u4f53\u529f\u80fd\u4ea4\u7ed9\u5b50\u7c7b\u53bb\u5b9e\u73b0&#xff0c;\u8fd9\u5c31\u662f\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u3002<\/p>\n<p>class Socket<br \/>\n{<br \/>\npublic:<br \/>\n    virtual void CreateSocket() &#061; 0;<br \/>\n    virtual void BindSocket() &#061; 0;<br \/>\n    virtual void ListenSocket() &#061; 0;<br \/>\n    virtual void ConnectSocket() &#061; 0;<br \/>\n    virtual void AcceptSocket() &#061; 0;<br \/>\n    virtual void RecvMessage() &#061; 0;<br \/>\n    virtual void SendMessage() &#061; 0;<br \/>\n    virtual void GetSockFd() &#061; 0;<br \/>\n    virtual void Close() &#061; 0;<\/p>\n<p>public:<br \/>\n    void TcpClientSocketMethod()<br \/>\n    {<br \/>\n        CreateSocket();<br \/>\n    }<br \/>\n    void TcpServerSocketMethod()<br \/>\n    {<br \/>\n        CreateSocket();<br \/>\n        BindSocket();<br \/>\n        ListenSocket();<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _listensockfd;<br \/>\n}; <\/p>\n<p>\u5199\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u7684\u65f6\u5019&#xff0c;\u6709\u4e2a\u5c0f\u6280\u5de7&#xff1a;\u5148\u628a\u9700\u8981\u7684\u51fd\u6570\u7edf\u7edf\u8bbe\u6210void\u8fd4\u56de\u503c\u3001void\u53c2\u6570\u7684\u5f62\u5f0f&#xff0c;\u628a\u9aa8\u67b6\u642d\u51fa\u6765&#xff0c;\u7b49\u5b9e\u73b0\u5b8c\u4e86&#xff0c;\u518d\u56de\u5934\u6539\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002<\/p>\n<h5 id=\"2.2.2%20Socket.hpp%EF%BC%9A%E5%B0%81%E8%A3%85%E5%AE%9E%E7%8E%B0%E4%B8%8E%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90\">2.2.2 Socket.hpp&#xff1a;\u5c01\u88c5\u5b9e\u73b0\u4e0e\u6e90\u7801\u89e3\u6790<\/h5>\n<p>#ifndef __SOCKET__<br \/>\n#define __SOCKET__<\/p>\n<p>#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<\/p>\n<p>using namespace LogModule;<br \/>\nusing namespace InetAddrModule;<\/p>\n<p>namespace SocketModule<br \/>\n{<br \/>\n    const int DEFAULT_BACKLOG &#061; 258;<\/p>\n<p>    class Socket<br \/>\n    {<br \/>\n    public:<br \/>\n        virtual void CreateSocket() &#061; 0;<br \/>\n        virtual bool BindSocket(InetAddr &amp;Server) &#061; 0;<br \/>\n        virtual bool ListenSocket(int backlog &#061; DEFAULT_BACKLOG) &#061; 0;<br \/>\n        virtual bool ConnectSocket(InetAddr &amp;peer) &#061; 0;<br \/>\n        virtual std::shared_ptr&lt;Socket&gt; AcceptSocket(InetAddr &amp;client) &#061; 0;<br \/>\n        virtual int RecvMessage(std::string *Message) &#061; 0;<br \/>\n        virtual bool SendMessage(const std::string &amp;Message) &#061; 0;<br \/>\n        virtual int GetSockFd() &#061; 0;<br \/>\n        virtual void Close() &#061; 0;<\/p>\n<p>    public:<br \/>\n        void TcpClientSocketMethod()<br \/>\n        {<br \/>\n            CreateSocket();<br \/>\n        }<\/p>\n<p>        void TcpServerSocketMethod(InetAddr &amp;Server,<br \/>\n                                   int backlog &#061; DEFAULT_BACKLOG)<br \/>\n        {<br \/>\n            CreateSocket();<br \/>\n            BindSocket(Server);<br \/>\n            ListenSocket(backlog);<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _listensockfd;<br \/>\n    };<\/p>\n<p>    class TcpSocket : public Socket<br \/>\n    {<br \/>\n    public:<br \/>\n        TcpSocket(int listensockfd &#061; 0)<br \/>\n            : _listensockfd(listensockfd)<br \/>\n        {}<\/p>\n<p>        ~TcpSocket()<br \/>\n        {}<\/p>\n<p>        void CreateSocket()<br \/>\n        {<br \/>\n            _listensockfd &#061; ::socket(AF_INET, SOCK_STREAM, 0);<br \/>\n            if (_listensockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u5957\u63a5\u5b57\u521b\u5efa\u5931\u8d25\u3002&#034; &lt;&lt; CAGE;<br \/>\n                exit(SOCKET_ERR);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        bool BindSocket(InetAddr &amp;Server)<br \/>\n        {<br \/>\n            int n &#061; ::bind(_listensockfd, Server.GetSockaddrPtr(),<br \/>\n                           Server.GetSockLine());<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u7ed1\u5b9a\u5957\u63a5\u5b57\u5931\u8d25\u3002&#034; &lt;&lt; CAGE;<br \/>\n                exit(BIND_ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        bool ListenSocket(int backlog &#061; DEFAULT_BACKLOG)<br \/>\n        {<br \/>\n            int n &#061; ::listen(_listensockfd, backlog);<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u76d1\u542c\u5957\u63a5\u5b57\u5931\u8d25\u3002&#034; &lt;&lt; CAGE;<br \/>\n                exit(LISTEN_ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        bool ConnectSocket(InetAddr &amp;peer)<br \/>\n        {<br \/>\n            int n &#061; ::connect(_listensockfd, peer.GetSockaddrPtr(),<br \/>\n                              peer.GetSockLine());<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25&#xff01;&#034; &lt;&lt; CAGE;<br \/>\n                exit(CONNECT__ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        std::shared_ptr&lt;Socket&gt; AcceptSocket(InetAddr &amp;client)<br \/>\n        {<br \/>\n            sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<\/p>\n<p>            int _sockfd &#061; ::accept(_listensockfd, (sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (_sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u670d\u52a1\u5668\u83b7\u53d6\u8fde\u63a5\u5931\u8d25&#xff01;&#034; &lt;&lt; CAGE;<br \/>\n                exit(ACCEPT_ERR);<br \/>\n            }<\/p>\n<p>            std::shared_ptr&lt;Socket&gt; ptr &#061;<br \/>\n                std::make_shared&lt;TcpSocket&gt;(_sockfd);<br \/>\n            client.SetAddr(peer);<br \/>\n            return ptr;<br \/>\n        }<\/p>\n<p>        int RecvMessage(std::string *Message)<br \/>\n        {<br \/>\n            char buffer[4096];<br \/>\n            int n &#061; ::recv(GetSockFd(), buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n                *Message &#061; buffer;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                Message-&gt;clear();<br \/>\n            }<br \/>\n            return n;<br \/>\n        }<\/p>\n<p>        bool SendMessage(const std::string &amp;Message)<br \/>\n        {<br \/>\n            LOG(LEVEL::DEBUG) &lt;&lt; &#034;\u5982\u679c\u80fd\u770b\u5230\u8fd9\u53e5&#xff0c;\u8bf4\u660e\u8fdb\u5165\u4e86\u53d1\u9001\u8bf7\u6c42\u4e32&#034; &lt;&lt; CAGE;<\/p>\n<p>            int n &#061; ::send(GetSockFd(),<br \/>\n                           Message.c_str(), Message.size(), 0);<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::DEBUG) &lt;&lt; &#034;\u53d1\u9001\u8bf7\u6c42\u4e32\u5931\u8d25&#034; &lt;&lt; CAGE;<br \/>\n                return false;<br \/>\n            }<\/p>\n<p>            LOG(LEVEL::DEBUG) &lt;&lt; &#034;\u53d1\u9001\u8bf7\u6c42\u4e32\u6210\u529f&#034; &lt;&lt; CAGE;<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        int GetSockFd() { return _listensockfd; }<\/p>\n<p>        void Close() { ::close(_listensockfd); }<\/p>\n<p>    private:<br \/>\n        int _listensockfd;<br \/>\n    };<br \/>\n}<\/p>\n<p>#endif <\/p>\n<h5 id=\"2.3%20%E7%90%86%E8%A7%A3%20Jsoncpp%E2%80%94%E2%80%94%E4%B8%80%E7%A7%8D%E8%BD%BB%E9%87%8F%E7%BA%A7%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%B7%A5%E5%85%B7\">2.3 \u8ba4\u8bc6JsonCpp&#xff1a;\u8f7b\u91cf\u7ea7JSON\u5904\u7406\u5de5\u5177<\/h5>\n<p>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u5de5\u5177\u4e0d\u5c11\u3002protobuf\u662f\u8c37\u6b4c\u5f00\u6e90\u7684\u4e00\u5957\u65b9\u6848&#xff0c;\u5728\u5f88\u591a\u516c\u53f8\u91cc\u7528\u5f97\u76f8\u5f53\u5e7f\u6cdb&#xff0c;\u6b64\u65b9\u4ee5\u540e\u4e5f\u4f1a\u4e13\u95e8\u5b66\u4e60\u548c\u51fa\u535a\u6587\u3002XML\u662f\u8001\u724c\u65b9\u6848&#xff0c;\u5982\u4eca\u4f7f\u7528\u573a\u666f\u76f8\u5bf9\u5c11\u4e86\u4e00\u4e9b\u3002<\/p>\n<p>\u90a3\u4e3a\u4ec0\u4e48\u8fd9\u6b21\u9009jsoncpp&#xff1f;\u56e0\u4e3a\u5b83\u8d85\u7ea7\u8f7b\u91cf&#xff0c;\u5b66\u4e60\u6210\u672c\u4f4e\u5230\u51e0\u4e4e\u53ef\u4ee5\u5ffd\u7565\u3002<\/p>\n<h6 id=\"2.3.1%20Jsoncpp%20%E6%98%AF%E4%BB%80%E4%B9%88\">2.3.1 JsonCpp\u662f\u4ec0\u4e48<\/h6>\n<p>Jsoncpp\u662f\u4e00\u4e2a\u5904\u7406JSON\u6570\u636e\u7684C&#043;&#043;\u5e93\u3002\u5b83\u63d0\u4f9b\u4e24\u5927\u80fd\u529b&#xff1a;\u628aJSON\u6570\u636e\u5e8f\u5217\u5316\u6210\u5b57\u7b26\u4e32&#xff0c;\u4ee5\u53ca\u628a\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u6210C&#043;&#043;\u6570\u636e\u7ed3\u6784\u3002\u5f00\u6e90&#xff0c;\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u9700\u8981\u5904\u7406JSON\u7684C&#043;&#043;\u9879\u76ee\u3002<\/p>\n<h6 id=\"2.3.2%20Jsoncpp%20%E7%9A%84%E7%89%B9%E6%80%A7\">2.3.2 JsonCpp\u7684\u4e3b\u8981\u7279\u70b9<\/h6>\n<ul>\n<li>\n<p>\u7b80\u5355\u6613\u7528&#xff1a;API\u8bbe\u8ba1\u76f4\u89c2&#xff0c;\u5904\u7406JSON\u6570\u636e\u4e0d\u7ed5\u5f2f\u5b50\u3002<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u6027\u80fd&#xff1a;\u7ecf\u8fc7\u4f18\u5316&#xff0c;\u5927\u91cfJSON\u6570\u636e\u4e5f\u80fd\u9ad8\u6548\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u5168\u9762\u652f\u6301&#xff1a;JSON\u6807\u51c6\u91cc\u7684\u6240\u6709\u6570\u636e\u7c7b\u578b&#xff0c;\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001null&#xff0c;\u7edf\u7edf\u652f\u6301\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u5904\u7406&#xff1a;\u89e3\u6790JSON\u65f6&#xff0c;\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u4f4d\u7f6e&#xff0c;\u8c03\u8bd5\u8d77\u6765\u6709\u8ff9\u53ef\u5faa\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecdJsoncpp\u4e2d\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u64cd\u4f5c\u3002<\/p>\n<h5 id=\"2.3.3%20%E5%AE%89%E8%A3%85JsonCpp\">2.3.3 \u5b89\u88c5JsonCpp<\/h5>\n<p>Ubuntu&#xff1a;<\/p>\n<p>sudo apt-get install libjsoncpp-dev <\/p>\n<h6 id=\"CentOS%EF%BC%9A\">CentOS&#xff1a;<\/h6>\n<p>sudo yum install jsoncpp-devel <\/p>\n<h6 id=\"2.3.4%20Jsoncpp%20%E5%BA%8F%E5%88%97%E5%8C%96\">2.3.4 JSON\u5e8f\u5217\u5316&#xff1a;\u5c06\u7ed3\u6784\u5316\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c<\/h6>\n<p>\u5e8f\u5217\u5316&#xff0c;\u5c31\u662f\u628a\u6570\u636e\u7ed3\u6784\u6216\u5bf9\u8c61\u8f6c\u6362\u6210\u4e00\u79cd\u4fbf\u4e8e\u7f51\u7edc\u4f20\u8f93\u6216\u6587\u4ef6\u5b58\u50a8\u7684\u683c\u5f0f\u3002Jsoncpp \u63d0\u4f9b\u4e86\u591a\u79cd\u5e8f\u5217\u5316\u65b9\u5f0f\u3002<\/p>\n<h6 id=\"1.%20%E4%BD%BF%E7%94%A8Json%3A%3AValue%E7%9A%84toStyledString%E6%96%B9%E6%B3%95\">2.3.4.1 \u4f7f\u7528Json::Value\u7684toStyledString()\u65b9\u6cd5<\/h6>\n<p>\u628aJson::Value\u5bf9\u8c61\u76f4\u63a5\u8f6c\u6210\u5e26\u683c\u5f0f\u7684JSON\u5b57\u7b26\u4e32&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;jsoncpp\/json\/json.h&gt;<\/p>\n<p>int main() {<br \/>\n    Json::Value root;<br \/>\n    root[&#034;name&#034;] &#061; &#034;joe&#034;;<br \/>\n    root[&#034;sex&#034;] &#061; &#034;\u7537&#034;;<\/p>\n<p>    std::string s &#061; root.toStyledString();<br \/>\n    std::cout &lt;&lt; s &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<h6 id=\"2.%20%E4%BD%BF%E7%94%A8Json%3A%3AStreamWriterBuilder%EF%BC%88%E6%8E%A8%E8%8D%90%EF%BC%89\">2.3.4.2 \u4f7f\u7528Json::StreamWriterBuilder\u4e0ewriteString()&#xff08;\u63a8\u8350&#xff09;<\/h6>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u591a\u5b9a\u5236\u9009\u9879&#xff0c;\u6bd4\u5982\u7f29\u8fdb\u3001\u6362\u884c\u7b26\u7b49&#xff1a;<\/p>\n<p>#include &lt;string&gt;<br \/>\n#include &lt;sstream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;jsoncpp\/json\/json.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    Json::Value root;<br \/>\n    root[&#034;name&#034;] &#061; &#034;joe&#034;;<br \/>\n    root[&#034;sex&#034;] &#061; &#034;\u7537&#034;;<\/p>\n<p>    Json::StreamWriterBuilder builder;  \/\/ StreamWriter \u7684\u5de5\u5382<br \/>\n    std::unique_ptr&lt;Json::StreamWriter&gt; writer(builder.newStreamWriter());<\/p>\n<p>    std::stringstream ss;<br \/>\n    writer-&gt;write(root, &amp;ss);<br \/>\n    std::cout &lt;&lt; ss.str() &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>{<br \/>\n    &#034;name&#034; : &#034;joe&#034;,<br \/>\n    &#034;sex&#034; : &#034;\u7537&#034;<br \/>\n} <\/p>\n<h6 id=\"3.%20%E4%BD%BF%E7%94%A8%20Json%3A%3AFastWriter%20%E6%88%96%20Json%3A%3AStyledWriter\">2.3.4.3 FastWriter\u4e0eStyledWriter\u7b49\u65e7\u63a5\u53e3<\/h6>\n<p>FastWriter\u66f4\u5feb&#xff0c;\u4e0d\u989d\u5916\u6dfb\u52a0\u7a7a\u683c\u548c\u6362\u884c&#xff1b;StyledWriter\u5219\u8f93\u51fa\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;jsoncpp\/json\/json.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    Json::Value root;<br \/>\n    root[&#034;name&#034;] &#061; &#034;joe&#034;;<br \/>\n    root[&#034;sex&#034;] &#061; &#034;\u7537&#034;;<\/p>\n<p>    \/\/ \u4f7f\u7528 FastWriter<br \/>\n    Json::FastWriter writer;<br \/>\n    std::string s &#061; writer.write(root);<br \/>\n    std::cout &lt;&lt; s &lt;&lt; std::endl;<\/p>\n<p>    \/\/ \u4f7f\u7528 StyledWriter<br \/>\n    Json::StyledWriter styled;<br \/>\n    std::string s2 &#061; styled.write(root);<br \/>\n    std::cout &lt;&lt; s2 &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>FastWriter\u8f93\u51fa&#xff1a;<\/p>\n<p>{&#034;name&#034;:&#034;joe&#034;,&#034;sex&#034;:&#034;\u7537&#034;} <\/p>\n<p>StyledWriter\u8f93\u51fa&#xff1a;<\/p>\n<p>{<br \/>\n   &#034;name&#034; : &#034;joe&#034;,<br \/>\n   &#034;sex&#034; : &#034;\u7537&#034;<br \/>\n} <\/p>\n<h6 id=\"2.3.5%20%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\">2.3.5 JSON\u53cd\u5e8f\u5217\u5316&#xff1a;\u4ece\u6587\u672c\u8fd8\u539f\u7ed3\u6784\u5316\u6570\u636e<\/h6>\n<p>\u53cd\u5e8f\u5217\u5316&#xff0c;\u5c31\u662f\u628a\u5e8f\u5217\u5316\u4e4b\u540e\u7684\u5b57\u7b26\u4e32&#xff0c;\u91cd\u65b0\u8fd8\u539f\u6210\u539f\u6765\u7684\u6570\u636e\u7ed3\u6784\u6216\u5bf9\u8c61\u3002Jsoncpp\u63d0\u4f9b\u4e86\u4e0b\u9762\u8fd9\u4e9b\u65b9\u6cd5\u6765\u505a\u8fd9\u4ef6\u4e8b\u3002<\/p>\n<h6 id=\"1.%20%E4%BD%BF%E7%94%A8%20Json%3A%3AReader\">2.3.5.1 \u4f7f\u7528Json::Reader\u65e7\u63a5\u53e3<\/h6>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u51fa\u9519\u4f4d\u7f6e&#xff0c;\u8c03\u8bd5\u7684\u65f6\u5019\u7279\u522b\u987a\u624b\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;jsoncpp\/json\/json.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ JSON \u5b57\u7b26\u4e32<br \/>\n    std::string json_string &#061; &#034;{\\\\&#034;name\\\\&#034;:\\\\&#034;\u5f20\u4e09\\\\&#034;, \\\\&#034;age\\\\&#034;:30, \\\\&#034;city\\\\&#034;:\\\\&#034;\u5317\u4eac\\\\&#034;}&#034;;<\/p>\n<p>    \/\/ \u89e3\u6790 JSON \u5b57\u7b26\u4e32<br \/>\n    Json::Reader reader;<br \/>\n    Json::Value root;<\/p>\n<p>    \/\/ \u4ece\u5b57\u7b26\u4e32\u4e2d\u8bfb\u53d6 JSON \u6570\u636e<br \/>\n    bool parsingSuccessful &#061; reader.parse(json_string, root);<br \/>\n    if (!parsingSuccessful) {<br \/>\n        \/\/ \u89e3\u6790\u5931\u8d25&#xff0c;\u8f93\u51fa\u9519\u8bef\u4fe1\u606f<br \/>\n        std::cout &lt;&lt; &#034;Failed to parse JSON: &#034; &lt;&lt; reader.getFormattedErrorMessage() &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbf\u95ee JSON \u6570\u636e<br \/>\n    std::string name &#061; root[&#034;name&#034;].asString();<br \/>\n    int age &#061; root[&#034;age&#034;].asInt();<br \/>\n    std::string city &#061; root[&#034;city&#034;].asString();<\/p>\n<p>    \/\/ \u8f93\u51fa\u7ed3\u679c<br \/>\n    std::cout &lt;&lt; &#034;Name: &#034; &lt;&lt; name &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;Age: &#034; &lt;&lt; age &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;City: &#034; &lt;&lt; city &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u8f93\u51fa\u7ed3\u679c&#xff1a;<\/p>\n<p>Name: \u5f20\u4e09<br \/>\nAge: 30<br \/>\nCity: \u5317\u4eac <\/p>\n<h6 id=\"2.%20%E4%BD%BF%E7%94%A8%20Json%3A%3ACharReader%20%E7%9A%84%E6%B4%BE%E7%94%9F%E7%B1%BB%EF%BC%88%E4%B8%8D%E6%8E%A8%E8%8D%90%EF%BC%89\">2.3.5.2 \u4f7f\u7528Json::CharReaderBuilder\u521b\u5efa\u89e3\u6790\u5668<\/h6>\n<p>\u6709\u65f6\u5019&#xff0c;\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u89e3\u6790\u8fc7\u7a0b\u505a\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236&#xff0c;\u90a3\u5c31\u5f97\u76f4\u63a5\u4f7f\u7528Json::CharReader\u7684\u6d3e\u751f\u7c7b\u3002\u4f46\u5927\u591a\u6570\u60c5\u51b5\u4e0b&#xff0c;\u7528Json::parseFromStream\u6216\u8005Json::Reader\u7684parse\u65b9\u6cd5\u5c31\u8db3\u591f\u4e86&#xff0c;\u6ca1\u5fc5\u8981\u628a\u4e8b\u60c5\u641e\u590d\u6742\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u7406\u89e3\u4e0a\u7684\u5173\u952e\u70b9&#xff1a;Json::Value\u662f\u4ecb\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7ed3\u679c\u4e4b\u95f4\u7684\u90a3\u4e2a\u201c\u4ecb\u8d28\u201d\u3002 \u5e8f\u5217\u5316\u65f6&#xff0c;\u6570\u636e\u5148\u88c5\u8fdbJson::Value&#xff0c;\u518d\u8f6c\u6210\u5b57\u7b26\u4e32&#xff1b;\u53cd\u5e8f\u5217\u5316\u65f6&#xff0c;\u5b57\u7b26\u4e32\u5148\u89e3\u6790\u8fdbJson::Value&#xff0c;\u518d\u4ece\u91cc\u9762\u53d6\u503c\u3002\u4e00\u8fdb\u4e00\u51fa&#xff0c;\u5b83\u90fd\u662f\u90a3\u4e2a\u4e2d\u8f6c\u7ad9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"255\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054115-6ad6fefb52dd0.png\" width=\"741\" \/><\/p>\n<h6 id=\"2.3.6%20Json%3A%3AValue%E8%A1%A5%E5%85%85%EF%BC%9A%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C%E4%B8%8E%E6%95%B0%E6%8D%AE%E8%AE%BF%E9%97%AE%EF%BC%88%E9%80%89%E8%AF%BB%EF%BC%89\" style=\"background-color:transparent\">2.3.6 Json::Value\u8865\u5145&#xff1a;\u5e38\u7528\u64cd\u4f5c\u4e0e\u6570\u636e\u8bbf\u95ee&#xff08;\u9009\u8bfb&#xff09;<\/h6>\n<p>Json::Value&#xff08;Jsoncpp&#xff09;API \u6574\u7406<\/p>\n<p>Json::Value\u662fJsoncpp\u5e93\u91cc\u7684\u6838\u5fc3\u7c7b&#xff0c;JSON\u6570\u636e\u7684\u8868\u793a\u548c\u64cd\u4f5c&#xff0c;\u5168\u9760\u5b83\u6491\u7740\u3002<\/p>\n<h6 id=\"1.%20%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\">2.3.6.1 \u6784\u9020\u4e0e\u521d\u59cb\u5316<\/h6>\n<ul>\n<li>Json::Value()&#xff1a;\u9ed8\u8ba4\u6784\u9020&#xff0c;\u9020\u51fa\u4e00\u4e2a\u7a7a\u7684Json::Value\u5bf9\u8c61\u3002<\/li>\n<li>Json::Value(ValueType type, bool allocated &#061; false)&#xff1a;\u6309\u7ed9\u5b9a\u7684ValueType\u521b\u5efa\u5bf9\u8c61&#xff0c;\u6bd4\u5982nullValue\u3001intValue\u3001stringValue\u8fd9\u4e9b\u3002<\/li>\n<\/ul>\n<h6 id=\"2.3.6.2%20%E5%85%83%E7%B4%A0%E8%AE%BF%E9%97%AE\" style=\"background-color:transparent\">2.3.6.2 \u5143\u7d20\u8bbf\u95ee<\/h6>\n<ul>\n<li>Json::Value&amp; operator[](const char* key)&#xff1a;\u7528\u5b57\u7b26\u4e32\u952e\u8bbf\u95ee\u5bf9\u8c61\u91cc\u7684\u5143\u7d20\u3002\u952e\u4e0d\u5b58\u5728&#xff1f;\u90a3\u5c31\u5f53\u573a\u65b0\u5efa\u4e00\u4e2a\u3002<\/li>\n<li>Json::Value&amp; operator[](const std::string&amp; key)&#xff1a;\u540c\u4e0a&#xff0c;\u53ea\u4e0d\u8fc7\u952e\u7684\u7c7b\u578b\u6362\u6210\u4e86std::string\u3002<\/li>\n<li>Json::Value&amp; operator[](ArrayIndex index)&#xff1a;\u7528\u7d22\u5f15\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u7d22\u5f15\u8d8a\u754c&#xff1f;\u7167\u6837\u65b0\u5efa\u4e00\u4e2a\u3002<\/li>\n<li>Json::Value&amp; at(const char* key)&#xff1a;\u7528\u5b57\u7b26\u4e32\u952e\u8bbf\u95ee\u5bf9\u8c61\u5143\u7d20&#xff0c;\u952e\u4e0d\u5b58\u5728\u5c31\u629b\u5f02\u5e38&#xff0c;\u7edd\u4e0d\u6084\u6084\u65b0\u5efa\u3002<\/li>\n<li>Json::Value&amp; at(const std::string&amp; key)&#xff1a;\u540c\u4e0a&#xff0c;\u952e\u7684\u7c7b\u578b\u662fstd::string\u3002<\/li>\n<\/ul>\n<h6 id=\"3.%20%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%9F%A5\">2.3.6.3 \u7c7b\u578b\u68c0\u67e5<\/h6>\n<ul>\n<li>bool isNull()&#xff1a;\u662f\u4e0d\u662fnull\u3002<\/li>\n<li>bool isBool()&#xff1a;\u662f\u4e0d\u662f\u5e03\u5c14\u7c7b\u578b\u3002<\/li>\n<li>bool isInt()&#xff1a;\u662f\u4e0d\u662f\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>bool isInt64()&#xff1a;\u662f\u4e0d\u662f64\u4f4d\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>bool isUInt()&#xff1a;\u662f\u4e0d\u662f\u65e0\u7b26\u53f7\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>bool isUInt64()&#xff1a;\u662f\u4e0d\u662f64\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>bool isIntegral()&#xff1a;\u662f\u4e0d\u662f\u6574\u6570&#xff0c;\u6216\u8005\u80fd\u8f6c\u6210\u6574\u6570\u7684\u6d6e\u70b9\u6570\u3002<\/li>\n<li>bool isDouble()&#xff1a;\u662f\u4e0d\u662f\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002<\/li>\n<li>bool isNumeric()&#xff1a;\u662f\u4e0d\u662f\u6570\u5b57&#xff0c;\u6574\u6570\u548c\u6d6e\u70b9\u6570\u90fd\u7b97\u3002<\/li>\n<li>bool isString()&#xff1a;\u662f\u4e0d\u662f\u5b57\u7b26\u4e32\u3002<\/li>\n<li>bool isArray()&#xff1a;\u662f\u4e0d\u662f\u6570\u7ec4\u3002<\/li>\n<li>bool isObject()&#xff1a;\u662f\u4e0d\u662f\u5bf9\u8c61&#xff0c;\u4e5f\u5c31\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002<\/li>\n<\/ul>\n<h6 id=\"4.%20%E8%B5%8B%E5%80%BC%E5%92%8C%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2\">2.3.6.4 \u8d4b\u503c\u4e0e\u7c7b\u578b\u8f6c\u6362<\/h6>\n<ul>\n<li>Json::Value&amp; operator&#061;(bool value)&#xff1a;\u628a\u5e03\u5c14\u503c\u8d4b\u7ed9Json::Value\u5bf9\u8c61\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(int value)&#xff1a;\u8d4b\u6574\u6570\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(unsigned int value)&#xff1a;\u8d4b\u65e0\u7b26\u53f7\u6574\u6570\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(Int64 value)&#xff1a;\u8d4b64\u4f4d\u6574\u6570\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(UInt64 value)&#xff1a;\u8d4b64\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(double value)&#xff1a;\u8d4b\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(const char* value)&#xff1a;\u8d4bC\u5b57\u7b26\u4e32\u3002<\/li>\n<li>Json::Value&amp; operator&#061;(const std::string&amp; value)&#xff1a;\u8d4bstd::string\u3002<\/li>\n<li>bool asBool()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u6210\u5e03\u5c14\u7c7b\u578b\u3002<\/li>\n<li>int asInt()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u6210\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>Int64 asInt64()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u621064\u4f4d\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>unsigned int asUInt()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u6210\u65e0\u7b26\u53f7\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>UInt64 asUInt64()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u621064\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u7c7b\u578b\u3002<\/li>\n<li>double asDouble()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u6210\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u7c7b\u578b\u3002<\/li>\n<li>std::string asString()&#xff1a;\u5c3d\u53ef\u80fd\u8f6c\u6210\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h6 id=\"5.%20%E6%95%B0%E7%BB%84%E5%92%8C%E5%AF%B9%E8%B1%A1%E6%93%8D%E4%BD%9C\">2.3.6.5 JSON\u6570\u7ec4\u4e0e\u5bf9\u8c61\u64cd\u4f5c<\/h6>\n<ul>\n<li>size_t size()&#xff1a;\u8fd4\u56de\u6570\u7ec4\u6216\u5bf9\u8c61\u91cc\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n<li>bool empty()&#xff1a;\u5224\u65ad\u6570\u7ec4\u6216\u5bf9\u8c61\u662f\u4e0d\u662f\u7a7a\u7684\u3002<\/li>\n<li>void resize(ArrayIndex newSize)&#xff1a;\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u3002<\/li>\n<li>void clear()&#xff1a;\u6e05\u7a7a\u6570\u7ec4\u6216\u5bf9\u8c61\u91cc\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<li>void append(const Json::Value&amp; value)&#xff1a;\u5728\u6570\u7ec4\u672b\u5c3e\u8ffd\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u3002<\/li>\n<li>Json::Value&amp; operator[](const char* key, const Json::Value&amp; defaultValue &#061; Json::nullValue)&#xff1a;\u5728\u5bf9\u8c61\u91cc\u63d2\u5165\u6216\u8bbf\u95ee\u4e00\u4e2a\u5143\u7d20&#xff0c;\u952e\u4e0d\u5b58\u5728\u5c31\u7528\u9ed8\u8ba4\u503c\u3002<\/li>\n<li>Json::Value&amp; operator[](const std::string&amp; key, const Json::Value&amp; defaultValue &#061; Json::nullValue)&#xff1a;\u540c\u4e0a&#xff0c;\u952e\u7684\u7c7b\u578b\u662f std::string\u3002<\/li>\n<\/ul>\n<h4 id=\"%E4%B8%89%E3%80%81%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\">\u4e09\u3001\u6df1\u5165\u7406\u89e3\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316&#xff1a;\u5e94\u7528\u5c42\u534f\u8bae\u7684\u6838\u5fc3<\/h4>\n<p>\u5e94\u7528\u5c42&#xff0c;\u662f\u7f51\u7edc\u4e94\u5c42\u6a21\u578b\u7684\u6700\u9876\u5c42\u3002\u7f51\u7edc\u901a\u4fe1\u91cc&#xff0c;\u4e0a\u5c42\u5e94\u7528\u5904\u7406\u7684\u5f80\u5f80\u662f\u7ed3\u6784\u5316\u7684\u4e1a\u52a1\u6570\u636e&#xff0c;\u53ef\u5e95\u5c42\u7684\u4f20\u8f93\u5c42&#xff08;TCP\/UDP&#xff09;\u53ea\u8d1f\u8d23\u642c\u8fd0\u65e0\u7ed3\u6784\u7684\u5b57\u8282\u6d41\u3002\u60f3\u8ba9\u7ed3\u6784\u5316\u6570\u636e\u5728\u7f51\u7edc\u91cc\u9ad8\u6548\u3001\u51c6\u786e\u5730\u8dd1\u8d77\u6765&#xff0c;\u5c31\u5f97\u5b9a\u4e00\u5957\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u518d\u5f15\u5165\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u673a\u5236\u3002<\/p>\n<h5 id=\"3.1%20%E5%BA%94%E7%94%A8%E5%B1%82%E5%8D%8F%E8%AE%AE%E4%B8%8E%E7%BB%93%E6%9E%84%E5%8C%96%E6%95%B0%E6%8D%AE\">3.1 \u5e94\u7528\u5c42\u534f\u8bae\u4e0e\u7ed3\u6784\u5316\u6570\u636e<\/h5>\n<p>\u6240\u8c13\u534f\u8bae\u5b9a\u5236&#xff0c;\u672c\u8d28\u4e0a\u5c31\u662f\u901a\u4fe1\u53cc\u65b9\u7ea6\u5b9a\u597d\u4e00\u5957\u5f7c\u6b64\u90fd\u80fd\u8bc6\u522b\u7684\u7ed3\u6784\u5316\u6570\u636e\u683c\u5f0f\u3002\u4f60\u6309\u8fd9\u4e2a\u683c\u5f0f\u6253\u5305&#xff0c;\u6211\u6309\u8fd9\u4e2a\u683c\u5f0f\u62c6\u5305&#xff0c;\u4e2d\u95f4\u5c31\u4e0d\u4f1a\u9e21\u540c\u9e2d\u8bb2\u3002<\/p>\n<h6 id=\"3.1.1%20%E7%BB%93%E6%9E%84%E5%8C%96%E6%95%B0%E6%8D%AE%E4%B8%8E%E5%8D%8F%E8%AE%AE%E5%AE%9A%E4%B9%89\">3.1.1 \u7ed3\u6784\u5316\u6570\u636e\u4e0e\u534f\u8bae\u683c\u5f0f\u8bbe\u8ba1<\/h6>\n<p>\u5728C\/C&#043;&#043;\u7a0b\u5e8f\u91cc&#xff0c;\u4e1a\u52a1\u6570\u636e\u901a\u5e38\u4ee5\u7ed3\u6784\u4f53&#xff08;struct&#xff09;\u6216\u7c7b&#xff08;class&#xff09;\u7684\u5f62\u5f0f\u5b58\u5728\u3002\u6bd4\u5982\u4e00\u4e2a\u804a\u5929\u6d88\u606f\u7ed3\u6784\u4f53&#xff1a;<\/p>\n<p>struct message {<br \/>\n    std::string path;      \/\/ \u5934\u50cf \u2192 \u56fe\u7247\u8def\u5f84<br \/>\n    std::string nick_name; \/\/ \u6635\u79f0<br \/>\n    std::string msg;       \/\/ \u6d88\u606f\u5185\u5bb9<br \/>\n}; <\/p>\n<p>\u8fd9\u4e2astruct message&#xff0c;\u5c31\u662f\u901a\u4fe1\u53cc\u65b9\u5171\u540c\u8ba4\u53ef\u7684\u5e94\u7528\u5c42\u534f\u8bae\u3002\u5b57\u6bb5\u600e\u4e48\u6392\u3001\u6bcf\u4e2a\u5b57\u6bb5\u4ec0\u4e48\u7c7b\u578b&#xff0c;\u5927\u5bb6\u90fd\u6309\u8fd9\u4e2a\u6765&#xff0c;\u6570\u636e\u624d\u80fd\u5bf9\u4e0a\u53f7\u3002<\/p>\n<h6 id=\"3.1.2%20%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E5%8F%91%E9%80%81%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BB%93%E6%9E%84%E4%BD%93%EF%BC%9F\">3.1.2 \u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u53d1\u9001\u4e8c\u8fdb\u5236\u7ed3\u6784\u4f53<\/h6>\n<p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b&#xff0c;\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u5185\u90e8\u901a\u4fe1&#xff0c;\u786e\u5b9e\u53ef\u4ee5\u901a\u8fc7\u4e8c\u8fdb\u5236\u6307\u9488\u76f4\u63a5\u4f20\u9012\u7ed3\u6784\u4f53\u5bf9\u8c61\u3002\u53ef\u4e00\u65e6\u8de8\u7f51\u7edc\u3001\u8d70Client-Server\u901a\u4fe1&#xff0c;\u76f4\u63a5\u7529\u4e00\u4e2a\u4e8c\u8fdb\u5236\u7ed3\u6784\u4f53\u8fc7\u53bb&#xff0c;\u5c31\u975e\u5e38\u4e0d\u63a8\u8350\u4e86\u3002\u539f\u56e0\u6709\u4e09&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u5185\u5b58\u5bf9\u9f50\u5dee\u5f02\u3002 \u4e0d\u540c\u7684\u673a\u5668\u3001\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u3001\u4e0d\u540c\u7684\u5e73\u53f0&#xff0c;\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5bf9\u9f50\u89c4\u5219\u53ef\u80fd\u5404\u4e0d\u76f8\u540c\u3002\u540c\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u5728\u8fd9\u53f0\u673a\u5668\u4e0asizeof\u662f24&#xff0c;\u5230\u90a3\u53f0\u673a\u5668\u4e0a\u53ef\u80fd\u5c31\u53d8\u6210\u4e8632&#xff1b;\u5185\u90e8\u5b57\u6bb5\u7684\u504f\u79fb\u91cf&#xff0c;\u4e5f\u672a\u5fc5\u5bf9\u5f97\u4e0a\u3002\u4f60\u6309\u81ea\u5df1\u7684\u5bf9\u9f50\u65b9\u5f0f\u6253\u5305\u53d1\u51fa\u53bb&#xff0c;\u5bf9\u9762\u6309\u81ea\u5df1\u7684\u89c4\u5219\u62c6&#xff0c;\u62c6\u51fa\u6765\u7684\u4e1c\u897f\u5c31\u5168\u4e71\u5957\u4e86\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u8de8\u8bed\u8a00\u517c\u5bb9\u6027\u3002 \u7f51\u7edc\u901a\u4fe1\u7684\u4e24\u7aef&#xff0c;\u672a\u5fc5\u90fd\u7528C&#043;&#043;\u3002\u5bf9\u9762\u53ef\u80fd\u662fJava&#xff0c;\u53ef\u80fd\u662fPython\u3002\u4e0d\u540c\u8bed\u8a00\u5404\u6709\u5404\u7684\u5bf9\u8c61\u6a21\u578b\u548c\u5185\u5b58\u5e03\u5c40&#xff0c;\u4f60\u53d1\u4e00\u4e2aC&#043;&#043;\u7684\u4e8c\u8fdb\u5236\u7ed3\u6784\u4f53\u8fc7\u53bb&#xff0c;\u4eba\u5bb6\u538b\u6839\u6ca1\u6cd5\u76f4\u63a5\u89e3\u6790\u3002\u8bed\u8a00\u4e0d\u901a&#xff0c;\u5f97\u9760\u4e00\u79cd\u201c\u5171\u540c\u8bed\u8a00\u201d\u6765\u7ffb\u8bd1\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;\u5927\u5c0f\u7aef\u5b57\u8282\u5e8f\u95ee\u9898\u3002 \u4e0d\u540c\u67b6\u6784\u7684CPU&#xff0c;\u5728\u5185\u5b58\u91cc\u4fdd\u5b58\u6570\u636e\u7684\u5b57\u8282\u5e8f\u53ef\u80fd\u4e0d\u540c\u3002x86 \u662f\u5c0f\u7aef&#xff0c;\u90e8\u5206ARM\u67b6\u6784\u53ef\u80fd\u7528\u5927\u7aef\u3002\u540c\u4e00\u4e2a\u6574\u6570&#xff0c;\u4e00\u4e2a\u6309\u5c0f\u7aef\u5b58&#xff0c;\u4e00\u4e2a\u6309\u5927\u7aef\u8bfb&#xff0c;\u6570\u503c\u76f4\u63a5\u7ffb\u4e2a\u5e95\u671d\u5929\u3002<\/p>\n<p>\u8de8\u7f51\u7edc\u4f20\u7ed3\u6784\u5316\u6570\u636e&#xff0c;\u4e0d\u80fd\u628a\u5185\u5b58\u91cc\u7684\u4e8c\u8fdb\u5236\u539f\u6837\u642c\u8fc7\u53bb\u3002\u5f97\u5148\u628a\u5b83\u201c\u7ffb\u8bd1\u201d\u6210\u4e00\u79cd\u53cc\u65b9\u90fd\u8ba4\u5f97\u7684\u683c\u5f0f&#xff0c;\u8fd9\u5c31\u662f\u5e8f\u5217\u5316\u8981\u5e72\u7684\u4e8b\u3002<\/p>\n<h5 id=\"3.2%20%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%9A%84%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5\" style=\"background-color:transparent\">3.2 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u7684\u57fa\u672c\u539f\u7406<\/h5>\n<p>\u4e8c\u8fdb\u5236\u76f4\u63a5\u4f20\u8f93\u8fd9\u6761\u8def\u8d70\u4e0d\u901a&#xff0c;\u7f51\u7edc\u901a\u4fe1\u5c31\u5728\u5e94\u7528\u5c42\u8865\u4e0a\u4e86\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u8fd9\u5957\u673a\u5236\u3002<\/p>\n<h6 id=\"3.2.1%20%E6%A6%82%E5%BF%B5%E5%AE%9A%E4%B9%89\">3.2.1 \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u7684\u6982\u5ff5<\/h6>\n<p>\u5e8f\u5217\u5316&#xff08;Serialization&#xff09;&#xff1a;\u628a\u7ed3\u6784\u5316\u6570\u636e&#xff08;\u6bd4\u5982struct message&#xff09;\u8f6c\u6362\u6210\u4e00\u6574\u6761\u5e26\u5206\u9694\u7b26\u6216\u7279\u6b8a\u7f16\u7801\u683c\u5f0f\u7684\u5b57\u8282\u6d41\/\u957f\u5b57\u7b26\u4e32&#xff0c;\u201c\u591a\u53d8\u4e00\u201d&#xff0c;\u65b9\u4fbf\u5728\u7f51\u7edc\u91cc\u4f20\u8f93\u3002<\/p>\n<p>\u53cd\u5e8f\u5217\u5316&#xff08;Deserialization&#xff09;&#xff1a;\u628a\u63a5\u6536\u5230\u7684\u5b57\u8282\u6d41\/\u957f\u5b57\u7b26\u4e32\u89e3\u6790\u8fd8\u539f\u6210\u7ed3\u6784\u5316\u6570\u636e&#xff0c;\u201c\u4e00\u53d8\u591a\u201d&#xff0c;\u65b9\u4fbf\u4e0a\u5c42\u4e1a\u52a1\u4ee3\u7801\u5904\u7406\u3002<\/p>\n<h6 id=\"3.2.2%20%E6%A0%B8%E5%BF%83%E4%BB%B7%E5%80%BC%E4%B8%8E%E4%BC%98%E5%8A%BF\">3.2.2 \u5e8f\u5217\u5316\u7684\u4f5c\u7528\u4e0e\u4f18\u52bf<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"736\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054115-6ad6fefb651f7.png\" width=\"2137\" \/><\/p>\n<p>[\u7ed3\u6784\u5316\u6570\u636e struct] &#8212;&gt; (\u5e8f\u5217\u5316) &#8212;&gt; [\u7f51\u7edc\u5b57\u8282\u6d41\/\u5b57\u7b26\u4e32] &#8212;&gt; (\u7f51\u7edc\u4f20\u8f93) &#8212;&gt; [\u7f51\u7edc\u5b57\u8282\u6d41\/\u5b57\u7b26\u4e32] &#8212;&gt; (\u53cd\u5e8f\u5217\u5316) &#8212;&gt; [\u7ed3\u6784\u5316\u6570\u636e struct] <\/p>\n<p>\u4fdd\u8bc1\u62a5\u6587\u5b8c\u6574\u6027\u3002 \u5e8f\u5217\u5316\u7684\u65f6\u5019&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u5206\u9694\u7b26&#xff08;\u7a7a\u683c\u3001\\\\r\\\\n\u6216\u8005\u7279\u5b9a\u5b57\u7b26&#xff09;&#xff0c;\u6216\u8005\u6253\u5305\u4e00\u4e2a\u957f\u5ea6\u5934\u90e8&#xff0c;\u786e\u4fdd\u63a5\u6536\u65b9\u80fd\u591f\u7cbe\u51c6\u62c6\u5206\u8fb9\u754c\u3002TCP\u7684\u7c98\u5305\u4e0e\u534a\u5305\u95ee\u9898&#xff0c;\u5c31\u5728\u8fd9\u4e00\u5c42\u88ab\u5316\u89e3\u3002<\/p>\n<p>\u65b9\u4fbf\u4e0a\u5c42\u4e1a\u52a1\u5904\u7406\u3002 \u53cd\u5e8f\u5217\u5316\u628a\u6d41\u6570\u636e\u8fd8\u539f\u6210\u5bf9\u8c61\u7ed3\u6784&#xff0c;\u4e0a\u5c42\u903b\u8f91\u76f4\u63a5\u8bfb\u7ed3\u6784\u4f53\u6210\u5458\u5c31\u884c&#xff0c;\u5e95\u5c42\u7684\u89e3\u6790\u7ec6\u8282&#xff0c;\u4e00\u6982\u4e0d\u7528\u64cd\u5fc3\u3002<\/p>\n<p>\u5e94\u7528\u5c42\u4e0e\u4f20\u8f93\u5c42\u89e3\u8026\u3002 \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316&#xff0c;\u628a\u5e94\u7528\u5c42\u548c\u4f20\u8f93\u5c42\u6709\u6548\u5730\u9694\u5f00\u4e86\u3002\u4e0a\u5c42\u534f\u8bae\u8981\u6539\u3001\u8981\u6269\u5c55&#xff0c;\u53ea\u8981\u540c\u6b65\u66f4\u65b0\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u89c4\u5219&#xff0c;\u5e95\u5c42\u7684\u5b57\u8282\u6d41\u4f20\u8f93\u673a\u5236\u5b8c\u5168\u4e0d\u53d7\u5f71\u54cd\u3002\u5404\u6539\u5404\u7684&#xff0c;\u4e92\u4e0d\u7275\u8fde\u3002<\/p>\n<h5 id=\"3.3%20%E5%BA%94%E7%94%A8%E5%B1%82%E9%80%9A%E4%BF%A1%E7%9A%84%E6%A0%87%E5%87%86%E5%8C%96%E5%BB%BA%E8%AE%AE\">3.3 \u5e94\u7528\u5c42\u901a\u4fe1\u7684\u6807\u51c6\u5316\u5efa\u8bae<\/h5>\n<p>\u73b0\u4ee3\u7f51\u7edc\u7f16\u7a0b\u91cc&#xff0c;\u9664\u975e\u73af\u5883\u6781\u5ea6\u7279\u6b8a\u4e14\u5b8c\u5168\u53d7\u63a7&#xff0c;\u6bd4\u5982\u786c\u4ef6\u540c\u6784\u3001\u8bed\u8a00\u7edf\u4e00&#xff0c;\u5426\u5219\u5728\u5e94\u7528\u5c42&#xff0c;\u5c31\u8be5\u8001\u8001\u5b9e\u5b9e\u5b88\u4f4f\u4e0b\u9762\u8fd9\u6761\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f3a\u63a8\u65b9\u6848&#xff1a;\u5e94\u7528\u5c42\u901a\u4fe1&#xff0c;\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528\u6807\u51c6\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u65b9\u6848&#xff0c;\u6bd4\u5982JSON\u3001Protobuf\u3001XML&#xff0c;\u6216\u8005\u81ea\u5b9a\u4e49\u7684\u5206\u9694\u7b26\u534f\u8bae\u3002<\/p>\n<\/li>\n<li>\n<p>\u89c4\u907f\u65b9\u6848&#xff1a;\u9664\u975e\u7279\u5b9a\u573a\u666f&#xff0c;\u7edd\u5bf9\u4e0d\u8981\u76f4\u63a5\u5f80\u7f51\u7edc\u91cc\u5199\u88f8\u7ed3\u6784\u4f53\u7684\u4e8c\u8fdb\u5236\u5185\u5b58\u5757\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e00\u53e5\u8bdd&#xff1a;\u8be5\u7ffb\u8bd1\u5c31\u7ffb\u8bd1&#xff0c;\u522b\u628a\u5185\u5b58\u91cc\u7684\u4e8c\u8fdb\u5236\u539f\u6837\u7529\u4e0a\u7f51\u3002<\/p>\n<h4 id=\"%E5%9B%9B%E3%80%81%E9%99%84%E4%B8%8A%E6%BA%90%E7%A0%81\">\u56db\u3001\u5b8c\u6574\u6e90\u7801\u4e0e\u8fd0\u884c\u6d4b\u8bd5<\/h4>\n<p>\u4e0b\u9762\u628a\u57fa\u4e8e\u81ea\u5b9a\u4e49\u5e94\u7528\u5c42\u534f\u8bae\u7684\u7f51\u7edc\u8ba1\u7b97\u5668\u5b8c\u6574\u4ee3\u7801\u6574\u7406\u51fa\u6765&#xff0c;\u5305\u62ec\u516c\u5171\u5934\u6587\u4ef6\u3001\u5730\u5740\u5c01\u88c5\u3001Socket\u5c01\u88c5\u3001\u534f\u8bae\u6a21\u5757&#xff08;\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\/\u7ec4\u5e27&#xff09;\u3001\u670d\u52a1\u7aef\u3001\u5ba2\u6237\u7aef\u548cMakefile\u3002<\/p>\n<h5 id=\"4.1%20Common.hpp%EF%BC%88%E5%85%AC%E5%85%B1%E5%A4%B4%E6%96%87%E4%BB%B6%EF%BC%89\">4.1 Common.hpp&#xff08;\u516c\u5171\u5934\u6587\u4ef6&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>\/\/ \u9519\u8bef\u7801\u5b9a\u4e49<br \/>\n#define SOCKET_ERR  1<br \/>\n#define BIND_ERR    2<br \/>\n#define LISTEN_ERR  3<br \/>\n#define CONNECT_ERR 4<br \/>\n#define ACCEPT_ERR  5 <\/p>\n<h5 id=\"4.2%20Log.hpp%EF%BC%88%E6%97%A5%E5%BF%97%EF%BC%89\">4.2 Log.hpp&#xff08;\u65e5\u5fd7&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>namespace LogModule<br \/>\n{<br \/>\n    const std::string CAGE &#061; &#034;\\\\n&#034;;<\/p>\n<p>    enum class LEVEL<br \/>\n    {<br \/>\n        DEBUG,<br \/>\n        INFO,<br \/>\n        WARNING,<br \/>\n        ERROR,<br \/>\n        FATAL<br \/>\n    };<\/p>\n<p>    inline void Log(LEVEL level, const std::string &amp;msg)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;[&#034; &lt;&lt; static_cast&lt;int&gt;(level) &lt;&lt; &#034;]&#034; &lt;&lt; msg &lt;&lt; CAGE;<br \/>\n    }<br \/>\n}<\/p>\n<p>#define LOG(level) LogModule::Log(level, std::string(__FILE__) &#043; &#034;:&#034; &#043; std::to_string(__LINE__) &#043; &#034; &#034;) <\/p>\n<h5 id=\"4.3%20InetAddr.hpp%EF%BC%88IP%20%E5%9C%B0%E5%9D%80%E5%B0%81%E8%A3%85%EF%BC%89\">4.3 InetAddr.hpp&#xff08;IP \u5730\u5740\u5c01\u88c5&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<\/p>\n<p>namespace InetAddrModule<br \/>\n{<br \/>\n    class InetAddr<br \/>\n    {<br \/>\n    public:<br \/>\n        InetAddr(const struct sockaddr_in &amp;addr)<br \/>\n            : _addr(addr)<br \/>\n        {<br \/>\n            _port &#061; ntohs(_addr.sin_port);<br \/>\n            char ipbuffer[64];<br \/>\n            inet_ntop(AF_INET, &amp;_addr.sin_addr, ipbuffer, sizeof(ipbuffer));<br \/>\n            _ip &#061; ipbuffer;<br \/>\n        }<\/p>\n<p>        InetAddr(const std::string &amp;ip, uint16_t port)<br \/>\n            : _ip(ip), _port(port)<br \/>\n        {<br \/>\n            memset(&amp;_addr, 0, sizeof(_addr));<br \/>\n            _addr.sin_family &#061; AF_INET;<br \/>\n            _addr.sin_port &#061; htons(_port);<br \/>\n            inet_pton(AF_INET, _ip.c_str(), &amp;_addr.sin_addr);<br \/>\n        }<\/p>\n<p>        void SetAddr(const struct sockaddr_in &amp;addr)<br \/>\n        {<br \/>\n            _addr &#061; addr;<br \/>\n            _port &#061; ntohs(_addr.sin_port);<br \/>\n            char ipbuffer[64];<br \/>\n            inet_ntop(AF_INET, &amp;_addr.sin_addr, ipbuffer, sizeof(ipbuffer));<br \/>\n            _ip &#061; ipbuffer;<br \/>\n        }<\/p>\n<p>        std::string Ip() const { return _ip; }<br \/>\n        uint16_t Port() const { return _port; }<br \/>\n        struct sockaddr_in *GetSockaddrPtr() { return &amp;_addr; }<br \/>\n        socklen_t GetSockLine() const { return sizeof(_addr); }<\/p>\n<p>    private:<br \/>\n        struct sockaddr_in _addr;<br \/>\n        std::string _ip;<br \/>\n        uint16_t _port;<br \/>\n    };<br \/>\n} <\/p>\n<h5 id=\"4.4%20Socket.hpp%EF%BC%88Socket%20%E5%B0%81%E8%A3%85%EF%BC%89\">4.4 Socket.hpp&#xff08;Socket \u5c01\u88c5&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;unistd.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;<\/p>\n<p>#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n#include &#034;InetAddr.hpp&#034;<\/p>\n<p>using namespace LogModule;<br \/>\nusing namespace InetAddrModule;<\/p>\n<p>namespace SocketModule<br \/>\n{<br \/>\n    const int DEFAULT_BACKLOG &#061; 258;<\/p>\n<p>    class Socket<br \/>\n    {<br \/>\n    public:<br \/>\n        virtual void CreateSocket() &#061; 0;<br \/>\n        virtual bool BindSocket(InetAddr &amp;Server) &#061; 0;<br \/>\n        virtual bool ListenSocket(int backlog &#061; DEFAULT_BACKLOG) &#061; 0;<br \/>\n        virtual bool ConnectSocket(InetAddr &amp;peer) &#061; 0;<br \/>\n        virtual std::shared_ptr&lt;Socket&gt; AcceptSocket(InetAddr &amp;client) &#061; 0;<br \/>\n        virtual int Recv(void *buf, size_t len) &#061; 0;<br \/>\n        virtual int Send(const void *buf, size_t len) &#061; 0;<br \/>\n        virtual int GetSockFd() &#061; 0;<br \/>\n        virtual void Close() &#061; 0;<\/p>\n<p>    public:<br \/>\n        void TcpClientSocketMethod()<br \/>\n        {<br \/>\n            CreateSocket();<br \/>\n        }<\/p>\n<p>        void TcpServerSocketMethod(InetAddr &amp;Server,<br \/>\n                                   int backlog &#061; DEFAULT_BACKLOG)<br \/>\n        {<br \/>\n            CreateSocket();<br \/>\n            BindSocket(Server);<br \/>\n            ListenSocket(backlog);<br \/>\n        }<br \/>\n    };<\/p>\n<p>    class TcpSocket : public Socket<br \/>\n    {<br \/>\n    public:<br \/>\n        TcpSocket(int sockfd &#061; -1)<br \/>\n            : _sockfd(sockfd)<br \/>\n        {}<\/p>\n<p>        ~TcpSocket() {}<\/p>\n<p>        void CreateSocket() override<br \/>\n        {<br \/>\n            _sockfd &#061; ::socket(AF_INET, SOCK_STREAM, 0);<br \/>\n            if (_sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u5957\u63a5\u5b57\u521b\u5efa\u5931\u8d25\u3002&#034;;<br \/>\n                exit(SOCKET_ERR);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        bool BindSocket(InetAddr &amp;Server) override<br \/>\n        {<br \/>\n            int n &#061; ::bind(_sockfd, (struct sockaddr *)Server.GetSockaddrPtr(),<br \/>\n                           Server.GetSockLine());<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u7ed1\u5b9a\u5957\u63a5\u5b57\u5931\u8d25\u3002&#034;;<br \/>\n                exit(BIND_ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        bool ListenSocket(int backlog &#061; DEFAULT_BACKLOG) override<br \/>\n        {<br \/>\n            int n &#061; ::listen(_sockfd, backlog);<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u81f4\u547d&#xff01;\u76d1\u542c\u5957\u63a5\u5b57\u5931\u8d25\u3002&#034;;<br \/>\n                exit(LISTEN_ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        bool ConnectSocket(InetAddr &amp;peer) override<br \/>\n        {<br \/>\n            int n &#061; ::connect(_sockfd, (struct sockaddr *)peer.GetSockaddrPtr(),<br \/>\n                              peer.GetSockLine());<br \/>\n            if (n &#061;&#061; -1)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25&#xff01;&#034;;<br \/>\n                exit(CONNECT_ERR);<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<\/p>\n<p>        std::shared_ptr&lt;Socket&gt; AcceptSocket(InetAddr &amp;client) override<br \/>\n        {<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<br \/>\n            int sockfd &#061; ::accept(_sockfd, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(LEVEL::FATAL) &lt;&lt; &#034;\u670d\u52a1\u5668\u83b7\u53d6\u8fde\u63a5\u5931\u8d25&#xff01;&#034;;<br \/>\n                exit(ACCEPT_ERR);<br \/>\n            }<br \/>\n            client.SetAddr(peer);<br \/>\n            return std::make_shared&lt;TcpSocket&gt;(sockfd);<br \/>\n        }<\/p>\n<p>        int Recv(void *buf, size_t len) override<br \/>\n        {<br \/>\n            return ::recv(_sockfd, buf, len, 0);<br \/>\n        }<\/p>\n<p>        int Send(const void *buf, size_t len) override<br \/>\n        {<br \/>\n            return ::send(_sockfd, buf, len, 0);<br \/>\n        }<\/p>\n<p>        int GetSockFd() override { return _sockfd; }<\/p>\n<p>        void Close() override<br \/>\n        {<br \/>\n            if (_sockfd &gt;&#061; 0)<br \/>\n            {<br \/>\n                ::close(_sockfd);<br \/>\n                _sockfd &#061; -1;<br \/>\n            }<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _sockfd;<br \/>\n    };<br \/>\n} <\/p>\n<h5 id=\"4.5%20Protocol.hpp%EF%BC%88%E5%8D%8F%E8%AE%AE%E6%A8%A1%E5%9D%97%EF%BC%9A%E5%BA%8F%E5%88%97%E5%8C%96%E3%80%81%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E3%80%81%E7%BB%84%E5%B8%A7%EF%BC%89\">4.5 Protocol.hpp&#xff08;\u534f\u8bae\u6a21\u5757&#xff1a;\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316\u3001\u7ec4\u5e27&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;string&gt;<br \/>\n#include &lt;sstream&gt;<br \/>\n#include &lt;jsoncpp\/json\/json.h&gt;<\/p>\n<p>namespace ProtocolModule<br \/>\n{<br \/>\n    \/\/ &#8212;&#8212;&#8212;- \u8bf7\u6c42\u7ed3\u6784\u4f53 &#8212;&#8212;&#8212;-<br \/>\n    struct Request<br \/>\n    {<br \/>\n        int x;<br \/>\n        int y;<br \/>\n        char op;  \/\/ &#043; &#8211; * \/ %<br \/>\n    };<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u54cd\u5e94\u7ed3\u6784\u4f53 &#8212;&#8212;&#8212;-<br \/>\n    struct Response<br \/>\n    {<br \/>\n        int result;<br \/>\n        int code;       \/\/ 0 \u6210\u529f&#xff0c;\u975e 0 \u5931\u8d25<br \/>\n        std::string msg;<br \/>\n    };<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u5e8f\u5217\u5316&#xff1a;Request \u2192 JSON \u5b57\u7b26\u4e32 &#8212;&#8212;&#8212;-<br \/>\n    inline std::string SerializeRequest(const Request &amp;req)<br \/>\n    {<br \/>\n        Json::Value root;<br \/>\n        root[&#034;x&#034;] &#061; req.x;<br \/>\n        root[&#034;y&#034;] &#061; req.y;<br \/>\n        root[&#034;op&#034;] &#061; std::string(1, req.op);<\/p>\n<p>        Json::StreamWriterBuilder builder;<br \/>\n        builder[&#034;indentation&#034;] &#061; &#034;&#034;;<br \/>\n        return Json::writeString(builder, root);<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u53cd\u5e8f\u5217\u5316&#xff1a;JSON \u5b57\u7b26\u4e32 \u2192 Request &#8212;&#8212;&#8212;-<br \/>\n    inline bool DeserializeRequest(const std::string &amp;json, Request &amp;req)<br \/>\n    {<br \/>\n        Json::CharReaderBuilder builder;<br \/>\n        Json::CharReaderBuilder::strictMode(&amp;builder.settings_);<br \/>\n        std::unique_ptr&lt;Json::CharReader&gt; reader(builder.newCharReader());<\/p>\n<p>        Json::Value root;<br \/>\n        std::string errs;<br \/>\n        if (!reader-&gt;parse(json.c_str(), json.c_str() &#043; json.size(), &amp;root, &amp;errs))<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        if (!root.isMember(&#034;x&#034;) || !root.isMember(&#034;y&#034;) || !root.isMember(&#034;op&#034;))<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        req.x &#061; root[&#034;x&#034;].asInt();<br \/>\n        req.y &#061; root[&#034;y&#034;].asInt();<br \/>\n        std::string opStr &#061; root[&#034;op&#034;].asString();<br \/>\n        if (opStr.size() !&#061; 1)<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<br \/>\n        req.op &#061; opStr[0];<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u5e8f\u5217\u5316&#xff1a;Response \u2192 JSON \u5b57\u7b26\u4e32 &#8212;&#8212;&#8212;-<br \/>\n    inline std::string SerializeResponse(const Response &amp;resp)<br \/>\n    {<br \/>\n        Json::Value root;<br \/>\n        root[&#034;result&#034;] &#061; resp.result;<br \/>\n        root[&#034;code&#034;] &#061; resp.code;<br \/>\n        root[&#034;msg&#034;] &#061; resp.msg;<\/p>\n<p>        Json::StreamWriterBuilder builder;<br \/>\n        builder[&#034;indentation&#034;] &#061; &#034;&#034;;<br \/>\n        return Json::writeString(builder, root);<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u53cd\u5e8f\u5217\u5316&#xff1a;JSON \u5b57\u7b26\u4e32 \u2192 Response &#8212;&#8212;&#8212;-<br \/>\n    inline bool DeserializeResponse(const std::string &amp;json, Response &amp;resp)<br \/>\n    {<br \/>\n        Json::CharReaderBuilder builder;<br \/>\n        Json::CharReaderBuilder::strictMode(&amp;builder.settings_);<br \/>\n        std::unique_ptr&lt;Json::CharReader&gt; reader(builder.newCharReader());<\/p>\n<p>        Json::Value root;<br \/>\n        std::string errs;<br \/>\n        if (!reader-&gt;parse(json.c_str(), json.c_str() &#043; json.size(), &amp;root, &amp;errs))<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        if (!root.isMember(&#034;result&#034;) || !root.isMember(&#034;code&#034;) || !root.isMember(&#034;msg&#034;))<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        resp.result &#061; root[&#034;result&#034;].asInt();<br \/>\n        resp.code &#061; root[&#034;code&#034;].asInt();<br \/>\n        resp.msg &#061; root[&#034;msg&#034;].asString();<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u7ec4\u5e27&#xff1a;\u5185\u5bb9 \u2192 &#034;len\\\\r\\\\ncontent&#034; &#8212;&#8212;&#8212;-<br \/>\n    inline std::string Encode(const std::string &amp;content)<br \/>\n    {<br \/>\n        return std::to_string(content.size()) &#043; &#034;\\\\r\\\\n&#034; &#043; content;<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;- \u89e3\u5e27&#xff1a;\u4ece\u7f13\u51b2\u533a\u63d0\u53d6\u4e00\u6761\u5b8c\u6574\u62a5\u6587 &#8212;&#8212;&#8212;-<br \/>\n    \/\/ \u8fd4\u56de\u503c&#xff1a;&gt; 0 \u6210\u529f\u89e3\u7801\u4e00\u6761\u62a5\u6587&#xff1b;0 \u9700\u8981\u66f4\u591a\u6570\u636e&#xff1b;-1 \u534f\u8bae\u9519\u8bef<br \/>\n    inline int Decode(std::string &amp;buffer, std::string &amp;content)<br \/>\n    {<br \/>\n        \/\/ 1. \u627e\u957f\u5ea6\u5934\u90e8\u7684\u7ed3\u675f\u7b26 \\\\r\\\\n<br \/>\n        size_t pos &#061; buffer.find(&#034;\\\\r\\\\n&#034;);<br \/>\n        if (pos &#061;&#061; std::string::npos)<br \/>\n        {<br \/>\n            \/\/ \u957f\u5ea6\u5934\u90e8\u8fd8\u6ca1\u6536\u5168&#xff0c;\u4f46\u4e5f\u4e0d\u80fd\u65e0\u9650\u957f<br \/>\n            if (buffer.size() &gt; 20)<br \/>\n            {<br \/>\n                return -1;<br \/>\n            }<br \/>\n            return 0;<br \/>\n        }<\/p>\n<p>        \/\/ 2. \u89e3\u6790\u957f\u5ea6<br \/>\n        std::string lenStr &#061; buffer.substr(0, pos);<br \/>\n        if (lenStr.empty() || lenStr.size() &gt; 10)<br \/>\n        {<br \/>\n            return -1;<br \/>\n        }<\/p>\n<p>        int len &#061; 0;<br \/>\n        for (char c : lenStr)<br \/>\n        {<br \/>\n            if (c &lt; &#039;0&#039; || c &gt; &#039;9&#039;)<br \/>\n            {<br \/>\n                return -1;<br \/>\n            }<br \/>\n            len &#061; len * 10 &#043; (c &#8211; &#039;0&#039;);<br \/>\n        }<\/p>\n<p>        \/\/ 3. \u5224\u65ad\u5185\u5bb9\u662f\u5426\u6536\u5168<br \/>\n        size_t totalLen &#061; pos &#043; 2 &#043; len;<br \/>\n        if (buffer.size() &lt; totalLen)<br \/>\n        {<br \/>\n            return 0;<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u63d0\u53d6\u5185\u5bb9&#xff0c;\u5e76\u4ece\u7f13\u51b2\u533a\u4e2d\u79fb\u9664<br \/>\n        content &#061; buffer.substr(pos &#043; 2, len);<br \/>\n        buffer.erase(0, totalLen);<br \/>\n        return 1;<br \/>\n    }<br \/>\n} <\/p>\n<h5 id=\"4.6%20TcpServer.hpp%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%B1%BB%EF%BC%89\">4.6 TcpServer.hpp&#xff08;\u670d\u52a1\u7aef\u7c7b&#xff09;<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;signal.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>#include &#034;Socket.hpp&#034;<br \/>\n#include &#034;Protocol.hpp&#034;<\/p>\n<p>using namespace SocketModule;<br \/>\nusing namespace ProtocolModule;<\/p>\n<p>namespace ServerModule<br \/>\n{<br \/>\n    class TcpServer<br \/>\n    {<br \/>\n    public:<br \/>\n        TcpServer(uint16_t port &#061; 8080)<br \/>\n            : _port(port)<br \/>\n        {<br \/>\n        }<\/p>\n<p>        void Init()<br \/>\n        {<br \/>\n            InetAddr server(&#034;0.0.0.0&#034;, _port);<br \/>\n            _listensock &#061; std::make_shared&lt;TcpSocket&gt;();<br \/>\n            _listensock-&gt;TcpServerSocketMethod(server);<br \/>\n        }<\/p>\n<p>        void Start()<br \/>\n        {<br \/>\n            \/\/ \u5ffd\u7565 SIGCHLD&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u7531\u7cfb\u7edf\u81ea\u52a8\u56de\u6536<br \/>\n            signal(SIGCHLD, SIG_IGN);<\/p>\n<p>            while (true)<br \/>\n            {<br \/>\n                InetAddr client;<br \/>\n                auto conn &#061; _listensock-&gt;AcceptSocket(client);<br \/>\n                LOG(LEVEL::INFO) &lt;&lt; &#034;\u5ba2\u6237\u7aef\u8fde\u63a5&#xff1a;&#034; &lt;&lt; client.Ip() &lt;&lt; &#034;:&#034; &lt;&lt; client.Port();<\/p>\n<p>                \/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u8be5\u5ba2\u6237\u7aef<br \/>\n                pid_t id &#061; fork();<br \/>\n                if (id &#061;&#061; 0)<br \/>\n                {<br \/>\n                    _listensock-&gt;Close();<br \/>\n                    Handler(conn);<br \/>\n                    conn-&gt;Close();<br \/>\n                    exit(0);<br \/>\n                }<\/p>\n<p>                \/\/ \u7236\u8fdb\u7a0b\u5173\u95ed\u670d\u52a1\u5957\u63a5\u5b57&#xff0c;\u7ee7\u7eed accept<br \/>\n                conn-&gt;Close();<br \/>\n            }<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        void Handler(std::shared_ptr&lt;Socket&gt; conn)<br \/>\n        {<br \/>\n            std::string buffer;<br \/>\n            char tmp[4096];<\/p>\n<p>            while (true)<br \/>\n            {<br \/>\n                int n &#061; conn-&gt;Recv(tmp, sizeof(tmp));<br \/>\n                if (n &lt;&#061; 0)<br \/>\n                {<br \/>\n                    break;<br \/>\n                }<\/p>\n<p>                buffer.append(tmp, n);<\/p>\n<p>                \/\/ \u5faa\u73af\u89e3\u5e27&#xff0c;\u5904\u7406\u6bcf\u4e00\u6761\u5b8c\u6574\u62a5\u6587<br \/>\n                std::string content;<br \/>\n                while (true)<br \/>\n                {<br \/>\n                    int ret &#061; Decode(buffer, content);<br \/>\n                    if (ret &#061;&#061; 0)<br \/>\n                    {<br \/>\n                        break;  \/\/ \u6570\u636e\u8fd8\u4e0d\u591f&#xff0c;\u7ee7\u7eed\u6536<br \/>\n                    }<br \/>\n                    if (ret &lt; 0)<br \/>\n                    {<br \/>\n                        LOG(LEVEL::ERROR) &lt;&lt; &#034;\u534f\u8bae\u89e3\u6790\u9519\u8bef&#xff0c;\u5173\u95ed\u8fde\u63a5&#034;;<br \/>\n                        return;<br \/>\n                    }<\/p>\n<p>                    \/\/ \u53cd\u5e8f\u5217\u5316\u8bf7\u6c42<br \/>\n                    Request req;<br \/>\n                    if (!DeserializeRequest(content, req))<br \/>\n                    {<br \/>\n                        LOG(LEVEL::ERROR) &lt;&lt; &#034;\u8bf7\u6c42\u53cd\u5e8f\u5217\u5316\u5931\u8d25&#034;;<br \/>\n                        Response badResp{0, 3, &#034;bad request&#034;};<br \/>\n                        std::string frame &#061; Encode(SerializeResponse(badResp));<br \/>\n                        conn-&gt;Send(frame.c_str(), frame.size());<br \/>\n                        continue;<br \/>\n                    }<\/p>\n<p>                    \/\/ \u8ba1\u7b97<br \/>\n                    Response resp &#061; Calculate(req);<br \/>\n                    LOG(LEVEL::INFO) &lt;&lt; &#034;\u8ba1\u7b97&#xff1a;&#034; &lt;&lt; req.x &lt;&lt; &#034; &#034; &lt;&lt; req.op &lt;&lt; &#034; &#034;<br \/>\n                                     &lt;&lt; req.y &lt;&lt; &#034; &#061; &#034; &lt;&lt; resp.result;<\/p>\n<p>                    \/\/ \u5e8f\u5217\u5316\u5e76\u7ec4\u5e27\u53d1\u9001<br \/>\n                    std::string frame &#061; Encode(SerializeResponse(resp));<br \/>\n                    conn-&gt;Send(frame.c_str(), frame.size());<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        Response Calculate(const Request &amp;req)<br \/>\n        {<br \/>\n            Response resp;<br \/>\n            resp.code &#061; 0;<br \/>\n            resp.msg &#061; &#034;success&#034;;<br \/>\n            resp.result &#061; 0;<\/p>\n<p>            switch (req.op)<br \/>\n            {<br \/>\n            case &#039;&#043;&#039;:<br \/>\n                resp.result &#061; req.x &#043; req.y;<br \/>\n                break;<br \/>\n            case &#039;-&#039;:<br \/>\n                resp.result &#061; req.x &#8211; req.y;<br \/>\n                break;<br \/>\n            case &#039;*&#039;:<br \/>\n                resp.result &#061; req.x * req.y;<br \/>\n                break;<br \/>\n            case &#039;\/&#039;:<br \/>\n                if (req.y &#061;&#061; 0)<br \/>\n                {<br \/>\n                    resp.code &#061; 1;<br \/>\n                    resp.msg &#061; &#034;divide by zero&#034;;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    resp.result &#061; req.x \/ req.y;<br \/>\n                }<br \/>\n                break;<br \/>\n            case &#039;%&#039;:<br \/>\n                if (req.y &#061;&#061; 0)<br \/>\n                {<br \/>\n                    resp.code &#061; 1;<br \/>\n                    resp.msg &#061; &#034;modulo by zero&#034;;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    resp.result &#061; req.x % req.y;<br \/>\n                }<br \/>\n                break;<br \/>\n            default:<br \/>\n                resp.code &#061; 2;<br \/>\n                resp.msg &#061; &#034;unknown operator&#034;;<br \/>\n                break;<br \/>\n            }<\/p>\n<p>            return resp;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        std::shared_ptr&lt;Socket&gt; _listensock;<br \/>\n        uint16_t _port;<br \/>\n    };<br \/>\n} <\/p>\n<h5 id=\"4.7%20TcpServer.cc%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89\">4.7 TcpServer.cc&#xff08;\u670d\u52a1\u7aef\u4e3b\u7a0b\u5e8f&#xff09;<\/h5>\n<p>#include &#034;TcpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    uint16_t port &#061; 8080;<br \/>\n    if (argc &#061;&#061; 2)<br \/>\n    {<br \/>\n        port &#061; static_cast&lt;uint16_t&gt;(std::stoi(argv[1]));<br \/>\n    }<\/p>\n<p>    std::unique_ptr&lt;ServerModule::TcpServer&gt; server &#061;<br \/>\n        std::make_unique&lt;ServerModule::TcpServer&gt;(port);<\/p>\n<p>    server-&gt;Init();<br \/>\n    server-&gt;Start();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h5 id=\"4.8%20TcpClient.cc%EF%BC%88%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%89\">4.8 TcpClient.cc&#xff08;\u5ba2\u6237\u7aef\u4e3b\u7a0b\u5e8f&#xff09;<\/h5>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;sstream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>#include &#034;Socket.hpp&#034;<br \/>\n#include &#034;Protocol.hpp&#034;<\/p>\n<p>using namespace SocketModule;<br \/>\nusing namespace ProtocolModule;<\/p>\n<p>int 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; static_cast&lt;uint16_t&gt;(std::stoi(argv[2]));<\/p>\n<p>    \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57\u5e76\u8fde\u63a5\u670d\u52a1\u5668<br \/>\n    std::shared_ptr&lt;Socket&gt; client &#061; std::make_shared&lt;TcpSocket&gt;();<br \/>\n    client-&gt;TcpClientSocketMethod();<\/p>\n<p>    InetAddr server(server_ip, server_port);<br \/>\n    client-&gt;ConnectSocket(server);<br \/>\n    std::cout &lt;&lt; &#034;\u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668 &#034; &lt;&lt; server_ip &lt;&lt; &#034;:&#034; &lt;&lt; server_port &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 2. \u5faa\u73af\u53d1\u9001\u8bf7\u6c42 &#043; \u63a5\u6536\u54cd\u5e94<br \/>\n    std::string line;<br \/>\n    while (true)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;\u8bf7\u8f93\u5165\u8868\u8fbe\u5f0f&#xff08;\u683c\u5f0f&#xff1a;x op y&#xff0c;\u4f8b\u5982 10 &#043; 20&#xff0c;\u8f93\u5165 quit \u9000\u51fa&#xff09;&#xff1a;&#034;;<br \/>\n        std::getline(std::cin, line);<\/p>\n<p>        if (line &#061;&#061; &#034;quit&#034; || line &#061;&#061; &#034;exit&#034;)<br \/>\n        {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u89e3\u6790\u8f93\u5165<br \/>\n        std::istringstream iss(line);<br \/>\n        Request req;<br \/>\n        if (!(iss &gt;&gt; req.x &gt;&gt; req.op &gt;&gt; req.y))<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;\u683c\u5f0f\u9519\u8bef&#xff0c;\u8bf7\u6309 \\\\&#034;x op y\\\\&#034; \u8f93\u5165&#034; &lt;&lt; std::endl;<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u5e8f\u5217\u5316 &#043; \u7ec4\u5e27 &#043; \u53d1\u9001<br \/>\n        std::string json &#061; SerializeRequest(req);<br \/>\n        std::string frame &#061; Encode(json);<br \/>\n        client-&gt;Send(frame.c_str(), frame.size());<\/p>\n<p>        \/\/ \u63a5\u6536\u54cd\u5e94<br \/>\n        std::string buffer;<br \/>\n        char tmp[4096];<br \/>\n        std::string content;<br \/>\n        bool gotResponse &#061; false;<\/p>\n<p>        while (!gotResponse)<br \/>\n        {<br \/>\n            int n &#061; client-&gt;Recv(tmp, sizeof(tmp));<br \/>\n            if (n &lt;&#061; 0)<br \/>\n            {<br \/>\n                std::cout &lt;&lt; &#034;\u670d\u52a1\u5668\u5df2\u65ad\u5f00\u8fde\u63a5&#034; &lt;&lt; std::endl;<br \/>\n                client-&gt;Close();<br \/>\n                return 0;<br \/>\n            }<\/p>\n<p>            buffer.append(tmp, n);<\/p>\n<p>            int ret &#061; Decode(buffer, content);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                std::cout &lt;&lt; &#034;\u534f\u8bae\u89e3\u6790\u9519\u8bef&#034; &lt;&lt; std::endl;<br \/>\n                client-&gt;Close();<br \/>\n                return 1;<br \/>\n            }<br \/>\n            if (ret &gt; 0)<br \/>\n            {<br \/>\n                gotResponse &#061; true;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u53cd\u5e8f\u5217\u5316\u54cd\u5e94<br \/>\n        Response resp;<br \/>\n        if (!DeserializeResponse(content, resp))<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;\u54cd\u5e94\u53cd\u5e8f\u5217\u5316\u5931\u8d25&#034; &lt;&lt; std::endl;<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        if (resp.code &#061;&#061; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;\u8ba1\u7b97\u7ed3\u679c&#xff1a;&#034; &lt;&lt; req.x &lt;&lt; &#034; &#034; &lt;&lt; req.op &lt;&lt; &#034; &#034;<br \/>\n                      &lt;&lt; req.y &lt;&lt; &#034; &#061; &#034; &lt;&lt; resp.result &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;\u8ba1\u7b97\u5931\u8d25&#xff1a;&#034; &lt;&lt; resp.msg &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    client-&gt;Close();<br \/>\n    return 0;<br \/>\n} <\/p>\n<h5 id=\"4.9%20Makefile\">4.9 Makefile<\/h5>\n<p>.PHONY: all<br \/>\nall: tcp_server tcp_client<\/p>\n<p>tcp_server: TcpServer.cc TcpServer.hpp Socket.hpp InetAddr.hpp Protocol.hpp Common.hpp Log.hpp<br \/>\ng&#043;&#043; -o $&#064; $&lt; -std&#061;c&#043;&#043;17 -ljsoncpp<\/p>\n<p>tcp_client: TcpClient.cc Socket.hpp InetAddr.hpp Protocol.hpp Common.hpp Log.hpp<br \/>\ng&#043;&#043; -o $&#064; $&lt; -std&#061;c&#043;&#043;17 -ljsoncpp<\/p>\n<p>.PHONY: clean<br \/>\nclean:<br \/>\nrm -f tcp_server tcp_client <\/p>\n<h5 id=\"4.10%20%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F\">4.10 \u8fd0\u884c\u65b9\u5f0f<\/h5>\n<p>\u7f16\u8bd1&#xff1a;<\/p>\n<p>make<\/p>\n<p>\u5148\u542f\u52a8\u670d\u52a1\u7aef&#xff08;\u9ed8\u8ba4\u7aef\u53e38080&#xff09;&#xff1a;<\/p>\n<p>.\/tcp_server<\/p>\n<p>\u518d\u542f\u52a8\u5ba2\u6237\u7aef&#xff1a;<\/p>\n<p>.\/tcp_client 127.0.0.1 8080<\/p>\n<h5 id=\"4.11%20%E5%8D%8F%E8%AE%AE%E8%AE%BE%E8%AE%A1%E8%AF%B4%E6%98%8E\">4.11 \u534f\u8bae\u8bbe\u8ba1\u8bf4\u660e<\/h5>\n<p>\u62a5\u6587\u683c\u5f0f&#xff1a;<\/p>\n<p>len\\\\r\\\\ncontent <\/p>\n<ul>\n<li>len&#xff1a;\u5185\u5bb9\u7684\u5b57\u8282\u957f\u5ea6&#xff08;\u5341\u8fdb\u5236\u6570\u5b57\u5b57\u7b26\u4e32&#xff09;<\/li>\n<li>content&#xff1a;JSON\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<p>\u8bf7\u6c42JSON&#xff1a;<\/p>\n<p>{&#034;x&#034;: 10, &#034;y&#034;: 20, &#034;op&#034;: &#034;&#043;&#034;}<\/p>\n<p>\u54cd\u5e94JSON&#xff1a;<\/p>\n<p>{&#034;result&#034;: 30, &#034;code&#034;: 0, &#034;msg&#034;: &#034;success&#034;}<\/p>\n<p>\u4e3a\u4ec0\u4e48\u7528\u957f\u5ea6\u5934&#xff1f;<\/p>\n<p>TCP\u662f\u5b57\u8282\u6d41&#xff0c;\u6ca1\u6709\u5929\u7136\u7684\u6d88\u606f\u8fb9\u754c\u3002\u957f\u5ea6\u5934\u660e\u786e\u544a\u8bc9\u63a5\u6536\u65b9\u201c\u540e\u9762\u8fd8\u6709\u591a\u5c11\u5b57\u8282\u201d&#xff0c;\u63a5\u6536\u65b9\u5c31\u80fd\u7cbe\u786e\u5730\u5207\u51fa\u6bcf\u4e00\u6761\u5b8c\u6574\u62a5\u6587&#xff0c;\u7c98\u5305\u548c\u534a\u5305\u95ee\u9898\u4e00\u5e76\u89e3\u51b3\u3002<\/p>\n<p>\u89e3\u5e27\u903b\u8f91&#xff1a;<\/p>\n<p>Decode\u51fd\u6570\u5148\u4ece\u7f13\u51b2\u533a\u91cc\u627e\\\\r\\\\n&#xff0c;\u62ff\u5230\u957f\u5ea6&#xff1b;\u518d\u5224\u65ad\u7f13\u51b2\u533a\u91cc\u5269\u4f59\u7684\u6570\u636e\u591f\u4e0d\u591f\u8fd9\u4e2a\u957f\u5ea6\u3002\u591f\u4e86&#xff0c;\u5c31\u5207\u51fa\u4e00\u6761\u5b8c\u6574\u62a5\u6587&#xff1b;\u4e0d\u591f&#xff0c;\u5c31\u8fd4\u56de0&#xff0c;\u7b49\u4e0b\u6b21recv\u7ee7\u7eed\u6536\u3002\u4e00\u6761\u4e00\u6761\u5904\u7406&#xff0c;\u8fb9\u754c\u6e05\u6670\u3002<\/p>\n<p>\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u7684\u4f4d\u7f6e&#xff1a;<\/p>\n<p>Protocol.hpp\u91cc\u7684SerializeRequest\/DeserializeRequest\u548cSerializeResponse\/DeserializeResponse&#xff0c;\u8d1f\u8d23JSON\u548c\u7ed3\u6784\u4f53\u4e4b\u95f4\u7684\u4e92\u8f6c\u3002Encode\/Decode\u8d1f\u8d23\u7ec4\u5e27\u548c\u89e3\u5e27\u3002\u4e24\u5c42\u804c\u8d23\u5206\u5f00&#xff0c;\u6539\u5e8f\u5217\u5316\u65b9\u6848\u4e0d\u5f71\u54cd\u7ec4\u5e27\u903b\u8f91&#xff0c;\u6539\u7ec4\u5e27\u683c\u5f0f\u4e5f\u4e0d\u5f71\u54cd\u5e8f\u5217\u5316\u3002<\/p>\n<hr \/>\n<p>\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u522b\u5fd8\u4e86\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u5173\u6ce8\u4e09\u8fde\u652f\u6301\u3002\u4f60\u7684\u6bcf\u4e00\u6b21\u53cd\u9988&#xff0c;\u90fd\u662f\u6211\u7ee7\u7eed\u786c\u6838\u8f93\u51fa\u7684\u6700\u5927\u52a8\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e24\u7bc7\u6211\u4eec\u8bb2\u4e86TCP\u4e0eUDP\u5957\u63a5\u5b57\u7684\u7f16\u7a0b\u57fa\u7840&#xff0c;\u4e5f\u5199\u4e86\u56db\u5957\u7b80\u5355\u7684Demo\u3002\u4ece\u672c\u6587\u5f00\u59cb&#xff0c;\u6211\u4eec\u6b63\u5f0f\u52a8\u624b\u8bbe\u8ba1\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u201c\u5e94\u7528\u5c42\u534f\u8bae\u201d&#xff0c;\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e00\u4e2a\u201c\u7f51\u7edc\u8ba1\u7b97\u5668\u201d\u3002\u597d&#xff0c;\u5e9f\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u76f4\u63a5\u5f00\u59cb\u3002<br \/>\n\u76ee\u5f55<br \/>\n\u4e00\u3001\u4ece\u9700\u6c42\u51fa\u53d1&#xff1a;\u8bbe\u8ba1\u4e00\u4e2a\u7f51\u7edc\u7248\u8ba1\u7b97\u5668<br \/>\n1.1 \u81ea\u4e3b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u9700\u8981\u89e3\u51b3\u54ea\u4e9b\u95ee\u9898<br \/>\n1.2 \u518d\u8c08\u201c\u534f\u8bae\u201d&#xff1a;\u7f51\u7edc\u901a\u4fe1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ea6\u5b9a<br \/>\n1.3 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1<\/p>\n","protected":false},"author":2,"featured_media":115864,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,970,205,78,191],"topic":[],"class_list":["post-115869","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-tcp","tag-tcp-ip","tag-78","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/115869.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e0a\u4e24\u7bc7\u6211\u4eec\u8bb2\u4e86TCP\u4e0eUDP\u5957\u63a5\u5b57\u7684\u7f16\u7a0b\u57fa\u7840&#xff0c;\u4e5f\u5199\u4e86\u56db\u5957\u7b80\u5355\u7684Demo\u3002\u4ece\u672c\u6587\u5f00\u59cb&#xff0c;\u6211\u4eec\u6b63\u5f0f\u52a8\u624b\u8bbe\u8ba1\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u201c\u5e94\u7528\u5c42\u534f\u8bae\u201d&#xff0c;\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e00\u4e2a\u201c\u7f51\u7edc\u8ba1\u7b97\u5668\u201d\u3002\u597d&#xff0c;\u5e9f\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u76f4\u63a5\u5f00\u59cb\u3002 \u76ee\u5f55 \u4e00\u3001\u4ece\u9700\u6c42\u51fa\u53d1&#xff1a;\u8bbe\u8ba1\u4e00\u4e2a\u7f51\u7edc\u7248\u8ba1\u7b97\u5668 1.1 \u81ea\u4e3b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u9700\u8981\u89e3\u51b3\u54ea\u4e9b\u95ee\u9898 1.2 \u518d\u8c08\u201c\u534f\u8bae\u201d&#xff1a;\u7f51\u7edc\u901a\u4fe1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ea6\u5b9a 1.3 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/115869.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-20T05:41:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054113-6ad6fef97cbd5.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=\"20 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115869.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/115869.html\",\"name\":\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-20T05:41:17+00:00\",\"dateModified\":\"2026-10-20T05:41:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/115869.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/115869.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115869.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/115869.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e0a\u4e24\u7bc7\u6211\u4eec\u8bb2\u4e86TCP\u4e0eUDP\u5957\u63a5\u5b57\u7684\u7f16\u7a0b\u57fa\u7840&#xff0c;\u4e5f\u5199\u4e86\u56db\u5957\u7b80\u5355\u7684Demo\u3002\u4ece\u672c\u6587\u5f00\u59cb&#xff0c;\u6211\u4eec\u6b63\u5f0f\u52a8\u624b\u8bbe\u8ba1\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u201c\u5e94\u7528\u5c42\u534f\u8bae\u201d&#xff0c;\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e00\u4e2a\u201c\u7f51\u7edc\u8ba1\u7b97\u5668\u201d\u3002\u597d&#xff0c;\u5e9f\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u76f4\u63a5\u5f00\u59cb\u3002 \u76ee\u5f55 \u4e00\u3001\u4ece\u9700\u6c42\u51fa\u53d1&#xff1a;\u8bbe\u8ba1\u4e00\u4e2a\u7f51\u7edc\u7248\u8ba1\u7b97\u5668 1.1 \u81ea\u4e3b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u9700\u8981\u89e3\u51b3\u54ea\u4e9b\u95ee\u9898 1.2 \u518d\u8c08\u201c\u534f\u8bae\u201d&#xff1a;\u7f51\u7edc\u901a\u4fe1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ea6\u5b9a 1.3 \u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1","og_url":"https:\/\/www.wsisp.com\/helps\/115869.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-20T05:41:17+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020054113-6ad6fef97cbd5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"20 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/115869.html","url":"https:\/\/www.wsisp.com\/helps\/115869.html","name":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-20T05:41:17+00:00","dateModified":"2026-10-20T05:41:17+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/115869.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/115869.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/115869.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u5165\u95e8\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\uff08\u4e03\uff09\uff1a\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1\u5b9e\u6218\u2014\u2014\u57fa\u4e8eTCP\u4e0eJsonCpp\u5b9e\u73b0\u7f51\u7edc\u8ba1\u7b97\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115869","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=115869"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115869\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/115864"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=115869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=115869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=115869"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=115869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}