{"id":47397,"date":"2025-07-30T07:36:57","date_gmt":"2025-07-29T23:36:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/47397.html"},"modified":"2025-07-30T07:36:57","modified_gmt":"2025-07-29T23:36:57","slug":"c%e5%ae%9e%e7%8e%b0%e7%ae%80%e6%98%93tcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84linux%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/47397.html","title":{"rendered":"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b"},"content":{"rendered":"<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b1823537.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528C&#043;&#043;\u8bed\u8a00\u7f16\u5199TCP\u7f51\u7edc\u901a\u4fe1\u7a0b\u5e8f&#xff0c;\u6db5\u76d6\u5957\u63a5\u5b57\u7684\u521b\u5efa\u4e0e\u7ba1\u7406\u3001\u591a\u7ebf\u7a0b\u5904\u7406\u4ee5\u53ca\u547d\u4ee4\u884c\u53c2\u6570\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u6e90\u4ee3\u7801\u6587\u4ef6  server.cpp  \u548c  client.cpp  &#xff0c;\u6f14\u793a\u4e00\u4e2a\u7b80\u6613\u7684TCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u6d41\u7a0b\u3002\u6b64\u5916&#xff0c;\u8fd8\u5c06\u63a2\u8ba8\u591a\u7ebf\u7a0b\u6280\u672f\u5728\u670d\u52a1\u5668\u7aef\u7684\u5e94\u7528&#xff0c;\u4ee5\u5b9e\u73b0\u5e76\u53d1\u8fde\u63a5\u7684\u5904\u7406\u3002\u901a\u8fc7\u672c\u9879\u76ee&#xff0c;\u8bfb\u8005\u5c06\u4e86\u89e3TCP\u534f\u8bae\u7684\u5de5\u4f5c\u539f\u7406&#xff0c;\u638c\u63e1Linux\u73af\u5883\u4e0b\u7684\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u3002  <img decoding=\"async\" alt=\"SimpleNetwork&#xff1a;\u7b80\u5355\u7684TCP\u670d\u52a1\u5668\u5ba2\u6237\u7aefC &#043;&#043; Linux\u5957\u63a5\u5b57\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b182b108.jpg\" \/> <\/p>\n<h2> 1. C&#043;&#043;\u5728Linux\u4e0b\u7684\u7f51\u7edc\u7f16\u7a0b <\/h2>\n<h3> \u7f51\u7edc\u7f16\u7a0b\u7b80\u4ecb <\/h3>\n<p> \u5728\u5f53\u4eca\u7684\u4fe1\u606f\u65f6\u4ee3&#xff0c;\u7f51\u7edc\u7f16\u7a0b\u662fIT\u4e13\u4e1a\u4eba\u5458\u5fc5\u987b\u638c\u63e1\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00&#xff0c;\u5b83\u6d89\u53ca\u5230\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u548c\u7f51\u7edc\u534f\u8bae\u6765\u6784\u5efa\u80fd\u591f\u76f8\u4e92\u901a\u4fe1\u7684\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u3002Linux\u7cfb\u7edf\u56e0\u5176\u5f00\u6e90\u548c\u7a33\u5b9a\u6027&#xff0c;\u5728\u670d\u52a1\u5668\u548c\u5d4c\u5165\u5f0f\u8bbe\u5907\u4e2d\u5e7f\u6cdb\u4f7f\u7528&#xff0c;\u800cC&#043;&#043;\u5219\u56e0\u5176\u6027\u80fd\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u9ad8&#xff0c;\u6210\u4e3a\u4e86\u7f51\u7edc\u7f16\u7a0b\u7684\u70ed\u95e8\u9009\u62e9\u3002 <\/p>\n<h3> C&#043;&#043;\u5728Linux\u4e0b\u7684\u7f51\u7edc\u7f16\u7a0b\u4f18\u52bf <\/h3>\n<p> C&#043;&#043;\u5728Linux\u4e0b\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u5177\u6709\u660e\u663e\u7684\u4f18\u52bf\u3002\u9996\u5148&#xff0c;C&#043;&#043;\u5177\u5907\u9762\u5411\u5bf9\u8c61\u7684\u7279\u6027&#xff0c;\u4f7f\u5f97\u4ee3\u7801\u6a21\u5757\u5316\u548c\u590d\u7528\u6027\u66f4\u5f3a&#xff0c;\u6709\u5229\u4e8e\u6784\u5efa\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u6b21&#xff0c;C&#043;&#043;\u76f4\u63a5\u652f\u6301\u6307\u9488\u548c\u5185\u5b58\u64cd\u4f5c&#xff0c;\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u5185\u5b58\u7ba1\u7406\u7684\u7f51\u7edc\u7f16\u7a0b\u6765\u8bf4&#xff0c;\u8fd9\u4e00\u70b9\u81f3\u5173\u91cd\u8981\u3002\u6700\u540e&#xff0c;Linux\u4e3aC&#043;&#043;\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u7f16\u7a0bAPI&#xff0c;\u5982Berkeley\u5957\u63a5\u5b57\u63a5\u53e3&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u63a5\u53e3\u8fdb\u884c\u5e95\u5c42\u7f51\u7edc\u901a\u4fe1\u7684\u6784\u5efa\u548c\u7ba1\u7406\u3002 <\/p>\n<h3> \u7f51\u7edc\u7f16\u7a0b\u7684\u8981\u70b9 <\/h3>\n<p> \u672c\u7ae0\u5c06\u63a2\u8ba8C&#043;&#043;\u5728Linux\u4e0b\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\u548c\u5b9e\u8df5\u6280\u5de7&#xff0c;\u5305\u62ecLinux\u4e0b\u7684\u5957\u63a5\u5b57\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6784\u5efa\u3001\u7f51\u7edc\u534f\u8bae\u7684\u5b9e\u73b0\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u7b49\u65b9\u9762\u3002\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c06\u9010\u4e00\u5206\u6790\u8fd9\u4e9b\u8981\u70b9&#xff0c;\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u6df1\u5165\u7406\u89e3C&#043;&#043;\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528&#xff0c;\u4e3a\u540e\u7eed\u7ae0\u8282\u7684\u6df1\u5165\u5b66\u4e60\u6253\u4e0b\u575a\u5b9e\u57fa\u7840\u3002 <\/p>\n<h2> 2. TCP\u534f\u8bae\u7684\u5e94\u7528\u4e0e\u539f\u7406 <\/h2>\n<h4> 2.1 TCP\u534f\u8bae\u57fa\u7840 <\/h4>\n<h5> 2.1.1 TCP\u534f\u8bae\u7684\u7279\u6027 <\/h5>\n<p> TCP&#xff08;Transmission Control Protocol&#xff0c;\u4f20\u8f93\u63a7\u5236\u534f\u8bae&#xff09;\u662f\u4e00\u79cd\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684\u3001\u57fa\u4e8e\u5b57\u8282\u6d41\u7684\u4f20\u8f93\u5c42\u901a\u4fe1\u534f\u8bae\u3002\u5b83\u4e3a\u6570\u636e\u4f20\u8f93\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5173\u952e\u7279\u6027&#xff1a; <\/p>\n<ul>\n<li>  \u9762\u5411\u8fde\u63a5  &#xff1a;\u5728\u6570\u636e\u4ea4\u6362\u4e4b\u524d&#xff0c;TCP\u901a\u8fc7\u4e09\u6b21\u63e1\u624b\u5efa\u7acb\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u4e86\u901a\u4fe1\u53cc\u65b9\u7684\u53ef\u7528\u6027\u3002 <\/li>\n<li>  \u53ef\u9760\u6027\u4fdd\u8bc1  &#xff1a;TCP\u901a\u8fc7\u5e8f\u5217\u53f7\u3001\u786e\u8ba4\u5e94\u7b54\u3001\u8d85\u65f6\u91cd\u4f20\u7b49\u673a\u5236\u4fdd\u8bc1\u6570\u636e\u7684\u51c6\u786e\u65e0\u8bef\u5730\u9001\u8fbe\u76ee\u7684\u5730\u3002 <\/li>\n<li>  \u6d41\u91cf\u63a7\u5236  &#xff1a;TCP\u53ef\u4ee5\u8c03\u6574\u53d1\u9001\u901f\u7387\u4ee5\u907f\u514d\u5feb\u901f\u53d1\u9001\u65b9\u6df9\u6ca1\u6162\u901f\u63a5\u6536\u65b9\u3002 <\/li>\n<li>  \u62e5\u585e\u63a7\u5236  &#xff1a;TCP\u80fd\u591f\u611f\u77e5\u7f51\u7edc\u62e5\u5835\u72b6\u51b5&#xff0c;\u5e76\u901a\u8fc7\u8c03\u6574\u6570\u636e\u5305\u53d1\u9001\u901f\u7387\u6765\u8fdb\u884c\u62e5\u585e\u907f\u514d\u3002 <\/li>\n<li>  \u5168\u53cc\u5de5\u901a\u4fe1  &#xff1a;\u8fde\u63a5\u7684\u53cc\u65b9\u53ef\u4ee5\u540c\u65f6\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002 <\/li>\n<\/ul>\n<h5> 2.1.2 TCP\u8fde\u63a5\u7684\u5efa\u7acb\u4e0e\u65ad\u5f00 <\/h5>\n<p> TCP\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u65ad\u5f00\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u63a7\u5236\u6d88\u606f\u6765\u5b8c\u6210\u7684&#xff0c;\u5177\u4f53\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u9636\u6bb5&#xff1a; <\/p>\n<ul>\n<li>  \u4e09\u6b21\u63e1\u624b  &#xff1a;\u5efa\u7acb\u8fde\u63a5\u65f6&#xff0c;\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u901a\u8fc7\u53d1\u9001SYN\u3001SYN-ACK\u548cACK\u4e09\u4e2a\u63a7\u5236\u5305\u6765\u540c\u6b65\u5e8f\u5217\u53f7\u548c\u786e\u8ba4\u53f7&#xff0c;\u5e76\u5efa\u7acb\u8fde\u63a5\u3002 <\/li>\n<li>  \u6570\u636e\u4f20\u8f93  &#xff1a;\u4e00\u65e6\u8fde\u63a5\u5efa\u7acb&#xff0c;\u6570\u636e\u5c31\u53ef\u4ee5\u5728\u8fd9\u4e24\u4e2a\u65b9\u5411\u4e0a\u4f20\u8f93\u3002 <\/li>\n<li>  \u56db\u6b21\u6325\u624b  &#xff1a;\u65ad\u5f00\u8fde\u63a5\u65f6&#xff0c;\u9700\u8981\u53d1\u9001\u56db\u4e2a\u63a7\u5236\u5305&#xff0c;\u5373\u4e00\u4e2aFIN\u548c\u4e00\u4e2aACK\u6765\u7ec8\u6b62\u6bcf\u4e00\u65b9\u5411\u7684\u4f20\u8f93&#xff0c;\u7136\u540e\u91ca\u653e\u8fde\u63a5\u3002 <\/li>\n<\/ul>\n<h4> 2.2 TCP\u5728\u6570\u636e\u4f20\u8f93\u4e2d\u7684\u4f5c\u7528 <\/h4>\n<h5> 2.2.1 \u6570\u636e\u6d41\u63a7\u5236\u4e0e\u53ef\u9760\u6027\u4fdd\u8bc1 <\/h5>\n<p> \u6570\u636e\u6d41\u63a7\u5236\u548c\u53ef\u9760\u6027\u4fdd\u8bc1\u662fTCP\u534f\u8bae\u7684\u6838\u5fc3\u529f\u80fd&#xff0c;\u5177\u4f53\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a; <\/p>\n<ul>\n<li>  \u5e8f\u5217\u53f7\u548c\u786e\u8ba4\u5e94\u7b54  &#xff1a;\u6bcf\u4e2aTCP\u6bb5\u90fd\u6709\u4e00\u4e2a\u5e8f\u5217\u53f7\u548c\u786e\u8ba4\u5e94\u7b54\u53f7&#xff0c;\u786e\u4fdd\u4e86\u6570\u636e\u7684\u987a\u5e8f\u548c\u5b8c\u6574\u6027\u3002 <\/li>\n<li>  \u91cd\u4f20\u673a\u5236  &#xff1a;\u5982\u679cTCP\u6bb5\u6ca1\u6709\u5728\u9884\u5b9a\u65f6\u95f4\u5185\u88ab\u63a5\u6536\u65b9\u786e\u8ba4&#xff0c;\u53d1\u9001\u65b9\u5c06\u91cd\u53d1\u8be5\u6bb5\u3002 <\/li>\n<li>  \u6821\u9a8c\u548c  &#xff1a;\u6bcf\u4e2aTCP\u6bb5\u90fd\u5305\u542b\u4e00\u4e2a\u6821\u9a8c\u548c\u5b57\u6bb5&#xff0c;\u7528\u4e8e\u68c0\u6d4b\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u4efb\u4f55\u635f\u574f\u3002 <\/li>\n<\/ul>\n<h5> 2.2.2 TCP\u4e0eUDP\u7684\u5bf9\u6bd4\u5206\u6790 <\/h5>\n<p> \u7528\u6237\u6570\u636e\u62a5\u534f\u8bae&#xff08;UDP&#xff09;\u662f\u4e00\u79cd\u65e0\u8fde\u63a5\u7684\u534f\u8bae&#xff0c;\u5176\u4e0eTCP\u5728\u591a\u4e2a\u65b9\u9762\u5b58\u5728\u663e\u8457\u5dee\u5f02&#xff0c;\u8fd9\u4e9b\u5dee\u5f02\u51b3\u5b9a\u4e86\u5404\u81ea\u7684\u5e94\u7528\u573a\u666f&#xff1a; <\/p>\n<ul>\n<li>  \u53ef\u9760\u6027  &#xff1a;TCP\u63d0\u4f9b\u4e86\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93&#xff0c;\u800cUDP\u4e0d\u4fdd\u8bc1\u6570\u636e\u7684\u53ef\u9760\u4f20\u8f93&#xff0c;\u56e0\u6b64\u53ef\u80fd\u4f1a\u4e22\u5931\u6216\u4e71\u5e8f\u3002 <\/li>\n<li>  \u6548\u7387  &#xff1a;UDP\u6ca1\u6709\u5efa\u7acb\u8fde\u63a5\u7684\u5f00\u9500&#xff0c;\u56e0\u6b64\u5728\u6548\u7387\u4e0a\u4f18\u4e8eTCP&#xff0c;\u9002\u7528\u4e8e\u5bf9\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u5e94\u7528\u3002 <\/li>\n<li>  \u8fde\u63a5\u72b6\u6001  &#xff1a;TCP\u7ef4\u62a4\u8fde\u63a5\u72b6\u6001\u4fe1\u606f&#xff0c;\u800cUDP\u5219\u6ca1\u6709\u3002 <\/li>\n<\/ul>\n<p> TCP\u4e0eUDP\u7684\u5bf9\u6bd4\u8868&#xff1a; <\/p>\n<table>\n<tr> \u7279\u6027  TCP  UDP <\/tr>\n<tbody>\n<tr>\n<td> \u8fde\u63a5 <\/td>\n<td> \u9762\u5411\u8fde\u63a5 <\/td>\n<td> \u65e0\u8fde\u63a5 <\/td>\n<\/tr>\n<tr>\n<td> \u53ef\u9760\u6027 <\/td>\n<td> \u53ef\u9760\u6570\u636e\u4f20\u8f93 <\/td>\n<td> \u4e0d\u4fdd\u8bc1\u53ef\u9760\u4f20\u8f93 <\/td>\n<\/tr>\n<tr>\n<td> \u6709\u5e8f\u6027 <\/td>\n<td> \u4fdd\u8bc1\u6570\u636e\u6709\u5e8f <\/td>\n<td> \u4e0d\u4fdd\u8bc1\u6570\u636e\u6709\u5e8f <\/td>\n<\/tr>\n<tr>\n<td> \u6548\u7387 <\/td>\n<td> \u8f83\u4f4e&#xff0c;\u6709\u989d\u5916\u5f00\u9500 <\/td>\n<td> \u8f83\u9ad8&#xff0c;\u65e0\u989d\u5916\u5f00\u9500 <\/td>\n<\/tr>\n<tr>\n<td> \u5e94\u7528\u573a\u666f <\/td>\n<td> \u6587\u4ef6\u4f20\u8f93\u3001\u90ae\u4ef6\u7b49 <\/td>\n<td> \u89c6\u9891\/\u97f3\u9891\u6d41\u3001\u5728\u7ebf\u6e38\u620f\u7b49 <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \u901a\u8fc7\u672c\u8282\u7684\u4ecb\u7ecd&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u7406\u89e3TCP\u534f\u8bae\u5728\u63d0\u4f9b\u7a33\u5b9a\u3001\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u670d\u52a1\u4e2d\u7684\u4f5c\u7528\u4e0e\u91cd\u8981\u6027\u3002\u5b83\u4e0d\u4ec5\u662f\u4e00\u79cd\u6280\u672f\u5b9e\u73b0&#xff0c;\u4e5f\u662f\u4e92\u8054\u7f51\u901a\u4fe1\u4e0d\u53ef\u6216\u7f3a\u7684\u57fa\u7840\u8bbe\u65bd\u4e4b\u4e00\u3002\u4e0b\u8282\u5c06\u6df1\u5165\u63a2\u8ba8\u5957\u63a5\u5b57\u7f16\u7a0b&#xff0c;\u8fd9\u662f\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u77f3\u3002 <\/p>\n<h2> 3. \u5957\u63a5\u5b57\u7f16\u7a0b\u57fa\u7840 <\/h2>\n<p> \u5957\u63a5\u5b57\u7f16\u7a0b\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3&#xff0c;\u5b83\u5141\u8bb8\u7a0b\u5e8f\u4e4b\u95f4\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u3002Linux\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5957\u63a5\u5b57API&#xff0c;\u4f9b\u5f00\u53d1\u8005\u4f7f\u7528\u3002\u5728\u672c\u7ae0\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u6df1\u5165\u4e86\u89e3Linux\u4e0b\u7684\u5957\u63a5\u5b57\u7f16\u7a0b&#xff0c;\u7279\u522b\u662fC&#043;&#043;\u5728\u5b9e\u73b0\u8fd9\u4e9bAPI\u65f6\u7684\u7279\u70b9\u548c\u5e94\u7528\u3002 <\/p>\n<h3> 3.1 Linux\u4e0b\u7684\u5957\u63a5\u5b57\u7f16\u7a0b\u6982\u8ff0 <\/h3>\n<p> \u5728\u6df1\u5165\u7ec6\u8282\u4e4b\u524d&#xff0c;\u9996\u5148\u4e86\u89e3Linux\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002 <\/p>\n<h4> 3.1.1 \u5957\u63a5\u5b57\u7684\u7c7b\u578b\u4e0e\u9009\u62e9 <\/h4>\n<p> \u5728Linux\u4e2d&#xff0c;\u5957\u63a5\u5b57\u4e3b\u8981\u6709\u4e09\u79cd\u7c7b\u578b&#xff1a;\u6d41\u5f0f\u5957\u63a5\u5b57&#xff08;SOCK_STREAM&#xff09;\u3001\u6570\u636e\u62a5\u5957\u63a5\u5b57&#xff08;SOCK_DGRAM&#xff09;\u548c\u539f\u59cb\u5957\u63a5\u5b57&#xff08;SOCK_RAW&#xff09;\u3002\u6d41\u5f0f\u5957\u63a5\u5b57\u57fa\u4e8eTCP\u534f\u8bae&#xff0c;\u63d0\u4f9b\u53ef\u9760\u7684\u9762\u5411\u8fde\u63a5\u7684\u670d\u52a1&#xff1b;\u6570\u636e\u62a5\u5957\u63a5\u5b57\u57fa\u4e8eUDP\u534f\u8bae&#xff0c;\u63d0\u4f9b\u65e0\u8fde\u63a5\u7684\u670d\u52a1&#xff0c;\u6570\u636e\u5305\u53ef\u80fd\u4e22\u5931\u6216\u91cd\u590d&#xff1b;\u539f\u59cb\u5957\u63a5\u5b57\u5141\u8bb8\u76f4\u63a5\u5bf9\u7f51\u7edc\u5c42\u534f\u8bae\u8fdb\u884c\u64cd\u4f5c&#xff0c;\u5e38\u7528\u4e8e\u5f00\u53d1\u65b0\u7684\u7f51\u7edc\u534f\u8bae\u6216\u8005\u5b9e\u73b0\u9632\u706b\u5899\u529f\u80fd\u3002 <\/p>\n<p> \u5728\u9009\u62e9\u5957\u63a5\u5b57\u7c7b\u578b\u65f6&#xff0c;\u9700\u8981\u8003\u8651\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u8981&#xff0c;\u4f8b\u5982\u662f\u5426\u9700\u8981\u9762\u5411\u8fde\u63a5\u7684\u901a\u4fe1&#xff0c;\u662f\u5426\u53ef\u4ee5\u63a5\u53d7\u6570\u636e\u5305\u7684\u4e22\u5931&#xff0c;\u4ee5\u53ca\u662f\u5426\u9700\u8981\u5bf9\u5e95\u5c42\u534f\u8bae\u8fdb\u884c\u63a7\u5236\u3002 <\/p>\n<h4> 3.1.2 \u7f51\u7edc\u5b57\u8282\u5e8f\u4e0e\u4e3b\u673a\u5b57\u8282\u5e8f <\/h4>\n<p> \u5728\u7f51\u7edc\u901a\u4fe1\u4e2d&#xff0c;\u5b57\u8282\u5e8f\u95ee\u9898\u662f\u4e00\u4e2a\u4e0d\u53ef\u5ffd\u89c6\u7684\u95ee\u9898\u3002\u4e0d\u540c\u7684\u786c\u4ef6\u5e73\u53f0\u53ef\u80fd\u91c7\u7528\u4e0d\u540c\u7684\u5b57\u8282\u5e8f&#xff0c;\u5373\u5927\u7aef\u5e8f&#xff08;big-endian&#xff09;\u6216\u5c0f\u7aef\u5e8f&#xff08;little-endian&#xff09;\u3002\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u7684\u4e00\u81f4\u6027&#xff0c;Linux\u4f7f\u7528\u6807\u51c6\u7684\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef\u5e8f&#xff09;\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002 <\/p>\n<p> \u5728C&#043;&#043;\u4e2d&#xff0c;\u53ef\u4ee5\u4f7f\u7528  htons()  ,  htonl()  ,  ntohs()  , \u548c  ntohl()  \u51fd\u6570\u6765\u8f6c\u6362\u4e3b\u673a\u5b57\u8282\u5e8f\u548c\u7f51\u7edc\u5b57\u8282\u5e8f\u3002\u4f8b\u5982&#xff0c;\u5f53\u51c6\u5907\u53d1\u9001\u4e00\u4e2a16\u4f4d\u7684\u6574\u6570\u65f6&#xff0c;\u9700\u8981\u5148\u7528  htons()  \u8f6c\u6362\u4e3a\u4e3b\u673a\u5230\u7f51\u7edc\u5b57\u8282\u5e8f\u3002 <\/p>\n<h3> 3.2 \u57fa\u4e8eC&#043;&#043;\u7684\u5957\u63a5\u5b57\u63a5\u53e3\u5b9e\u73b0 <\/h3>\n<p> \u672c\u5c0f\u8282\u6211\u4eec\u5c06\u63a2\u7d22\u5982\u4f55\u5728C&#043;&#043;\u4e2d\u5b9e\u73b0\u5957\u63a5\u5b57\u63a5\u53e3&#xff0c;\u5305\u62ec\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u5730\u5740&#xff0c;\u76d1\u542c\u8fde\u63a5&#xff0c;\u63a5\u53d7\u8fde\u63a5\u4ee5\u53ca\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u7b49\u3002 <\/p>\n<h4> 3.2.1 \u5957\u63a5\u5b57\u7684\u521b\u5efa\u4e0e\u7ed1\u5b9a <\/h4>\n<p> \u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\u5f88\u7b80\u5355&#xff0c;\u4f7f\u7528  socket()  \u7cfb\u7edf\u8c03\u7528\u5373\u53ef\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<\/p>\n<p>int socket(int domain, int type, int protocol);<\/p>\n<p> \u8fd9\u91cc&#xff0c;  domain  \u6307\u5b9a\u4e86\u534f\u8bae\u65cf&#xff0c;\u6bd4\u5982  AF_INET  &#xff08;IPv4&#xff09;\u6216  AF_INET6  &#xff08;IPv6&#xff09;&#xff1b;  type  \u6307\u5b9a\u4e86\u5957\u63a5\u5b57\u7c7b\u578b&#xff0c;\u4f8b\u5982  SOCK_STREAM  \u6216  SOCK_DGRAM  &#xff1b;  protocol  \u5141\u8bb8\u8fdb\u4e00\u6b65\u6307\u5b9a\u534f\u8bae\u7c7b\u578b&#xff0c;\u901a\u5e38\u5bf9\u4e8eTCP\u548cUDP&#xff0c;\u53ef\u4ee5\u8bbe\u4e3a0\u3002 <\/p>\n<p> \u521b\u5efa\u5957\u63a5\u5b57\u540e&#xff0c;\u9700\u8981\u5c06\u5176\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u4e0a&#xff0c;\u4f7f\u7528  bind()  \u51fd\u6570&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/p>\n<p> \u8fd9\u91cc\u7684  sockfd  \u662f  socket()  \u51fd\u6570\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;  addr  \u662f\u4e00\u4e2a\u6307\u5411  sockaddr  \u7ed3\u6784\u7684\u6307\u9488&#xff0c;\u5b83\u5305\u542b\u4e86\u8981\u7ed1\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u4fe1\u606f&#xff0c;  addrlen  \u662f  addr  \u7684\u5927\u5c0f\u3002 <\/p>\n<h4> 3.2.2 \u5957\u63a5\u5b57\u7684\u76d1\u542c\u3001\u63a5\u53d7\u4e0e\u53d1\u9001 <\/h4>\n<p> \u4e00\u65e6\u5957\u63a5\u5b57\u7ed1\u5b9a\u4e86\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u5c31\u53ef\u4ee5\u76d1\u542c\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u4e86\u3002\u4f7f\u7528  listen()  \u51fd\u6570\u6765\u542f\u7528\u76d1\u542c&#xff1a; <\/p>\n<p>int listen(int sockfd, int backlog);<\/p>\n<p>  sockfd  \u662f\u76d1\u542c\u5957\u63a5\u5b57\u7684\u63cf\u8ff0\u7b26&#xff0c;  backlog  \u662f\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u5728\u62d2\u7edd\u8fde\u63a5\u4e4b\u524d\u7684\u5df2\u5b8c\u6210\u8fde\u63a5\u7684\u961f\u5217\u957f\u5ea6\u3002 <\/p>\n<p> \u5bf9\u4e8eTCP\u5957\u63a5\u5b57&#xff0c;\u5ba2\u6237\u7aef\u8fde\u63a5\u901a\u8fc7  accept()  \u51fd\u6570\u6765\u63a5\u53d7&#xff1a; <\/p>\n<p>int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<\/p>\n<p> \u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7528\u4e8e\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u3002  addr  \u548c  addrlen  \u7528\u4e8e\u586b\u5145\u5ba2\u6237\u7aef\u5730\u5740\u4fe1\u606f\u3002 <\/p>\n<p> \u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536&#xff0c;\u5206\u522b\u901a\u8fc7  send()  \u548c  recv()  \u51fd\u6570\u6765\u5b8c\u6210&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<\/p>\n<p>ssize_t send(int sockfd, const void *buf, size_t len, int flags);<br \/>\nssize_t recv(int sockfd, void *buf, size_t len, int flags);<\/p>\n<p>  sockfd  \u662f\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u63cf\u8ff0\u7b26&#xff0c;  buf  \u662f\u6570\u636e\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;  len  \u662f\u5e0c\u671b\u53d1\u9001\u6216\u63a5\u6536\u7684\u5b57\u8282\u6570\u3002  flags  \u53ef\u4ee5\u63a7\u5236\u53d1\u9001\u6216\u63a5\u6536\u7684\u884c\u4e3a&#xff0c;\u6bd4\u5982  MSG_OOB  \u8868\u793a\u53d1\u9001\u6216\u63a5\u6536\u5e26\u5916\u6570\u636e\u3002 <\/p>\n<p> \u4e3a\u4e86\u6f14\u793a\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u57fa\u672c\u6b65\u9aa4&#xff0c;\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684TCP\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801&#xff0c;\u4f7f\u7528C&#043;&#043;\u5b9e\u73b0&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<\/p>\n<p>int main() {<br \/>\n    int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sockfd &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Unable to create socket&#034; &lt;&lt; std::endl;<br \/>\n        return -1;<br \/>\n    }<br \/>\n    sockaddr_in serverAddr;<br \/>\n    std::memset(&amp;serverAddr, 0, sizeof(serverAddr));<br \/>\n    serverAddr.sin_family &#061; AF_INET;<br \/>\n    serverAddr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    serverAddr.sin_port &#061; htons(8080);<\/p>\n<p>    if (bind(sockfd, (struct sockaddr*)&amp;serverAddr, sizeof(serverAddr)) &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Unable to bind&#034; &lt;&lt; std::endl;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    if (listen(sockfd, 3) &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Unable to listen&#034; &lt;&lt; std::endl;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    sockaddr_in clientAddr;<br \/>\n    socklen_t clientAddrLength &#061; sizeof(clientAddr);<br \/>\n    int newsockfd &#061; accept(sockfd, (struct sockaddr*)&amp;clientAddr, &amp;clientAddrLength);<br \/>\n    if (newsockfd &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Unable to accept&#034; &lt;&lt; std::endl;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    char buffer[1024];<br \/>\n    ssize_t readSize &#061; recv(newsockfd, buffer, sizeof(buffer), 0);<br \/>\n    if (readSize &gt; 0) {<br \/>\n        std::cout &lt;&lt; &#034;Received data: &#034; &lt;&lt; std::string(buffer, readSize) &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ Send response to the client<br \/>\n    const char *response &#061; &#034;Hello from server&#034;;<br \/>\n    send(newsockfd, response, strlen(response), 0);<br \/>\n    close(newsockfd);<br \/>\n    close(sockfd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u6b64\u4ee3\u7801\u6bb5\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aTCP\u6d41\u5f0f\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u4e86IP\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u5e76\u5f00\u59cb\u76d1\u542c\u8fde\u63a5\u3002\u5f53\u63a5\u6536\u5230\u4e00\u4e2a\u8fde\u63a5\u8bf7\u6c42\u65f6&#xff0c;\u5b83\u63a5\u53d7\u8fde\u63a5\u5e76\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e&#xff0c;\u7136\u540e\u53d1\u9001\u4e00\u4e2a\u54cd\u5e94\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef\u3002\u6700\u540e&#xff0c;\u5173\u95ed\u5957\u63a5\u5b57\u4ee5\u91ca\u653e\u8d44\u6e90\u3002 <\/p>\n<p> \u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u8bb2\u89e3&#xff0c;\u76f8\u4fe1\u8bfb\u8005\u5df2\u5bf9\u5957\u63a5\u5b57\u7f16\u7a0b\u6709\u4e86\u521d\u6b65\u7684\u7406\u89e3\u3002\u5728\u4e0b\u4e00\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u7d22\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6280\u672f\u6765\u5904\u7406\u5e76\u53d1\u8fde\u63a5&#xff0c;\u8fd9\u662f\u670d\u52a1\u5668\u7aef\u7f16\u7a0b\u7684\u4e00\u4e2a\u91cd\u8981\u4e3b\u9898\u3002 <\/p>\n<h2> 4. \u591a\u7ebf\u7a0b\u6280\u672f\u5e94\u7528 <\/h2>\n<p> \u591a\u7ebf\u7a0b\u6280\u672f\u662f\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u7528\u4e8e\u5e76\u884c\u5904\u7406\u4efb\u52a1\u7684\u673a\u5236&#xff0c;\u5b83\u5141\u8bb8\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u6027\u548c\u6027\u80fd\u3002\u5728\u670d\u52a1\u5668\u7f16\u7a0b\u4e2d&#xff0c;\u591a\u7ebf\u7a0b\u5c24\u5176\u91cd\u8981&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u6765\u81ea\u591a\u4e2a\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42&#xff0c;\u4ece\u800c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3002\u672c\u7ae0\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u591a\u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5&#xff0c;\u521b\u5efa\u65b9\u6cd5&#xff0c;\u4ee5\u53ca\u5728\u670d\u52a1\u5668\u7f16\u7a0b\u4e2d\u5982\u4f55\u5e94\u7528\u591a\u7ebf\u7a0b\u6280\u672f\u3002 <\/p>\n<h3> 4.1 \u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u521b\u5efa <\/h3>\n<h4> 4.1.1 \u7ebf\u7a0b\u4e0e\u8fdb\u7a0b\u7684\u533a\u522b <\/h4>\n<p> \u5728\u6df1\u5165\u63a2\u8ba8\u591a\u7ebf\u7a0b\u6280\u672f\u4e4b\u524d&#xff0c;\u9996\u5148\u9700\u8981\u7406\u89e3\u7ebf\u7a0b\u4e0e\u8fdb\u7a0b\u4e4b\u95f4\u7684\u533a\u522b\u3002\u8fdb\u7a0b\u662f\u4e00\u4e2a\u6267\u884c\u4e2d\u7684\u7a0b\u5e8f\u5b9e\u4f8b&#xff0c;\u662f\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u8d44\u6e90\u5206\u914d\u548c\u8c03\u5ea6\u7684\u4e00\u4e2a\u72ec\u7acb\u5355\u4f4d\u3002\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u5982\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5185\u5b58\u3002\u800c\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u5185\u90e8\u7684\u4e00\u4e2a\u6267\u884c\u5355\u5143&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\u3002\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u548c\u5176\u4ed6\u8d44\u6e90&#xff0c;\u56e0\u6b64\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u9ad8\u6548\u3002 <\/p>\n<p> \u7ebf\u7a0b\u76f8\u8f83\u4e8e\u8fdb\u7a0b\u6709\u4ee5\u4e0b\u51e0\u70b9\u4f18\u52bf&#xff1a; <\/p>\n<ul>\n<li> \u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u6bd4\u8fdb\u7a0b\u5c0f\u3002 <\/li>\n<li> \u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u7b80\u5355&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u3002 <\/li>\n<li> \u7ebf\u7a0b\u53ef\u4ee5\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf&#xff0c;\u5b9e\u73b0\u5e76\u884c\u5904\u7406\u3002 <\/li>\n<\/ul>\n<h4> 4.1.2 C&#043;&#043;\u4e2d\u7684\u7ebf\u7a0b\u521b\u5efa\u4e0e\u7ba1\u7406 <\/h4>\n<p> \u4eceC&#043;&#043;11\u5f00\u59cb&#xff0c;C&#043;&#043;\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5bf9\u7ebf\u7a0b\u7684\u652f\u6301\u3002  std::thread  \u7c7b\u4f4d\u4e8e  &lt;thread&gt;  \u5934\u6587\u4ef6\u4e2d&#xff0c;\u662fC&#043;&#043;\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u7684\u4e3b\u8981\u65b9\u5f0f\u3002 <\/p>\n<p> \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528  std::thread  \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<\/p>\n<p>void print_number(int num) {<br \/>\n    std::cout &lt;&lt; &#034;Number is &#034; &lt;&lt; num &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int my_number &#061; 10;<br \/>\n    std::thread my_thread(print_number, my_number);<br \/>\n    my_thread.join(); \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b&#xff0c;\u8fd0\u884c  print_number  \u51fd\u6570&#xff0c;\u5e76\u5c06  my_number  \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u3002  my_thread.join()  \u8c03\u7528\u786e\u4fdd\u4e3b\u51fd\u6570\u4f1a\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f&#xff0c;\u8fd9\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u64cd\u4f5c&#xff0c;\u4fdd\u8bc1\u7ebf\u7a0b\u7684\u5b89\u5168\u540c\u6b65\u3002 <\/p>\n<p> \u521b\u5efa\u7ebf\u7a0b\u65f6&#xff0c;\u8981\u6ce8\u610f\u5f02\u5e38\u5b89\u5168\u6027&#xff0c;\u56e0\u4e3a\u7ebf\u7a0b\u5728\u6784\u9020\u51fd\u6570\u4e2d\u542f\u52a8&#xff0c;\u6240\u4ee5\u9700\u8981\u786e\u4fdd\u5f02\u5e38\u4e0d\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u9732\u3002\u53ef\u4ee5\u4f7f\u7528  try&#8230;catch  \u5757\u6355\u83b7\u5f02\u5e38&#xff0c;\u5e76\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u5b89\u5168\u5730\u6e05\u7406\u8d44\u6e90\u3002 <\/p>\n<h3> 4.2 \u591a\u7ebf\u7a0b\u5728\u670d\u52a1\u5668\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528 <\/h3>\n<h4> 4.2.1 \u7ebf\u7a0b\u6c60\u7684\u6982\u5ff5\u4e0e\u5b9e\u73b0 <\/h4>\n<p> \u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u7684\u6280\u672f&#xff0c;\u5b83\u9884\u5148\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b&#xff0c;\u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d&#xff0c;\u7531\u8fd9\u4e9b\u7ebf\u7a0b\u4f9d\u6b21\u6267\u884c\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u51cf\u5c11\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90\u3002 <\/p>\n<p> \u5b9e\u73b0\u7ebf\u7a0b\u6c60\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u7ec4\u4ef6&#xff1a; <\/p>\n<ul>\n<li> \u4efb\u52a1\u961f\u5217&#xff1a;\u7528\u4e8e\u5b58\u653e\u5f85\u5904\u7406\u4efb\u52a1\u3002 <\/li>\n<li> \u5de5\u4f5c\u7ebf\u7a0b&#xff1a;\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\u3002 <\/li>\n<li> \u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668&#xff1a;\u8d1f\u8d23\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u4efb\u52a1\u5206\u914d\u548c\u7ebf\u7a0b\u56de\u6536\u3002 <\/li>\n<\/ul>\n<p> \u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4f2a\u4ee3\u7801&#xff1a; <\/p>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;condition_variable&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;future&gt;<\/p>\n<p>class ThreadPool {<br \/>\npublic:<br \/>\n    using Task &#061; std::function&lt;void()&gt;;<\/p>\n<p>    explicit ThreadPool(size_t);<\/p>\n<p>    template&lt;class F, class&#8230; Args&gt;<br \/>\n    auto enqueue(F&amp;&amp; f, Args&amp;&amp;&#8230; args)<br \/>\n        -&gt; std::future&lt;typename std::result_of&lt;F(Args&#8230;)&gt;::type&gt;;<\/p>\n<p>    ~ThreadPool();<\/p>\n<p>private:<br \/>\n    \/\/ \u9700\u8981\u5b9e\u73b0\u7684\u4efb\u52a1\u961f\u5217\u3001\u5de5\u4f5c\u7ebf\u7a0b\u7684\u7ba1\u7406\u7b49<br \/>\n};<\/p>\n<h4> 4.2.2 \u7ebf\u7a0b\u540c\u6b65\u4e0e\u6570\u636e\u5171\u4eab\u95ee\u9898 <\/h4>\n<p> \u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u4e3b\u8981\u95ee\u9898\u662f\u7ebf\u7a0b\u540c\u6b65\u548c\u6570\u636e\u5171\u4eab\u3002\u7531\u4e8e\u7ebf\u7a0b\u5171\u4eab\u5730\u5740\u7a7a\u95f4&#xff0c;\u5982\u679c\u6ca1\u6709\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236&#xff0c;\u90a3\u4e48\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u4fee\u6539\u540c\u4e00\u6570\u636e&#xff0c;\u5bfc\u81f4\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\u3002 <\/p>\n<p> \u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u4e92\u65a5\u9501&#xff08;  std::mutex  &#xff09;\u6765\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002\u6b64\u5916&#xff0c;C&#043;&#043;11\u4e2d\u5f15\u5165\u7684  std::atomic  \u7c7b\u578b\u4e3a\u539f\u5b50\u64cd\u4f5c\u63d0\u4f9b\u652f\u6301&#xff0c;\u5141\u8bb8\u8fdb\u884c\u4e0d\u9700\u8981\u4e92\u65a5\u9501\u7684\u65e0\u9501\u540c\u6b65\u3002 <\/p>\n<p> \u8003\u8651\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\u4f8b\u5b50&#xff0c;\u591a\u4e2a\u7ebf\u7a0b\u9700\u8981\u5bf9\u540c\u4e00\u4e2a\u8ba1\u6570\u5668\u589e\u52a0&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<\/p>\n<p>std::mutex mtx;<br \/>\nint counter &#061; 0;<\/p>\n<p>void increase_counter() {<br \/>\n    for (int i &#061; 0; i &lt; 1000; &#043;&#043;i) {<br \/>\n        mtx.lock();<br \/>\n        &#043;&#043;counter;<br \/>\n        mtx.unlock();<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::vector&lt;std::thread&gt; threads;<br \/>\n    for (int i &#061; 0; i &lt; 10; &#043;&#043;i) {<br \/>\n        threads.emplace_back(increase_counter);<br \/>\n    }<br \/>\n    for (auto&amp; t : threads) {<br \/>\n        t.join();<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;Counter: &#034; &lt;&lt; counter &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;\u6211\u4eec\u901a\u8fc7\u4e92\u65a5\u9501  mtx  \u6765\u4fdd\u62a4\u5bf9\u5171\u4eab\u8d44\u6e90  counter  \u7684\u8bbf\u95ee&#xff0c;\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002 <\/p>\n<p> \u7ebf\u7a0b\u540c\u6b65\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u673a\u5236&#xff0c;\u4f8b\u5982\u4fe1\u53f7\u91cf&#xff08;  std::counting_semaphore  &#xff09;&#xff0c;\u4e8b\u4ef6&#xff08;  std::condition_variable  &#xff09;\u7b49\u6765\u5b9e\u73b0\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5e94\u5f53\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u540c\u6b65\u673a\u5236\u3002 <\/p>\n<p> \u901a\u8fc7\u672c\u7ae0\u8282\u7684\u4ecb\u7ecd&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86\u591a\u7ebf\u7a0b\u6280\u672f\u5728\u670d\u52a1\u5668\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528&#xff0c;\u53ca\u5176\u5728\u63d0\u5347\u5e76\u53d1\u6027\u80fd\u65b9\u9762\u7684\u4f18\u52bf\u3002\u4e0b\u4e00\u7ae0\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u547d\u4ee4\u884c\u53c2\u6570\u7684\u89e3\u6790\u4e0e\u5904\u7406&#xff0c;\u8fd9\u5bf9\u4e8e\u5f00\u53d1\u53ef\u914d\u7f6e\u7684\u547d\u4ee4\u884c\u5de5\u5177\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<h2> 5. \u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012 <\/h2>\n<p> \u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d&#xff0c;\u547d\u4ee4\u884c\u5de5\u5177\u662f\u5e38\u89c1\u7684\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u5f0f&#xff0c;\u800c\u8fd9\u4e9b\u5de5\u5177\u5f80\u5f80\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u8fdb\u884c\u914d\u7f6e\u3002\u53c2\u6570\u4f20\u9012\u673a\u5236\u5141\u8bb8\u7528\u6237\u5728\u6267\u884c\u7a0b\u5e8f\u65f6&#xff0c;\u901a\u8fc7\u8f93\u5165\u4e00\u7cfb\u5217\u53c2\u6570\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u884c\u4e3a&#xff0c;\u8fd9\u5bf9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u914d\u7f6e\u6027\u81f3\u5173\u91cd\u8981\u3002\u5728\u672c\u7ae0\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u6df1\u5165\u4e86\u89e3\u547d\u4ee4\u884c\u53c2\u6570\u7684\u89e3\u6790\u548c\u5904\u7406\u65b9\u5f0f&#xff0c;\u540c\u65f6\u63a2\u7d22\u5982\u4f55\u5728C&#043;&#043;\u4e2d\u5b9e\u73b0\u6709\u6548\u7684\u53c2\u6570\u4f20\u9012\u548c\u9a8c\u8bc1\u3002 <\/p>\n<h3> 5.1 \u547d\u4ee4\u884c\u53c2\u6570\u7684\u89e3\u6790 <\/h3>\n<h4> 5.1.1 \u53c2\u6570\u89e3\u6790\u7684\u610f\u4e49\u4e0e\u65b9\u6cd5 <\/h4>\n<p> \u547d\u4ee4\u884c\u53c2\u6570\u662f\u7a0b\u5e8f\u542f\u52a8\u65f6\u4ece\u5916\u90e8\u4f20\u5165\u7684\u53c2\u6570&#xff0c;\u5b83\u4eec\u53ef\u4ee5\u662f\u9009\u9879\u6807\u5fd7\u3001\u6570\u636e\u503c\u6216\u8005\u5176\u4ed6\u63a7\u5236\u7a0b\u5e8f\u8fd0\u884c\u7684\u6307\u4ee4\u3002\u53c2\u6570\u89e3\u6790\u7684\u76ee\u7684\u5728\u4e8e\u5c06\u8fd9\u4e9b\u4f20\u5165\u7684\u53c2\u6570\u8f6c\u6362\u4e3a\u7a0b\u5e8f\u5185\u90e8\u53ef\u4ee5\u7406\u89e3\u548c\u64cd\u4f5c\u7684\u6570\u636e\u3002 <\/p>\n<p> \u5e38\u89c1\u7684\u53c2\u6570\u89e3\u6790\u65b9\u6cd5\u5305\u62ec&#xff1a; <\/p>\n<ul>\n<li>  \u4f4d\u7f6e\u53c2\u6570  &#xff1a;\u6309\u987a\u5e8f\u89e3\u6790\u547d\u4ee4\u884c\u8f93\u5165\u7684\u53c2\u6570\u3002 <\/li>\n<li>  \u6807\u5fd7\u53c2\u6570  &#xff1a;\u4f7f\u7528\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u7684\u524d\u7f00&#xff0c;\u5982  -h  \u6216  &#8211;help  \u3002 <\/li>\n<li>  \u9009\u9879\u53c2\u6570  &#xff1a;\u4e0e\u6807\u5fd7\u53c2\u6570\u7c7b\u4f3c&#xff0c;\u4f46\u662f\u901a\u5e38\u4f1a\u8ddf\u4e00\u4e2a\u503c&#xff0c;\u4f8b\u5982  -d \/var\/log  \u3002 <\/li>\n<\/ul>\n<p> C&#043;&#043;\u4e2d\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684  main  \u51fd\u6570&#xff0c;\u5b83\u80fd\u591f\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570&#xff1a; <\/p>\n<p>int main(int argc, char* argv[]) {<br \/>\n    \/\/ argc \u8868\u793a\u53c2\u6570\u4e2a\u6570&#xff0c;argv \u662f\u53c2\u6570\u5b57\u7b26\u4e32\u6570\u7ec4<br \/>\n    \/\/ \u901a\u8fc7\u904d\u5386 argv \u6570\u7ec4\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u4f20\u5165\u7684\u53c2\u6570<br \/>\n}<\/p>\n<h4> 5.1.2 C&#043;&#043;\u4e2d\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5de5\u5177 <\/h4>\n<p> C&#043;&#043;\u63d0\u4f9b\u4e86\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6d41&#xff08;iostream&#xff09;\u548c\u6807\u51c6\u5e93&#xff08;Standard Library&#xff09;\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u9664\u4e86\u76f4\u63a5\u4f7f\u7528  main  \u51fd\u6570\u7684\u53c2\u6570\u4e4b\u5916&#xff0c;\u8fd8\u53ef\u4ee5\u4f7f\u7528  &lt;getopt.h&gt;  \u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u5982  Boost.Program_options  \u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u53c2\u6570\u89e3\u6790\u3002 <\/p>\n<p> \u4e0b\u9762\u5c55\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50&#xff0c;\u4f7f\u7528  main  \u51fd\u6570\u7684\u53c2\u6570&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main(int argc, char* argv[]) {<br \/>\n    if (argc &lt; 2) {<br \/>\n        std::cout &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; [options] &lt;file&gt;&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    std::string option;<br \/>\n    for (int i &#061; 1; i &lt; argc; &#043;&#043;i) {<br \/>\n        if (argv[i][0] &#061;&#061; &#039;-&#039;) {<br \/>\n            option &#061; argv[i];<br \/>\n        } else {<br \/>\n            std::cout &lt;&lt; &#034;Processing file: &#034; &lt;&lt; argv[i] &lt;&lt; std::endl;<br \/>\n            \/\/ \u5904\u7406\u6587\u4ef6<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u6839\u636e\u9009\u9879\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u903b\u8f91<br \/>\n    if (option &#061;&#061; &#034;-v&#034;) {<br \/>\n        std::cout &lt;&lt; &#034;Verbosity mode enabled.&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u9996\u5148\u68c0\u67e5\u8f93\u5165\u53c2\u6570\u7684\u6570\u91cf\u662f\u5426\u7b26\u5408\u8981\u6c42&#xff0c;\u7136\u540e\u904d\u5386\u6bcf\u4e2a\u53c2\u6570\u3002\u5982\u679c\u53c2\u6570\u4ee5  &#8211;  \u5f00\u5934&#xff0c;\u6211\u4eec\u5c06\u5176\u8bc6\u522b\u4e3a\u4e00\u4e2a\u9009\u9879&#xff0c;\u5e76\u6839\u636e\u4e0d\u540c\u7684\u9009\u9879\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u3002 <\/p>\n<h3> 5.2 \u53c2\u6570\u6709\u6548\u6027\u9a8c\u8bc1\u4e0e\u5904\u7406 <\/h3>\n<h4> 5.2.1 \u8f93\u5165\u9a8c\u8bc1\u7684\u91cd\u8981\u6027 <\/h4>\n<p> \u53c2\u6570\u6709\u6548\u6027\u9a8c\u8bc1\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u548c\u6570\u636e\u6b63\u786e\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5982\u679c\u53c2\u6570\u5904\u7406\u4e0d\u5f53&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u4ea7\u751f\u4e0d\u53ef\u9884\u671f\u7684\u884c\u4e3a\u3002\u56e0\u6b64&#xff0c;\u5bf9\u4f20\u5165\u7684\u53c2\u6570\u8fdb\u884c\u9a8c\u8bc1\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002 <\/p>\n<p> \u9a8c\u8bc1\u901a\u5e38\u5305\u62ec&#xff1a; <\/p>\n<ul>\n<li> \u786e\u8ba4\u53c2\u6570\u7684\u6570\u91cf\u662f\u5426\u6b63\u786e <\/li>\n<li> \u6821\u9a8c\u53c2\u6570\u7c7b\u578b\u662f\u5426\u5408\u6cd5 <\/li>\n<li> \u68c0\u67e5\u53c2\u6570\u503c\u662f\u5426\u5728\u5408\u7406\u7684\u8303\u56f4\u5185 <\/li>\n<li> \u9a8c\u8bc1\u9009\u9879\u6807\u5fd7\u662f\u5426\u6709\u6548 <\/li>\n<\/ul>\n<h4> 5.2.2 \u53c2\u6570\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef\u4e0e\u5e94\u5bf9\u7b56\u7565 <\/h4>\n<p> \u5728\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef&#xff0c;\u4f8b\u5982&#xff1a; <\/p>\n<ul>\n<li> \u53c2\u6570\u4e0d\u8db3\u6216\u8fc7\u591a <\/li>\n<li> \u65e0\u6548\u7684\u53c2\u6570\u6807\u5fd7\u6216\u503c <\/li>\n<li> \u4f9d\u8d56\u5916\u90e8\u6587\u4ef6\u6216\u8d44\u6e90\u65f6\u7684\u7f3a\u5931\u6587\u4ef6\u9519\u8bef <\/li>\n<\/ul>\n<p> \u9488\u5bf9\u8fd9\u4e9b\u95ee\u9898&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u7b56\u7565\u6765\u5904\u7406&#xff1a; <\/p>\n<ul>\n<li> \u63d0\u4f9b\u8be6\u7ec6\u7684\u4f7f\u7528\u8bf4\u660e&#xff0c;\u5e2e\u52a9\u7528\u6237\u6b63\u786e\u8f93\u5165\u53c2\u6570\u3002 <\/li>\n<li> \u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u9519\u8bef\u5904\u7406\u903b\u8f91&#xff0c;\u5982\u4f7f\u7528try-catch\u8bed\u53e5\u5757\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002 <\/li>\n<li> \u4e3a\u5173\u952e\u51fd\u6570\u8c03\u7528\u63d0\u4f9b\u9ed8\u8ba4\u53c2\u6570\u503c\u6216\u5408\u7406\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002 <\/li>\n<\/ul>\n<p> \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u53c2\u6570\u89e3\u6790\u793a\u4f8b&#xff0c;\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u5e93  Boost.Program_options  &#xff1a; <\/p>\n<p>#include &lt;boost\/program_options.hpp&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>namespace po &#061; boost::program_options;<\/p>\n<p>int main(int argc, char* argv[]) {<br \/>\n    try {<br \/>\n        std::string input_file;<br \/>\n        int line_count;<br \/>\n        po::options_description desc(&#034;Allowed options&#034;);<br \/>\n        desc.add_options()<br \/>\n            (&#034;help,h&#034;, &#034;produce help message&#034;)<br \/>\n            (&#034;input-file,i&#034;, po::value&lt;std::string&gt;(&amp;input_file), &#034;set input file&#034;)<br \/>\n            (&#034;lines,l&#034;, po::value&lt;int&gt;(&amp;line_count)-&gt;default_value(10), &#034;number of lines to display&#034;)<br \/>\n        ;<\/p>\n<p>        po::variables_map vm;<br \/>\n        po::store(po::parse_command_line(argc, argv, desc), vm);<br \/>\n        po::notify(vm);<\/p>\n<p>        if (vm.count(&#034;help&#034;)) {<br \/>\n            std::cout &lt;&lt; desc &lt;&lt; &#034;\\\\n&#034;;<br \/>\n            return 1;<br \/>\n        }<\/p>\n<p>        if (vm.count(&#034;input-file&#034;)) {<br \/>\n            std::cout &lt;&lt; &#034;Input file: &#034; &lt;&lt; input_file &lt;&lt; &#034;\\\\n&#034;;<br \/>\n        } else {<br \/>\n            std::cout &lt;&lt; &#034;No input file provided.\\\\n&#034;;<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;Number of lines to display: &#034; &lt;&lt; line_count &lt;&lt; &#034;\\\\n&#034;;<br \/>\n    } catch(std::exception&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;Error: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    } catch(&#8230;) {<br \/>\n        std::cerr &lt;&lt; &#034;Unknown error!&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;\u6211\u4eec\u4f7f\u7528\u4e86Boost\u5e93\u7684  program_options  \u6a21\u5757\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u7a0b\u5e8f\u4e0d\u4ec5\u53ef\u4ee5\u6253\u5370\u51fa\u5e2e\u52a9\u4fe1\u606f&#xff0c;\u8fd8\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u548c\u9700\u8981\u5c55\u793a\u7684\u884c\u6570&#xff0c;\u5e76\u4e14\u80fd\u591f\u6355\u83b7\u5e76\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\u3002 <\/p>\n<p> \u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9&#xff0c;\u672c\u7ae0\u8282\u5c55\u793a\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u7684\u5404\u4e2a\u65b9\u9762&#xff0c;\u4ece\u57fa\u672c\u7684\u53c2\u6570\u89e3\u6790\u65b9\u6cd5&#xff0c;\u5230\u590d\u6742\u7684\u53c2\u6570\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406&#xff0c;\u4e3aC&#043;&#043;\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u9762\u7684\u53c2\u6570\u5904\u7406\u6307\u5357\u3002 <\/p>\n<h2> 6. \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u6d41\u7a0b <\/h2>\n<p> \u5728\u73b0\u4ee3\u7f51\u7edc\u5e94\u7528\u4e2d&#xff0c;\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684\u901a\u4fe1\u662f\u6784\u6210\u7f51\u7edc\u670d\u52a1\u7684\u6838\u5fc3\u90e8\u5206\u3002\u672c\u7ae0\u8282\u5c06\u6df1\u5165\u63a2\u8ba8\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u4e4b\u95f4\u4ea4\u4e92\u7684\u6d41\u7a0b&#xff0c;\u5e76\u8be6\u7ec6\u89e3\u6790\u5982\u4f55\u8bbe\u8ba1\u548c\u5b9e\u73b0\u670d\u52a1\u5668\u7aef\u4e0e\u5ba2\u6237\u7aef&#xff0c;\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u901a\u4fe1\u6d41\u7a0b\u3002 <\/p>\n<h3> 6.1 \u670d\u52a1\u5668\u7aef\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 <\/h3>\n<p> \u670d\u52a1\u5668\u7684\u4e3b\u8981\u4efb\u52a1\u662f\u54cd\u5e94\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42&#xff0c;\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u670d\u52a1\u3002\u4e00\u4e2a\u670d\u52a1\u5668\u7aef\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u90e8\u5206&#xff1a; <\/p>\n<h4> 6.1.1 \u670d\u52a1\u5668\u7684\u4e3b\u5faa\u73af\u903b\u8f91 <\/h4>\n<p> \u670d\u52a1\u5668\u7684\u4e3b\u5faa\u73af\u903b\u8f91\u662f\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u6838\u5fc3&#xff0c;\u8d1f\u8d23\u76d1\u542c\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5e76\u6839\u636e\u4e0d\u540c\u7684\u8bf7\u6c42\u7c7b\u578b\u5206\u53d1\u6267\u884c\u76f8\u5e94\u7684\u670d\u52a1\u3002\u4ee5\u4e0b\u662f\u8bbe\u8ba1\u4e3b\u5faa\u73af\u903b\u8f91\u7684\u57fa\u672c\u6b65\u9aa4&#xff1a; <\/p>\n<li>  \u521d\u59cb\u5316\u670d\u52a1\u7aef\u5957\u63a5\u5b57  &#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2a\u672c\u5730IP\u5730\u5740\u548c\u7aef\u53e3\u4e0a&#xff0c;\u7136\u540e\u8fdb\u884c\u76d1\u542c\u3002 <\/li>\n<li>  \u5faa\u73af\u7b49\u5f85\u8fde\u63a5\u8bf7\u6c42  &#xff1a;\u4f7f\u7528  accept()  \u51fd\u6570\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002 <\/li>\n<li>  \u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42  &#xff1a;\u4e00\u65e6\u63a5\u53d7\u5230\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u670d\u52a1\u5668\u4f1a\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u6765\u5904\u7406\u8be5\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002 <\/li>\n<li>  \u4e3b\u5faa\u73af\u8fed\u4ee3  &#xff1a;\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u4e3b\u5faa\u73af\u7ee7\u7eed\u76d1\u542c\u5176\u4ed6\u8fde\u63a5\u8bf7\u6c42\u3002 <\/li>\n<p>  \u793a\u4f8b\u4ee3\u7801  \u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u5668\u7aef\u4e3b\u5faa\u73af\u7684\u5b9e\u73b0&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>int main() {<br \/>\n    int server_fd, new_socket;<br \/>\n    struct sockaddr_in address;<br \/>\n    int opt &#061; 1;<br \/>\n    int addrlen &#061; sizeof(address);<br \/>\n    \/\/ \u521b\u5efasocket\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    if ((server_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &#061;&#061; 0) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u7ed1\u5b9asocket\u5230\u7aef\u53e38080<br \/>\n    address.sin_family &#061; AF_INET;<br \/>\n    address.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    address.sin_port &#061; htons(8080);<\/p>\n<p>    if (bind(server_fd, (struct sockaddr *)&amp;address, sizeof(address)) &lt; 0) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u76d1\u542c\u7aef\u53e3<br \/>\n    if (listen(server_fd, 3) &lt; 0) {<br \/>\n        perror(&#034;listen&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    while(true) {<br \/>\n        std::cout &lt;&lt; &#034;Waiting for connections&#8230;&#034; &lt;&lt; std::endl;<\/p>\n<p>        if ((new_socket &#061; accept(server_fd, (struct sockaddr *)&amp;address, (socklen_t*)&amp;addrlen)) &lt; 0) {<br \/>\n            perror(&#034;accept&#034;);<br \/>\n            exit(EXIT_FAILURE);<br \/>\n        }<\/p>\n<p>        \/\/ \u751f\u6210\u65b0\u7ebf\u7a0b\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n        std::thread(handle_client, new_socket).detach();<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4> 6.1.2 \u5904\u7406\u5e76\u53d1\u8fde\u63a5\u7684\u7b56\u7565 <\/h4>\n<p> \u670d\u52a1\u5668\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u4e3a\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807&#xff0c;\u6709\u51e0\u79cd\u7b56\u7565\u53ef\u4f9b\u9009\u62e9&#xff1a; <\/p>\n<li>  \u591a\u7ebf\u7a0b\u6a21\u578b  &#xff1a;\u4e3a\u6bcf\u4e2a\u5ba2\u6237\u7aef\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u9ad8\u5e76\u53d1\u4e14\u5355\u4e2a\u8fde\u63a5\u5904\u7406\u65f6\u95f4\u8f83\u957f\u7684\u60c5\u51b5\u3002 <\/li>\n<li>  \u591a\u8fdb\u7a0b\u6a21\u578b  &#xff1a;\u7c7b\u4f3c\u4e8e\u591a\u7ebf\u7a0b\u6a21\u578b&#xff0c;\u4f46\u4f7f\u7528\u8fdb\u7a0b\u800c\u975e\u7ebf\u7a0b&#xff0c;\u66f4\u52a0\u7a33\u5b9a\u548c\u5b89\u5168&#xff0c;\u6d88\u8017\u8d44\u6e90\u66f4\u591a\u3002 <\/li>\n<li>  \u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b  &#xff1a;\u4f7f\u7528\u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u5982epoll&#xff0c;\u5728Linux\u4e0a\u6548\u7387\u66f4\u9ad8&#xff0c;\u9002\u5408\u5927\u91cf\u5feb\u901f\u7684\u77ed\u8fde\u63a5\u5904\u7406\u3002 <\/li>\n<p> \u5728\u5b9e\u73b0\u5e76\u53d1\u8fde\u63a5\u65f6&#xff0c;\u670d\u52a1\u5668\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20&#xff1a; <\/p>\n<ul>\n<li>  \u8d44\u6e90\u7ba1\u7406  &#xff1a;\u786e\u4fdd\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u8d44\u6e90&#xff0c;\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002 <\/li>\n<li>  \u540c\u6b65\u673a\u5236  &#xff1a;\u5408\u7406\u4f7f\u7528\u4e92\u65a5\u9501\u7b49\u540c\u6b65\u673a\u5236&#xff0c;\u9632\u6b62\u6570\u636e\u7ade\u4e89\u548c\u6761\u4ef6\u7ade\u4e89\u3002 <\/li>\n<li>  \u8d1f\u8f7d\u5747\u8861  &#xff1a;\u5408\u7406\u5206\u914d\u4efb\u52a1&#xff0c;\u907f\u514d\u8fc7\u8f7d\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u5bfc\u81f4\u670d\u52a1\u5668\u6027\u80fd\u4e0b\u964d\u3002 <\/li>\n<\/ul>\n<h3> 6.2 \u5ba2\u6237\u7aef\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 <\/h3>\n<p> \u5ba2\u6237\u7aef\u662f\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5e76\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u4ea4\u4e92\u7684\u7a0b\u5e8f\u3002\u4e00\u4e2a\u826f\u597d\u7684\u5ba2\u6237\u7aef\u8bbe\u8ba1\u9700\u8981\u5173\u6ce8\u8fde\u63a5\u7684\u5efa\u7acb\u3001\u6570\u636e\u7684\u53d1\u9001\u4e0e\u63a5\u6536\u3001\u9519\u8bef\u5904\u7406\u7b49\u65b9\u9762\u3002 <\/p>\n<h4> 6.2.1 \u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u4e0e\u4ea4\u4e92\u6d41\u7a0b <\/h4>\n<p> \u5ba2\u6237\u7aef\u7684\u8bbe\u8ba1\u6d41\u7a0b\u53ef\u6982\u62ec\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4&#xff1a; <\/p>\n<li>  \u5efa\u7acb\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5  &#xff1a;\u5ba2\u6237\u7aef\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57&#xff0c;\u5e76\u5c1d\u8bd5\u8fde\u63a5\u5230\u670d\u52a1\u5668\u7684\u6307\u5b9aIP\u5730\u5740\u548c\u7aef\u53e3\u4e0a\u3002 <\/li>\n<li>  \u53d1\u9001\u8bf7\u6c42  &#xff1a;\u901a\u8fc7\u5957\u63a5\u5b57\u53d1\u9001\u8bf7\u6c42\u7ed9\u670d\u52a1\u5668\u3002 <\/li>\n<li>  \u63a5\u6536\u54cd\u5e94  &#xff1a;\u63a5\u6536\u5e76\u5904\u7406\u6765\u81ea\u670d\u52a1\u5668\u7684\u54cd\u5e94\u6570\u636e\u3002 <\/li>\n<li>  \u5173\u95ed\u8fde\u63a5  &#xff1a;\u6570\u636e\u4ea4\u4e92\u5b8c\u6210\u540e&#xff0c;\u5173\u95ed\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u8fde\u63a5\u3002 <\/li>\n<p>  \u793a\u4f8b\u4ee3\u7801  \u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\u7684\u5b9e\u73b0&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>int main() {<br \/>\n    int sock &#061; 0;<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    char buffer[1024] &#061; {0};<\/p>\n<p>    \/\/ \u521b\u5efasocket<br \/>\n    if ((sock &#061; socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {<br \/>\n        std::cout &lt;&lt; &#034;\\\\n Socket creation error \\\\n&#034;;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    serv_addr.sin_family &#061; AF_INET;<br \/>\n    serv_addr.sin_port &#061; htons(8080);<\/p>\n<p>    \/\/ \u5c06IPv4\u548cIPv6\u5730\u5740\u4ece\u6587\u672c\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f<br \/>\n    if(inet_pton(AF_INET, &#034;127.0.0.1&#034;, &amp;serv_addr.sin_addr) &lt;&#061; 0) {<br \/>\n        std::cout &lt;&lt; &#034;\\\\nInvalid address\/ Address not supported \\\\n&#034;;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u8fde\u63a5\u5230\u670d\u52a1\u5668<br \/>\n    if (connect(sock, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        std::cout &lt;&lt; &#034;\\\\nConnection Failed \\\\n&#034;;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u6570\u636e<br \/>\n    std::cout &lt;&lt; &#034;Enter message to send: &#034;;<br \/>\n    std::cin.getline(buffer, 1024);<br \/>\n    send(sock, buffer, strlen(buffer), 0);<br \/>\n    std::cout &lt;&lt; &#034;Message sent\\\\n&#034;;<\/p>\n<p>    \/\/ \u63a5\u6536\u6570\u636e<br \/>\n    int valread &#061; read(sock, buffer, 1024);<br \/>\n    std::cout &lt;&lt; &#034;Server: &#034; &lt;&lt; buffer &lt;&lt; std::endl;<\/p>\n<p>    \/\/ \u5173\u95ed\u8fde\u63a5<br \/>\n    close(sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4> 6.2.2 \u9519\u8bef\u5904\u7406\u4e0e\u5f02\u5e38\u7ba1\u7406 <\/h4>\n<p> \u5ba2\u6237\u7aef\u5728\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u548c\u9519\u8bef\u60c5\u51b5&#xff0c;\u5982\u7f51\u7edc\u5ef6\u8fdf\u3001\u65ad\u7ebf\u3001\u670d\u52a1\u5668\u65e0\u54cd\u5e94\u7b49\u3002\u4e3a\u4e86\u4fdd\u8bc1\u5ba2\u6237\u7aef\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c&#xff0c;\u826f\u597d\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u7ba1\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002 <\/p>\n<p> \u4ee5\u4e0b\u662f\u5ba2\u6237\u7aef\u7a0b\u5e8f\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u5904\u7406\u673a\u5236&#xff1a; <\/p>\n<li>  \u4f7f\u7528\u5f02\u5e38\u6355\u83b7  &#xff1a;\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5f02\u5e38\u6355\u83b7\u673a\u5236\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002 <\/li>\n<li>  \u8d85\u65f6\u8bbe\u7f6e  &#xff1a;\u5bf9\u4e8e\u7f51\u7edc\u8bf7\u6c42\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u907f\u514d\u56e0\u7f51\u7edc\u5ef6\u8fdf\u5bfc\u81f4\u7684\u957f\u65f6\u95f4\u7b49\u5f85\u3002 <\/li>\n<li>  \u91cd\u8bd5\u673a\u5236  &#xff1a;\u5728\u9047\u5230\u4e34\u65f6\u6027\u7684\u7f51\u7edc\u95ee\u9898\u65f6&#xff0c;\u53ef\u4ee5\u5b9e\u73b0\u91cd\u8bd5\u673a\u5236&#xff0c;\u589e\u52a0\u7f51\u7edc\u8bf7\u6c42\u7684\u6210\u529f\u6982\u7387\u3002 <\/li>\n<li>  \u65e5\u5fd7\u8bb0\u5f55  &#xff1a;\u8be6\u7ec6\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u548c\u5f02\u5e38\u60c5\u51b5&#xff0c;\u4fbf\u4e8e\u95ee\u9898\u7684\u8ffd\u8e2a\u548c\u8c03\u8bd5\u3002 <\/li>\n<p> \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5e94\u7ed3\u5408\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883&#xff0c;\u5408\u7406\u5730\u8bbe\u8ba1\u9519\u8bef\u5904\u7406\u7b56\u7565&#xff0c;\u786e\u4fdd\u5ba2\u6237\u7aef\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\u548c\u7528\u6237\u7684\u826f\u597d\u4f53\u9a8c\u3002 <\/p>\n<h2> 7. Linux Berkeley\u5957\u63a5\u5b57API\u4f7f\u7528\u8be6\u89e3 <\/h2>\n<h3> 7.1 \u57fa\u672c\u5957\u63a5\u5b57API\u7684\u4f7f\u7528 <\/h3>\n<h4> 7.1.1 \u5efa\u7acb\u8fde\u63a5\u7684socket\u51fd\u6570 <\/h4>\n<p> \u5728Linux\u4e0b\u4f7f\u7528C&#043;&#043;\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u65f6&#xff0c;  socket()  \u51fd\u6570\u662f\u5efa\u7acb\u8fde\u63a5\u7684\u57fa\u77f3&#xff0c;\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7528\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002  socket()  \u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b&#xff1a; <\/p>\n<p>int socket(int domain, int type, int protocol);<\/p>\n<ul>\n<li>  domain  \u53c2\u6570\u5b9a\u4e49\u4e86\u901a\u4fe1\u7684\u534f\u8bae\u65cf\u3002\u4f8b\u5982&#xff0c;\u5bf9\u4e8eInternet\u534f\u8bae\u65cf&#xff0c;\u5e94\u4f7f\u7528  AF_INET  \u3002 <\/li>\n<li>  type  \u53c2\u6570\u6307\u5b9a\u4e86\u5957\u63a5\u5b57\u7c7b\u578b&#xff0c;\u6bd4\u5982\u9762\u5411\u8fde\u63a5\u7684\u6d41\u5f0f\u5957\u63a5\u5b57\u4f7f\u7528  SOCK_STREAM  \u3002 <\/li>\n<li>  protocol  \u53c2\u6570\u8868\u793a\u8981\u4f7f\u7528\u7684\u5177\u4f53\u534f\u8bae&#xff0c;\u5bf9\u4e8eTCP\u534f\u8bae&#xff0c;\u901a\u5e38\u4f7f\u7528  IPPROTO_TCP  \u3002 <\/li>\n<\/ul>\n<p> \u521b\u5efa\u4e00\u4e2aTCP\u5957\u63a5\u5b57\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>int create_tcp_socket() {<br \/>\n    int sock &#061; socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);<br \/>\n    if (sock &#061;&#061; -1) {<br \/>\n        perror(&#034;socket&#034;);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    return sock;<br \/>\n}<\/p>\n<p> \u5728\u6b64\u4ee3\u7801\u4e2d&#xff0c;\u521b\u5efa\u5957\u63a5\u5b57\u540e&#xff0c;\u9700\u8981\u5bf9\u5957\u63a5\u5b57\u8fdb\u884c\u4e00\u7cfb\u5217\u8bbe\u7f6e\u548c\u7ed1\u5b9a\u64cd\u4f5c&#xff0c;\u4e4b\u540e\u624d\u80fd\u8fdb\u884c\u8fde\u63a5\u3002 <\/p>\n<h4> 7.1.2 \u6570\u636e\u4ea4\u6362\u7684send\u548crecv\u51fd\u6570 <\/h4>\n<p>  send()  \u548c  recv()  \u662f\u7528\u4e8e\u5728\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\u4e0a\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u7684\u51fd\u6570\u3002  send()  \u51fd\u6570\u7528\u4e8e\u53d1\u9001\u6570\u636e&#xff0c;\u800c  recv()  \u51fd\u6570\u7528\u4e8e\u63a5\u6536\u6570\u636e\u3002 <\/p>\n<p>  send()  \u51fd\u6570\u539f\u578b\u5982\u4e0b&#xff1a; <\/p>\n<p>ssize_t send(int sockfd, const void *buf, size_t len, int flags);<\/p>\n<ul>\n<li>  sockfd  \u662f\u4e4b\u524d\u901a\u8fc7  socket()  \u521b\u5efa\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 <\/li>\n<li>  buf  \u662f\u6307\u5411\u5305\u542b\u8981\u53d1\u9001\u6570\u636e\u7684\u7f13\u51b2\u533a\u7684\u6307\u9488\u3002 <\/li>\n<li>  len  \u662f\u8981\u53d1\u9001\u7684\u6570\u636e\u957f\u5ea6\u3002 <\/li>\n<li>  flags  \u7528\u4e8e\u4fee\u6539  send()  \u7684\u9ed8\u8ba4\u884c\u4e3a&#xff0c;\u4e00\u822c\u8bbe\u7f6e\u4e3a0\u3002 <\/li>\n<\/ul>\n<p>  recv()  \u51fd\u6570\u539f\u578b\u5982\u4e0b&#xff1a; <\/p>\n<p>ssize_t recv(int sockfd, void *buf, size_t len, int flags);<\/p>\n<p> \u8fd9\u91cc\u53c2\u6570\u542b\u4e49\u4e0e  send()  \u7c7b\u4f3c&#xff0c;\u4f46\u662f  flags  \u4e5f\u53ef\u7528\u4e8e\u5904\u7406\u4f8b\u5982  MSG_OOB  &#xff08;\u63a5\u6536\u5e26\u5916\u6570\u636e&#xff09;\u7b49\u7279\u6b8a\u60c5\u51b5\u3002 <\/p>\n<p> \u4ee5\u4e0b\u662f\u4e00\u4e2a\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u7684\u793a\u4f8b&#xff1a; <\/p>\n<p>#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>\/\/ \u5047\u8bbesockfd\u5df2\u7ecf\u662f\u4e00\u4e2a\u6709\u6548\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\nvoid send_and_receive(int sockfd) {<br \/>\n    const char *message &#061; &#034;Hello, World!&#034;;<br \/>\n    char buffer[1024];<\/p>\n<p>    \/\/ \u53d1\u9001\u6570\u636e<br \/>\n    if (send(sockfd, message, strlen(message), 0) &#061;&#061; -1) {<br \/>\n        perror(&#034;send&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u63a5\u6536\u6570\u636e<br \/>\n    ssize_t bytes_received &#061; recv(sockfd, buffer, sizeof(buffer), 0);<br \/>\n    if (bytes_received &#061;&#061; -1) {<br \/>\n        perror(&#034;recv&#034;);<br \/>\n    } else {<br \/>\n        buffer[bytes_received] &#061; &#039;\\\\0&#039;;  \/\/ \u786e\u4fdd\u5b57\u7b26\u4e32\u6b63\u786e\u7ed3\u675f<br \/>\n        printf(&#034;Received message: %s\\\\n&#034;, buffer);<br \/>\n    }<br \/>\n}<\/p>\n<h3> 7.2 \u9ad8\u7ea7\u5957\u63a5\u5b57API\u7684\u4f7f\u7528 <\/h3>\n<h4> 7.2.1 \u975e\u963b\u585e\u548c\u5f02\u6b65IO\u64cd\u4f5c <\/h4>\n<p> Linux Berkeley\u5957\u63a5\u5b57API\u652f\u6301\u975e\u963b\u585eIO\u548c\u5f02\u6b65IO\u64cd\u4f5c&#xff0c;\u8fd9\u5bf9\u4e8e\u5b9e\u73b0\u9ad8\u6027\u80fd\u548c\u53ef\u4f38\u7f29\u7684\u7f51\u7edc\u5e94\u7528\u975e\u5e38\u5173\u952e\u3002\u5bf9\u4e8e\u975e\u963b\u585eIO&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528  fcntl()  \u7cfb\u7edf\u8c03\u7528\u6765\u6539\u53d8\u5957\u63a5\u5b57\u7684IO\u6a21\u5f0f\u3002 <\/p>\n<p> \u4f8b\u5982&#xff0c;\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26  sockfd  \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u7684\u4ee3\u7801\u5982\u4e0b&#xff1a; <\/p>\n<p>int flags &#061; fcntl(sockfd, F_GETFL, 0);<br \/>\nfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);<\/p>\n<p> \u4f7f\u7528\u975e\u963b\u585eIO\u65f6&#xff0c;  send()  \u548c  recv()  \u51fd\u6570\u5728\u4e0d\u80fd\u7acb\u5373\u5b8c\u6210\u65f6\u4f1a\u8fd4\u56de\u9519\u8bef  EAGAIN  \u6216  EWOULDBLOCK  \u3002 <\/p>\n<h4> 7.2.2 \u5957\u63a5\u5b57\u9009\u9879\u7684\u914d\u7f6e\u4e0e\u83b7\u53d6 <\/h4>\n<p> \u5957\u63a5\u5b57\u9009\u9879\u5141\u8bb8\u5bf9\u5957\u63a5\u5b57\u7684\u884c\u4e3a\u8fdb\u884c\u7cbe\u7ec6\u63a7\u5236\u3002\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u83b7\u53d6\u548c\u8bbe\u7f6e\u8d85\u65f6&#xff0c;\u542f\u7528\u6216\u7981\u7528\u5730\u5740\u91cd\u7528\u7b49\u3002\u4f7f\u7528  getsockopt()  \u548c  setsockopt()  \u51fd\u6570\u6765\u914d\u7f6e\u8fd9\u4e9b\u9009\u9879\u3002 <\/p>\n<p> \u4e0b\u9762\u662f\u4e00\u4e2a\u83b7\u53d6\u5957\u63a5\u5b57\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u4f8b\u5b50&#xff1a; <\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>void get_recv_buffer_size(int sockfd) {<br \/>\n    int size;<br \/>\n    socklen_t length &#061; sizeof(size);<\/p>\n<p>    if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &amp;size, &amp;length) &#061;&#061; -1) {<br \/>\n        perror(&#034;getsockopt&#034;);<br \/>\n    } else {<br \/>\n        printf(&#034;Receive buffer size: %d bytes\\\\n&#034;, size);<br \/>\n    }<br \/>\n}<\/p>\n<p> \u901a\u8fc7\u8fd9\u4e9b\u9ad8\u7ea7API&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u4f18\u5316\u7f51\u7edc\u7a0b\u5e8f\u7684\u6027\u80fd&#xff0c;\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002 <\/p>\n<p> \u901a\u8fc7\u7b2c\u4e03\u7ae0\u7684\u5b66\u4e60&#xff0c;\u4f60\u5e94\u5df2\u7ecf\u638c\u63e1\u4e86Linux Berkeley\u5957\u63a5\u5b57API\u7684\u57fa\u7840\u548c\u9ad8\u7ea7\u7528\u6cd5&#xff0c;\u8fd9\u5c06\u6709\u52a9\u4e8e\u4f60\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u7f51\u7edc\u901a\u4fe1\u9700\u6c42\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b1823537.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528C&#043;&#043;\u8bed\u8a00\u7f16\u5199TCP\u7f51\u7edc\u901a\u4fe1\u7a0b\u5e8f&#xff0c;\u6db5\u76d6\u5957\u63a5\u5b57\u7684\u521b\u5efa\u4e0e\u7ba1\u7406\u3001\u591a\u7ebf\u7a0b\u5904\u7406\u4ee5\u53ca\u547d\u4ee4\u884c\u53c2\u6570\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u6e90\u4ee3\u7801\u6587\u4ef6  server.cpp  \u548c  client.cpp  &#xff0c;\u6f14\u793a\u4e00\u4e2a\u7b80\u6613\u7684TCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u6d41\u7a0b\u3002\u6b64\u5916&#xff0c;\u8fd8\u5c06\u63a2\u8ba8\u591a\u7ebf\u7a0b\u6280\u672f\u5728\u670d\u52a1\u5668\u7aef\u7684\u5e94\u7528&#xff0c;\u4ee5\u5b9e\u73b0\u5e76\u53d1\u8fde\u63a5\u7684\u5904\u7406\u3002\u901a\u8fc7\u672c\u9879\u76ee&#xff0c;\u8bfb\u8005\u5c06\u4e86\u89e3TCP\u534f\u8bae\u7684\u5de5\u4f5c\u539f\u7406&#xff0c;\u638c\u63e1Linux\u73af\u5883\u4e0b\u7684\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u3002 <\/p>\n<p>   \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6   <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b1823537.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/>  <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb748\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5728\u5f53\u4eca\u7684\u4fe1\u606f\u65f6\u4ee3\uff0c\u7f51\u7edc\u7f16\u7a0b\u662fIT\u4e13\u4e1a\u4eba\u5458\u5fc5\u987b\u638c\u63e1\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\uff0c\u5b83\u6d89\u53ca\u5230\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u548c\u7f51\u7edc\u534f\u8bae\u6765\u6784\u5efa\u80fd\u591f\u76f8\u4e92\u901a\u4fe1\u7684\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u3002Linux\u7cfb\u7edf\u56e0\u5176\u5f00\u6e90\u548c\u7a33\u5b9a\u6027\uff0c\u5728\u670d\u52a1\u5668\u548c\u5d4c\u5165\u5f0f\u8bbe\u5907\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u800cC++\u5219\u56e0\u5176\u6027\u80fd\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u9ad8\uff0c\u6210\u4e3a\u4e86\u7f51\u7edc\u7f16\u7a0b\u7684\u70ed\u95e8\u9009\u62e9\u3002\u5728\u6df1\u5165\u7ec6\u8282\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3Linux\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002_linux c++ tcp \u5e94\u7528<\/p>\n","protected":false},"author":2,"featured_media":47395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-47397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \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\/47397.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb748\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5728\u5f53\u4eca\u7684\u4fe1\u606f\u65f6\u4ee3\uff0c\u7f51\u7edc\u7f16\u7a0b\u662fIT\u4e13\u4e1a\u4eba\u5458\u5fc5\u987b\u638c\u63e1\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\uff0c\u5b83\u6d89\u53ca\u5230\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u548c\u7f51\u7edc\u534f\u8bae\u6765\u6784\u5efa\u80fd\u591f\u76f8\u4e92\u901a\u4fe1\u7684\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u3002Linux\u7cfb\u7edf\u56e0\u5176\u5f00\u6e90\u548c\u7a33\u5b9a\u6027\uff0c\u5728\u670d\u52a1\u5668\u548c\u5d4c\u5165\u5f0f\u8bbe\u5907\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u800cC++\u5219\u56e0\u5176\u6027\u80fd\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u9ad8\uff0c\u6210\u4e3a\u4e86\u7f51\u7edc\u7f16\u7a0b\u7684\u70ed\u95e8\u9009\u62e9\u3002\u5728\u6df1\u5165\u7ec6\u8282\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3Linux\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002_linux c++ tcp \u5e94\u7528\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/47397.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T23:36:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b1823537.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47397.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/47397.html\",\"name\":\"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T23:36:57+00:00\",\"dateModified\":\"2025-07-29T23:36:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/47397.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/47397.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47397.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \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\/47397.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb748\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf18\u6b21\u3002\u5728\u5f53\u4eca\u7684\u4fe1\u606f\u65f6\u4ee3\uff0c\u7f51\u7edc\u7f16\u7a0b\u662fIT\u4e13\u4e1a\u4eba\u5458\u5fc5\u987b\u638c\u63e1\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\uff0c\u5b83\u6d89\u53ca\u5230\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u548c\u7f51\u7edc\u534f\u8bae\u6765\u6784\u5efa\u80fd\u591f\u76f8\u4e92\u901a\u4fe1\u7684\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u3002Linux\u7cfb\u7edf\u56e0\u5176\u5f00\u6e90\u548c\u7a33\u5b9a\u6027\uff0c\u5728\u670d\u52a1\u5668\u548c\u5d4c\u5165\u5f0f\u8bbe\u5907\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u800cC++\u5219\u56e0\u5176\u6027\u80fd\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u9ad8\uff0c\u6210\u4e3a\u4e86\u7f51\u7edc\u7f16\u7a0b\u7684\u70ed\u95e8\u9009\u62e9\u3002\u5728\u6df1\u5165\u7ec6\u8282\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3Linux\u5957\u63a5\u5b57\u7f16\u7a0b\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002_linux c++ tcp \u5e94\u7528","og_url":"https:\/\/www.wsisp.com\/helps\/47397.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T23:36:57+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729233656-68895b1823537.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/47397.html","url":"https:\/\/www.wsisp.com\/helps\/47397.html","name":"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T23:36:57+00:00","dateModified":"2025-07-29T23:36:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/47397.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/47397.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/47397.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u5b9e\u73b0\u7b80\u6613TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684Linux\u5957\u63a5\u5b57\u7f16\u7a0b"}]},{"@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\/47397","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=47397"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47397\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/47395"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=47397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=47397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=47397"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=47397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}