对 WebSocket 的理解 [英] Understanding of WebSockets

查看:39
本文介绍了对 WebSocket 的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是一个socket对应一个网络标识符、端口和TCP标识符.[1]

My understanding is that a socket corresponds to a network identifier, port and TCP identifier. [1]

操作系统使进程能够与端口相关联(IIUC 是使进程在网络上可寻址以获取入站数据的一种方式).

Operating systems enable a process to be associated with a port (which IIUC is a way of making the process addressable on the network for inbound data).

因此,WebSocket 服务器通常会与一个众所周知的端口相关联,该端口用于接受和理解升级请求的 HTTP(如 443),然后使用 TCP 标识符为单个服务器进程同时打开多个网络套接字和单个端口.

So a WebSocket server will typically be associated with a port well-known for accepting and understanding HTTP for the upgrade request (like 443) and then use TCP identifiers to enable multiple network sockets to be open concurrently for a single server process and a single port.

请有人确认或更正我的理解吗?

Please can someone confirm or correct my understanding?

[1] 在每个 TCP,我们连接一个 NETWORK 标识符和一个 TCP 标识符使用端口名称创建唯一的 SOCKET 名称在所有连接在一起的网络中." https://tools.ietf.org/html/rfc675

推荐答案

当客户端通过给定端口连接到您的服务器时,客户端连接来自 IP 地址和客户端端口号.客户端端口号由客户端自动生成,并且对于该客户端是唯一的.因此,您最终会得到四个建立联系的项目.

When a client connects to your server on a given port, the client connection is coming from an IP address and a client-side port number. The client-side port number is automatically generated by the client and will be unique for that client. So, you end up with four items that make a connection.

Server IP address  (well known to all clients)
Server port        (well known to all clients)
Client IP address  (unique for that client)
Client port        (dynamically unique for that client and that socket)

因此,正是这四项的组合构成了一个独特的 TCP 连接.如果同一个客户端与同一个服务器和端口建立第二个连接,那么第二个连接将有一个不同的客户端端口号(一个客户端建立的每个连接都将被赋予一个不同的客户端端口号),因此上述四项的组合第二个客户端连接会有所不同,允许它的流量与客户端建立的第一个连接完全分开.

So, it is the combination of these four items that make a unique TCP connection. If the same client makes a second connection to the same server and port, then that second connection will have a different client port number (each connection a client makes will be given a different client port number) and thus the combination of those four items above will be different for that second client connection, allowing it's traffic to be completely separate from the first connection that client made.

因此,TCP 套接字是上述四项的独特组合.要了解如何使用它,让我们看看一些流量是如何流动的.

So, a TCP socket is a unique combination of the four items above. To see how that is used, let's look at how some traffic flows.

在客户端连接到服务器并创建一个 TCP 套接字来表示该连接后,客户端发送一个数据包.数据包是从客户端 IP 地址和该特定套接字使用的唯一客户端端口号发送的.当服务器在其自己的端口号上接收到该数据包时,它可以看到该数据包来自客户端 IP 地址和该特定客户端端口号.它可以使用这些项目在其表中查找并查看此流量与哪个 TCP 套接字相关联并为该特定套接字触发事件.这会将客户端的流量与当前连接的所有其他套接字(无论它们是来自同一客户端的其他连接还是来自其他客户端的连接)分开.

After a client connects to the server and a TCP socket is created to represent that connection, then the client sends a packet. The packet is sent from the client IP address and from the unique client port number that that particular socket is using. When the server receives that packet on its own port number, it can see that the packet is coming from the client IP address and from that particular client port number. It can use these items to look up in its table and see which TCP socket this traffic is associated with and trigger an event for that particular socket. This separates that client's traffic from all the other currently connected sockets (whether they are other connections from that same client or connections from other clients).

现在,服务器想要向该客户端发送响应.数据包被发送到客户端的 IP 地址和客户端端口号.客户端 TCP 堆栈做同样的事情.它从服务器 IP/端口接收数据包并寻址到特定的客户端端口号,然后可以将该数据包与客户端上适当的 TCP 套接字相关联,以便它可以在正确的套接字上触发事件.

Now, the server wants to send a response to that client. The packet is sent to the client's IP address and client port number. The client TCP stack does the same thing. It receives the packet from the server IP/port and addressed to the specific client port number and can then associate that packet with the appropriate TCP socket on the client so it can trigger an event on the right socket.

所有流量都可以通过这种方式唯一地与适当的客户端或服务器 TCP 套接字相关联,即使许多客户端可能连接到相同的服务器 IP 和端口.客户端 IP/端口的唯一性允许两端告诉给定的数据包属于哪个套接字.

