当我们想使用 Javascript 通过 WebSocket 进行通信时,套接字如何将端口从 HTTP 80 更改为另一个端口? [英] How does a socket change ports from HTTP 80 to another port when we want to communicate over WebSocket using Javascript?

查看:132
本文介绍了当我们想使用 Javascript 通过 WebSocket 进行通信时,套接字如何将端口从 HTTP 80 更改为另一个端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个服务器 XYZ,它监听 TCP 客户端的 50000 端口和 HTTP 客户端的 80 端口.另一方面,我有一个客户端,它使用 WebSocket 建立到端口 50000 的套接字连接,并将使用 HTTP 端口 80 进行握手(当然).

Let say I have a server XYZ that listens on port 50000 for TCP clients and port 80 for HTTP clients. And on the other side, I have a client that uses a WebSocket to establish a socket connection to port 50000 and will use HTTP port 80 for the handshake (of course).

现在,当客户端开始时,它首先会通过 HTTP 端口 80 向服务器 XYZ 发送请求,服务器将在端口 80 上收到握手请求并发送响应欢迎.因此,在这种情况下,双方都使用端口 80(当然).

Now, when the client begins, it will first send a request to server XYZ via the HTTP port 80, and the server will receive its request on port 80 for the handshake and will send a response for welcome. So, in that case, both parties are using port 80 (of course).

现在,当握手完成时,标准文档说用于握手目的的 HTTP 请求/响应的相同 TCP 连接然后转换为 TCP 套接字连接.好吧.

Now, when the handshake is done, the standard documentation says that the same TCP connection that is used for HTTP request/response for handshake purposes is then converted to the TCP socket connection. Ok right.

但是,但是如果整个握手过程和 HTTP 请求/响应的 TCP 连接第一次使用端口 80,并且相同的 TCP 连接被转换为 TCP 套接字连接,并且整个过程是通过端口 80 完成的,那么对于双方的 TCP 套接字,相同的 TCP 连接如何转换为端口 50000?客户端是否在内部初始化另一个 TCP 连接以更改为端口 50000?

But, but if this whole handshake process and TCP connection for the HTTP request/response uses port 80 the first time, and that the same TCP connection is converted to the TCP socket connection, and this whole process is done via port 80, then how does the same TCP connection get converted to port 50000 for the TCP socket on both parties? Does the client initialize another TCP connection internally for changing to port 50000?

那么,谁能告诉我们在 WebSocket 中端口转换是如何执行和工作的,从 80 端口到双方不同的端口?如何在不同端口上建立完整的单套接字连接?相同的 TCP 连接如何更改/翻转其端口?

So, can anyone tell how the port conversion is performed and works in the WebSocket from port 80 to a different port in both parties? How does a complete single socket connection get established on the different ports? How does the same TCP connection change/flip its ports?

推荐答案

TCP 套接字连接根本无法更改端口.建立连接后,其端口将被锁定且无法更改.如果您在端口 80 上有 TCP 套接字连接,则在端口 50000 上有连接的唯一方法是建立一个完全独立的 TCP 套接字连接.

A TCP socket connection cannot change ports at all. Once a connection has been established, its ports are locked in and cannot be changed. If you have a TCP socket connection on port 80, the only way to have a connection on port 50000 is to make a completely separate TCP socket connection.

WebSocket 无法连接到端口 80 然后切换到端口 50000.但是,从端口 80 提供给浏览器的 HTML 页面可以包含客户端脚本,允许浏览器创建 WebSocket 对象并将其连接到端口 50000.两个 TCP 连接(HTTP 和 WebSocket)彼此完全分离(实际上,一旦提供 HTML,HTTP 套接字连接甚至不需要保持打开状态,因为 HTTP 是无状态协议).

A WebSocket cannot connect to port 80 and then switch to port 50000. However, an HTML page that is served to a browser from port 80 can contain client-side scripting that allows the browser to make a WebSocket object and connect it to port 50000. The two TCP connections (HTTP and WebSocket) are completely separate from each other (in fact, the HTTP socket connection does not even need to stay open once the HTML is served, since HTTP is a stateless protocol).

这篇关于当我们想使用 Javascript 通过 WebSocket 进行通信时,套接字如何将端口从 HTTP 80 更改为另一个端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