{"id":537,"date":"2025-12-06T15:57:17","date_gmt":"2025-12-06T07:57:17","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=537"},"modified":"2025-12-06T15:57:17","modified_gmt":"2025-12-06T07:57:17","slug":"%e5%89%8d%e7%ab%af%e8%bf%9e%e6%8e%a5tcp%e6%9c%8d%e5%8a%a1%e6%8e%a5%e6%94%b6%e6%95%b0%e6%8d%ae%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4%e9%99%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%af%e8%bf%9e%e6%8e%a5tcp%e6%9c%8d%e5%8a%a1%e6%8e%a5%e6%94%b6%e6%95%b0%e6%8d%ae%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4%e9%99%84%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u524d\u7aef\u8fde\u63a5tcp\u670d\u52a1\u63a5\u6536\u6570\u636e\u7684\u5b9e\u73b0\u6b65\u9aa4(\u9644\u4ee3\u7801)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u524d\u7aef\u8fde\u63a5 TCP \u670d\u52a1\u5e76\u63a5\u6536\u6570\u636e\u7684\u5b8c\u6574\u5b9e\u73b0\u6b65\u9aa4 + \u53ef\u76f4\u63a5\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b<\/strong>\u3002<br>\u26a0\ufe0f\u6ce8\u610f\uff1a<strong>\u6d4f\u89c8\u5668\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528 TCP Socket<\/strong>\uff08\u5b89\u5168\u9650\u5236\uff09\uff0c\u6240\u4ee5\u5fc5\u987b\u901a\u8fc7\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6d4f\u89c8\u5668\u524d\u7aef \u2194 WebSocket \u2194 WebSocket-\u8f6c-TCP \u670d\u52a1\u5668 \u2194 \u771f\u5b9e TCP \u670d\u52a1<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u4f60\u5b8c\u6574\u6d41\u7a0b\u3001\u67b6\u6784\u3001\u4ee3\u7801\uff08Node.js WebSocket\u2192TCP \u8f6c\u53d1\u5668 + \u524d\u7aef JS\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>\u4e00\u3001\u5b9e\u73b0\u65b9\u5f0f\u67b6\u6784\u56fe<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;\u6d4f\u89c8\u5668\u524d\u7aef] -- WebSocket --&gt; &#x5B;Node.js \u8f6c\u53d1\u5668] -- TCP Socket --&gt; &#x5B;TCP \u670d\u52a1\u7aef]\n\n<\/pre><\/div>\n\n\n<p>\u6d4f\u89c8\u5668\u53ea\u80fd\u8fde WebSocket<br>Node.js \u4f7f\u7528 <code>net<\/code> \u6a21\u5757\u8fde TCP<br>Node \u901a\u8fc7 WebSocket \u628a TCP \u6570\u636e\u8f6c\u53d1\u7ed9\u524d\u7aef<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>\u4e8c\u3001\u5b9e\u73b0\u6b65\u9aa4\uff08\u975e\u5e38\u6e05\u6670\uff09<\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa\u4e00\u4e2a Node.js WebSocket \u2192 TCP \u8f6c\u53d1\u670d\u52a1<\/strong><\/h2>\n\n\n\n<p>\u7528\u4e8e\u628a WebSocket \u5ba2\u6237\u7aef\uff08\u6d4f\u89c8\u5668\uff09\u8f6c\u6210 TCP socket\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u5b89\u88c5\u4f9d\u8d56<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm install ws\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc server.js<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ server.js\nconst WebSocket = require(&#039;ws&#039;);\nconst net = require(&#039;net&#039;);\n\n\/\/ WebSocket \u670d\u52a1\u7aef\nconst wss = new WebSocket.Server({ port: 8080 });\n\nconsole.log(&#039;WebSocket Server started on ws:\/\/localhost:8080&#039;);\n\nwss.on(&#039;connection&#039;, (ws) =&gt; {\n  console.log(&#039;Browser connected&#039;);\n\n  \/\/ \u6bcf\u4e2a\u6d4f\u89c8\u5668\u8fde\u63a5 \u2192 \u521b\u5efa\u4e00\u4e2a TCP \u8fde\u63a5\n  const tcpClient = new net.Socket();\n\n  tcpClient.connect(5000, &#039;127.0.0.1&#039;, () =&gt; {\n    console.log(&#039;Connected to TCP server&#039;);\n  });\n\n  \/\/ TCP \u2192 WebSocket\uff08\u63a8\u7ed9\u524d\u7aef\uff09\n  tcpClient.on(&#039;data&#039;, (data) =&gt; {\n    ws.send(data.toString());\n  });\n\n  \/\/ TCP \u5173\u95ed\n  tcpClient.on(&#039;close&#039;, () =&gt; {\n    console.log(&#039;TCP connection closed&#039;);\n    ws.close();\n  });\n\n  tcpClient.on(&#039;error&#039;, (err) =&gt; {\n    console.error(&#039;TCP error:&#039;, err);\n    ws.close();\n  });\n\n  \/\/ WebSocket \u2192 TCP\uff08\u524d\u7aef\u53d1\u7684\u6570\u636e\u5199\u5230 TCP\uff09\n  ws.on(&#039;message&#039;, (msg) =&gt; {\n    tcpClient.write(msg);\n  });\n\n  ws.on(&#039;close&#039;, () =&gt; {\n    console.log(&#039;Browser disconnected&#039;);\n    tcpClient.destroy();\n  });\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u670d\u52a1\u5668<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnode server.js\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>\u6b65\u9aa4 2\uff1a\u6d4f\u89c8\u5668\u524d\u7aef\u8fde\u63a5 WebSocket\uff0c\u95f4\u63a5\u6536 TCP \u6570\u636e<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc index.html<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html&gt;\n&amp;lt;body&gt;\n  &amp;lt;h2&gt;\u524d\u7aef\u8fde\u63a5 TCP \u7684\u6f14\u793a&amp;lt;\/h2&gt;\n  &amp;lt;div id=&quot;log&quot;&gt;&amp;lt;\/div&gt;\n\n  &amp;lt;script&gt;\n    const ws = new WebSocket(&quot;ws:\/\/localhost:8080&quot;);\n\n    ws.onopen = () =&gt; {\n      console.log(&quot;\u5df2\u8fde\u63a5 Node WebSocket&quot;);\n      ws.send(&quot;hello tcp&quot;);   \/\/ \u7ed9 TCP \u670d\u52a1\u53d1\u70b9\u6d4b\u8bd5\u6570\u636e\n    };\n\n    ws.onmessage = (event) =&gt; {\n      const log = document.getElementById(&quot;log&quot;);\n      log.innerHTML += `&amp;lt;p&gt;\u6536\u5230\u6570\u636e\uff1a${event.data}&amp;lt;\/p&gt;`;\n      console.log(&quot;TCP \u6d88\u606f \u2192 WebSocket \u2192 \u524d\u7aef\uff1a&quot;, event.data);\n    };\n\n    ws.onclose = () =&gt; {\n      console.log(&quot;\u8fde\u63a5\u5173\u95ed&quot;);\n    };\n  &amp;lt;\/script&gt;\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<p>\u53ea\u8981 TCP \u670d\u52a1\u7aef\u53d1\u9001\u6570\u636e \u2192 \u524d\u7aef\u5c31\u80fd\u9a6c\u4e0a\u6536\u5230\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>\u6b65\u9aa4 3\uff1a\u51c6\u5907\u4f60\u7684 TCP \u670d\u52a1\u7aef\u6d4b\u8bd5\uff08\u53ef\u9009\uff09<\/strong><\/h1>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u5feb\u901f\u6d4b\u8bd5\uff0c\u53ef\u7528\u8fd9\u4e2a 5000 \u7aef\u53e3\u7684\u5c0f TCP server\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ tcp-server.js\nconst net = require(&#039;net&#039;);\n\nconst server = net.createServer((socket) =&gt; {\n  console.log(&#039;Client connected&#039;);\n\n  socket.write(&quot;\u6b22\u8fce\u6765\u81eaTCP\u7684\u7b2c\u4e00\u6761\u6d88\u606f\\n&quot;);\n\n  socket.on(&#039;data&#039;, (data) =&gt; {\n    console.log(&quot;\u6536\u5230\u524d\u7aef\u53d1\u6765\u7684\uff1a&quot;, data.toString());\n    socket.write(&quot;\u5df2\u6536\u5230\u4f60\u53d1\u7684\uff1a&quot; + data.toString());\n  });\n});\n\nserver.listen(5000, () =&gt; {\n  console.log(&#039;TCP server on port 5000&#039;);\n});\n\n<\/pre><\/div>\n\n\n<p>\u8fd0\u884c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnode tcp-server.js\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>\u4e09\u3001\u6548\u679c\uff1a<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6d4f\u89c8\u5668\u6253\u5f00 index.html<\/li>\n\n\n\n<li>\u81ea\u52a8\u8fde\u63a5 ws:\/\/localhost:8080<\/li>\n\n\n\n<li>Node.js \u81ea\u52a8\u8fde\u63a5 TCP \u670d\u52a1 127.0.0.1:5000<\/li>\n\n\n\n<li>TCP \u2192 Node \u2192 WebSocket \u2192 \u524d\u7aef<br>\u524d\u7aef\u5c31\u80fd\u770b\u5230 TCP \u670d\u52a1\u53d1\u6765\u7684\u6d88\u606f\u4e86<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udf81 <strong>\u56db\u3001\u53ef\u6269\u5c55\u529f\u80fd\uff08\u4f60\u9700\u8981\u6211\u53ef\u4ee5\u7ee7\u7eed\u5199\uff09<\/strong><\/h1>\n\n\n\n<p>\u6211\u8fd8\u53ef\u4ee5\u5e2e\u4f60\u6269\u5c55\uff1a<\/p>\n\n\n\n<p>\u2713 \u81ea\u52a8\u91cd\u8fde TCP<br>\u2713 \u591a\u5ba2\u6237\u7aef\u4e92\u76f8\u9694\u79bb<br>\u2713 \u5b9e\u73b0 JSON \u534f\u8bae\u5c01\u88c5<br>\u2713 \u670d\u52a1\u5fc3\u8df3\u673a\u5236<br>\u2713 \u4e0d\u540c TCP \u670d\u52a1\u7684\u8d1f\u8f7d\u5747\u8861<br>\u2713 \u642d\u5efa\u751f\u4ea7\u73af\u5883\u7248\u672c\uff08pm2 + nginx \u53cd\u4ee3\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u524d\u7aef\u8fde\u63a5 TCP \u670d\u52a1\u5e76\u63a5\u6536\u6570\u636e\u7684\u5b8c\u6574\u5b9e\u73b0\u6b65\u9aa4 + \u53ef\u76f4\u63a5\u8fd0\u884c\u7684\u4ee3&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%af%e8%bf%9e%e6%8e%a5tcp%e6%9c%8d%e5%8a%a1%e6%8e%a5%e6%94%b6%e6%95%b0%e6%8d%ae%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4%e9%99%84%e4%bb%a3%e7%a0%81\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=537"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/537\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/537\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}