All traffic can uniquely be associated with the appropriate client or server TCP socket in this way, even though many clients may connect to the same server IP and port. The uniqueness of the client IP/port allows both ends to tell which socket a given packet belongs to.

webSocket 连接以 HTTP 连接(这是一个运行 HTTP 协议的 TCP 套接字)开始.该初始 HTTP 请求包含一个升级"标头,请求服务器将协议从 HTTP 升级到 webSocket.如果服务器同意升级,那么它会返回一个响应,表明协议将更改为 webSocket 协议.TCP 套接字保持不变,但双方同意他们现在将使用 webSocket 协议而不是 HTTP 协议.因此,一旦连接,您就会拥有一个 TCP 套接字,其中双方都在使用 webSocket 协议.此 TCP 连接使用与上述相同的逻辑来保持与同一服务器的其他 TCP 连接的唯一性.

webSocket connections start out with an HTTP connection (which is a TCP socket running the HTTP protocol). That initial HTTP request contains an "upgrade" header requesting the server to upgrade the protocol from HTTP to webSocket. If the server agrees to the upgrade, then it returns a response that indicates that the protocol will be changed to the webSocket protocol. The TCP socket remains the same, but both sides agree that they will now speak the webSocket protocol instead of the HTTP protocol. So, once connected, you then have a TCP socket where both sides are speaking the webSocket protocol. This TCP connection uses the same logic described above to remain unique from other TCP connections to the same server.

通过这种方式,您可以在一个端口上拥有一个服务器,该服务器同时适用于 HTTP 连接和 webSocket 连接.到该服务器的所有连接都以 HTTP 连接开始,但在双方同意更改协议后,有些连接会转换为 webSocket 连接.保留 HTTP 连接的 HTTP 连接将是典型的请求/响应,然后套接字将被关闭.升级"到 webSocket 协议的 HTTP 连接将在 webSocket 会话期间保持打开状态(可以是长期存在的).您可以有许多并发打开的 webSocket 连接,这些连接都彼此不同,而新的 HTTP 连接定期由同一台服务器提供服务.上面的 TCP 逻辑用于跟踪进出同一服务器/端口的哪些数据包属于哪个连接.

In this manner, you can have a single server on a single port that works for both HTTP connections and webSocket connections. All connections to that server start out as HTTP connections, but some are converted to webSocket connections after both sides agree to change the protocol. The HTTP connections that remain HTTP connections will be typical request/response and then the socket will be closed. The HTTP connections that are "upgraded" to the webSocket protocol will remain open for the duration of the webSocket session (which can be long lived). You can have many concurrent open webSocket connections that are all distinct from one another while new HTTP connections are regularly serviced all by the same server. The TCP logic above is used to keep track of which packets to/from the same server/port belong to which connection.

仅供参考,您可能听说过 NAT(网络地址转换).这通常用于允许专用网络(如家庭或公司网络)连接到公共网络(如 Internet).使用 NAT,服务器可能会将多个客户端视为具有相同的客户端 IP 地址,即使它们是专用网络上物理上不同的计算机).使用 NAT,多台计算机通过一个公共 IP 地址进行路由,但 NAT 仍然保证客户端 IP 地址和客户端端口号仍然是唯一的组合,因此上述方案仍然有效.使用 NAT 时,发往特定客户端的传入数据包将到达共享 IP 地址.然后将 IP/端口转换为专用网络上的实际客户端 IP 地址和端口号,然后将数据包转发到该设备.服务器通常不知道这种转换和数据包转发.因为 NAT 服务器仍然保持客户端 IP/客户端端口组合的唯一性,所以即使看起来许多客户端共享一个公共 IP 地址,服务器的逻辑仍然可以正常工作).请注意,家庭网络路由通常配置为使用 NAT,因为家庭网络上的所有计算机将共享"您的路由器在访问互联网时拥有的一个公共 IP 地址.

FYI, you may have heard about NAT (Network Address Translation). This is commonly used to allow private networks (like a home or corporate network) to interface to a public network (like the internet). With NAT a server may see multiple clients as having the same client IP address even though they are physically different computers on a private network). With NAT, multiple computers are routed through a common IP address, but NAT still guarantees that the client IP address and client port number are still a unique combination so the above scheme still works. When using NAT an incoming packet destined for a particular client arrives at the shared IP address. The IP/port is then translated to the actual client IP address and port number on the private network and then packet is forwarded to that device. The server is generally unaware of this translation and packet forwarding. Because the NAT server still maintains the uniqueness of the client IP/client port combination, the server's logic still works just fine even though it appears that many clients are sharing a common IP address). Note, home network routes are usually configured to use NAT since all computers on the home network will "share" the one public IP address that your router has when accessing the internet.

这篇关于对 WebSocket 的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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