{"id":85667,"date":"2026-07-27T20:25:06","date_gmt":"2026-07-27T12:25:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85667.html"},"modified":"2026-07-27T20:25:06","modified_gmt":"2026-07-27T12:25:06","slug":"%e6%89%8b%e5%86%99%e4%b8%80%e4%b8%aac-tcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%ae%9a%e4%b9%89%e5%8d%8f%e8%ae%ae%e9%a1%ba%e4%be%bf%e8%a7%a3%e5%86%b3%e7%b2%98%e5%8c%85%e9%97%ae","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85667.html","title":{"rendered":"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898)"},"content":{"rendered":"<p>\u5728\u4e4b\u524d\u7684\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86\u5173\u4e8eUDP\u548cTCP\u7684\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u76f4\u89c2\u7684\u611f\u53d7\u4e86\u4e00\u4e0b\u7f51\u7edc\u5957\u63a5\u5b57\u662f\u5982\u4f55\u4f7f\u7528\u7684&#xff0c;\u5e76\u4e14\u6210\u529f\u7684\u5b8c\u6210\u4e86\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5c0f\u7ec6\u8282\u6211\u4eec\u53ef\u80fd\u4f1a\u5ffd\u7565&#xff0c;\u5c31\u662fUDP\u662f\u57fa\u4e8e\u6570\u636e\u62a5\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u4e00\u4e0b\u5b50\u5c31\u5c06\u6240\u6709\u6211\u4eec\u8981\u53d1\u9001\u7684\u4fe1\u606f\u4f20\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u4f46\u662f\u6211\u4eec\u7684TCP\u53ef\u662f\u57fa\u4e8e\u5b57\u8282\u6d41\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u6211\u4eec\u5982\u4f55\u4fdd\u8bc1\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e&#xff0c;\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u5462&#xff1f;<\/p>\n<p>\u6211\u4eec\u5728\u8fdb\u884cTCP\u7f51\u7edc\u901a\u4fe1\u7684\u65f6\u5019&#xff0c;\u901a\u8fc7\u8c03\u7528connec\u51fd\u6570\u8c03\u7528&#xff0c;\u4f7f\u5ba2\u6237\u7aef\u53ef\u4ee5\u548c\u670d\u52a1\u7aef\u4fdd\u6301\u94fe\u63a5\u4e4b\u540e&#xff0c;\u5ba2\u6237\u7aef\u5c06\u81ea\u5df1\u60f3\u8981\u53d1\u9001\u7684\u6570\u636e\u901a\u8fc7write\u7cfb\u7edf\u8c03\u7528\u5199\u8fdb\u5bf9\u5e94\u7684socket\u51fd\u6570\u8c03\u7528\u7ed9\u6211\u4eec\u8fd4\u56de\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6240\u5bf9\u5e94\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u6211\u4eec\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u7684\u65f6\u5019&#xff0c;\u76f4\u63a5\u5c06\u5176\u653e\u5165\u5373\u53ef&#xff0c;\u4f46\u662f\u60f3\u8981\u5f80\u51fa\u62ff\u7684\u65f6\u5019\u5c31\u6709\u70b9\u56f0\u96be\u4e86&#xff0c;\u60f3\u8981\u5f80\u51fa\u62ff\u7684\u4eba\u5982\u679c\u4e0d\u77e5\u9053\u653e\u7684\u4eba\u662f\u5982\u4f55\u653e\u7684&#xff0c;\u5c31\u4f1a\u9020\u6210\u4e00\u7cfb\u5217\u7684\u9519\u8bef&#xff0c;\u8fd9\u5c31\u597d\u6bd4\u653e\u6570\u636e\u65f6\u5148\u653e\u4e86\u4e00\u4e2a\u6574\u5f62&#xff0c;\u53c8\u653e\u4e86\u4e00\u4e2a\u6d6e\u70b9\u6570&#xff0c;\u8fd8\u653e\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u7136\u800c\u62ff\u7684\u4eba\u6309\u7167\u5b57\u7b26\u4e32&#xff0c;\u6574\u5f62&#xff0c;\u6d6e\u70b9\u6570\u8fd9\u6837\u7684\u65b9\u5f0f\u8fdb\u884c\u83b7\u53d6&#xff0c;\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u73b0\u8c61&#xff0c;\u6240\u4ee5\u4e00\u65e6\u6211\u4eec\u8981\u53d1\u9001\u4e00\u4e9b\u5e26\u6709\u7ed3\u6784\u5316\u7684\u6570\u636e\u65f6&#xff0c;\u5c31\u5fc5\u987b\u518d\u6b21\u5236\u5b9a\u2014\u2014\u534f\u8bae&#xff0c;\u8fd9\u6837\u624d\u80fd\u6ee1\u8db3\u6211\u4eec\u60f3\u8981\u8fd4\u9001\u4e00\u4e9b\u7ed3\u6784\u5316\u6570\u636e\u7684\u9700\u6c42\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"419\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727122504-6a674e202361c.png\" width=\"1743\" \/><\/p>\n<p>TCP\u662f\u4f20\u8f93\u63a7\u5236\u534f\u8bae&#xff0c;\u5b83\u4e3b\u8981\u8d1f\u8d23\u7684\u5185\u5bb9\u6709&#xff1a;<\/p>\n<ul>\n<li>\u4ec0\u4e48\u65f6\u5019\u53d1\u9001\u6570\u636e<\/li>\n<li>\u4e00\u6b21\u53d1\u9001\u591a\u5c11\u6570\u636e<\/li>\n<li>\u53d1\u9001\u8fc7\u7a0b\u4e2d\u51fa\u9519\u4e86\u8be5\u600e\u4e48\u529e<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5e73\u65f6\u4f7f\u7528\u7684read\u548cwrite\u8fd9\u4e9b\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\u662f\u4ece\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d\u5230\u5185\u6838\u7a7a\u95f4&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u5728\u5e94\u7528\u5c42\u5c06\u9700\u8981\u53d1\u9001\u7684\u6570\u636e\u62f7\u8d1d\u5230TCP\u7684\u53d1\u9001\u7f13\u51b2\u533a\u4e2d&#xff0c;\u8fd9\u5c31\u7ed3\u675f\u4e86&#xff0c;\u81f3\u4e8e\u5230\u5e95\u4ec0\u4e48\u65f6\u5019\u53d1\u9001\u8fd9\u4e9b\u6570\u636e\u7ed9\u5bf9\u65b9&#xff0c;\u5168\u6743\u7531TCP\u8fdb\u884c\u63a7\u5236&#xff0c;\u8fd9\u6837\u8fdb\u884c\u6570\u636e\u7684\u4f20\u8f93\u3002<\/p>\n<p>\u4f46\u662f\u6b63\u56e0\u5982\u6b64&#xff0c;\u8fd9\u5c31\u4f1a\u5012\u662f\u5728\u63a5\u6536\u7aef\u5c31\u4f1a\u5728\u63a5\u6536\u7684\u65f6\u5019\u5341\u5206\u7684\u56f0\u96be&#xff0c;\u6bd4\u5982\u53d1\u9001\u65b9\u53d1\u9001\u4e86\u4e00\u4e2a\u957f\u62a5\u6587&#xff0c;\u4f46\u662f\u63a5\u6536\u65b9\u53ea\u63a5\u6536\u4e00\u70b9\u70b9\u6570\u636e\u5c31\u7ed9\u7528\u6237\u8fd4\u56de\u4e86&#xff0c;\u8fd9\u5c31\u4f1a\u5012\u662f\u63a5\u6536\u65b9\u63a5\u6536\u7684\u6570\u636e\u65f6&#xff0c;\u4f1a\u6709\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u8fd9\u5c31\u597d\u6bd4\u5047\u5982\u73b0\u5728\u548c\u5973\u670b\u53cb\u5435\u67b6\u4e86&#xff0c;\u8fd9\u4e2a\u65f6\u5019\u4f60\u901a\u8fc7\u5fae\u4fe1\u8fdb\u884c\u9053\u6b49&#xff0c;\u539f\u672c\u4f60\u60f3\u8bf4\u7684\u662f\u6211\u4e0d\u540e\u6094\u6211\u7231\u4f60&#xff0c;\u7ed3\u679c\u7531\u4e8e\u63a5\u6536\u65b9\u63a5\u6536\u6570\u636e\u7684\u95ee\u9898&#xff0c;\u53d8\u6210\u4e86\u6211\u4e0d\u540e\u6094&#xff0c;\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u4f60\u5973\u670b\u53cb\u610f\u5473\u4f60\u4e0d\u540e\u6094\u548c\u5979\u5435\u67b6&#xff0c;\u7ed3\u679c\u4f60\u5c31\u65e0\u8f9c\u7684\u6062\u590d\u4e86\u5355\u8eab\u6c49\u3002<\/p>\n<p>\u6240\u4ee5\u540c\u7406\u4e3a\u4e86\u907f\u514dTCP\u5728\u63a5\u6536\u6570\u636e\u65f6\u7684\u5dee\u5f02&#xff0c;\u6211\u4eec\u5c31\u5fc5\u987b\u505a\u597d\u5e94\u7528\u5c42\u7684\u534f\u8bae&#xff0c;\u8fd9\u6837\u624d\u80fd\u4fdd\u8bc1\u53d1\u9001\u7684\u6570\u636e\u5168\u90e8\u63a5\u6536\u5230\u5e76\u8fd4\u56de\u7ed9\u7528\u6237&#xff0c;\u4fdd\u8bc1\u4e86\u63a5\u6536\u65b9\u53ef\u4ee5\u5b8c\u6574\u7684\u63a5\u6536\u5230\u5168\u90e8\u7684\u6570\u636e\u3002<\/p>\n<p>\u90a3\u4e48\u5e94\u7528\u5c42\u534f\u8bae\u5e94\u8be5\u5982\u4f55\u5b9a\u5236\u5462&#xff1f;\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5217\u5b50\u8fdb\u884c\u7406\u89e3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"490\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727122504-6a674e20a1608.png\" width=\"1310\" \/><\/p>\n<p>\u73b0\u5728\u5047\u5982\u6211\u4eec\u5728\u4e00\u4e2a\u804a\u5929\u7fa4\u91cc\u8fdb\u884c\u804a\u5929&#xff0c;\u5927\u5bb6\u5f7c\u6b64\u4e4b\u95f4\u7545\u8c08\u751a\u6b22&#xff0c;\u73b0\u5728\u5047\u5982\u6211\u53d1\u9001\u4e86\u4e00\u53e5\u54c8\u54c8\u54c8&#xff0c;\u5927\u5bb6\u5728\u7fa4\u91cc\u770b\u5230\u7684\u80af\u5b9a\u4e0d\u6b62\u53ea\u6709\u54c8\u54c8\u54c8\u8fd9\u4e09\u4e2a\u5b57&#xff0c;\u8fd8\u4f1a\u6709\u6211\u4eec\u7684\u6635\u79f0&#xff0c;\u6211\u4eec\u7684\u5934\u50cf&#xff0c;\u6211\u4ec0\u4e48\u65f6\u5019\u53d1\u9001\u7684\u7b49\u7b49\u4fe1\u606f&#xff0c;\u6240\u4ee5\u770b\u4f3c\u6211\u53ea\u662f\u53d1\u9001\u4e86\u4e00\u4e2a\u54c8\u54c8\u54c8\u4e09\u4e2a\u5b57&#xff0c;\u4f46\u5176\u5b9e\u8fd8\u6709\u5f88\u591a\u7684\u9644\u52a0\u6570\u636e\u540c\u65f6\u8fdb\u884c\u4e86\u53d1\u9001\u3002<\/p>\n<p>\u800c\u6211\u4eec\u7684\u6570\u636e\u662f\u901a\u8fc7\u5b57\u8282\u6d41\u7684\u65b9\u5f0f\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5c06\u8fd9\u4e9b\u6570\u636e(\u6635\u79f0-\u5934\u50cf-\u65f6\u95f4-\u4fe1\u606f)\u90fd\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u7136\u540e\u4e00\u8d77\u4f20\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u5f53\u5bf9\u65b9\u63a5\u6536\u5230\u8fd9\u4e2a\u6570\u636e\u65f6&#xff0c;\u518d\u5c06\u8fd9\u4e00\u4e2a\u5b57\u7b26\u4e32\u4fe1\u606f\u7684\u5185\u5bb9\u8fdb\u884c\u5206\u522b\u62c6\u89e3&#xff0c;\u6700\u540e\u663e\u793a\u5230\u6211\u4eec\u7684\u663e\u793a\u5668\u4e0a\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5c31\u662f\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u6240\u4ee5\u73b0\u5728\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7edc\u7248\u672c\u7684\u8ba1\u7b97\u5668\u8fdb\u884c\u7406\u89e3\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/p>\n<h2>TCP\u670d\u52a1\u5668\u6574\u4f53\u7ed3\u6784<\/h2>\n<p>Client<br \/>\n\u00a0 \u00a0\u2502<br \/>\n\u00a0 \u00a0\u2502 \u00a0request<br \/>\n\u00a0 \u00a0\u25bc<br \/>\nTcpServer<br \/>\n\u00a0 \u00a0\u2502<br \/>\n\u00a0 \u00a0\u2502 \u00a0callback<br \/>\n\u00a0 \u00a0\u25bc<br \/>\nCalculatorServer<br \/>\n\u00a0 \u00a0\u2502<br \/>\n\u00a0 \u00a0\u2502 \u00a0result<br \/>\n\u00a0 \u00a0\u25bc<br \/>\nresponse<\/p>\n<p>\n\u4e3b\u8981\u6a21\u5757&#xff1a;<\/p>\n<table>\n<tr>\u6a21\u5757\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>Socket<\/td>\n<td>\u5c01\u88c5 socket API<\/td>\n<\/tr>\n<tr>\n<td>TcpServer<\/td>\n<td>TCP\u670d\u52a1\u5668\u6846\u67b6<\/td>\n<\/tr>\n<tr>\n<td>Protocol<\/td>\n<td>\u534f\u8bae\u5c01\u88c5<\/td>\n<\/tr>\n<tr>\n<td>CalculatorServer<\/td>\n<td>\u8ba1\u7b97\u903b\u8f91<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"background-color:transparent\">\u81ea\u5b9a\u4e49\u5e94\u7528\u5c42\u534f\u8bae\u8bbe\u8ba1<\/h2>\n<p>\u7531\u4e8e TCP \u662f \u5b57\u8282\u6d41\u534f\u8bae&#xff0c;\u4e00\u6b21 read() \u53ef\u80fd&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8bfb\u5230\u534a\u4e2a\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u8bfb\u5230\u591a\u4e2a\u6570\u636e<\/p>\n<\/li>\n<\/ul>\n<p>\u5c31\u6bd4\u5982&#xff1a;<\/p>\n<p>\u5ba2\u6237\u7aef\u53d1\u9001\u4e24\u4e2a\u8bf7\u6c42<\/p>\n<p>10 &#043; 20<br \/>\n5 * 6<\/p>\n<p>\u670d\u52a1\u5668\u53ef\u80fd\u8bfb\u5230&#xff1a;<\/p>\n<p>10 &#043; 20\\\\n5 * 6\\\\n<\/p>\n<p>\u6216\u8005&#xff1a;<\/p>\n<p>10 &#043;\u00a0<\/p>\n<p>\u6b63\u662f\u56e0\u4e3a\u5982\u6b64&#xff0c;\u6240\u4ee5\u6211\u4eec\u8981\u81ea\u5b9a\u4e49\u534f\u8bae&#xff0c;\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec\u7f51\u7edc\u7248\u672c\u7684\u8ba1\u7b97\u5668\u7684\u534f\u8bae\u8bbe\u8ba1&#xff1a;<\/p>\n<h3>\u534f\u8bae\u683c\u5f0f<\/h3>\n<p>\u534f\u8bae\u7684\u683c\u5f0f\u8bbe\u8ba1\u5982\u4e0b&#xff1a;<\/p>\n<p>len\\\\n<br \/>\ncontent\\\\n<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\u5c31\u662f&#xff1a;<\/p>\n<p>6<br \/>\n10 &#043; 5<\/p>\n<p>\u7f16\u7801\u8868\u793a\u5c31\u662f&#xff1a;<\/p>\n<p>6\\\\n10 &#043; 5\\\\n<\/p>\n<p>\u603b\u4e4b\u5c31\u662f\u5982\u4e0b\u7684\u683c\u5f0f&#xff1a;<\/p>\n<p>|len| \\\\n |content| \\\\n<\/p>\n<h2>\u534f\u8bae\u5c01\u88c5\u5b9e\u73b0<\/h2>\n<h3>Encode(\u5c01\u88c5\u62a5\u6587)<\/h3>\n<p>std::string Encode(std::string &amp;content)<br \/>\n{<br \/>\n    std::string s;<br \/>\n    size_t len &#061; content.size();<\/p>\n<p>    s &#043;&#061; std::to_string(len);<br \/>\n    s &#043;&#061; &#034;\\\\n&#034;;<br \/>\n    s &#043;&#061; content;<br \/>\n    s &#043;&#061; &#034;\\\\n&#034;;<\/p>\n<p>    return s;<br \/>\n}<\/p>\n<p>\u4f8b\u5982:<\/p>\n<p>10 &#043; 20<\/p>\n<p>\u5c31\u4f1a\u53d8\u4e3a&#xff1a;<\/p>\n<p>7\\\\n10 &#043; 20\\\\n<\/p>\n<h3 style=\"background-color:transparent\">Decode(\u89e3\u6790\u62a5\u6587)<\/h3>\n<p>bool Decode(std::string &amp;s, std::string *content)<br \/>\n{<br \/>\n    size_t left_pos &#061; s.find(&#034;\\\\n&#034;);<\/p>\n<p>    if (left_pos &#061;&#061; std::string::npos)<br \/>\n        return false;<\/p>\n<p>    std::string content_len &#061; s.substr(0, left_pos);<br \/>\n    int len &#061; std::stoi(content_len);<\/p>\n<p>    if (s.size() &lt; content_len.size() &#043; len &#043; 2)<br \/>\n        return false;<\/p>\n<p>    *content &#061; s.substr(left_pos &#043; 1, len);<\/p>\n<p>    s.erase(0, content_len.size() &#043; len &#043; 2);<\/p>\n<p>    return true;<br \/>\n}<\/p>\n<p>Decode \u505a\u4e86\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<li>\n<p>\u5224\u65ad\u662f\u5426\u6709\u5b8c\u6574\u5934\u90e8&#xff08;\u957f\u5ea6&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5224\u65ad\u6570\u636e\u662f\u5426\u5b8c\u6574<\/p>\n<\/li>\n<li>\n<p>\u89e3\u6790 &#043; \u4ece\u7f13\u51b2\u533a\u79fb\u9664<\/p>\n<\/li>\n<h2>\u8bf7\u6c42\u4e0e\u54cd\u5e94\u8bbe\u8ba1<\/h2>\n<h3 style=\"background-color:transparent\">\u8bf7\u6c42 request<\/h3>\n<p>\u5ba2\u6237\u7aef\u53d1\u9001&#xff1a;<\/p>\n<p>10 &#043; 5<\/p>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<p>class request<br \/>\n{<br \/>\npublic:<br \/>\n    int x_;<br \/>\n    int y_;<br \/>\n    char op_;<br \/>\n};<\/p>\n<p>\u5e8f\u5217\u5316&#xff1a;<\/p>\n<p>&#034;10 &#043; 5&#034;<\/p>\n<p>\u53cd\u5e8f\u5217\u5316&#xff1a;<\/p>\n<p>string -&gt; request<\/p>\n<h3 style=\"background-color:transparent\">\u54cd\u5e94 response<\/h3>\n<p>\u670d\u52a1\u5668\u8fd4\u56de&#xff1a;<\/p>\n<p>&#034;15 0&#034;<\/p>\n<p>\u7ed3\u6784&#xff1a;<\/p>\n<p>class response<br \/>\n{<br \/>\npublic:<br \/>\n    int result_;<br \/>\n    int code_;<br \/>\n};<\/p>\n<p>code\u7684\u542b\u4e49&#xff1a;<\/p>\n<table>\n<tr>code\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>\u6210\u529f<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\u96640<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u53d6\u6a210<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u975e\u6cd5\u64cd\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u6838\u5fc3\u96be\u70b9&#xff1a;\u89e3\u51b3 TCP \u7c98\u5305\u95ee\u9898<\/h2>\n<h3>\u2753 \u4ec0\u4e48\u662f\u7c98\u5305&#xff1f;<\/h3>\n<p>TCP \u662f\u9762\u5411\u5b57\u8282\u6d41\u7684\u534f\u8bae&#xff1a;<\/p>\n<p>&#x1f449; \u53d1\u9001&#xff1a;<\/p>\n<p>\u8bf7\u6c421 &#043; \u8bf7\u6c422 &#043; \u8bf7\u6c423<\/p>\n<p>&#x1f449; \u63a5\u6536&#xff1a;<\/p>\n<p>\u53ef\u80fd\u53d8\u6210&#xff1a;<br \/>\n\u8bf7\u6c421\u8bf7\u6c422 | \u8bf7\u6c423<\/p>\n<h3>\u89e3\u7801\u51fd\u6570 Decode&#xff08;\u91cd\u70b9&#xff09;<\/h3>\n<p>bool Decode(std::string &amp;s, std::string *content)<br \/>\n{<br \/>\n    size_t pos &#061; s.find(&#034;\\\\n&#034;);<br \/>\n    if (pos &#061;&#061; std::string::npos)<br \/>\n        return false;<\/p>\n<p>    int len &#061; std::stoi(s.substr(0, pos));<\/p>\n<p>    if (s.size() &lt; pos &#043; 1 &#043; len &#043; 1)<br \/>\n        return false;<\/p>\n<p>    *content &#061; s.substr(pos &#043; 1, len);<br \/>\n    s.erase(0, pos &#043; 1 &#043; len &#043; 1);<\/p>\n<p>    return true;<br \/>\n}<\/p>\n<h3>\u670d\u52a1\u5668\u7684\u5904\u7406\u65b9\u6cd5Calculator&#xff08;\u91cd\u70b9&#xff09;<\/h3>\n<p>    std::string Calculator(std::string&amp; s)<br \/>\n    {<br \/>\n        std::string content;<br \/>\n        if (Decode(s, &amp;content) &#061;&#061; false)<br \/>\n        {<br \/>\n            return &#034;&#034;;<br \/>\n        }<br \/>\n        request req;<br \/>\n        bool r &#061; req.Deserialization(content);<\/p>\n<p>        if (!r)<br \/>\n        {<br \/>\n            return &#034;&#034;;<br \/>\n        }<br \/>\n        response res &#061; CalculatorHandler(req);<\/p>\n<p>        std::string ret &#061; res.serialization();<br \/>\n        ret &#061; Encode(ret);<br \/>\n        return ret;<br \/>\n    }<\/p>\n<h3>\u670d\u52a1\u5668\u6838\u5fc3\u4ee3\u7801&#xff1a;&#xff08;\u91cd\u70b9&#xff09;<\/h3>\n<p>while (true)<br \/>\n{<br \/>\n    int sockfd &#061; listenfd_.Accept(&amp;client_port, &amp;client_ip);<\/p>\n<p>    if (fork() &#061;&#061; 0)<br \/>\n    {<br \/>\n        while (1)<br \/>\n        {<br \/>\n            char buffer[1280];<\/p>\n<p>            ssize_t s &#061; read(sockfd, buffer, sizeof buffer &#8211; 1);<\/p>\n<p>            if (s &gt; 0)<br \/>\n            {<br \/>\n                inbuffer_stream &#043;&#061; buffer;<\/p>\n<p>                while (true)<br \/>\n                {<br \/>\n                    std::string info &#061; callback_(inbuffer_stream);<\/p>\n<p>                    if (info.empty())<br \/>\n                        break;<\/p>\n<p>                    write(sockfd, info.c_str(), info.size());<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u7684\u670d\u52a1\u5668\u5728\u6536\u5230\u4e00\u4e2a\u62a5\u6587\u4e4b\u540e&#xff0c;\u9996\u5148\u4f1a\u8c03\u7528\u670d\u52a1\u5668\u7684\u5904\u7406\u65b9\u6cd5Calculator&#xff0c;\u5728\u5904\u7406\u65b9\u6cd5\u4e2d\u4f1a\u8fdb\u884c\u89e3\u7801&#xff0c;\u5982\u679c\u6536\u5230\u7684\u62a5\u6587\u4e0d\u80fd\u591f\u5206\u89e3\u4e3a\u7c7b\u4f3c&#xff08;6\\\\n10 &#043; 5\\\\n&#xff09;\u8fd9\u6837\u7684\u683c\u5f0f&#xff0c;\u6211\u4eec\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32&#xff0c;\u800c\u4e00\u65e6\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32&#xff0c;\u6211\u4eec\u7684\u670d\u52a1\u5668\u5c31\u77e5\u9053\u8fd9\u4e2a\u62a5\u6587\u4e0d\u5b8c\u6574&#xff0c;\u5c31\u4f1a\u7ee7\u7eed\u63a5\u6536\u65b0\u7684\u62a5\u6587&#xff0c;\u76f4\u5230\u63a5\u6536\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u4e14\u53ef\u4ee5\u901a\u8fc7Decode\u89e3\u7801\u6210\u529f\u4e4b\u540e&#xff0c;\u6211\u4eec\u7684\u7a0b\u5e8f\u624d\u4f1a\u7ee7\u7eed\u8fdb\u884c&#xff0c;\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u6211\u4eec\u6bcf\u6b21\u670d\u52a1\u5668\u5904\u7406\u7684\u80af\u5b9a\u662f\u4e00\u4e2a\u6b63\u786e\u683c\u5f0f\u7684\u62a5\u6587\u3002<\/p>\n<h2>\u5b8c\u6574\u4ee3\u7801<\/h2>\n<h3>\u81ea\u5b9a\u4e49\u534f\u8bae<\/h3>\n<p>#include &lt;string&gt;<\/p>\n<p>#define blank_sep &#034; &#034;<br \/>\n#define protocol_sep &#034;\\\\n&#034;<\/p>\n<p>class request<br \/>\n{<br \/>\npublic:<br \/>\n    request(int x, int y, char op)<br \/>\n        : x_(x), y_(y), op_(op)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    request()<br \/>\n    {<br \/>\n    }<\/p>\n<p>    ~request()<br \/>\n    {<br \/>\n    }<\/p>\n<p>    std::string serialization()<br \/>\n    {<br \/>\n        std::string str;<br \/>\n        str &#043;&#061; std::to_string(x_);<br \/>\n        str &#043;&#061; blank_sep;<br \/>\n        str &#043;&#061; op_;<br \/>\n        str &#043;&#061; blank_sep;<br \/>\n        str &#043;&#061; std::to_string(y_);<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    bool Deserialization(std::string &amp;in)<br \/>\n    {<br \/>\n        size_t leftpos &#061; in.find(blank_sep);<br \/>\n        if (leftpos &#061;&#061; std::string::npos)<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<br \/>\n        std::string str_x &#061; in.substr(0, leftpos);<br \/>\n        x_ &#061; std::stoi(str_x);<\/p>\n<p>        op_ &#061; in[leftpos &#043; 1];<\/p>\n<p>        size_t rightpos &#061; in.rfind(blank_sep);<br \/>\n        if (rightpos &#061;&#061; std::string::npos)<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<br \/>\n        std::string str_y &#061; in.substr(rightpos &#043; 1);<br \/>\n        y_ &#061; std::stoi(str_y);<br \/>\n        return true;<br \/>\n    }<br \/>\n    void DebugPrint()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;\u65b0\u8bf7\u6c42\u6784\u5efa\u5b8c\u6210:  &#034; &lt;&lt; x_ &lt;&lt; op_ &lt;&lt; y_ &lt;&lt; &#034;&#061;?&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    int x_;<br \/>\n    int y_;<br \/>\n    char op_;<br \/>\n};<\/p>\n<p>class response<br \/>\n{<br \/>\npublic:<br \/>\n    response()<br \/>\n    {<br \/>\n    }<br \/>\n    response(int result, int code)<br \/>\n        : result_(result), code_(code)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    ~response()<br \/>\n    {<br \/>\n    }<\/p>\n<p>    std::string serialization()<br \/>\n    {<br \/>\n        std::string str;<br \/>\n        str &#043;&#061; std::to_string(result_);<br \/>\n        str &#043;&#061; blank_sep;<br \/>\n        str &#043;&#061; std::to_string(code_);<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    bool Deserialization(std::string &amp;in)<br \/>\n    {<br \/>\n        size_t pos &#061; in.find(blank_sep);<\/p>\n<p>        if (pos &#061;&#061; std::string::npos)<br \/>\n        {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        std::string str_result &#061; in.substr(0, pos);<br \/>\n        result_ &#061; std::stoi(str_result);<\/p>\n<p>        std::string str_code &#061; in.substr(pos &#043; 1);<br \/>\n        code_ &#061; std::stoi(str_code);<br \/>\n        return true;<br \/>\n    }<br \/>\n    void DebugPrint()<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;\u7ed3\u679c\u54cd\u5e94\u5b8c\u6210, result: &#034; &lt;&lt; result_ &lt;&lt; &#034;, code: &#034; &lt;&lt; code_ &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    int result_;<br \/>\n    int code_;<br \/>\n};<\/p>\n<p>std::string Encode(std::string &amp;content)<br \/>\n{<br \/>\n    std::string s;<br \/>\n    size_t len &#061; content.size();<br \/>\n    s &#043;&#061; std::to_string(len);<br \/>\n    s &#043;&#061; protocol_sep;<br \/>\n    s &#043;&#061; content;<br \/>\n    s &#043;&#061; protocol_sep;<br \/>\n    return s;<br \/>\n}<\/p>\n<p>bool Decode(std::string &amp;s, std::string *content)<br \/>\n{<br \/>\n    size_t left_pos &#061; s.find(protocol_sep);<br \/>\n    if (left_pos &#061;&#061; std::string::npos)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    std::string content_len &#061; s.substr(0, left_pos);<br \/>\n    int len &#061; std::stoi(content_len);<br \/>\n    if (s.size() &lt; content_len.size() &#043; len &#043; 2)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    *content &#061; s.substr(left_pos &#043; 1, len);<br \/>\n    s.erase(0, content_len.size() &#043; len &#043; 2);<br \/>\n    return true;<br \/>\n}<\/p>\n<h3>\u670d\u52a1\u7aef<\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>enum error<br \/>\n{<br \/>\n    SocketErr &#061; 2,<br \/>\n    BindErr,<br \/>\n    ListenErr,<br \/>\n    ConnectErr,<br \/>\n};<\/p>\n<p>class Socket<br \/>\n{<br \/>\npublic:<br \/>\n    Socket()<br \/>\n    {<br \/>\n        sockfd_ &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (sockfd_ &lt; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;socket fail&#034; &lt;&lt; std::endl;<br \/>\n            exit(SocketErr);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Bind(uint16_t &amp;port, std::string &amp;ip)<br \/>\n    {<br \/>\n        struct sockaddr_in server;<br \/>\n        server.sin_family &#061; AF_INET;<br \/>\n        server.sin_port &#061; htons(port);<br \/>\n        inet_pton(AF_INET, ip.c_str(), &amp;server.sin_addr);<br \/>\n        if (bind(sockfd_, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;server bind fail!&#034; &lt;&lt; std::endl;<br \/>\n            exit(BindErr);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;server bind successful&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void Listen()<br \/>\n    {<br \/>\n        if (listen(sockfd_, 10) &lt; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;server listen fail!&#034; &lt;&lt; std::endl;<br \/>\n            exit(ListenErr);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;server listen successful&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    int Accept(uint16_t *client_port, std::string *client_ip)<br \/>\n    {<br \/>\n        struct sockaddr_in client;<br \/>\n        socklen_t len &#061; sizeof(client);<br \/>\n        int sockfd &#061; accept(sockfd_, (struct sockaddr *)&amp;client, &amp;len);<br \/>\n        if (sockfd &lt; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;accept fail!&#034; &lt;&lt; std::endl;<br \/>\n            return -1;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;accept successful&#034; &lt;&lt; std::endl;<br \/>\n        *client_port &#061; ntohs(client.sin_port);<br \/>\n        char ip[64];<br \/>\n        inet_ntop(AF_INET, &amp;client.sin_addr, ip, sizeof ip);<br \/>\n        *client_ip &#061; ip;<br \/>\n        return sockfd;<br \/>\n    }<\/p>\n<p>    void Connect(uint16_t &amp;server_port, std::string &amp;server_ip)<br \/>\n    {<br \/>\n        struct sockaddr_in server;<br \/>\n        server.sin_family &#061; AF_INET;<br \/>\n        server.sin_port &#061; htons(server_port);<br \/>\n        inet_pton(AF_INET, server_ip.c_str(), &amp;server.sin_addr);<br \/>\n        if (connect(sockfd_, (struct sockaddr *)&amp;server, sizeof server) &lt; 0)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;connect fail!&#034; &lt;&lt; std::endl;<br \/>\n            exit(ConnectErr);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;connect successful!&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    void Close()<br \/>\n    {<br \/>\n        close(sockfd_);<br \/>\n    }<\/p>\n<p>    int fd()<br \/>\n    {<br \/>\n        return sockfd_;<br \/>\n    }<\/p>\n<p>    ~Socket()<br \/>\n    {<br \/>\n        close(sockfd_);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int sockfd_;<br \/>\n};<br \/>\nclass CalculatorServer<br \/>\n{<br \/>\npublic:<br \/>\n    CalculatorServer()<br \/>\n    {<br \/>\n    }<\/p>\n<p>    response CalculatorHandler(const request &amp;req)<br \/>\n    {<br \/>\n        response res(0, 0);<br \/>\n        switch (req.op_)<br \/>\n        {<br \/>\n        case &#039;&#043;&#039;:<br \/>\n            res.result_ &#061; req.x_ &#043; req.y_;<br \/>\n            break;<br \/>\n        case &#039;-&#039;:<br \/>\n            res.result_ &#061; req.x_ &#8211; req.y_;<br \/>\n            break;<br \/>\n        case &#039;*&#039;:<br \/>\n            res.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                res.code_ &#061; 1;<br \/>\n                break;<br \/>\n            }<br \/>\n            res.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                res.code_ &#061; 2;<br \/>\n                break;<br \/>\n            }<br \/>\n            res.result_ &#061; req.x_ % req.y_;<br \/>\n            break;<br \/>\n        default:<br \/>\n            res.code_ &#061; 3;<br \/>\n            break;<br \/>\n        }<br \/>\n        return res;<br \/>\n    }<\/p>\n<p>    std::string Calculator(std::string&amp; s)<br \/>\n    {<br \/>\n        std::string content;<br \/>\n        if (Decode(s, &amp;content) &#061;&#061; false)<br \/>\n        {<br \/>\n            return &#034;&#034;;<br \/>\n        }<br \/>\n        request req;<br \/>\n        bool r &#061; req.Deserialization(content);<\/p>\n<p>        if (!r)<br \/>\n        {<br \/>\n            return &#034;&#034;;<br \/>\n        }<br \/>\n        response res &#061; CalculatorHandler(req);<\/p>\n<p>        std::string ret &#061; res.serialization();<br \/>\n        ret &#061; Encode(ret);<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    ~CalculatorServer()<br \/>\n    {<br \/>\n    }<br \/>\n};<br \/>\nusing func_t &#061; std::function&lt;std::string(std::string &amp;)&gt;;<br \/>\nclass TcpServer<br \/>\n{<br \/>\npublic:<br \/>\n    TcpServer(uint16_t port, std::string ip, func_t callback)<br \/>\n        : port_(port), ip_(ip), callback_(callback)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    void InitServer()<br \/>\n    {<br \/>\n        listenfd_.Bind(port_, ip_);<br \/>\n        listenfd_.Listen();<br \/>\n        std::cout &lt;&lt; &#034;init server successful!&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void start()<br \/>\n    {<br \/>\n        signal(SIGCHLD, SIG_IGN);<br \/>\n        signal(SIGPIPE, SIG_IGN);<br \/>\n        while (true)<br \/>\n        {<br \/>\n            uint16_t client_port;<br \/>\n            std::string client_ip;<br \/>\n            int sockfd &#061; listenfd_.Accept(&amp;client_port, &amp;client_ip);<\/p>\n<p>            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            if (fork() &#061;&#061; 0)<br \/>\n            {<br \/>\n                \/\/ \u5b50\u8fdb\u7a0b<br \/>\n                listenfd_.Close();<br \/>\n                std::string inbuffer_stream;<\/p>\n<p>                while (1)<br \/>\n                {<br \/>\n                    char buffer[1280];<br \/>\n                    ssize_t s &#061; read(sockfd, buffer, sizeof buffer &#8211; 1);<br \/>\n                    if (s &gt; 0)<br \/>\n                    {<br \/>\n                        buffer[s] &#061; 0;<br \/>\n                        \/\/ std::cout &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n                        inbuffer_stream &#043;&#061; buffer;<br \/>\n                        while (true)<br \/>\n                        {<br \/>\n                            std::string info &#061; callback_(inbuffer_stream);<br \/>\n                            std::cout &lt;&lt; info &lt;&lt; std::endl;<br \/>\n                            if (info.empty())<br \/>\n                            {<br \/>\n                                break;<br \/>\n                            }<\/p>\n<p>                            write(sockfd, info.c_str(), info.size());<br \/>\n                        }<br \/>\n                    }<br \/>\n                    else if (s &#061;&#061; 0)<br \/>\n                    {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                    else<br \/>\n                    {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                }<br \/>\n                close(sockfd);<br \/>\n                exit(0);<br \/>\n            }<br \/>\n            close(sockfd);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    ~TcpServer()<br \/>\n    {<br \/>\n    }<\/p>\n<p>private:<br \/>\n    Socket listenfd_;<br \/>\n    uint16_t port_;<br \/>\n    std::string ip_;<br \/>\n    func_t callback_;<br \/>\n};<br \/>\nint main(int argc,char* argv[])<br \/>\n{<br \/>\n    if(argc !&#061; 3)<br \/>\n    {<br \/>\n        exit(0);<br \/>\n    }<\/p>\n<p>    uint16_t server_port &#061; std::atoi(argv[2]);<br \/>\n    std::string server_ip &#061; argv[1];<br \/>\n    CalculatorServer cal;<br \/>\n    TcpServer* ser &#061; new TcpServer(server_port,server_ip,std::bind(&amp;CalculatorServer::Calculator, &amp;cal, std::placeholders::_1));<br \/>\n    ser-&gt;InitServer();<br \/>\n    ser-&gt;start();<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3>\u5ba2\u6237\u7aef<\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &#034;Protocol.hpp&#034;<br \/>\n#include &#034;Socket.hpp&#034;<\/p>\n<p>static void Usage(const std::string &amp;proc)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;\\\\nUsage: &#034; &lt;&lt; proc &lt;&lt; &#034; serverip serverport\\\\n&#034;<br \/>\n              &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\/\/ .\/clientcal ip port<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 3)<br \/>\n    {<br \/>\n        Usage(argv[0]);<br \/>\n        exit(0);<br \/>\n    }<br \/>\n    std::string serverip &#061; argv[1];<br \/>\n    uint16_t serverport &#061; std::stoi(argv[2]);<\/p>\n<p>    Socket sockfd;<br \/>\n    sockfd.Connect(serverport, serverip);<\/p>\n<p>    srand(time(nullptr) ^ getpid());<br \/>\n    int cnt &#061; 1;<br \/>\n    const std::string opers &#061; &#034;&#043;-*\/%&#061;-&#061;&amp;^&#034;;<\/p>\n<p>    std::string inbuffer_stream;<br \/>\n    while (cnt &lt;&#061; 10)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;\u7b2c&#034; &lt;&lt; cnt &lt;&lt; &#034;\u6b21\u6d4b\u8bd5&#8230;.., &#034; &lt;&lt; &#034;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; std::endl;<br \/>\n        int x &#061; rand() % 100 &#043; 1;<br \/>\n        usleep(1234);<br \/>\n        int y &#061; rand() % 100;<br \/>\n        usleep(4321);<br \/>\n        char oper &#061; opers[rand() % opers.size()];<br \/>\n        request req(x, y, oper);<br \/>\n        req.DebugPrint();<\/p>\n<p>        std::string package;<br \/>\n        package &#061; req.serialization();<\/p>\n<p>        package &#061; Encode(package);<br \/>\n        std::cout &lt;&lt; package &lt;&lt; std::endl;<br \/>\n        write(sockfd.fd(), package.c_str(), package.size());<\/p>\n<p>        char buffer[128];<br \/>\n        ssize_t n &#061; read(sockfd.fd(), buffer, sizeof(buffer) &#8211; 1);<br \/>\n        if (n &gt; 0)<br \/>\n        {<br \/>\n            buffer[n] &#061; 0;<br \/>\n            inbuffer_stream &#043;&#061; buffer; \/\/ &#034;len&#034;\\\\n&#034;result code&#034;\\\\n<br \/>\n            std::cout &lt;&lt; inbuffer_stream &lt;&lt; std::endl;<br \/>\n            std::string content;<br \/>\n            bool r &#061; Decode(inbuffer_stream, &amp;content); \/\/ &#034;result code&#034;<br \/>\n            assert(r);<\/p>\n<p>            response resp;<br \/>\n            r &#061; resp.Deserialization(content);<br \/>\n            assert(r);<\/p>\n<p>            resp.DebugPrint();<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt; std::endl;<br \/>\n        sleep(1);<\/p>\n<p>        cnt&#043;&#043;;<br \/>\n    }<\/p>\n<p>    sockfd.Close();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"809\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727122504-6a674e20ddb64.png\" width=\"917\" \/><\/p>\n<\/p>\n<p>\u5230\u8fd9\u91cc&#xff0c;\u6211\u4eec\u5df2\u7ecf\u5b8c\u6574\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8e C&#043;&#043; \u7684 TCP \u8ba1\u7b97\u5668\u670d\u52a1\u5668&#xff0c;\u5230\u73b0\u5728&#xff0c;\u6211\u4eec\u5e94\u8be5\u5efa\u7acb\u8fd9\u6837\u4e00\u4e2a\u8ba4\u77e5&#xff1a;<\/p>\n<p>TCP\u7f16\u7a0b\u7684\u672c\u8d28\u4e0d\u662f\u6536\u53d1\u6570\u636e&#xff0c;\u800c\u662f\u5982\u4f55\u6b63\u786e\u89e3\u6790\u6570\u636e\u7684\u8fb9\u754c<\/p>\n<p>\u603b\u800c\u8a00\u4e4b&#xff0c;\u6838\u5fc3\u601d\u60f3\u5c31\u4e00\u53e5\u8bdd&#xff0c;\u5c31\u662fTCP\u6ca1\u6709\u6d88\u606f\u8fb9\u754c&#xff0c;\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u8bbe\u8ba1\u5e94\u7528\u5c42\u534f\u8bae\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e4b\u524d\u7684\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86\u5173\u4e8eUDP\u548cTCP\u7684\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u76f4\u89c2\u7684\u611f\u53d7\u4e86\u4e00\u4e0b\u7f51\u7edc\u5957\u63a5\u5b57\u662f\u5982\u4f55\u4f7f\u7528\u7684&#xff0c;\u5e76\u4e14\u6210\u529f\u7684\u5b8c\u6210\u4e86\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5c0f\u7ec6\u8282\u6211\u4eec\u53ef\u80fd\u4f1a\u5ffd\u7565&#xff0c;\u5c31\u662fUDP\u662f\u57fa\u4e8e\u6570\u636e\u62a5\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u4e00\u4e0b\u5b50\u5c31\u5c06\u6240\u6709\u6211\u4eec\u8981\u53d1\u9001\u7684\u4fe1\u606f\u4f20\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u4f46\u662f\u6211\u4eec\u7684TCP\u53ef\u662f\u57fa\u4e8e\u5b57\u8282\u6d41\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u6211\u4eec\u5982\u4f55\u4fdd\u8bc1\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e&#xff0c;\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u5462&amp;#xff1f<\/p>\n","protected":false},"author":2,"featured_media":85664,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,970,16,396],"topic":[],"class_list":["post-85667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-tcp","tag-16","tag-396"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \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\/85667.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728\u4e4b\u524d\u7684\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86\u5173\u4e8eUDP\u548cTCP\u7684\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u76f4\u89c2\u7684\u611f\u53d7\u4e86\u4e00\u4e0b\u7f51\u7edc\u5957\u63a5\u5b57\u662f\u5982\u4f55\u4f7f\u7528\u7684&#xff0c;\u5e76\u4e14\u6210\u529f\u7684\u5b8c\u6210\u4e86\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5c0f\u7ec6\u8282\u6211\u4eec\u53ef\u80fd\u4f1a\u5ffd\u7565&#xff0c;\u5c31\u662fUDP\u662f\u57fa\u4e8e\u6570\u636e\u62a5\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u4e00\u4e0b\u5b50\u5c31\u5c06\u6240\u6709\u6211\u4eec\u8981\u53d1\u9001\u7684\u4fe1\u606f\u4f20\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u4f46\u662f\u6211\u4eec\u7684TCP\u53ef\u662f\u57fa\u4e8e\u5b57\u8282\u6d41\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u6211\u4eec\u5982\u4f55\u4fdd\u8bc1\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e&#xff0c;\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u5462&amp;#xff1f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85667.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T12:25:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727122504-6a674e202361c.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=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85667.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85667.html\",\"name\":\"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T12:25:06+00:00\",\"dateModified\":\"2026-07-27T12:25:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85667.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85667.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85667.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898)\"}]},{\"@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":"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \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\/85667.html","og_locale":"zh_CN","og_type":"article","og_title":"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728\u4e4b\u524d\u7684\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86\u5173\u4e8eUDP\u548cTCP\u7684\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u76f4\u89c2\u7684\u611f\u53d7\u4e86\u4e00\u4e0b\u7f51\u7edc\u5957\u63a5\u5b57\u662f\u5982\u4f55\u4f7f\u7528\u7684&#xff0c;\u5e76\u4e14\u6210\u529f\u7684\u5b8c\u6210\u4e86\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u7684\u7f51\u7edc\u901a\u4fe1&#xff0c;\u4f46\u662f\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5c0f\u7ec6\u8282\u6211\u4eec\u53ef\u80fd\u4f1a\u5ffd\u7565&#xff0c;\u5c31\u662fUDP\u662f\u57fa\u4e8e\u6570\u636e\u62a5\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u4e00\u4e0b\u5b50\u5c31\u5c06\u6240\u6709\u6211\u4eec\u8981\u53d1\u9001\u7684\u4fe1\u606f\u4f20\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u4f46\u662f\u6211\u4eec\u7684TCP\u53ef\u662f\u57fa\u4e8e\u5b57\u8282\u6d41\u8fdb\u884c\u4f20\u8f93\u7684&#xff0c;\u6211\u4eec\u5982\u4f55\u4fdd\u8bc1\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e&#xff0c;\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u5462&amp;#xff1f","og_url":"https:\/\/www.wsisp.com\/helps\/85667.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T12:25:06+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727122504-6a674e202361c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85667.html","url":"https:\/\/www.wsisp.com\/helps\/85667.html","name":"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T12:25:06+00:00","dateModified":"2026-07-27T12:25:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85667.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85667.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85667.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u624b\u5199\u4e00\u4e2aC++ TCP\u670d\u52a1\u5668\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae(\u987a\u4fbf\u89e3\u51b3\u7c98\u5305\u95ee\u9898)"}]},{"@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\/85667","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=85667"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85667\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/85664"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85667"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}