{"id":6306,"date":"2024-07-11T14:59:17","date_gmt":"2024-07-11T06:59:17","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6306"},"modified":"2024-07-11T14:59:17","modified_gmt":"2024-07-11T06:59:17","slug":"websocket-%e5%92%8c-socket-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6306","title":{"rendered":"WebSocket \u548c Socket \u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<p>WebSocket \u548c Socket \u662f\u4e24\u79cd\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u7684\u6280\u672f\uff0c\u5b83\u4eec\u5728\u7528\u9014\u3001\u529f\u80fd\u548c\u5b9e\u73b0\u65b9\u5f0f\u4e0a\u6709\u4e00\u4e9b\u663e\u8457\u7684\u533a\u522b\u3002<\/p>\n<h3>1. WebSocket<\/h3>\n<p><strong>\u6982\u8ff0<\/strong>\uff1a<\/p>\n<ul>\n<li>WebSocket \u662f\u4e00\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u63d0\u4f9b\u4e86\u57fa\u4e8e TCP \u7684\u5168\u53cc\u5de5\u901a\u4fe1\u901a\u9053\u3002\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e4b\u95f4\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\uff0c\u800c\u65e0\u9700\u91cd\u590d\u5efa\u7acb\u8fde\u63a5\u3002<\/li>\n<li>WebSocket \u7531 IETF \u6807\u51c6\u5316\uff0cRFC 6455 \u662f\u5176\u89c4\u8303\u3002<\/li>\n<li>WebSocket \u9002\u7528\u4e8e\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5b9e\u65f6\u901a\u4fe1\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5b9e\u65f6\u5e94\u7528\uff0c\u5982\u804a\u5929\u5e94\u7528\u3001\u5728\u7ebf\u6e38\u620f\u3001\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u7b49\u3002<\/li>\n<\/ul>\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5168\u53cc\u5de5\u901a\u4fe1<\/strong>\uff1a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u53ef\u4ee5\u968f\u65f6\u53d1\u9001\u6d88\u606f\uff0c\u800c\u65e0\u9700\u7b49\u5f85\u5bf9\u65b9\u54cd\u5e94\u3002<\/li>\n<li><strong>\u4f4e\u5ef6\u8fdf<\/strong>\uff1a\u76f8\u6bd4\u4e8e HTTP \u8f6e\u8be2\uff0cWebSocket \u63d0\u4f9b\u4e86\u66f4\u4f4e\u7684\u5ef6\u8fdf\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u91cd\u590d\u5efa\u7acb\u8fde\u63a5\u7684\u5f00\u9500\u3002<\/li>\n<li><strong>\u57fa\u4e8e\u6d88\u606f<\/strong>\uff1aWebSocket \u4f20\u8f93\u7684\u662f\u5b8c\u6574\u7684\u6d88\u606f\uff0c\u800c\u4e0d\u662f\u6570\u636e\u6d41\u4e2d\u7684\u7247\u6bb5\u3002<\/li>\n<li><strong>\u957f\u8fde\u63a5<\/strong>\uff1aWebSocket \u8fde\u63a5\u53ef\u4ee5\u957f\u65f6\u95f4\u4fdd\u6301\u6253\u5f00\uff0c\u51cf\u5c11\u4e86\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u5173\u95ed\u7684\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p><strong>\u5de5\u4f5c\u6d41\u7a0b<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u63e1\u624b\u9636\u6bb5<\/strong>\uff1aWebSocket \u8fde\u63a5\u4ece HTTP \u534f\u8bae\u5347\u7ea7\u800c\u6765\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a\u5e26\u6709\u7279\u5b9a\u5934\u7684 HTTP \u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u54cd\u5e94\u5e76\u5b8c\u6210\u534f\u8bae\u5347\u7ea7\u3002<\/li>\n<li><strong>\u6570\u636e\u4f20\u8f93<\/strong>\uff1a\u63e1\u624b\u5b8c\u6210\u540e\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u53ef\u4ee5\u8fdb\u884c\u53cc\u5411\u6570\u636e\u4f20\u8f93\uff0c\u6570\u636e\u5e27\u4ee5\u6d88\u606f\u7684\u5f62\u5f0f\u53d1\u9001\u3002<\/li>\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong>\uff1a\u4efb\u4f55\u4e00\u65b9\u90fd\u53ef\u4ee5\u968f\u65f6\u5173\u95ed WebSocket \u8fde\u63a5\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5ba2\u6237\u7aef JavaScript \u793a\u4f8b\nconst socket = new WebSocket(&#039;ws:\/\/example.com\/socket&#039;);\n\nsocket.onopen = function(event) {\n    console.log(&#039;WebSocket is open now.&#039;);\n    socket.send(&#039;Hello Server!&#039;);\n};\n\nsocket.onmessage = function(event) {\n    console.log(&#039;Message from server&#039;, event.data);\n};\n\nsocket.onclose = function(event) {\n    console.log(&#039;WebSocket is closed now.&#039;);\n};\n\nsocket.onerror = function(error) {\n    console.log(&#039;WebSocket error:&#039;, error);\n};<\/code><\/pre>\n<h3>2. Socket<\/h3>\n<p><strong>\u6982\u8ff0<\/strong>\uff1a<\/p>\n<ul>\n<li>Socket \u662f\u4e00\u79cd\u66f4\u5e95\u5c42\u7684\u7f51\u7edc\u901a\u4fe1\u673a\u5236\uff0c\u7528\u4e8e\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u5b83\u53ef\u4ee5\u57fa\u4e8e\u591a\u79cd\u534f\u8bae\uff0c\u5982 TCP \u548c UDP\u3002<\/li>\n<li>Socket \u7f16\u7a0b\u63a5\u53e3\u63d0\u4f9b\u4e86\u5728\u7f51\u7edc\u4e0a\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u7684\u80fd\u529b\uff0c\u662f\u6784\u5efa\u5404\u79cd\u7f51\u7edc\u5e94\u7528\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u534f\u8bae\u65e0\u5173<\/strong>\uff1aSocket \u53ef\u4ee5\u4f7f\u7528 TCP\u3001UDP \u7b49\u534f\u8bae\uff0c\u63d0\u4f9b\u4e0d\u540c\u7684\u901a\u4fe1\u7279\u6027\u3002<\/li>\n<li><strong>\u57fa\u4e8e\u6d41\u6216\u6570\u636e\u62a5<\/strong>\uff1a\u4f7f\u7528 TCP \u65f6\uff0cSocket \u63d0\u4f9b\u57fa\u4e8e\u6d41\u7684\u901a\u4fe1\uff1b\u4f7f\u7528 UDP \u65f6\uff0cSocket \u63d0\u4f9b\u57fa\u4e8e\u6570\u636e\u62a5\u7684\u901a\u4fe1\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1aSocket \u7f16\u7a0b\u7075\u6d3b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u81ea\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\u548c\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0<\/strong>\uff1aSocket \u662f\u8de8\u5e73\u53f0\u7684\uff0c\u51e0\u4e4e\u6240\u6709\u64cd\u4f5c\u7cfb\u7edf\u548c\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301 Socket \u7f16\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>\u5de5\u4f5c\u6d41\u7a0b\uff08\u57fa\u4e8e TCP\uff09<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa Socket<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a Socket \u5bf9\u8c61\u3002<\/li>\n<li><strong>\u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3<\/strong>\uff1a\u5c06 Socket \u7ed1\u5b9a\u5230\u7279\u5b9a\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u3002<\/li>\n<li><strong>\u76d1\u542c\u8fde\u63a5<\/strong>\uff1a\u5728\u670d\u52a1\u5668\u7aef\uff0cSocket \u76d1\u542c\u4f20\u5165\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u5efa\u7acb\u8fde\u63a5<\/strong>\uff1a\u5ba2\u6237\u7aef\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u63a5\u53d7\u8fde\u63a5\u3002<\/li>\n<li><strong>\u6570\u636e\u4f20\u8f93<\/strong>\uff1a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u901a\u8fc7 Socket \u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong>\uff1a\u5b8c\u6210\u6570\u636e\u4f20\u8f93\u540e\uff0c\u5173\u95ed Socket \u8fde\u63a5\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\"># \u670d\u52a1\u5668\u7aef Python \u793a\u4f8b\nimport socket\n\nserver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nserver_socket.bind((&#039;localhost&#039;, 8080))\nserver_socket.listen(5)\n\nwhile True:\n    client_socket, addr = server_socket.accept()\n    print(&#039;Connection from&#039;, addr)\n    data = client_socket.recv(1024)\n    if data:\n        print(&#039;Received:&#039;, data.decode(&#039;utf-8&#039;))\n        client_socket.sendall(b&#039;Hello Client&#039;)\n    client_socket.close()<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5ba2\u6237\u7aef Python \u793a\u4f8b\nimport socket\n\nclient_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nclient_socket.connect((&#039;localhost&#039;, 8080))\nclient_socket.sendall(b&#039;Hello Server&#039;)\ndata = client_socket.recv(1024)\nprint(&#039;Received:&#039;, data.decode(&#039;utf-8&#039;))\nclient_socket.close()<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<ul>\n<li><strong>WebSocket<\/strong> \u662f\u4e00\u79cd\u9ad8\u7ea7\u534f\u8bae\uff0c\u4e13\u95e8\u7528\u4e8e\u5728 Web \u5e94\u7528\u4e2d\u5b9e\u73b0\u4f4e\u5ef6\u8fdf\u3001\u5168\u53cc\u5de5\u901a\u4fe1\uff0c\u7279\u522b\u9002\u5408\u5b9e\u65f6\u5e94\u7528\u3002<\/li>\n<li><strong>Socket<\/strong> \u662f\u5e95\u5c42\u7f51\u7edc\u901a\u4fe1\u673a\u5236\uff0c\u652f\u6301\u591a\u79cd\u534f\u8bae\uff08\u5982 TCP \u548c UDP\uff09\uff0c\u5177\u6709\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u5e7f\u6cdb\u7684\u5e94\u7528\u8303\u56f4\u3002<\/li>\n<\/ul>\n<p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528 WebSocket \u6216 Socket\u3002\u4f8b\u5982\uff0c\u6784\u5efa\u5b9e\u65f6 Web \u5e94\u7528\u65f6\uff0cWebSocket \u66f4\u4e3a\u9002\u5408\uff1b\u800c\u5b9e\u73b0\u5b9a\u5236\u5316\u7f51\u7edc\u534f\u8bae\u6216\u9700\u8981\u66f4\u9ad8\u63a7\u5236\u529b\u65f6\uff0c\u4f7f\u7528 Socket\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebSocket \u548c Socket \u662f\u4e24\u79cd\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u7684\u6280\u672f\uff0c\u5b83\u4eec\u5728\u7528\u9014\u3001\u529f\u80fd\u548c\u5b9e\u73b0\u65b9\u5f0f\u4e0a\u6709\u4e00\u4e9b\u663e\u8457\u7684\u533a\u522b\u3002&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6306\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[118],"class_list":["post-6306","post","type-post","status-publish","format-standard","hentry","category-5","tag-socket"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6306"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6306\/revisions"}],"predecessor-version":[{"id":6307,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6306\/revisions\/6307"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}