{"id":112453,"date":"2026-10-03T18:40:54","date_gmt":"2026-10-03T10:40:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/112453.html"},"modified":"2026-10-03T18:40:54","modified_gmt":"2026-10-03T10:40:54","slug":"%e3%80%90%e4%bb%93%e9%a2%89%e8%af%ad%e8%a8%80%e5%85%a5%e9%97%a8-%c2%b7-%e7%ac%ac24%e8%af%be%e3%80%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/112453.html","title":{"rendered":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011"},"content":{"rendered":"<h2>\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8&#xff1a;\u4ece socket \u5230 HTTP&#xff0c;\u8ba9\u7a0b\u5e8f\u5f00\u53e3\u8bf4\u8bdd<\/h2>\n<p>\u7b2c 23 \u8bfe\u7ed3\u5c3e\u8bf4\u8fc7&#xff1a;JSON \u6700\u5e38\u89c1\u7684\u51fa\u573a\u4f4d\u7f6e\u4e0d\u662f\u6587\u4ef6&#xff0c;\u800c\u662f\u7f51\u7edc\u2014\u2014\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u8981\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u628a\u7ed3\u679c\u4ee5 JSON \u8fd4\u56de\u3002\u90a3&#034;\u7f51\u7edc&#034;\u8fd9\u4e00\u5c42\u5230\u5e95\u957f\u4ec0\u4e48\u6837&#xff1f;\u672c\u8bfe\u4ece\u6700\u5e95\u5c42\u7684 TCP\/UDP socket \u8bb2\u8d77&#xff0c;\u4eb2\u624b\u5199\u4e00\u4e2a\u56de\u663e\u670d\u52a1&#xff1b;\u518d\u4e0a\u5230\u5e94\u7528\u5c42\u7684 HTTP&#xff0c;\u7528 stdx \u6269\u5c55\u5e93\u53d1\u8bf7\u6c42\u3001\u642d\u670d\u52a1&#xff1b;\u6700\u540e\u628a\u7b2c 23 \u8bfe\u7684 JSON \u63a5\u4e0a\u6765&#xff0c;\u505a\u4e00\u4e2a\u771f\u6b63\u7684&#034;\u5f85\u529e\u6e05\u5355 API&#034;&#xff1a;\u5ba2\u6237\u7aef POST \u4e00\u6761 JSON&#xff0c;\u670d\u52a1\u7aef\u5b58\u4e0b\u6765&#xff0c;GET \u65f6\u518d\u8fd4\u56de\u5b8c\u6574\u5217\u8868\u3002<\/p>\n<p>\u672c\u6587\u6240\u6709\u4ee3\u7801\u4e0e\u62a5\u9519\u6587\u6848\u5747\u5728\u4ed3\u9889 SDK 1.2.0 &#043; stdx 1.2.0-beta.02.1 \u4e0b\u9010\u884c\u5b9e\u6d4b\u7f16\u8bd1\u8fd0\u884c\u3002<\/p>\n<hr \/>\n<h3>\u76ee\u5f55&#xff08;\u7cfb\u5217\u5bfc\u822a&#xff09;<\/h3>\n<p>\u6574\u5957\u8def\u7ebf\u5171 7 \u4e2a\u6a21\u5757\u300130 \u8bfe&#xff1a;<\/p>\n<table>\n<tr>\u6a21\u5757\u8bfe\u6b21\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u4e00\u3001\u73af\u5883\u4e0e\u5165\u95e8<\/td>\n<td>01&#xff5e;05<\/td>\n<td>\u73af\u5883\u642d\u5efa\u4e0e Hello World\u3001\u53d8\u91cf\u4e0e\u57fa\u672c\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u4e0e\u8f93\u5165\u8f93\u51fa\u3001\u5206\u652f\u3001\u5faa\u73af<\/td>\n<\/tr>\n<tr>\n<td>\u4e8c\u3001\u5e38\u7528\u7c7b\u578b\u4e0e\u6570\u636e\u7ec4\u7ec7<\/td>\n<td>06&#xff5e;10<\/td>\n<td>\u5b57\u7b26\u4e32\u3001\u6570\u7ec4\u4e0e\u533a\u95f4\u3001ArrayList\/HashMap\/HashSet\u3001\u53ef\u7a7a\u7c7b\u578b\u3001\u9519\u8bef\u5904\u7406<\/td>\n<\/tr>\n<tr>\n<td>\u4e09\u3001\u51fd\u6570\u4e0e\u51fd\u6570\u5f0f<\/td>\n<td>11&#xff5e;14<\/td>\n<td>\u51fd\u6570\u3001Lambda \u4e0e\u9ad8\u9636\u51fd\u6570\u3001\u95ed\u5305\u3001\u8fed\u4ee3\u5668\u4e0e\u60f0\u6027\u5e8f\u5217<\/td>\n<\/tr>\n<tr>\n<td>\u56db\u3001\u9762\u5411\u5bf9\u8c61\u4e0e\u7c7b\u578b\u7cfb\u7edf<\/td>\n<td>15&#xff5e;20<\/td>\n<td>struct\/class\u3001\u6784\u9020\u4e0e\u5c5e\u6027\u3001\u63a5\u53e3\u3001\u679a\u4e3e\u4e0e match \u6a21\u5f0f\u5339\u914d\u3001\u6cdb\u578b\u3001\u6269\u5c55<\/td>\n<\/tr>\n<tr>\n<td>\u4e94\u3001\u5de5\u7a0b\u5316\u4e0e\u6807\u51c6\u5e93<\/td>\n<td>21&#xff5e;25<\/td>\n<td>cjpm \u5305\u7ba1\u7406\u4e0e\u591a\u6587\u4ef6\u3001\u6587\u4ef6 IO\u3001JSON \u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u3001\u5355\u5143\u6d4b\u8bd5<\/td>\n<\/tr>\n<tr>\n<td>\u516d\u3001\u5e76\u53d1\u7f16\u7a0b<\/td>\n<td>26&#xff5e;28<\/td>\n<td>\u7ebf\u7a0b\u3001Channel \u901a\u9053\u4e0e\u540c\u6b65\u539f\u8bed\u3001\u5e76\u53d1\u5b9e\u6218<\/td>\n<\/tr>\n<tr>\n<td>\u4e03\u3001\u9879\u76ee\u5b9e\u6218<\/td>\n<td>29&#xff5e;30<\/td>\n<td>\u547d\u4ee4\u884c\u5c0f\u5de5\u5177\u3001GeoJSON \u6570\u636e\u5904\u7406\u5b9e\u6218<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<li>\u73af\u5883\u642d\u5efa\u4e0e\u7b2c\u4e00\u4e2a\u4ed3\u9889\u7a0b\u5e8f<\/li>\n<li>\u53d8\u91cf\u4e0e\u5e38\u91cf&#xff1a;let \/ var \u4e0e\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/li>\n<li>\u8fd0\u7b97\u7b26\u4e0e\u6807\u51c6\u8f93\u5165\u8f93\u51fa<\/li>\n<li>\u5206\u652f\u7ed3\u6784&#xff1a;if \u4e0e match \u8868\u8fbe\u5f0f<\/li>\n<li>\u5faa\u73af\u7ed3\u6784&#xff1a;while \/ for \/ Range<\/li>\n<li>\u5b57\u7b26\u4e32\u8be6\u89e3\u4e0e\u5b57\u7b26\u4e32\u63d2\u503c<\/li>\n<li>\u6570\u7ec4 Array \u4e0e\u533a\u95f4 Range<\/li>\n<li>\u96c6\u5408\u6846\u67b6&#xff1a;ArrayList\u3001HashMap\u3001HashSet<\/li>\n<li>\u53ef\u7a7a\u7c7b\u578b ? \u4e0e Option<\/li>\n<li>\u9519\u8bef\u5904\u7406&#xff1a;\u5f02\u5e38\u673a\u5236\u4e0e Result<\/li>\n<li>\u51fd\u6570\u5b9a\u4e49\u3001\u53c2\u6570\u4e0e\u8fd4\u56de\u503c<\/li>\n<li>Lambda \u4e0e\u9ad8\u9636\u51fd\u6570<\/li>\n<li>\u95ed\u5305\u3001\u4f5c\u7528\u57df\u4e0e\u51fd\u6570\u7c7b\u578b<\/li>\n<li>\u8fed\u4ee3\u5668 Iterator \u4e0e Sequence<\/li>\n<li>\u7ed3\u6784\u4f53 struct \u4e0e\u7c7b class<\/li>\n<li>\u6784\u9020\u51fd\u6570\u3001\u5c5e\u6027\u4e0e\u65b9\u6cd5<\/li>\n<li>\u63a5\u53e3 interface \u4e0e\u5b9e\u73b0<\/li>\n<li>\u679a\u4e3e enum\u3001\u4ee3\u6570\u6570\u636e\u7c7b\u578b\u4e0e match \u6a21\u5f0f\u5339\u914d<\/li>\n<li>\u6cdb\u578b\u7f16\u7a0b<\/li>\n<li>\u6269\u5c55\u3001\u7c7b\u578b\u522b\u540d\u4e0e\u53ef\u89c1\u6027\u63a7\u5236<\/li>\n<li>cjpm \u5305\u7ba1\u7406\u4e0e\u591a\u6587\u4ef6\u9879\u76ee\u7ec4\u7ec7<\/li>\n<li>\u6587\u4ef6\u4e0e\u76ee\u5f55 IO<\/li>\n<li>JSON \u5904\u7406&#xff08;\u7ed3\u5408 stdx \u6269\u5c55\u5e93&#xff09;<\/li>\n<li>\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8&#xff08;\u672c\u6587&#xff09;<\/li>\n<li>\u5355\u5143\u6d4b\u8bd5<\/li>\n<li>\u5e76\u53d1\u57fa\u7840&#xff1a;\u7ebf\u7a0b\u7684\u521b\u5efa\u4e0e\u7b49\u5f85<\/li>\n<li>Channel \u901a\u9053\u4e0e\u540c\u6b65\u539f\u8bed<\/li>\n<li>\u5e76\u53d1\u5b9e\u6218&#xff1a;\u591a\u7ebf\u7a0b\u4efb\u52a1\u5904\u7406<\/li>\n<li>\u5b9e\u6218\u4e00&#xff1a;\u5e26\u6587\u4ef6\u6301\u4e45\u5316\u7684\u547d\u4ee4\u884c\u5c0f\u5de5\u5177<\/li>\n<li>\u5b9e\u6218\u4e8c&#xff1a;GeoJSON \u6570\u636e\u5904\u7406\u7a0b\u5e8f<\/li>\n<hr \/>\n<h3>\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u5230\u5e95\u5728\u505a\u4ec0\u4e48<\/h3>\n<p>\u524d\u51e0\u8bfe\u7684\u7a0b\u5e8f\u90fd\u662f&#034;\u5355\u673a\u81ea\u95ed\u73af&#034;&#xff1a;\u6570\u636e\u5728\u5185\u5b58\u91cc\u8f6c&#xff0c;\u6700\u591a\u8bfb\u5199\u81ea\u5df1\u786c\u76d8\u4e0a\u7684\u6587\u4ef6\u3002\u800c\u73b0\u5b9e\u4e2d\u7684\u7a0b\u5e8f\u51e0\u4e4e\u90fd\u8981\u8de8\u8fdb\u7a0b\u3001\u8de8\u673a\u5668\u4ea4\u6362\u6570\u636e&#xff1a;\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u7ad9\u3001\u624b\u673a App \u8c03\u540e\u7aef\u63a5\u53e3\u3001\u6e38\u620f\u8054\u673a\u5bf9\u6218\u2026\u2026\u8fd9\u80cc\u540e\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<p>\u5148\u5efa\u7acb\u56db\u4e2a\u6982\u5ff5&#xff0c;\u540e\u9762\u5168\u9760\u5b83\u4eec&#xff1a;<\/p>\n<h4>1.1 IP \u5730\u5740\u4e0e\u7aef\u53e3&#xff1a;\u7f51\u7edc\u4e0a\u7684&#034;\u95e8\u724c\u53f7 &#043; \u623f\u95f4\u53f7&#034;<\/h4>\n<ul>\n<li>IP \u5730\u5740\u6807\u8bc6\u7f51\u7edc\u4e0a\u7684\u4e00\u53f0\u673a\u5668&#xff0c;\u5982 192.168.1.10\u3002\u6709\u4e00\u4e2a\u7279\u6b8a\u7684 IP \u8981\u8bb0\u4f4f&#xff1a;127.0.0.1&#xff0c;\u53eb\u56de\u73af\u5730\u5740&#xff0c;\u6c38\u8fdc\u6307\u5411&#034;\u672c\u673a\u81ea\u5df1&#034;\u2014\u2014\u672c\u8bfe\u6240\u6709\u5b9e\u9a8c\u90fd\u5728\u81ea\u5df1\u7535\u8111\u4e0a\u5b8c\u6210&#xff0c;\u5168\u9760\u5b83\u3002<\/li>\n<li>\u4e00\u53f0\u673a\u5668\u4e0a\u540c\u65f6\u8dd1\u7740\u5f88\u591a\u7a0b\u5e8f&#xff0c;\u5149\u6709 IP \u8fd8\u4e0d\u591f\u3002\u7aef\u53e3&#xff08;port&#xff09; \u662f 0&#xff5e;65535 \u7684\u7f16\u53f7&#xff0c;\u7528\u6765\u533a\u5206\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u7684\u4e0d\u540c\u7a0b\u5e8f\u3002\u6bd4\u5982\u7ea6\u5b9a\u4fd7\u6210&#xff1a;HTTP \u670d\u52a1\u7528 80&#xff0c;HTTPS \u7528 443&#xff0c;\u4f60\u81ea\u5df1\u5199\u7684\u670d\u52a1\u968f\u4fbf\u6311\u4e00\u4e2a\u6ca1\u88ab\u5360\u7528\u7684&#xff0c;\u5982 8080\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5&#034;\u8fde\u5230\u67d0\u53f0\u673a\u5668\u7684\u67d0\u4e2a\u7a0b\u5e8f&#034; &#061; \u201cIP &#043; \u7aef\u53e3\u201d&#xff0c;\u4f8b\u5982 127.0.0.1:8080\u3002<\/p>\n<h4>1.2 TCP \u4e0e UDP&#xff1a;\u4e24\u79cd\u6027\u683c\u8fe5\u5f02\u7684\u8fd0\u8f93\u65b9\u5f0f<\/h4>\n<p>\u7f51\u7edc\u4f20\u8f93\u5c42\u6709\u4e24\u4e2a\u6700\u5e38\u7528\u7684\u534f\u8bae&#xff1a;<\/p>\n<table>\n<tr>TCPUDP<\/tr>\n<tbody>\n<tr>\n<td>\u6bd4\u55bb<\/td>\n<td>\u6253\u7535\u8bdd&#xff1a;\u5148\u63a5\u901a\u518d\u804a<\/td>\n<td>\u5bc4\u4fe1&#xff1a;\u5199\u597d\u5730\u5740\u5c31\u6254\u51fa\u53bb<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5<\/td>\n<td>\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5<\/td>\n<td>\u65e0\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u9760\u6027<\/td>\n<td>\u4fdd\u8bc1\u9001\u8fbe\u3001\u4fdd\u8bc1\u987a\u5e8f\u3001\u4e0d\u4e22\u5305<\/td>\n<td>\u4e0d\u4fdd\u8bc1&#xff08;\u53ef\u80fd\u4e22\u3001\u53ef\u80fd\u4e71\u5e8f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6d41\u5f0f<\/td>\n<td>\u5b57\u8282\u6d41&#xff08;\u6ca1\u6709\u5929\u7136\u7684\u6d88\u606f\u8fb9\u754c&#xff09;<\/td>\n<td>\u6570\u636e\u62a5&#xff08;\u4e00\u53d1\u4e00\u6536&#xff0c;\u8fb9\u754c\u6e05\u6670&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u5e94\u7528<\/td>\n<td>\u7f51\u9875\u3001\u804a\u5929\u3001\u6587\u4ef6\u4f20\u8f93<\/td>\n<td>\u89c6\u9891\u76f4\u64ad\u3001DNS \u67e5\u8be2\u3001\u6e38\u620f\u72b6\u6001\u540c\u6b65<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7b80\u5355\u8bb0&#xff1a;\u8981\u53ef\u9760\u7528 TCP&#xff0c;\u8981\u5feb\u4e14\u4e22\u4e00\u70b9\u65e0\u6240\u8c13\u7528 UDP\u3002\u672c\u8bfe\u4e24\u4e2a\u90fd\u4f1a\u5199\u3002<\/p>\n<h4>1.3 HTTP&#xff1a;\u8dd1\u5728 TCP \u4e4b\u4e0a\u7684&#034;\u5e94\u7528\u5c42\u666e\u901a\u8bdd&#034;<\/h4>\n<p>TCP \u53ea\u8d1f\u8d23\u628a\u5b57\u8282\u5b89\u5168\u9001\u8fbe&#xff0c;\u4f46&#034;\u8fd9\u4e9b\u5b57\u8282\u662f\u4ec0\u4e48\u610f\u601d&#034;\u9700\u8981\u5e94\u7528\u5c42\u81ea\u5df1\u6765\u7ea6\u5b9a\u3002HTTP \u5c31\u662f\u5168\u4e16\u754c\u7ea6\u5b9a\u4fd7\u6210\u7684\u90a3\u5957\u5e94\u7528\u5c42\u534f\u8bae&#xff1a;\u5ba2\u6237\u7aef\u53d1\u4e00\u6bb5\u5e26\u683c\u5f0f\u7684\u6587\u672c&#xff08;\u65b9\u6cd5\u3001\u8def\u5f84\u3001\u5934\u90e8\u3001\u6b63\u6587&#xff09;&#xff0c;\u670d\u52a1\u7aef\u56de\u4e00\u6bb5\u5e26\u683c\u5f0f\u7684\u6587\u672c&#xff08;\u72b6\u6001\u7801\u3001\u5934\u90e8\u3001\u6b63\u6587&#xff09;\u3002\u4f60\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u6572\u7f51\u5740&#xff0c;\u80cc\u540e\u5c31\u662f\u4e00\u6b21 HTTP \u8bf7\u6c42\u3002<\/p>\n<p>\u4e09\u4e2a\u5c42\u6b21\u7684\u5173\u7cfb&#xff1a;<\/p>\n<p>\u5e94\u7528\u5c42&#xff1a;  HTTP&#xff08;\u89c4\u5b9a&#034;\u8bf7\u6c42\/\u54cd\u5e94&#034;\u7684\u683c\u5f0f&#xff09;      \u2190 \u672c\u8bfe\u7b2c\u56db&#xff5e;\u516d\u8282<br \/>\n\u4f20\u8f93\u5c42&#xff1a;  TCP&#xff08;\u53ef\u9760\u5b57\u8282\u6d41&#xff09; \/ UDP&#xff08;\u6570\u636e\u62a5&#xff09;   \u2190 \u672c\u8bfe\u7b2c\u4e8c\u3001\u4e09\u8282<br \/>\n\u7f51\u7edc\u5c42&#xff1a;  IP &#043; \u7aef\u53e3&#xff08;\u627e\u5230\u90a3\u53f0\u673a\u5668\u90a3\u4e2a\u7a0b\u5e8f&#xff09;<\/p>\n<h4>1.4 \u4ed3\u9889\u91cc\u7684\u5bf9\u5e94\u7269<\/h4>\n<table>\n<tr>\u5c42\u6b21\u4ed3\u9889\u7684\u5305\u5728\u54ea<\/tr>\n<tbody>\n<tr>\n<td>TCP \/ UDP socket<\/td>\n<td>std.net<\/td>\n<td>\u6807\u51c6\u5e93\u81ea\u5e26&#xff0c;\u65e0\u9700\u4efb\u4f55\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>HTTP \u5ba2\u6237\u7aef \/ \u670d\u52a1\u7aef<\/td>\n<td>stdx.net.http<\/td>\n<td>stdx \u6269\u5c55\u5e93&#xff08;\u7b2c 23 \u8bfe\u5df2\u914d\u597d&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>std.net \u968f SDK \u5f00\u7bb1\u5373\u7528&#xff0c;\u8fd9\u662f\u5b83\u548c\u7b2c 23 \u8bfe JSON \u6700\u5927\u7684\u4e0d\u540c\u2014\u2014\u5199 socket \u4e0d\u9700\u8981\u52a8 cjpm.toml\u3002HTTP \u5219\u9700\u8981 stdx \u7684 bin-dependencies \u914d\u7f6e&#xff0c;\u7b2c 23 \u8bfe\u914d\u8fc7\u7684\u5de5\u7a0b\u53ef\u4ee5\u76f4\u63a5\u63a5\u7740\u7528&#xff0c;\u6ca1\u914d\u8fc7\u7684\u56de\u5230\u7b2c 23 \u8bfe\u7b2c\u4e8c\u8282\u7167\u505a\u4e00\u6b21\u5373\u53ef\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001TCP&#xff1a;\u5199\u4e00\u4e2a\u56de\u663e\u670d\u52a1<\/h3>\n<p>\u6700\u7ecf\u5178\u7684\u7f51\u7edc\u7a0b\u5e8f\u5165\u95e8\u9898&#xff1a;echo \u670d\u52a1\u2014\u2014\u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u7aef\u539f\u6837\u56de\u4ec0\u4e48\u3002\u5b83\u80fd\u4e00\u6b21\u8dd1\u901a TCP \u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<h4>2.1 \u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u5404\u81ea\u7684\u4e09\u6b65\u66f2<\/h4>\n<p>TCP \u662f&#034;\u6253\u7535\u8bdd&#034;\u6a21\u578b&#xff0c;\u4e24\u8fb9\u89d2\u8272\u4e0d\u540c&#xff1a;<\/p>\n<p>\u670d\u52a1\u7aef&#xff08;\u63a5\u7535\u8bdd\u7684&#xff09;                \u5ba2\u6237\u7aef&#xff08;\u6253\u7535\u8bdd\u7684&#xff09;<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500            \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n\u2460 TcpServerSocket \u521b\u5efa            \u2460 TcpSocket \u521b\u5efa<br \/>\n\u2461 bind() \u7ed1\u5b9a\u7aef\u53e3\u5f00\u59cb\u76d1\u542c          \u2461 connect() \u62e8\u53f7\u5efa\u7acb\u8fde\u63a5<br \/>\n\u2462 accept() \u7b49\u6765\u7535&#xff0c;\u5f97\u5230\u4e00\u4e2a\u8fde\u63a5     \u2462 connect \u8fd4\u56de &#061; \u63a5\u901a<br \/>\n   \u21c5 \u7136\u540e\u53cc\u65b9 read\/write \u4e92\u53d1\u5b57\u8282 \u21c5<\/p>\n<p>\u6ce8\u610f accept() \u548c read() \u90fd\u662f\u963b\u585e\u7684&#xff1a;\u6ca1\u6709\u5ba2\u6237\u7aef\u6765&#xff0c;accept \u5c31\u4e00\u76f4\u7b49&#xff1b;\u5bf9\u65b9\u6ca1\u53d1\u6570\u636e&#xff0c;read \u5c31\u4e00\u76f4\u7b49\u3002\u4e3a\u4e86\u8ba9\u4e00\u4e2a\u7a0b\u5e8f\u91cc\u540c\u65f6\u8dd1\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef&#xff0c;\u9700\u8981\u628a\u670d\u52a1\u7aef\u653e\u8fdb\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u2014\u2014\u7528\u4ed3\u9889\u7684 spawn { &#8230; } \u5173\u952e\u5b57&#xff0c;\u82b1\u62ec\u53f7\u91cc\u7684\u4ee3\u7801\u4f1a\u5728\u65b0\u7ebf\u7a0b\u91cc\u5e76\u53d1\u6267\u884c&#xff08;\u7b2c 26 \u8bfe\u4f1a\u4e13\u95e8\u8bb2\u5e76\u53d1&#xff0c;\u8fd9\u91cc\u5148\u7167\u6284&#xff09;\u3002<\/p>\n<h4>2.2 \u5b8c\u6574\u4ee3\u7801<\/h4>\n<p>\u65b0\u5efa\u5de5\u7a0b&#xff0c;\u628a src\/main.cj \u5199\u6210&#xff1a;<\/p>\n<p>package netdemo<\/p>\n<p>import std.net.*<\/p>\n<p>main(): Int64 {<br \/>\n    \/\/ \u670d\u52a1\u7aef&#xff1a;bindAt: 0 \u8868\u793a&#034;\u8ba9\u7cfb\u7edf\u6311\u4e00\u4e2a\u7a7a\u95f2\u7aef\u53e3&#034;&#xff0c;\u907f\u514d\u548c\u522b\u7684\u7a0b\u5e8f\u649e\u8f66<br \/>\n    let server &#061; TcpServerSocket(bindAt: 0)<br \/>\n    server.bind()<br \/>\n    \/\/ localAddress \u662f\u62bd\u8c61\u7684 SocketAddress&#xff0c;\u53d6\u7aef\u53e3\u53f7\u8981\u8f6c\u6210 IPSocketAddress<br \/>\n    let port &#061; (server.localAddress as IPSocketAddress)?.port ?? 0<br \/>\n    println(&#034;\u670d\u52a1\u7aef\u5df2\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3&#xff1a;${port}&#034;)<\/p>\n<p>    \/\/ \u670d\u52a1\u7aef\u903b\u8f91\u653e\u8fdb\u65b0\u7ebf\u7a0b&#xff1a;accept\/read \u90fd\u4f1a\u963b\u585e\u7b49\u5f85<br \/>\n    spawn {<br \/>\n        let conn &#061; server.accept()              \/\/ \u7b49\u5230\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u8fdb\u6765<br \/>\n        let buf &#061; Array&lt;Byte&gt;(1024, {_ &#061;&gt; 0})<br \/>\n        let n &#061; conn.read(buf)                  \/\/ \u8bfb\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u5b57\u8282&#xff0c;\u8fd4\u56de\u5b9e\u9645\u8bfb\u5230\u7684\u4e2a\u6570<br \/>\n        let msg &#061; String.fromUtf8(buf[0..n])<br \/>\n        println(&#034;\u670d\u52a1\u7aef\u6536\u5230&#xff1a;${msg}&#034;)<br \/>\n        conn.write(&#034;echo: ${msg}&#034;.toArray())    \/\/ \u52a0\u4e2a\u524d\u7f00\u5199\u56de\u53bb<br \/>\n        conn.close()<br \/>\n        server.close()<br \/>\n    }<\/p>\n<p>    \/\/ \u4e3b\u7ebf\u7a0b\u626e\u6f14\u5ba2\u6237\u7aef&#xff1a;\u5411\u670d\u52a1\u7aef\u62e8\u53f7<br \/>\n    let client &#061; TcpSocket(&#034;127.0.0.1&#034;, port)<br \/>\n    client.connect()<br \/>\n    println(&#034;\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5&#xff1a;\u672c\u5730 ${client.localAddress} \u2192 \u5bf9\u7aef ${client.remoteAddress}&#034;)<br \/>\n    client.write(&#034;ping&#034;.toArray())              \/\/ \u53d1\u9001\u7684\u662f\u5b57\u8282\u6570\u7ec4<br \/>\n    let buf &#061; Array&lt;Byte&gt;(1024, {_ &#061;&gt; 0})<br \/>\n    let n &#061; client.read(buf)<br \/>\n    println(&#034;\u5ba2\u6237\u7aef\u6536\u5230&#xff1a;${String.fromUtf8(buf[0..n])}&#034;)<br \/>\n    client.close()<br \/>\n    return 0<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u8f93\u51fa&#xff08;\u7aef\u53e3\u53f7\u6bcf\u6b21\u8fd0\u884c\u90fd\u4e0d\u540c&#xff0c;\u8fd9\u662f\u7cfb\u7edf\u5206\u914d\u7684&#xff09;&#xff1a;<\/p>\n<p>\u670d\u52a1\u7aef\u5df2\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3&#xff1a;53013<br \/>\n\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5&#xff1a;\u672c\u5730 127.0.0.1:53014 \u2192 \u5bf9\u7aef 127.0.0.1:53013<br \/>\n\u670d\u52a1\u7aef\u6536\u5230&#xff1a;ping<br \/>\n\u5ba2\u6237\u7aef\u6536\u5230&#xff1a;echo: ping<\/p>\n<h4>2.3 \u9010\u6bb5\u89e3\u8bfb<\/h4>\n<p>\u51e0\u4e2a\u7b2c\u4e00\u6b21\u89c1\u9762\u7684\u77e5\u8bc6\u70b9&#xff1a;<\/p>\n<p>\u2460 \u7f51\u7edc\u4e0a\u7684\u6570\u636e\u53ea\u6709\u5b57\u8282&#xff0c;\u6ca1\u6709\u5b57\u7b26\u4e32\u3002 \u53d1\u9001\u524d\u8981\u628a\u5b57\u7b26\u4e32\u53d8\u6210\u5b57\u8282\u6570\u7ec4&#xff1a;&#034;ping&#034;.toArray()&#xff08;UTF-8 \u7f16\u7801&#xff0c;\u7b2c 6 \u8bfe\u8bb2\u8fc7&#xff09;&#xff1b;\u6536\u5230\u540e\u628a\u5b57\u8282\u5207\u7247\u8f6c\u56de\u5b57\u7b26\u4e32&#xff1a;String.fromUtf8(buf[0..n])\u3002\u8fd9\u4e2a&#034;\u5b57\u7b26\u4e32 \u2194 \u5b57\u8282&#034;\u7684\u8f6c\u6362\u662f\u6240\u6709\u7f51\u7edc\u7a0b\u5e8f\u7684\u6807\u914d\u52a8\u4f5c\u3002<\/p>\n<p>\u2461 read(buf) \u7684\u8fd4\u56de\u503c\u5f88\u91cd\u8981\u3002 \u5b83\u628a\u6570\u636e\u586b\u8fdb\u4f60\u7ed9\u7684\u7f13\u51b2\u533a&#xff0c;\u8fd4\u56de\u5b9e\u9645\u8bfb\u5230\u7684\u5b57\u8282\u6570 n\u2014\u2014\u4e0d\u4e00\u5b9a\u586b\u6ee1\u6574\u4e2a\u7f13\u51b2\u533a&#xff01;\u6240\u4ee5\u5207\u7247\u8981\u7528 buf[0..n] \u800c\u4e0d\u662f\u6574\u4e2a buf\u3002\u6b63\u5f0f\u7a0b\u5e8f\u91cc\u8981\u6309\u534f\u8bae\u7ea6\u5b9a\u5faa\u73af\u8bfb\u5230&#034;\u6536\u6ee1&#034;\u4e3a\u6b62&#xff08;\u89c1 FAQ&#xff09;\u3002<\/p>\n<p>\u2462 \u7528\u5b8c\u8981 close()\u3002 socket \u662f\u64cd\u4f5c\u7cfb\u7edf\u8d44\u6e90&#xff08;\u53e5\u67c4&#xff09;&#xff0c;\u4e0d\u5173\u5c31\u4f1a\u6cc4\u6f0f\u3002\u5ba2\u6237\u7aef\u5173 client&#xff0c;\u670d\u52a1\u7aef\u6bcf\u4e2a\u8fde\u63a5\u5173 conn&#xff0c;\u76d1\u542c\u53e3\u5173 server\u3002<\/p>\n<p>\u2463 \u7aef\u53e3 0 \u7684\u5999\u7528\u3002 bindAt: 0 \u8ba9\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u7a7a\u95f2\u7aef\u53e3&#xff0c;\u6d4b\u8bd5\u65f6\u4e0d\u7528\u64cd\u5fc3\u7aef\u53e3\u88ab\u5360\u7528&#xff1b;\u901a\u8fc7 localAddress \u80fd\u67e5\u5b9e\u9645\u5206\u5230\u51e0\u53f7\u3002localAddress \u7684\u7c7b\u578b\u662f\u62bd\u8c61\u7684 SocketAddress&#xff0c;\u8981\u7528\u7b2c 17 \u8bfe\u7684 as \u5411\u4e0b\u8f6c\u578b\u6210 IPSocketAddress \u624d\u6709 .port \u5c5e\u6027\u3002<\/p>\n<p>\u2464 \u8fde\u63a5\u5931\u8d25\u629b SocketException\u3002 \u6bd4\u5982\u8fde\u4e00\u4e2a\u6ca1\u4eba\u76d1\u542c\u7684\u7aef\u53e3&#xff0c;\u4f1a\u5f97\u5230\u8fd9\u6837\u7684\u5f02\u5e38&#xff08;\u5b9e\u6d4b\u6587\u6848&#xff09;&#xff1a;<\/p>\n<p>SocketException: Failed to connect 10061: No connection could be made because the target machine actively refused it.<\/p>\n<p>\u9519\u8bef\u7801 10061 \u662f Windows \u7684&#034;\u76ee\u6807\u673a\u5668\u62d2\u7edd\u8fde\u63a5&#034;\u3002\u7528\u7b2c 10 \u8bfe\u7684 try-catch \u628a\u5b83\u515c\u4f4f&#xff0c;\u5c31\u80fd\u7ed9\u51fa\u53cb\u597d\u63d0\u793a\u800c\u4e0d\u662f\u76f4\u63a5\u5d29\u6e83\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001UDP&#xff1a;\u4e0d\u6253\u7535\u8bdd&#xff0c;\u5bc4\u4fe1<\/h3>\n<p>UDP \u6ca1\u6709\u8fde\u63a5\u6982\u5ff5&#xff1a;\u4e0d\u9700\u8981 accept\u3001\u4e0d\u9700\u8981 connect&#xff0c;\u5199\u4e0a\u6536\u4ef6\u5730\u5740\u5c31\u53d1&#xff0c;\u5bf9\u65b9\u7ed1\u5b9a\u7aef\u53e3\u5c31\u80fd\u6536\u3002<\/p>\n<h4>3.1 \u4e00\u53d1\u4e00\u6536\u7684\u6700\u5c0f\u4f8b\u5b50<\/h4>\n<p>package netdemo<\/p>\n<p>import std.net.*<\/p>\n<p>main(): Int64 {<br \/>\n    \/\/ \u63a5\u6536\u7aef&#xff1a;\u7ed1\u5b9a 9999 \u7aef\u53e3\u7b49\u7740\u6536<br \/>\n    let receiver &#061; UdpSocket(bindAt: 9999)<br \/>\n    receiver.bind()<\/p>\n<p>    \/\/ \u53d1\u9001\u7aef&#xff1a;bindAt: 0&#xff0c;\u7aef\u53e3\u8ba9\u7cfb\u7edf\u968f\u4fbf\u5206<br \/>\n    let sender &#061; UdpSocket(bindAt: 0)<br \/>\n    sender.bind()<\/p>\n<p>    \/\/ \u6307\u5b9a\u6536\u4ef6\u5730\u5740&#xff0c;\u76f4\u63a5\u53d1\u2014\u2014\u6ca1\u6709&#034;\u5efa\u7acb\u8fde\u63a5&#034;\u8fd9\u4e00\u6b65<br \/>\n    let target &#061; IPSocketAddress.parse(&#034;127.0.0.1:9999&#034;)<br \/>\n    sender.sendTo(target, &#034;hello udp&#034;.toArray())<\/p>\n<p>    \/\/ \u6536&#xff1a;receiveFrom \u8fd4\u56de (\u6765\u6e90\u5730\u5740, \u5b9e\u9645\u5b57\u8282\u6570) \u5143\u7ec4<br \/>\n    let buf &#061; Array&lt;Byte&gt;(1024, {_ &#061;&gt; 0})<br \/>\n    let (from, n) &#061; receiver.receiveFrom(buf)<br \/>\n    println(&#034;\u6765\u81ea ${from} \u7684 ${n} \u5b57\u8282&#xff1a;${String.fromUtf8(buf[0..n])}&#034;)<\/p>\n<p>    sender.close()<br \/>\n    receiver.close()<br \/>\n    return 0<br \/>\n}<\/p>\n<p>\u8f93\u51fa&#xff08;\u6765\u6e90\u7aef\u53e3\u662f\u7cfb\u7edf\u968f\u673a\u5206\u7684&#xff09;&#xff1a;<\/p>\n<p>\u6765\u81ea 127.0.0.1:53619 \u7684 9 \u5b57\u8282&#xff1a;hello udp<\/p>\n<h4>3.2 \u548c TCP \u5bf9\u7167\u7740\u8bb0<\/h4>\n<table>\n<tr>\u52a8\u4f5cTCPUDP<\/tr>\n<tbody>\n<tr>\n<td>\u521b\u5efa<\/td>\n<td>TcpServerSocket(bindAt: port) \/ TcpSocket(ip, port)<\/td>\n<td>UdpSocket(bindAt: port)<\/td>\n<\/tr>\n<tr>\n<td>\u5efa\u7acb\u5173\u7cfb<\/td>\n<td>\u670d\u52a1\u7aef accept()\u3001\u5ba2\u6237\u7aef connect()<\/td>\n<td>\u6ca1\u6709&#xff0c;\u53cc\u65b9\u5404\u81ea bind() \u5373\u53ef<\/td>\n<\/tr>\n<tr>\n<td>\u53d1<\/td>\n<td>write(bytes)<\/td>\n<td>sendTo(\u5730\u5740, bytes)<\/td>\n<\/tr>\n<tr>\n<td>\u6536<\/td>\n<td>read(buf): Int64<\/td>\n<td>receiveFrom(buf): (SocketAddress, Int64)<\/td>\n<\/tr>\n<tr>\n<td>\u5730\u5740\u5bf9\u8c61<\/td>\n<td>\u6784\u9020\u65f6\u76f4\u63a5\u7ed9 ip\u3001port<\/td>\n<td>IPSocketAddress.parse(&#034;ip:port&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f receiveFrom \u8fd4\u56de\u7684\u662f\u5143\u7ec4&#xff08;\u7b2c 11 \u8bfe&#xff09;&#xff1a;\u7b2c\u4e00\u4e2a\u5206\u91cf\u662f\u53d1\u4ef6\u4eba\u5730\u5740\u2014\u2014UDP \u6ca1\u8fde\u63a5&#xff0c;\u4e0d\u9760\u8fd9\u4e2a\u8fd4\u56de\u503c\u4f60\u6839\u672c\u4e0d\u77e5\u9053\u8fd9\u5305\u662f\u8c01\u5bc4\u6765\u7684&#xff1b;\u60f3\u56de\u4fe1&#xff0c;\u628a\u521a\u624d\u7684 from \u5f53\u6536\u4ef6\u5730\u5740 sendTo \u56de\u53bb\u5c31\u884c\u3002<\/p>\n<p>UDP \u7684\u7b80\u5355\u662f\u6709\u4ee3\u4ef7\u7684&#xff1a;\u53d1\u51fa\u53bb\u7684\u6570\u636e\u62a5\u53ef\u80fd\u4e22\u5931\u3001\u53ef\u80fd\u4e71\u5e8f\u3001\u91cd\u590d\u5230\u8fbe&#xff0c;\u534f\u8bae\u4e00\u6982\u4e0d\u7ba1\u3002\u6240\u4ee5\u5b83\u53ea\u9002\u5408&#034;\u4e22\u4e00\u5e27\u65e0\u6240\u8c13&#034;\u7684\u573a\u666f\u3002\u9700\u8981\u53ef\u9760\u4f20\u8f93\u65f6&#xff0c;\u56de\u5230 TCP\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001HTTP \u5ba2\u6237\u7aef&#xff1a;\u4e09\u884c\u4ee3\u7801\u53d1\u4e00\u4e2a\u8bf7\u6c42<\/h3>\n<p>socket \u8981\u81ea\u5df1\u8bbe\u8ba1&#034;\u5b57\u8282\u600e\u4e48\u89e3\u91ca&#034;&#xff0c;\u800c HTTP \u5df2\u7ecf\u66ff\u4f60\u5b9a\u597d\u4e86\u683c\u5f0f\u3002\u65e5\u5e38\u5f00\u53d1\u4e2d\u4f60\u63a5\u89e6\u6700\u591a\u7684\u5176\u5b9e\u662f\u8fd9\u4e00\u5c42\u3002\u4ed3\u9889\u7684 HTTP \u80fd\u529b\u5728 stdx \u6269\u5c55\u5e93\u91cc&#xff08;\u7b2c 23 \u8bfe\u914d\u8fc7 bin-dependencies \u7684\u5de5\u7a0b\u76f4\u63a5\u7528&#xff09;\u3002<\/p>\n<h4>4.1 GET \u8bf7\u6c42<\/h4>\n<p>\u672c\u8282\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u8bbf\u95ee\u7684\u662f http:\/\/127.0.0.1:8080 \u4e0a\u7684\u672c\u5730\u670d\u52a1\u2014\u2014\u5b83\u7531\u7b2c\u4e94\u8282\u7684\u6700\u5c0f HTTP \u670d\u52a1\u63d0\u4f9b\u3002\u9605\u8bfb\u987a\u5e8f\u4e0d\u53d7\u5f71\u54cd&#xff0c;\u4f46\u8981\u4eb2\u624b\u8dd1\u901a\u7684\u8bdd&#xff0c;\u8bf7\u5148\u8df3\u5230\u7b2c\u4e94\u8282\u628a\u670d\u52a1\u7aef\u8dd1\u8d77\u6765&#xff08;\u7ec8\u7aef\u505c\u5728 serve \u963b\u585e&#xff09;&#xff0c;\u518d\u56de\u5230\u672c\u8282\u8fd0\u884c\u5ba2\u6237\u7aef\u3002<\/p>\n<p>package netdemo<\/p>\n<p>import stdx.net.http.*<\/p>\n<p>main(): Int64 {<br \/>\n    let client &#061; ClientBuilder().build()          \/\/ \u5ba2\u6237\u7aef\u7528 Builder \u9020<br \/>\n    let resp &#061; client.get(&#034;http:\/\/127.0.0.1:8080\/hello&#034;)<\/p>\n<p>    println(&#034;\u72b6\u6001\u7801&#xff1a;${resp.status}&#034;)              \/\/ UInt16&#xff0c;\u6bd4\u5982 200<\/p>\n<p>    \/\/ body \u662f InputStream&#xff08;\u8f93\u5165\u6d41&#xff09;&#xff0c;\u8981\u8bfb\u51fa\u6765\u624d\u662f\u5185\u5bb9<br \/>\n    let buf &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n    let n &#061; resp.body.read(buf)<br \/>\n    println(&#034;\u6b63\u6587&#xff1a;${String.fromUtf8(buf[0..n])}&#034;)<\/p>\n<p>    client.close()<br \/>\n    return 0<br \/>\n}<\/p>\n<p>\u914d\u5408\u7b2c\u4e94\u8282\u7684\u670d\u52a1\u8fd0\u884c&#xff0c;\u5b9e\u6d4b\u8f93\u51fa&#xff1a;<\/p>\n<p>\u72b6\u6001\u7801&#xff1a;200<br \/>\n\u6b63\u6587&#xff1a;Hello from Cangjie!<\/p>\n<p>\u4e09\u4e2a\u8981\u70b9&#xff1a;<\/p>\n<li>\u5ba2\u6237\u7aef\u7531 ClientBuilder().build() \u521b\u5efa&#xff0c;\u4e0d\u662f\u76f4\u63a5 Client()&#xff08;\u90a3\u6837\u7f16\u8bd1\u4f1a\u62a5 no matching constructor&#xff09;\u3002Builder \u4e0a\u8fd8\u80fd\u94fe\u5f0f\u8bbe\u7f6e\u8d85\u65f6\u7b49&#xff1a;ClientBuilder().readTimeout(Duration.second * 10).build()\u3002<\/li>\n<li>resp.body \u662f InputStream \u63a5\u53e3&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u63d2\u503c\u6253\u5370&#xff08;\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a should implement interface &#039;ToString&#039;&#xff09;&#xff0c;\u5fc5\u987b read \u5230\u5b57\u8282\u6570\u7ec4\u518d\u8f6c\u5b57\u7b26\u4e32\u2014\u2014\u548c socket \u8bfb\u6570\u636e\u662f\u540c\u4e00\u5957\u601d\u8def\u3002<\/li>\n<li>\u7528\u5b8c client.close()\u3002<\/li>\n<h4>4.2 POST \u63d0\u4ea4\u6570\u636e<\/h4>\n<p>post \u6709\u4e09\u4e2a\u91cd\u8f7d&#xff1a;\u6b63\u6587\u53ef\u4ee5\u7ed9 String\u3001Array&lt;UInt8&gt; \u6216 InputStream\u3002\u4f20\u5b57\u7b26\u4e32\u6700\u5e38\u7528&#xff1a;<\/p>\n<p>let json &#061; &#034;{\\\\&#034;name\\\\&#034;:\\\\&#034;cangjie\\\\&#034;,\\\\&#034;lesson\\\\&#034;:24}&#034;<br \/>\nlet resp &#061; client.post(&#034;http:\/\/127.0.0.1:8080\/echo&#034;, json)<br \/>\nprintln(&#034;\u63d0\u4ea4\u7ed3\u679c&#xff1a;${resp.status}&#034;)    \/\/ \u670d\u52a1\u7aef\u5904\u7406\u6210\u529f\u4e00\u822c\u56de 200 \u6216 201<\/p>\n<p>\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5\u4e00\u4e00\u5bf9\u5e94 HTTP \u52a8\u8bcd&#xff1a;client.put(url, body)\u3001client.delete(url)\u3001client.head(url)\u3001client.options(url)\u3002<\/p>\n<h4>4.3 \u8bfb\u54cd\u5e94\u5934<\/h4>\n<p>\u54cd\u5e94\u5934\u6309\u540d\u5b57\u53d6&#xff0c;getFirst \u8fd4\u56de Option&lt;String&gt;&#xff08;\u540c\u540d\u5934\u53ef\u80fd\u6709\u591a\u4e2a\u503c&#xff0c;\u4e5f\u6709\u4e0d\u5e26\u5934\u7684\u54cd\u5e94&#xff0c;\u6240\u4ee5\u662f Option&#xff09;&#xff1a;<\/p>\n<p>match (resp.headers.getFirst(&#034;Content-Type&#034;)) {<br \/>\n    case Some(ct) &#061;&gt; println(&#034;\u5185\u5bb9\u7c7b\u578b&#xff1a;${ct}&#034;)<br \/>\n    case None &#061;&gt; println(&#034;\u54cd\u5e94\u6ca1\u6709 Content-Type \u5934&#034;)<br \/>\n}<\/p>\n<h4>4.4 \u4e00\u4e2a\u5b9e\u6d4b\u51fa\u6765\u7684\u786c\u9650\u5236&#xff1a;HTTPS \u8981\u914d TLS<\/h4>\n<p>\u5982\u679c\u76f4\u63a5 client.get(&#034;https:\/\/&#8230;&#034;)&#xff0c;\u4f1a\u6536\u5230\u8fd9\u4e2a\u5f02\u5e38&#xff08;\u5b9e\u6d4b\u6587\u6848&#xff09;&#xff1a;<\/p>\n<p>HttpException: TLS must be configured when HTTPS requests are sent.<\/p>\n<p>\u610f\u601d\u662f&#xff1a;\u8bbf\u95ee HTTPS \u7ad9\u70b9\u5fc5\u987b\u5148\u7ed9\u5ba2\u6237\u7aef\u914d\u7f6e TLS \u8bc1\u4e66&#xff08;ClientBuilder().tlsConfig(&#8230;)&#xff09;\u3002\u8bc1\u4e66\u914d\u7f6e\u6d89\u53ca CA \u8bc1\u4e66\u6587\u4ef6&#xff0c;\u8d85\u51fa\u5165\u95e8\u8bfe\u8303\u56f4\u2014\u2014\u672c\u8bfe\u6240\u6709\u5b9e\u9a8c\u90fd\u7528 http:\/\/127.0.0.1 \u672c\u673a\u670d\u52a1&#xff0c;\u4e0d\u8e29\u8fd9\u4e2a\u5751\u3002\u77e5\u9053\u6709\u8fd9\u56de\u4e8b\u5373\u53ef\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001HTTP \u670d\u52a1\u7aef&#xff1a;\u6ce8\u518c\u8def\u5f84&#xff0c;\u5199\u4e0b\u54cd\u5e94<\/h3>\n<p>\u670d\u52a1\u7aef\u540c\u6837\u662f Builder \u521b\u5efa&#xff0c;\u6838\u5fc3\u52a8\u4f5c\u53ea\u6709\u4e00\u4e2a&#xff1a;\u7ed9\u8def\u5f84\u6ce8\u518c\u5904\u7406\u51fd\u6570&#xff08;handler&#xff09;\u3002<\/p>\n<h4>5.1 \u6700\u5c0f HTTP \u670d\u52a1<\/h4>\n<p>package netdemo<\/p>\n<p>import stdx.net.http.*<\/p>\n<p>main(): Int64 {<br \/>\n    let server &#061; ServerBuilder().addr(&#034;127.0.0.1&#034;).port(8080).build()<\/p>\n<p>    \/\/ \u6ce8\u518c&#xff1a;\u51e1\u662f\u8bf7\u6c42\u8def\u5f84 \/hello \u7684&#xff0c;\u90fd\u4ea4\u7ed9\u8fd9\u4e2a lambda \u5904\u7406<br \/>\n    server.distributor.register(&#034;\/hello&#034;, { ctx &#061;&gt;<br \/>\n        ctx.responseBuilder.body(&#034;Hello from Cangjie!&#034;).build()<br \/>\n    })<\/p>\n<p>    println(&#034;\u670d\u52a1\u5df2\u542f\u52a8&#xff1a;http:\/\/127.0.0.1:8080\/hello  &#xff08;\u6309 Ctrl&#043;C \u505c\u6b62&#xff09;&#034;)<br \/>\n    server.serve()    \/\/ \u963b\u585e\u5728\u8fd9\u91cc&#xff0c;\u4e00\u76f4\u63a5\u5ba2<br \/>\n    return 0<br \/>\n}<\/p>\n<p>\u8dd1\u8d77\u6765\u540e&#xff0c;\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/127.0.0.1:8080\/hello&#xff0c;\u9875\u9762\u4e0a\u5c31\u80fd\u770b\u5230 Hello from Cangjie!\u3002\u8bbf\u95ee\u4efb\u4f55\u6ca1\u6ce8\u518c\u7684\u8def\u5f84&#xff08;\u5982 \/abc&#xff09;&#xff0c;\u670d\u52a1\u7aef\u4f1a\u81ea\u52a8\u8fd4\u56de 404\u2014\u2014distributor&#xff08;\u5206\u53d1\u5668&#xff09;\u5185\u7f6e\u4e86\u8fd9\u4e2a\u515c\u5e95\u884c\u4e3a\u3002<\/p>\n<p>\u4e09\u4e2a\u6838\u5fc3\u5bf9\u8c61&#xff1a;<\/p>\n<table>\n<tr>\u5bf9\u8c61\u5e72\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>server.distributor<\/td>\n<td>\u5206\u53d1\u5668&#xff1a;register(\u8def\u5f84, \u5904\u7406\u51fd\u6570) \u628a URL \u8def\u5f84\u548c handler \u7ed1\u5b9a<\/td>\n<\/tr>\n<tr>\n<td>ctx&#xff08;HttpContext&#xff09;<\/td>\n<td>\u4e00\u6b21\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587&#xff1a;ctx.request \u62ff\u8bf7\u6c42&#xff0c;ctx.responseBuilder \u9020\u54cd\u5e94<\/td>\n<\/tr>\n<tr>\n<td>ctx.responseBuilder<\/td>\n<td>\u54cd\u5e94\u6784\u9020\u5668&#xff1a;status()\u3001header()\u3001body() \u94fe\u5f0f\u8bbe\u7f6e&#xff0c;\u6700\u540e build() \u53d1\u51fa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>handler \u7684\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5c31\u662f\u8fd4\u56de\u52a8\u4f5c&#xff0c;build() \u8c03\u7528\u5373\u5b8c\u6210\u54cd\u5e94&#xff1b;\u4ed3\u9889\u5141\u8bb8 lambda \u672b\u5c3e\u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c\u88ab\u81ea\u52a8\u4e22\u5f03&#xff0c;\u6240\u4ee5\u4e0d\u9700\u8981\u989d\u5916\u5199 return\u3002<\/p>\n<h4>5.2 \u8bfb\u8bf7\u6c42&#xff1a;\u65b9\u6cd5\u3001\u8def\u5f84\u3001\u6b63\u6587<\/h4>\n<p>\u5728 handler \u91cc\u901a\u8fc7 ctx.request \u80fd\u62ff\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u4e00\u5207&#xff1a;<\/p>\n<p>server.distributor.register(&#034;\/echo&#034;, { ctx &#061;&gt;<br \/>\n    let req &#061; ctx.request<br \/>\n    println(&#034;\u65b9\u6cd5&#061;${req.method}  \u8def\u5f84&#061;${req.url}  \u534f\u8bae&#061;${req.version}&#034;)<\/p>\n<p>    \/\/ \u8bf7\u6c42\u6b63\u6587\u540c\u6837\u662f\u8f93\u5165\u6d41&#xff0c;\u8bfb\u51fa\u6765<br \/>\n    let buf &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n    let n &#061; req.body.read(buf)<br \/>\n    let text &#061; String.fromUtf8(buf[0..n])<\/p>\n<p>    ctx.responseBuilder<br \/>\n        .status(201)                        \/\/ \u81ea\u5b9a\u4e49\u72b6\u6001\u7801<br \/>\n        .header(&#034;X-Reply&#034;, &#034;cangjie&#034;)       \/\/ \u81ea\u5b9a\u4e49\u54cd\u5e94\u5934<br \/>\n        .body(&#034;you sent: ${text}&#034;)<br \/>\n        .build()<br \/>\n})<\/p>\n<p>\u7528\u5ba2\u6237\u7aef\u9a8c\u8bc1&#xff08;client.post(&#034;http:\/\/127.0.0.1:8080\/echo&#034;, &#034;ping body&#034;)&#xff09;&#xff0c;\u5b9e\u6d4b\u7ed3\u679c&#xff1a;<\/p>\n<p>\u670d\u52a1\u7aef\u6253\u5370&#xff1a;\u65b9\u6cd5&#061;POST  \u8def\u5f84&#061;\/echo  \u534f\u8bae&#061;HTTP\/1.1<br \/>\n           &#xff08;\u4ee5\u53ca&#xff09;req body: ping body<br \/>\n\u5ba2\u6237\u7aef\u62ff\u5230&#xff1a;status&#061;201\u3001X-Reply&#061;Some(cangjie)\u3001\u6b63\u6587 &#034;you sent: ping body&#034;<\/p>\n<h4>5.3 \u6309 HTTP \u65b9\u6cd5\u5206\u6d41<\/h4>\n<p>REST \u98ce\u683c\u7684\u670d\u52a1\u4e60\u60ef&#034;\u540c\u4e00\u8def\u5f84\u3001\u4e0d\u540c\u65b9\u6cd5\u5e72\u4e0d\u540c\u4e8b&#034;&#xff1a;GET \/todos \u67e5\u5217\u8868\u3001POST \/todos \u65b0\u589e\u3002\u5728 handler \u91cc if (req.method &#061;&#061; &#034;GET&#034;) \u5206\u652f\u5373\u53ef&#xff0c;\u4e0b\u4e00\u8282\u7684\u5b9e\u6218\u5c31\u662f\u8fd9\u4e48\u505a\u7684\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u7efc\u5408\u5b9e\u6218&#xff1a;\u5f85\u529e\u6e05\u5355 JSON API<\/h3>\n<p>\u628a\u524d\u4e09\u8bfe\u7684\u4e1c\u897f\u4e32\u8d77\u6765&#xff1a;HTTP&#xff08;\u672c\u8bfe&#xff09;\u4f20\u6570\u636e &#043; JSON&#xff08;\u7b2c 23 \u8bfe&#xff09;\u505a\u683c\u5f0f &#043; \u96c6\u5408&#xff08;\u7b2c 8 \u8bfe&#xff09;\u5b58\u5185\u5b58\u3002\u505a\u4e00\u4e2a\u8ff7\u4f60\u540e\u7aef&#xff1a;<\/p>\n<ul>\n<li>GET \/todos \u2192 \u8fd4\u56de\u5168\u90e8\u5f85\u529e\u7684 JSON \u6570\u7ec4&#xff1b;<\/li>\n<li>POST \/todos \u2192 \u8bf7\u6c42\u4f53\u662f {&#034;title&#034;:&#034;&#8230;&#034;}&#xff0c;\u670d\u52a1\u7aef\u89e3\u6790\u540e\u52a0\u8fdb\u5217\u8868&#xff0c;\u8fd4\u56de 201&#xff1b;<\/li>\n<li>\u5176\u4ed6\u65b9\u6cd5 \u2192 405 Method Not Allowed\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u8ba9\u4f60\u4e00\u6761 cjpm run \u770b\u5230\u5b8c\u6574\u6d41\u7a0b&#xff0c;\u670d\u52a1\u7aef\u7528 spawn \u8d77\u5728\u540e\u53f0\u7ebf\u7a0b&#xff0c;\u4e3b\u7ebf\u7a0b\u5f53\u5ba2\u6237\u7aef\u8fde\u6253\u4e09\u4e2a\u8bf7\u6c42&#xff1a;<\/p>\n<p>package netdemo<\/p>\n<p>import stdx.net.http.*<br \/>\nimport stdx.encoding.json.*<br \/>\nimport std.collection.*<\/p>\n<p>main(): Int64 {<br \/>\n    \/\/ \u5185\u5b58\u91cc\u7684\u5f85\u529e\u5217\u8868&#xff08;\u7a0b\u5e8f\u9000\u51fa\u5373\u4e22\u5931&#xff0c;\u6301\u4e45\u5316\u662f\u7b2c 22 \u8bfe\u6587\u4ef6 IO \u7684\u6d3b&#xff09;<br \/>\n    let todos &#061; ArrayList&lt;String&gt;([&#034;\u5b66\u4e60\u4ed3\u9889&#034;, &#034;\u5199\u7b2c24\u8bfe&#034;])<\/p>\n<p>    \/\/ \u2500\u2500 \u670d\u52a1\u7aef \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n    let server &#061; ServerBuilder().addr(&#034;127.0.0.1&#034;).port(8082).build()<br \/>\n    server.distributor.register(&#034;\/todos&#034;, { ctx &#061;&gt;<br \/>\n        let req &#061; ctx.request<br \/>\n        if (req.method &#061;&#061; &#034;GET&#034;) {<br \/>\n            \/\/ \u5217\u8868 \u2192 JSON \u6570\u7ec4<br \/>\n            let arr &#061; JsonArray()<br \/>\n            for (t in todos) {<br \/>\n                arr.add(JsonString(t))<br \/>\n            }<br \/>\n            ctx.responseBuilder<br \/>\n                .header(&#034;Content-Type&#034;, &#034;application\/json; charset&#061;utf-8&#034;)<br \/>\n                .body(arr.toJsonString())<br \/>\n                .build()<br \/>\n            return<br \/>\n        }<br \/>\n        if (req.method &#061;&#061; &#034;POST&#034;) {<br \/>\n            \/\/ \u8bfb\u8bf7\u6c42\u4f53 \u2192 \u89e3\u6790 JSON \u2192 \u53d6\u51fa title<br \/>\n            let buf &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n            let n &#061; req.body.read(buf)<br \/>\n            let obj &#061; JsonValue.fromStr(String.fromUtf8(buf[0..n])).asObject()<br \/>\n            let title &#061; obj[&#034;title&#034;].asString().getValue()<br \/>\n            todos.add(title)<br \/>\n            ctx.responseBuilder.status(201).body(&#034;{\\\\&#034;added\\\\&#034;:\\\\&#034;${title}\\\\&#034;}&#034;).build()<br \/>\n            return<br \/>\n        }<br \/>\n        ctx.responseBuilder.status(405).body(&#034;Method Not Allowed&#034;).build()<br \/>\n    })<br \/>\n    spawn { server.serve() }<br \/>\n    sleep(500 * Duration.millisecond)    \/\/ \u7b49\u670d\u52a1\u5c31\u7eea&#xff08;sleep \u4e0e Duration \u6765\u81ea std.core&#xff09;<\/p>\n<p>    \/\/ \u2500\u2500 \u5ba2\u6237\u7aef \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n    let client &#061; ClientBuilder().build()<\/p>\n<p>    \/\/ \u2460 GET \u5217\u8868<br \/>\n    let r1 &#061; client.get(&#034;http:\/\/127.0.0.1:8082\/todos&#034;)<br \/>\n    let b1 &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n    let n1 &#061; r1.body.read(b1)<br \/>\n    println(&#034;GET \/todos \u2192 ${r1.status}: ${String.fromUtf8(b1[0..n1])}&#034;)<\/p>\n<p>    \/\/ \u2461 POST \u65b0\u589e\u4e00\u6761<br \/>\n    let r2 &#061; client.post(&#034;http:\/\/127.0.0.1:8082\/todos&#034;, &#034;{\\\\&#034;title\\\\&#034;:\\\\&#034;\u53d1\u5e03\u6559\u7a0b\\\\&#034;}&#034;)<br \/>\n    let b2 &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n    let n2 &#061; r2.body.read(b2)<br \/>\n    println(&#034;POST \/todos \u2192 ${r2.status}: ${String.fromUtf8(b2[0..n2])}&#034;)<\/p>\n<p>    \/\/ \u2462 \u518d GET&#xff0c;\u786e\u8ba4\u65b0\u6761\u76ee\u771f\u7684\u5b58\u8fdb\u53bb\u4e86<br \/>\n    let r3 &#061; client.get(&#034;http:\/\/127.0.0.1:8082\/todos&#034;)<br \/>\n    let b3 &#061; Array&lt;Byte&gt;(8192, {_ &#061;&gt; 0})<br \/>\n    let n3 &#061; r3.body.read(b3)<br \/>\n    println(&#034;GET \/todos \u2192 ${r3.status}: ${String.fromUtf8(b3[0..n3])}&#034;)<\/p>\n<p>    \/\/ \u2463 \u53d1\u4e2a DELETE&#xff0c;\u89e6\u53d1 405 \u5206\u652f<br \/>\n    let r4 &#061; client.delete(&#034;http:\/\/127.0.0.1:8082\/todos&#034;)<br \/>\n    println(&#034;DELETE \/todos \u2192 ${r4.status}&#034;)<\/p>\n<p>    client.close()<br \/>\n    server.close()<br \/>\n    return 0<br \/>\n}<\/p>\n<p>\u5b9e\u6d4b\u8f93\u51fa&#xff1a;<\/p>\n<p>GET \/todos \u2192 200: [<br \/>\n  &#034;\u5b66\u4e60\u4ed3\u9889&#034;,<br \/>\n  &#034;\u5199\u7b2c24\u8bfe&#034;<br \/>\n]<br \/>\nPOST \/todos \u2192 201: {&#034;added&#034;:&#034;\u53d1\u5e03\u6559\u7a0b&#034;}<br \/>\nGET \/todos \u2192 200: [<br \/>\n  &#034;\u5b66\u4e60\u4ed3\u9889&#034;,<br \/>\n  &#034;\u5199\u7b2c24\u8bfe&#034;,<br \/>\n  &#034;\u53d1\u5e03\u6559\u7a0b&#034;<br \/>\n]<br \/>\nDELETE \/todos \u2192 405<\/p>\n<p>&#xff08;JsonArray.toJsonString() \u9ed8\u8ba4\u8f93\u51fa\u5e26\u6362\u884c\u7684\u7f8e\u5316\u683c\u5f0f&#xff0c;\u6240\u4ee5\u6570\u7ec4\u662f\u591a\u884c\u7684\u3002&#xff09;<\/p>\n<p>\u8fd0\u884c\u672b\u5c3e\u4f60\u53ef\u80fd\u8fd8\u4f1a\u770b\u5230\u4e00\u884c WARN &#8230; ConnectionException: Socket is closed.\u2014\u2014\u8fd9\u662f server.close() \u5173\u95ed\u76d1\u542c\u65f6\u5185\u90e8\u65e5\u5fd7\u7684\u6b63\u5e38\u566a\u97f3&#xff0c;\u4e0d\u5f71\u54cd\u7ed3\u679c&#xff0c;\u65e0\u89c6\u5373\u53ef\u3002<\/p>\n<p>\u8fd9\u4e00\u4e2a\u7a0b\u5e8f\u91cc\u5176\u5b9e\u5df2\u7ecf\u85cf\u7740\u771f\u5b9e Web \u540e\u7aef\u7684\u5b8c\u6574\u9aa8\u67b6&#xff1a;\u8def\u7531\u6ce8\u518c\u3001\u65b9\u6cd5\u5206\u6d41\u3001\u8bf7\u6c42\u4f53\u89e3\u6790\u3001\u4e1a\u52a1\u6570\u636e\u8bfb\u5199\u3001JSON \u54cd\u5e94\u3001\u72b6\u6001\u7801\u8bed\u4e49\u3002\u628a\u5b83\u62c6\u6210\u4e24\u4e2a\u5de5\u7a0b&#xff08;\u4e00\u4e2a\u53ea serve\u3001\u4e00\u4e2a\u53ea\u53d1\u8bf7\u6c42&#xff09;&#xff0c;\u5c31\u662f\u751f\u4ea7\u5f62\u6001\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001CIDE \u5b9e\u64cd<\/h3>\n<li>\u65b0\u5efa\u5de5\u7a0b&#xff1a;CIDE \u83dc\u5355&#034;\u6587\u4ef6 \u2192 \u65b0\u5efa\u9879\u76ee&#034;&#xff0c;\u7c7b\u578b\u9009&#034;\u53ef\u6267\u884c\u7a0b\u5e8f&#034;&#xff0c;\u9879\u76ee\u540d\u5982 netdemo\u3002<\/li>\n<li>HTTP \u90e8\u5206\u8981\u914d stdx&#xff1a;\u672c\u8bfe\u7b2c\u4e8c\u3001\u4e09\u8282&#xff08;TCP\/UDP&#xff09;\u53ea\u7528 std.net&#xff0c;\u4e0d\u914d\u4e5f\u80fd\u8dd1&#xff1b;\u7b2c\u56db&#xff5e;\u516d\u8282\u7528\u5230 stdx.net.http&#xff0c;\u9700\u8981\u6309\u7b2c 23 \u8bfe\u7b2c\u4e8c\u8282\u5728 cjpm.toml \u672b\u5c3e\u8ffd\u52a0 bin-dependencies \u7684 path-option \u6307\u5411 stdx \u89e3\u538b\u76ee\u5f55\u3002<\/li>\n<li>\u7c98\u8d34\u4ee3\u7801\u8fd0\u884c&#xff1a;\u628a\u4efb\u4e00\u8282\u4ee3\u7801\u7c98\u8d34\u8fdb src\/main.cj&#xff0c;\u70b9\u51fb\u8fd0\u884c&#xff08;\u6216\u7ec8\u7aef cjpm run&#xff09;\u3002<\/li>\n<li>\u6d4f\u89c8\u5668\u8054\u52a8&#xff1a;\u7b2c\u4e94\u8282\u7684\u6700\u5c0f\u670d\u52a1\u8dd1\u8d77\u6765\u540e&#xff08;\u7ec8\u7aef\u505c\u5728 serve \u963b\u585e&#xff09;&#xff0c;\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/127.0.0.1:8080\/hello \u4eb2\u773c\u770b\u54cd\u5e94&#xff1b;\u5728 CIDE \u91cc\u6309&#034;\u505c\u6b62&#034;\u7ed3\u675f\u670d\u52a1\u3002<\/li>\n<li>\u9632\u706b\u5899\u63d0\u793a&#xff1a;\u9996\u6b21\u7ed1\u5b9a\u7aef\u53e3\u65f6 Windows \u53ef\u80fd\u5f39\u51fa&#034;\u662f\u5426\u5141\u8bb8\u6b64\u5e94\u7528\u8bbf\u95ee\u7f51\u7edc&#034;\u2014\u2014\u672c\u8bfe\u90fd\u662f\u672c\u673a\u56de\u73af&#xff08;127.0.0.1&#xff09;&#xff0c;\u5141\u8bb8\u6216\u62d2\u7edd\u90fd\u4e0d\u5f71\u54cd\u672c\u673a\u5b9e\u9a8c\u3002<\/li>\n<hr \/>\n<h3>\u516b\u3001\u5e38\u7528 API \u901f\u67e5<\/h3>\n<p>std.net&#xff08;TCP\/UDP&#xff0c;\u6807\u51c6\u5e93\u81ea\u5e26&#xff09;<\/p>\n<table>\n<tr>\u7c7b\u578b \/ \u6210\u5458\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>TcpServerSocket(bindAt: port)<\/td>\n<td>\u521b\u5efa TCP \u76d1\u542c socket<\/td>\n<\/tr>\n<tr>\n<td>.bind() \/ .accept(): TcpSocket<\/td>\n<td>\u7ed1\u5b9a\u5e76\u76d1\u542c \/ \u963b\u585e\u7b49\u5f85\u4e00\u4e2a\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>.localAddress: SocketAddress<\/td>\n<td>\u672c\u5730\u5730\u5740&#xff08;\u8f6c IPSocketAddress \u540e\u53d6 .port&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>TcpSocket(ip, port)<\/td>\n<td>\u521b\u5efa TCP \u5ba2\u6237\u7aef socket<\/td>\n<\/tr>\n<tr>\n<td>.connect()<\/td>\n<td>\u5efa\u7acb\u8fde\u63a5&#xff08;\u5931\u8d25\u629b SocketException&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>.write(Array&lt;Byte&gt;) \/ .read(Array&lt;Byte&gt;): Int64<\/td>\n<td>\u53d1\u9001 \/ \u63a5\u6536&#xff08;\u8fd4\u56de\u5b9e\u9645\u5b57\u8282\u6570&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>.remoteAddress \/ .localAddress<\/td>\n<td>\u5bf9\u7aef \/ \u672c\u5730\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>UdpSocket(bindAt: port)<\/td>\n<td>\u521b\u5efa UDP socket<\/td>\n<\/tr>\n<tr>\n<td>.sendTo(SocketAddress, Array&lt;Byte&gt;)<\/td>\n<td>\u5411\u6307\u5b9a\u5730\u5740\u53d1\u4e00\u4e2a\u6570\u636e\u62a5<\/td>\n<\/tr>\n<tr>\n<td>.receiveFrom(buf): (SocketAddress, Int64)<\/td>\n<td>\u6536\u4e00\u4e2a\u6570\u636e\u62a5&#xff0c;\u8fd4\u56de&#xff08;\u6765\u6e90\u5730\u5740&#xff0c;\u5b57\u8282\u6570&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>IPSocketAddress.parse(&#034;ip:port&#034;)<\/td>\n<td>\u89e3\u6790\u5730\u5740\u5b57\u7b26\u4e32&#xff08;\u53e6\u6709 tryParse \u8fd4\u56de Option&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u7528 .close() \/ .isClosed()<\/td>\n<td>\u91ca\u653e\u8d44\u6e90 \/ \u67e5\u72b6\u6001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>stdx.net.http&#xff08;HTTP&#xff0c;\u9700 stdx&#xff09;<\/p>\n<table>\n<tr>\u7c7b\u578b \/ \u6210\u5458\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>ClientBuilder().build(): Client<\/td>\n<td>\u521b\u5efa\u5ba2\u6237\u7aef&#xff08;\u53ef\u52a0 .readTimeout(&#8230;) \u7b49&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>client.get(url) \/ client.delete(url)<\/td>\n<td>\u65e0\u6b63\u6587\u7684\u8bf7\u6c42<\/td>\n<\/tr>\n<tr>\n<td>client.post(url, body) \/ client.put(url, body)<\/td>\n<td>body \u652f\u6301 String \/ Array&lt;UInt8&gt; \/ InputStream<\/td>\n<\/tr>\n<tr>\n<td>resp.status: UInt16<\/td>\n<td>\u72b6\u6001\u7801&#xff08;200 \/ 404 \/ 500\u2026&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>resp.body: InputStream<\/td>\n<td>\u54cd\u5e94\u6b63\u6587&#xff0c;\u9700 read<\/td>\n<\/tr>\n<tr>\n<td>resp.headers.getFirst(name): Option&lt;String&gt;<\/td>\n<td>\u53d6\u54cd\u5e94\u5934<\/td>\n<\/tr>\n<tr>\n<td>client.close()<\/td>\n<td>\u5173\u95ed\u5ba2\u6237\u7aef\u8fde\u63a5\u6c60<\/td>\n<\/tr>\n<tr>\n<td>ServerBuilder().addr(ip).port(p).build(): Server<\/td>\n<td>\u521b\u5efa\u670d\u52a1\u7aef<\/td>\n<\/tr>\n<tr>\n<td>server.distributor.register(path, handler)<\/td>\n<td>\u6ce8\u518c\u8def\u5f84\u5904\u7406\u51fd\u6570&#xff08;\u672a\u6ce8\u518c\u8def\u5f84\u81ea\u52a8 404&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>ctx.request.method \/ .url \/ .body \/ .headers<\/td>\n<td>\u8bfb\u8bf7\u6c42<\/td>\n<\/tr>\n<tr>\n<td>ctx.responseBuilder.status(&#8230;).header(&#8230;).body(&#8230;).build()<\/td>\n<td>\u9020\u54cd\u5e94<\/td>\n<\/tr>\n<tr>\n<td>server.serve() \/ server.close()<\/td>\n<td>\u542f\u52a8&#xff08;\u963b\u585e&#xff09;\/ \u505c\u6b62<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u4e5d\u3001\u5e38\u89c1\u95ee\u9898 FAQ<\/h3>\n<p>Q1&#xff1a;\u8fd0\u884c TCP \u4ee3\u7801\u5361\u5728 accept \u6216 read \u4e0d\u52a8&#xff1f; \u8fd9\u4e24\u4e2a\u65b9\u6cd5\u90fd\u662f\u963b\u585e\u7b49\u5f85&#xff1a;\u6ca1\u6709\u8fde\u63a5\u8fdb\u6765 accept \u4e0d\u8fd4\u56de&#xff0c;\u6ca1\u6709\u6570\u636e\u5230\u8fbe read \u4e0d\u8fd4\u56de\u3002\u68c0\u67e5\u5ba2\u6237\u7aef\u662f\u5426\u771f\u7684\u53d1\u8d77\u4e86 connect\/write&#xff1b;\u672c\u8bfe\u793a\u4f8b\u7528 spawn \u628a\u670d\u52a1\u7aef\u653e\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5c31\u662f\u4e3a\u4e86\u907f\u514d\u4e3b\u7ebf\u7a0b\u88ab\u5b83\u5361\u6b7b\u3002<\/p>\n<p>Q2&#xff1a;read(buf) \u4e3a\u4ec0\u4e48\u6709\u65f6\u8bfb\u5230\u7684\u6bd4\u9884\u671f\u5c11&#xff1f; TCP \u662f\u5b57\u8282\u6d41&#xff0c;\u4e0d\u4fdd\u8bc1\u4e00\u6b21 read \u6b63\u597d\u6536\u6ee1\u5bf9\u65b9\u4e00\u6b21 write \u7684\u5185\u5bb9&#xff08;\u53ef\u80fd\u62c6\u6210\u591a\u6b21&#xff0c;\u4e5f\u53ef\u80fd\u51e0\u6b21\u5408\u5e76&#xff09;\u2014\u2014\u8fd9\u5c31\u662f\u6240\u8c13&#034;\u7c98\u5305\/\u534a\u5305&#034;\u3002\u6b63\u5f0f\u534f\u8bae\u8981\u81ea\u5e26\u8fb9\u754c&#xff08;\u5982\u56fa\u5b9a\u957f\u5ea6\u3001\u5206\u9694\u7b26\u3001\u6216\u50cf HTTP \u90a3\u6837\u5148\u58f0\u660e Content-Length&#xff09;&#xff0c;\u7136\u540e\u5faa\u73af read \u76f4\u5230\u6536\u6ee1\u3002\u5165\u95e8\u9636\u6bb5\u7684\u5c0f\u6d88\u606f\u5927\u6982\u7387\u4e00\u6b21\u6536\u9f50&#xff0c;\u77e5\u9053\u8fd9\u4e2a\u5751\u5b58\u5728\u5373\u53ef\u3002<\/p>\n<p>Q3&#xff1a;\u91cd\u590d\u542f\u52a8\u670d\u52a1\u6ca1\u62a5&#034;\u7aef\u53e3\u88ab\u5360\u7528&#034;&#xff0c;\u4f46\u65b0\u5b9e\u4f8b\u4e00\u4e2a\u8bf7\u6c42\u90fd\u6536\u4e0d\u5230&#xff1f; \u8fd9\u4e0d\u662f\u7384\u5b66&#xff0c;\u800c\u662f\u4e00\u4e2a\u6bd4\u62a5\u9519\u66f4\u9690\u853d\u7684\u5751&#xff08;\u5df2\u5b9e\u6d4b&#xff09;&#xff1a;\u4ed3\u9889\u7684 TcpServerSocket \u9ed8\u8ba4\u5f00\u542f\u7aef\u53e3\u590d\u7528&#xff08;SO_REUSEADDR&#xff09;&#xff0c;\u4e24\u4e2a\u5b9e\u4f8b bind \u540c\u4e00\u4e2a\u7aef\u53e3\u90fd\u4f1a\u6210\u529f\u2014\u2014\u7528 netstat -ano \u80fd\u770b\u5230\u4e24\u6761 LISTENING \u8bb0\u5f55\u2014\u2014\u4f46\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u4f1a\u88ab\u5176\u4e2d\u4e00\u4e2a\u5b9e\u4f8b&#xff08;\u5b9e\u6d4b\u4e3a\u5148\u542f\u52a8\u7684\u90a3\u4e2a&#xff09;\u5168\u90e8\u63a5\u8d70&#xff0c;\u53e6\u4e00\u4e2a\u5b9e\u4f8b\u9759\u9ed8\u6536\u4e0d\u5230\u4efb\u4f55\u8bf7\u6c42\u3002\u6240\u4ee5\u5f00\u53d1\u65f6\u522b\u8ba9\u4e24\u4e2a\u5b9e\u4f8b\u7528\u540c\u4e00\u7aef\u53e3&#xff1b;\u6d4b\u8bd5\u56fe\u7701\u4e8b\u5c31\u7528 bindAt: 0 \u8ba9\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u3002<\/p>\n<p>\u8865\u5145&#xff1a;\u5982\u679c\u7ed1\u5b9a\u7cfb\u7edf\u4fdd\u7559\u6216\u9700\u8981\u7279\u6743\u7684\u7aef\u53e3&#xff08;\u5b9e\u6d4b\u7528 135\u3001445&#xff09;&#xff0c;\u4f1a\u6536\u5230 10013 \u9519\u8bef\u800c\u4e0d\u662f&#034;\u7aef\u53e3\u5360\u7528&#034;&#xff1a; SocketException: Failed to bind 10013: An attempt was made to access a socket in a way forbidden by its access permissions.<\/p>\n<p>Q4&#xff1a;\u8bbf\u95ee https \u7f51\u5740\u629b HttpException: TLS must be configured&#8230;&#xff1f; \u89c1 4.4 \u8282&#xff1a;HTTPS \u9700\u8981\u7528 ClientBuilder().tlsConfig(&#8230;) \u914d\u7f6e\u8bc1\u4e66\u3002\u5165\u95e8\u5b9e\u9a8c\u8bf7\u7528\u672c\u673a http:\/\/127.0.0.1 \u670d\u52a1\u3002<\/p>\n<p>Q5&#xff1a;resp.body \u76f4\u63a5 println \u62a5 should implement interface &#039;ToString&#039;&#xff1f; body \u662f InputStream \u6d41&#xff0c;\u4e0d\u662f\u5b57\u7b26\u4e32\u3002\u5148 read \u8fdb\u5b57\u8282\u6570\u7ec4\u518d String.fromUtf8&#xff0c;\u672c\u8bfe\u6bcf\u4e2a\u793a\u4f8b\u90fd\u662f\u8fd9\u4e48\u505a\u7684\u3002<\/p>\n<p>Q6&#xff1a;\u7a0b\u5e8f\u7ed3\u675f\u65f6\u6253\u5370\u4e00\u884c WARN &#8230; ConnectionException: Socket is closed.&#xff1f; \u8fd9\u662f\u5173\u95ed\u670d\u52a1\u7aef\u65f6\u5e95\u5c42\u65e5\u5fd7\u7684\u6b63\u5e38\u8f93\u51fa&#xff0c;\u4e0d\u662f\u9519\u8bef&#xff0c;\u5ffd\u7565\u5373\u53ef\u3002<\/p>\n<p>Q7&#xff1a;\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u4e4b\u95f4\u600e\u4e48\u8f6c&#xff1f; \u53d1\u9001&#xff1a;&#034;&#8230;&#034;.toArray()&#xff08;UTF-8 \u7f16\u7801&#xff09;&#xff1b;\u63a5\u6536&#xff1a;String.fromUtf8(buf[0..n])\u3002\u6ce8\u610f\u5207\u7247\u8981\u7528\u5b9e\u9645\u8bfb\u5230\u7684\u957f\u5ea6 n\u3002<\/p>\n<hr \/>\n<h3>\u5341\u3001\u8bfe\u540e\u7ec3\u4e60<\/h3>\n<li>\u5e26\u65f6\u95f4\u6233\u7684\u56de\u663e&#xff1a;\u6539\u9020 2.2 \u7684 echo \u670d\u52a1&#xff0c;\u670d\u52a1\u7aef\u56de\u5199\u65f6\u5728\u6d88\u606f\u524d\u52a0\u4e0a\u6536\u5230\u65f6\u7684\u7cfb\u7edf\u65f6\u95f4&#xff08;\u63d0\u793a&#xff1a;import std.time.* \u540e\u7528 DateTime.now()&#xff09;&#xff0c;\u5ba2\u6237\u7aef\u6253\u5370\u5b8c\u6574\u56de\u663e\u3002<\/li>\n<li>UDP \u95ee\u7b54\u673a&#xff1a;\u5199\u4e00\u5bf9 UDP \u7a0b\u5e8f&#xff08;\u6216\u5355\u7a0b\u5e8f\u53cc socket&#xff09;&#xff1a;\u53d1\u9001\u7aef\u53d1\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\u5982 &#034;7&#034;&#xff0c;\u63a5\u6536\u7aef receiveFrom \u540e\u628a\u5b83\u89e3\u6790\u6210\u6574\u6570\u3001\u8ba1\u7b97\u5e73\u65b9&#xff0c;\u7528\u6765\u6e90\u5730\u5740 sendTo \u56de &#034;49&#034;&#xff1b;\u53d1\u9001\u7aef\u518d receiveFrom \u6253\u5370\u7b54\u6848\u3002<\/li>\n<li>\u54cd\u5e94\u5934\u89c2\u5bdf\u5458&#xff1a;\u7528 HTTP \u5ba2\u6237\u7aef GET \u7b2c\u4e94\u8282\u81ea\u5efa\u7684 \/hello \u670d\u52a1&#xff0c;\u9664\u4e86\u6253\u5370\u72b6\u6001\u7801\u548c\u6b63\u6587&#xff0c;\u518d\u7528 resp.headers.getFirst(&#034;Content-Type&#034;) \u7684 match \u5206\u652f\u6253\u5370\u5185\u5bb9\u7c7b\u578b\u3002<\/li>\n<li>\u81ea\u6211\u56de\u663e\u670d\u52a1&#xff1a;\u5199\u4e00\u4e2a HTTP \u670d\u52a1\u6ce8\u518c \/reflect&#xff0c;handler \u628a\u8bf7\u6c42\u7684 method \u548c url \u62fc\u6210\u4e00\u884c\u6587\u672c\u8fd4\u56de&#xff08;\u5982 \u4f60\u53d1\u7684\u662f GET \/reflect&#xff09;&#xff1b;\u7528\u5ba2\u6237\u7aef\u5206\u522b\u4ee5 GET \u548c POST \u8bbf\u95ee\u9a8c\u8bc1\u3002<\/li>\n<li>\u7ed9\u5f85\u529e API \u52a0\u4e24\u4e2a\u7aef\u70b9&#xff1a;\u5728\u7b2c\u516d\u8282\u5b9e\u6218\u57fa\u7840\u4e0a&#xff1a;\u2460 \u6ce8\u518c \/todos\/count&#xff0c;GET \u8fd4\u56de {&#034;count&#034;: 3}&#xff08;\u5f53\u524d\u6761\u6570&#xff09;&#xff1b;\u2461 \u628a 405 \u5206\u652f\u7684\u54cd\u5e94\u4e5f\u6539\u6210 JSON&#xff1a;{&#034;error&#034;:&#034;method not allowed&#034;}&#xff0c;\u5e76\u628a\u72b6\u6001\u7801\u5e38\u91cf\u5199\u5728\u9876\u90e8 let STATUS_METHOD_NOT_ALLOWED: UInt16 &#061; 405\u3002<\/li>\n<hr \/>\n<h3>\u4e0b\u8282\u9884\u544a<\/h3>\n<p>\u672c\u8bfe\u7684\u5f85\u529e API \u662f&#034;\u4eba\u8089\u9a8c\u8bc1&#034;\u7684\u2014\u2014\u8dd1\u4e00\u6b21\u3001\u770b\u8f93\u51fa\u3001\u51ed\u773c\u775b\u5224\u65ad\u5bf9\u4e0d\u5bf9\u3002\u529f\u80fd\u4e00\u591a&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u65e2\u4e0d\u9760\u8c31\u4e5f\u6ca1\u6cd5\u56de\u5f52\u3002\u7b2c 25 \u8bfe \u5355\u5143\u6d4b\u8bd5 \u5c06\u4ecb\u7ecd\u4ed3\u9889\u6807\u51c6\u5e93\u81ea\u5e26\u7684 std.unittest&#xff1a;&#064;Test \u6807\u8bb0\u6d4b\u8bd5\u7528\u4f8b\u3001&#064;Assert \u65ad\u8a00\u7ed3\u679c\u3001cjpm test \u4e00\u952e\u8dd1\u5168\u5957\u2014\u2014\u8ba9\u4f60\u7ed9\u51fd\u6570\u5199\u4e00\u4efd&#034;\u81ea\u52a8\u5316\u7684\u8003\u5377&#034;&#xff0c;\u4ee5\u540e\u6bcf\u6539\u4e00\u884c\u4ee3\u7801\u90fd\u80fd\u7acb\u523b\u77e5\u9053\u6709\u6ca1\u6709\u628a\u65e7\u529f\u80fd\u78b0\u574f\u3002<\/p>\n<hr \/>\n<p>\u7cfb\u5217\u8bf4\u660e&#xff1a;\u672c\u7cfb\u5217\u57fa\u4e8e Windows \u5e73\u53f0 &#043; CIDE &#043; \u4ed3\u9889 SDK&#xff08;1.2.0&#xff09;&#043; stdx&#xff08;1.2.0-beta.02.1&#xff09;\u7f16\u5199&#xff0c;\u6240\u6709\u4ee3\u7801\u5747\u5df2\u5b9e\u9645\u7f16\u8bd1\u8fd0\u884c\u901a\u8fc7\u3002\u5982\u9047 SDK \/ stdx \u7248\u672c\u5dee\u5f02\u5bfc\u81f4\u7684\u7ec6\u8282\u51fa\u5165&#xff0c;\u4ee5\u4f60\u672c\u5730\u7248\u672c\u4e3a\u51c6&#xff0c;\u6b22\u8fce\u8bc4\u8bba\u533a\u4ea4\u6d41\u3002<\/p>\n<hr \/>\n<h3>&#x1f4e5; \u5de5\u5177\u4e0b\u8f7d<\/h3>\n<p>\u672c\u7cfb\u5217\u5168\u7a0b\u4f7f\u7528\u7684\u4ed3\u9889 IDE \u2014\u2014 CIDE&#xff08;\u514d\u8d39\u5f00\u6e90\u3001\u793e\u533a\u7248&#xff09;&#xff1a;<\/p>\n<ul>\n<li>GitCode \u4ed3\u5e93 \/ \u5b89\u88c5\u5305\u4e0b\u8f7d&#xff1a;https:\/\/gitcode.com\/wp_upala\/cide<\/li>\n<li>\u6253\u5f00\u9875\u9762\u540e\u8fdb\u5165 \u53d1\u884c\u7248&#xff08;Releases&#xff09;&#xff0c;\u4e24\u79cd\u5305\u4efb\u9009\u5176\u4e00&#xff1a;\n<ul>\n<li>\u5b89\u88c5\u7248&#xff1a;\u4e0b\u8f7d CIDE-&lt;\u7248\u672c&gt;-x64-Setup.exe&#xff0c;\u53cc\u51fb\u5b89\u88c5&#xff0c;\u9002\u5408\u65e5\u5e38\u957f\u671f\u4f7f\u7528&#xff1b;<\/li>\n<li>\u514d\u5b89\u88c5\u7248&#xff08;Portable&#xff09;&#xff1a;\u4e0b\u8f7d CIDE-&lt;\u7248\u672c&gt;-x64-Portable.zip&#xff0c;\u89e3\u538b\u5230\u4efb\u610f\u76ee\u5f55\u5373\u7528&#xff0c;\u4e0d\u5199\u6ce8\u518c\u8868\u3001\u4e0d\u7559\u5b89\u88c5\u75d5\u8ff9&#xff0c;\u62f7\u5230 U \u76d8\u4e5f\u80fd\u5728\u522b\u7684\u7535\u8111\u76f4\u63a5\u8fd0\u884c&#xff08;\u5305\u5185\u9644\u300a\u4f7f\u7528\u8bf4\u660e.txt\u300b&#xff09;\u3002\u9002\u5408\u5148\u8bd5\u7528\u3001\u6216\u5728\u53d7\u9650\u7535\u8111\u4e0a\u5b66\u4e60\u672c\u7cfb\u5217\u8bfe\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4ed3\u9889 SDK \u4e0e\u6269\u5c55\u5e93 stdx \u8bf7\u524d\u5f80\u4ed3\u9889\u7f16\u7a0b\u8bed\u8a00\u5b98\u7f51\u4e0b\u8f7d&#xff1a;https:\/\/cangjie-lang.cn<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8&#xff1a;\u4ece socket \u5230 HTTP&#xff0c;\u8ba9\u7a0b\u5e8f\u5f00\u53e3\u8bf4\u8bdd \u7b2c 23 \u8bfe\u7ed3\u5c3e\u8bf4\u8fc7&#xff1a;JSON \u6700\u5e38\u89c1\u7684\u51fa\u573a\u4f4d\u7f6e\u4e0d\u662f\u6587\u4ef6&#xff0c;\u800c\u662f\u7f51\u7edc\u2014\u2014\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u8981\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u628a\u7ed3\u679c\u4ee5 JSON \u8fd4\u56de\u3002\u90a3\\&#8221;\u7f51\u7edc\\&#8221;\u8fd9\u4e00\u5c42\u5230\u5e95\u957f\u4ec0\u4e48\u6837&#xff1f;\u672c\u8bfe\u4ece\u6700\u5e95\u5c42\u7684 TCP\/UDP socket \u8bb2\u8d77&#xff0c;\u4eb2\u624b\u5199\u4e00\u4e2a\u56de\u663e\u670d\u52a1&#xff1b;\u518d\u4e0a\u5230\u5e94\u7528\u5c42\u7684 HTTP&#xff0c;\u7528 std<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[70,190,227],"topic":[],"class_list":["post-112453","post","type-post","status-publish","format-standard","hentry","category-server","tag-ide","tag-190","tag-227"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \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\/112453.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8&#xff1a;\u4ece socket \u5230 HTTP&#xff0c;\u8ba9\u7a0b\u5e8f\u5f00\u53e3\u8bf4\u8bdd \u7b2c 23 \u8bfe\u7ed3\u5c3e\u8bf4\u8fc7&#xff1a;JSON \u6700\u5e38\u89c1\u7684\u51fa\u573a\u4f4d\u7f6e\u4e0d\u662f\u6587\u4ef6&#xff0c;\u800c\u662f\u7f51\u7edc\u2014\u2014\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u8981\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u628a\u7ed3\u679c\u4ee5 JSON \u8fd4\u56de\u3002\u90a3&quot;\u7f51\u7edc&quot;\u8fd9\u4e00\u5c42\u5230\u5e95\u957f\u4ec0\u4e48\u6837&#xff1f;\u672c\u8bfe\u4ece\u6700\u5e95\u5c42\u7684 TCP\/UDP socket \u8bb2\u8d77&#xff0c;\u4eb2\u624b\u5199\u4e00\u4e2a\u56de\u663e\u670d\u52a1&#xff1b;\u518d\u4e0a\u5230\u5e94\u7528\u5c42\u7684 HTTP&#xff0c;\u7528 std\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/112453.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-03T10:40:54+00:00\" \/>\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=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/112453.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/112453.html\",\"name\":\"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-03T10:40:54+00:00\",\"dateModified\":\"2026-10-03T10:40:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/112453.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/112453.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/112453.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011\"}]},{\"@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":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \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\/112453.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8&#xff1a;\u4ece socket \u5230 HTTP&#xff0c;\u8ba9\u7a0b\u5e8f\u5f00\u53e3\u8bf4\u8bdd \u7b2c 23 \u8bfe\u7ed3\u5c3e\u8bf4\u8fc7&#xff1a;JSON \u6700\u5e38\u89c1\u7684\u51fa\u573a\u4f4d\u7f6e\u4e0d\u662f\u6587\u4ef6&#xff0c;\u800c\u662f\u7f51\u7edc\u2014\u2014\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u8981\u6570\u636e&#xff0c;\u670d\u52a1\u5668\u628a\u7ed3\u679c\u4ee5 JSON \u8fd4\u56de\u3002\u90a3\"\u7f51\u7edc\"\u8fd9\u4e00\u5c42\u5230\u5e95\u957f\u4ec0\u4e48\u6837&#xff1f;\u672c\u8bfe\u4ece\u6700\u5e95\u5c42\u7684 TCP\/UDP socket \u8bb2\u8d77&#xff0c;\u4eb2\u624b\u5199\u4e00\u4e2a\u56de\u663e\u670d\u52a1&#xff1b;\u518d\u4e0a\u5230\u5e94\u7528\u5c42\u7684 HTTP&#xff0c;\u7528 std","og_url":"https:\/\/www.wsisp.com\/helps\/112453.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-03T10:40:54+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/112453.html","url":"https:\/\/www.wsisp.com\/helps\/112453.html","name":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-03T10:40:54+00:00","dateModified":"2026-10-03T10:40:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/112453.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/112453.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/112453.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u4ed3\u9889\u8bed\u8a00\u5165\u95e8 \u00b7 \u7b2c24\u8bfe\u3011"}]},{"@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\/112453","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=112453"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/112453\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=112453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=112453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=112453"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=112453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}