带有临时端口的 Websocket [英] Websockets with ephemeral ports

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

问题描述

我正在编写一个库,它将使用 Websockets 在不同进程(以及以不同语言运行,甚至可能在不同机器上运行的进程)之间进行通信.我想避免与可能已经在机器上运行的现有服务器发生端口冲突.我了解了一些关于临时端口的知识,我猜测我可以将临时端口与 Websockets 一起使用,以避免端口冲突.

I am writing a library which will use Websockets to communicate between different processes (and processes running in different languages, and maybe even on different machines). I want to avoid port conflicts with existing servers that might already be running on a machine. I learned a little bit about ephemeral ports, and I am guessing I could use ephemeral ports with Websockets in order to avoid port conflicts.

但是,我很难找到有关这是否可行的信息.为了使它有价值,我需要一个可以处理临时端口的 Python、Go、Java 和 Node.js 中的 Websocket 实现.我唯一熟悉的 Websocket 实现是 JavaScript 世界中的 Socket.io.

However, I am having a lot of trouble finding information about whether this is possible. In order to make this worthwhile, I need a Websocket implementation in Python, Go, Java and Node.js which can handle ephemeral ports. The only Websocket implementation I am familiar with is Socket.io in the JavaScript world.

我的问题是:

  1. 有谁知道是否可以将临时端口与socket.io 和 websockets 一般?
  2. 您能否提供代码示例或链接以说明如何完成此操作?

推荐答案

webSocket 连接基于 TCP,因此连接使用端口的方式就像 TCP.因此,您有一侧发起连接(客户端),另一侧正在侦听传入连接(服务器).在 TCP 中,在两个 IP 地址/端口对之间建立连接.因此,您有一个 IP1/port1 连接到 IP2/port2.

A webSocket connection is based on TCP and thus the way a connection uses a port is just like TCP. As such, you have one side initiating the connection (the client) and one side that is listening for incoming connections (the server). In TCP, a connection is established between two IP address/port pairs. So, you have a IP1/port1 connecting to IP2/port2.

客户端可以使用它想要的任何端口作为自己的端口,但它必须连接到服务器上的特定端口,那里有一个服务器进程侦听传入的连接.您可以选择任何尚未使用的端口供服务器侦听(尽管推荐用于此类用途的端口范围),但客户端必须事先知道该端口,以便客户端可以在该端口上进行连接.

The client can use any port it wants for its own port, but it must connect to a specific port on the server where there is a server process listening for incoming connections. You can pick any port not already in use for the server to listen on (though there are port ranges recommended for such use), but that port must be known in advance to the client so the client can connect on that port.

因此,客户端连接到服务器正在侦听的已知端口上的服务器 IP.客户端将来自某个特定客户端端口上的客户端 IP.客户端端口通常是由底层 TCP 堆栈自动分配的临时端口.这个临时端口在初始连接中与服务器通信,用于将返回客户端的网络流量定向到客户端上的相应套接字.

So, the client connects to the server IP on the known port that the server is listening on. The client will be coming from the client IP on some specific client port. The client port would typically be an ephemeral port that was automatically allocated by the underlying TCP stack. This ephemeral port is communicated to the server in the initial connection and is used for directing network traffic coming back to the client to the appropriate socket on the client.

服务器通常不会使用临时端口,也不能使用动态分配的端口,因为那样客户端将不知道要连接哪个端口.

A server would not typically use an ephemeral port and it can't use a dynamically allocated port because then the client wouldn't know what port to connect on.

因此,通常的做法是让正在设置服务器的人了解服务器上正在运行哪些进程以及正在使用哪些端口,然后在用于服务器侦听的范围内选择一个端口不会发生冲突,然后通过客户端配置文件或仅将该端口构建到客户端来将其传达给客户端.

So, the usual way this is done is for someone who is setting up the server to understand what processes are running on the server and what ports are being used by what and then pick a port in the range intended for server listening that will not conflict and then communicate that to the client, either via a client config file or by just building that port into the client.

如果你真的想要一个动态监听端口,你可以使用一个众所周知的服务器进程和端口(例如端口 80 上的 Web 服务器),然后制作一个 http 请求,其唯一目的是询问服务器端口是什么用于传入的 webSocket 通信.但是,这仍然需要一个众所周知的端口号,因此它可能不会真正保存任何内容,除非端口 80 恰好没有在计算机上使用并且您不想将它用于您的 webSocket.或者,如果您真的想对此感到困惑(通常是矫枉过正),您可以使用一种资源发现协议,该协议允许设备发现如何相互连接.

If you really wanted a dynamic listening port, you could use a well-known server process and port (such as a web server on port 80) and then craft an http request whose sole purpose was to ask the server what port is being used for your incoming webSocket communication. But, that still requires a well known port number so it probably isn't really saving anything unless port 80 happens to not be in use on the computer and you don't want to use it for your webSocket. Or, if you really wanted to go nuts with this (generally overkill), you could use one of the resource discovery protocols that would allow devices to discover how to connect to one another.

实际上,大多数人只是在 2000-9999 范围内选择一个四位数的端口号,该端口号不会与同一台机器上的任何其他服务器发生冲突.

In practice, most people just pick a four digit port number in the range of 2000-9999 that doesn't conflict with any other servers on the same box.

IANA 表示临时端口在 49152 到 65535 的范围内,尽管一些 Linux 内核使用 32768 到 61000.

IANA says that ephemeral ports are in the range of 49152 to 65535, though some Linux kernels use 32768 to 61000.

1024 以下的端口需要超级用户权限,非标准服务器通常避免使用.

Ports below 1024 require super-user privileges and are usually avoided for non-standard servers.

有一长串服务器类型以及它们通常使用的端口号此处,尽管您不太可能在自己的服务器上找到其中许多进程.

There's a long list of types of servers and what port numbers they typically use here, though you are unlikely to find many of these processes running on your own server.

这篇关于带有临时端口的 Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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