连接到同一个路由器 = 相同的 ip 和端口?

Connected to the same router = Same ip and port?

连接到同一路由器的三台计算机将具有相同的 public IP。如果这些计算机向我的服务器发送请求,它们是否也具有相同的 PORT 还是有例外?

编辑:当我从浏览器收到请求时,端口对于它创建的每个连接都是不同的。浏览器客户端是否只是选择路由器上可用的随机端口?

Three computers connected to the same router will have the same public IP.

正确,从服务器的角度来看,而不是从客户端的角度。

If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?

不,它们在路由器上不会有相同的端口(尽管它们可能在每台客户端 PC 上)。

TCP 连接由元组 {protocol, local-ip, local-port, remote-ip, remote-port} 唯一标识。因此,当多个 TCP 连接具有相同的 {remote-ip, remote-port}(IOW,当多个客户端连接到同一服务器时),则每个 {local-ip, local-port} 必须是唯一的。反之亦然,当多个 TCP 连接具有相同的 {local-ip, local-port}(IOW,当客户端连接到多个服务器时),则每个 {remote-ip, remote-port} 必须是唯一的。

当通过路由器时,在客户端看到的每个 TCP 连接将是 {TCP, lan-ip, lan-port, server-ip, server-port},而在服务器端每个连接将被视为 {TCP, listen-ip, listen-port, client-ip, client-port},其中 {client-ip, client-port} 将是路由器的 {public-ip, public-port},因此每个 {public-ip, public-port} 必须是唯一的。

因此,多个客户端通过路由器连接到同一个服务器根本就不能使用路由器上的同一个出端口,否则服务器将无法区分这些连接。

When I get requests from the browser, the PORT is different for each connection it creates.

正确。

Does the browser client just pick a random port that is available on the router?

不,浏览器也不关心路由器是否存在。浏览器创建一个本地套接字端点并将其绑定到可用的 {local-ip, local-port},然后使用它连接到服务器的 {server-ip, server-port}。数据包转到 OS,OS 将它们发送到路由器,路由器为每个新连接打开自己可用的 {public-ip, public-port},然后将这些数据包转发到服务器。当服务器发回数据包时,路由器将在其 public NIC 上接收这些数据包,将它们转发到适当的客户端 OS,客户端将它们传递到适当的套接字端点。

 -------------
| Client PC A |
 -------------
 {tcp, client-lan-ip, client-lan-port, server-ip, server-port}

   /|\
    |
   \|/

 {tcp, router-lan-ip, router-lan-port, client-lan-ip, client-lan-port}
 --------
| Router |
 --------
 {tcp, router-public-ip, router-public-port, server-ip, server-port}

   /|\
    |
   \|/

{tcp, listen-ip, listen-port, router-public-ip, router-public-port}
 --------
| Server |
 --------