名称空间中的所有套接字是否都连接到 socket.io 中服务器上的同一端口?
Do all the sockets in a namespace connect to the same port on the server in socket.io?
我以为当服务器启动时,它会在计算机上创建特定数量的 TCP 端口。因此,每当有新连接进入时,它都会为该客户端分配一个端口 ('connection')。最近我打开了 tutorialsPoint 网站'https://www.tutorialspoint.com/socket.io/socket.io_namespaces.htm',里面写着:
"Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server".
这部分我没看懂:"Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server"。我的问题是所有连接如何共享网络服务器上的一个端口。
任何帮助将不胜感激。
Do all the sockets in a namespace connect to the same port on the server in socket.io?
是的,他们有。
首先 socket.io 建立在底层 webSocket 协议之上。 webSocket 连接从建立在 TCP 连接之上的 http 连接开始,然后双方同意 "upgrade" 开始讨论 webSocket 协议而不是 http 协议的协议。
因此,当 socket.io 连接进入时,它最初是一个 http 连接。
其次,任何 TCP 服务器都在侦听已知端口上的入站连接。客户端必须知道该端口是什么,并且客户端会尝试连接到 IP 地址和端口的组合。仅使用一个网络适配器的常规 TCP 服务器将只侦听该端口。 所有入站客户端连接都将到达该端口。
I thought when a server is started, it creates a specific number of TCP ports on a computer. so whenever a new connection comes in, it assigns a port to that client ('connection').
事情不是这样的。 监听服务器创建一个被动套接字监听一个特定端口上的入站连接。当 TCP 客户端启动出站连接时,该客户端会为该出站连接选择一个动态选择的端口号(该端口号对于该客户端是唯一的,当前未使用)。这个源端口号在 TCP、http、webSocket 或 socket.io 编程中通常是不可见的(尽管你可以根据需要看到它是什么——你只是不必在我们通常编程的级别上自己使用它).它是 TCP 管道的一部分,可帮助将数据包传送到正确的套接字。因此,此时它具有源 IP 地址和源端口号。然后它会尝试连接到目标端口上的目标 IP 地址。
这四个参数的独特组合:
source IP
source port (dynamically assigned on the client)
target IP (known in advance by the client)
target port (known in advance by the client)
定义一个唯一的 TCP 连接。没有两个 TCP 连接会有相同的四个参数。如果同一个客户端对同一个目标 IP 和端口进行另一个 TCP 连接,它将被分配一个不同的源端口号,因此它将是一个不同的唯一组合。
这里有一个小的(有点令人困惑的)方面,我会让你知道,但不要试图过度解释或混淆事情。许多客户端实际上是在私有网络上并且拥有私有 IP 地址。该私有 IP 地址并不是服务器实际视为连接源的地址。在某些时候,连接会通过一个将专用网络连接到 public 网络的网关。此网关将执行 NAT(网络地址转换)。它会将私有源 IP/port 交换为对应于网关本身的 public 源 IP/port。它会记住它交换的内容,以便当数据包直接返回另一个时,它可以将其交换回来。因此,目标服务器实际上认为它正在与网关通信,但目标发送到网关的任何内容都 "forwarded" 到原始发件人的私有 IP address/port 上。因此,您真的不需要了解网关的详细信息,除了它充当专用网络上某台计算机的专用 IP 地址与您尝试访问的 public Internet 上某台计算机之间的代理连接至。它执行所谓的 "network address translation" 来使这一切正常工作。在接下来的讨论中,你应该忘记这一点,假装源和目标都在 public 互联网上,IP 地址为 public(尽管这几乎不是实际情况,但是网关使它就像它们一样工作。
"Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server".
在 socket.io 中,当您连接命名空间时,您正在创建一个新的底层 webSocket 连接到同一目标 IP/port。一台服务器可以有许多到同一个 IP/port 的入站连接。每个都有自己的 TCP 套接字,上面提到的四个参数唯一地定义了每个套接字。当入站网络数据包到达最低级别时,TCP 可以知道它来自哪个源 IP 和源端口以及发送了哪个目标 IP/port 并且允许 TCP 驱动程序确定该数据包属于哪个套接字数据包可以传送到监视该特定套接字的代码。
This part i did not understand: "Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server". My question is how can all the connections share a single port on the web-server.
要在 socket.io 中使用命名空间,您需要与该特定命名空间建立新的 socket.io 连接。您不要在单个 socket.io 连接上使用多个命名空间。但是,命名空间在比 TCP 或 webSocket 连接逻辑更高的级别上运行。它位于应用程序层之上。因此,所有命名空间连接,无论您使用的是哪个命名空间,都连接到同一 IP 和同一端口上的同一服务器。建立连接后,socket.io 发送一些数据,表明它希望在此命名空间上建立 "logical" 连接,然后接收 socket.io 代码被告知新连接属于此命名空间。
这里有一篇关于该主题的有用文章:Understanding socket and port in TCP。
我以为当服务器启动时,它会在计算机上创建特定数量的 TCP 端口。因此,每当有新连接进入时,它都会为该客户端分配一个端口 ('connection')。最近我打开了 tutorialsPoint 网站'https://www.tutorialspoint.com/socket.io/socket.io_namespaces.htm',里面写着:
"Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server".
这部分我没看懂:"Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server"。我的问题是所有连接如何共享网络服务器上的一个端口。
任何帮助将不胜感激。
Do all the sockets in a namespace connect to the same port on the server in socket.io?
是的,他们有。
首先 socket.io 建立在底层 webSocket 协议之上。 webSocket 连接从建立在 TCP 连接之上的 http 连接开始,然后双方同意 "upgrade" 开始讨论 webSocket 协议而不是 http 协议的协议。
因此,当 socket.io 连接进入时,它最初是一个 http 连接。
其次,任何 TCP 服务器都在侦听已知端口上的入站连接。客户端必须知道该端口是什么,并且客户端会尝试连接到 IP 地址和端口的组合。仅使用一个网络适配器的常规 TCP 服务器将只侦听该端口。 所有入站客户端连接都将到达该端口。
I thought when a server is started, it creates a specific number of TCP ports on a computer. so whenever a new connection comes in, it assigns a port to that client ('connection').
事情不是这样的。 监听服务器创建一个被动套接字监听一个特定端口上的入站连接。当 TCP 客户端启动出站连接时,该客户端会为该出站连接选择一个动态选择的端口号(该端口号对于该客户端是唯一的,当前未使用)。这个源端口号在 TCP、http、webSocket 或 socket.io 编程中通常是不可见的(尽管你可以根据需要看到它是什么——你只是不必在我们通常编程的级别上自己使用它).它是 TCP 管道的一部分,可帮助将数据包传送到正确的套接字。因此,此时它具有源 IP 地址和源端口号。然后它会尝试连接到目标端口上的目标 IP 地址。
这四个参数的独特组合:
source IP
source port (dynamically assigned on the client)
target IP (known in advance by the client)
target port (known in advance by the client)
定义一个唯一的 TCP 连接。没有两个 TCP 连接会有相同的四个参数。如果同一个客户端对同一个目标 IP 和端口进行另一个 TCP 连接,它将被分配一个不同的源端口号,因此它将是一个不同的唯一组合。
这里有一个小的(有点令人困惑的)方面,我会让你知道,但不要试图过度解释或混淆事情。许多客户端实际上是在私有网络上并且拥有私有 IP 地址。该私有 IP 地址并不是服务器实际视为连接源的地址。在某些时候,连接会通过一个将专用网络连接到 public 网络的网关。此网关将执行 NAT(网络地址转换)。它会将私有源 IP/port 交换为对应于网关本身的 public 源 IP/port。它会记住它交换的内容,以便当数据包直接返回另一个时,它可以将其交换回来。因此,目标服务器实际上认为它正在与网关通信,但目标发送到网关的任何内容都 "forwarded" 到原始发件人的私有 IP address/port 上。因此,您真的不需要了解网关的详细信息,除了它充当专用网络上某台计算机的专用 IP 地址与您尝试访问的 public Internet 上某台计算机之间的代理连接至。它执行所谓的 "network address translation" 来使这一切正常工作。在接下来的讨论中,你应该忘记这一点,假装源和目标都在 public 互联网上,IP 地址为 public(尽管这几乎不是实际情况,但是网关使它就像它们一样工作。
"Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server".
在 socket.io 中,当您连接命名空间时,您正在创建一个新的底层 webSocket 连接到同一目标 IP/port。一台服务器可以有许多到同一个 IP/port 的入站连接。每个都有自己的 TCP 套接字,上面提到的四个参数唯一地定义了每个套接字。当入站网络数据包到达最低级别时,TCP 可以知道它来自哪个源 IP 和源端口以及发送了哪个目标 IP/port 并且允许 TCP 驱动程序确定该数据包属于哪个套接字数据包可以传送到监视该特定套接字的代码。
This part i did not understand: "Multiple namespaces actually share the same WebSockets connection thus saving us socket ports on the server". My question is how can all the connections share a single port on the web-server.
要在 socket.io 中使用命名空间,您需要与该特定命名空间建立新的 socket.io 连接。您不要在单个 socket.io 连接上使用多个命名空间。但是,命名空间在比 TCP 或 webSocket 连接逻辑更高的级别上运行。它位于应用程序层之上。因此,所有命名空间连接,无论您使用的是哪个命名空间,都连接到同一 IP 和同一端口上的同一服务器。建立连接后,socket.io 发送一些数据,表明它希望在此命名空间上建立 "logical" 连接,然后接收 socket.io 代码被告知新连接属于此命名空间。
这里有一篇关于该主题的有用文章:Understanding socket and port in TCP。