简体   繁体   English

为什么Java ServerSocket accept()返回一个与ServerSocket具有相同端口的套​​接字?

[英]Why Java ServerSocket accept() returns a socket with the same port as ServerSocket?

In the server side, i use this code : 在服务器端,我使用此代码:

ServerSocket server = new ServerSocket(1234);
Socket server_socket = server.accept();

I found the server is listening on port 1234. 我发现服务器正在侦听端口1234。

When one or more client sockets are connected, they are all using the same port 1234 ! 当连接一个或多个客户端套接字时,它们都使用相同的端口1234!

That is really confusing : 这真令人困惑:

在此输入图像描述

I remember that multi sockets can't use the same port, isn't it right ? 我记得多套接字不能使用同一个端口,不是吗? Thanks. 谢谢。

A TCP connection is identified by four numbers: TCP连接由四个数字标识:

  • client (or peer 1) IP 客户端(或对等方1)IP
  • server (or peer 2) IP 服务器(或对等2)IP
  • client port 客户端口
  • server port 服务器端口

A typical TCP connection is open as follows: 典型的TCP连接打开如下:

  • The client IP is given by the client's ISP or NAT. 客户端IP由客户端的ISP或NAT提供。
  • The server IP is given by the user or looked up in a DNS. 服务器IP由用户提供或在DNS中查找。
  • The client chooses a port arbitrarily from the unassigned range (while avoiding duplicate quadruples) 客户端从未分配的范围中任意选择一个端口(同时避免重复四倍)
  • The server port is given by the protocol or explicitly. 服务器端口由协议提供或明确提供。

The port that you specify in the ServerSocket is the one the clients connect to. 您在ServerSocket中指定的端口是客户端连接到的端口。 It's nothing more than a port number that the OS knows that belongs to your application and an object that passes the events from the OS to your application. 它只不过是操作系统知道属于您的应用程序的端口号,以及将事件从操作系统传递到应用程序的对象。

The ServerSocket#accept method returns a Socket . ServerSocket#accept方法返回一个Socket A Socket is an object that wraps a single TCP connection. Socket是一个包装单个TCP连接的对象。 That is, the client IP, the server IP, the client TCP port and the server TCP port (and some methods to pass the associated data around) 也就是客户端IP,服务器IP,客户端TCP端口和服务器TCP端口(以及传递相关数据的一些方法)

The first TCP packet that the client sends must contain the server port that your app listens on, otherwise the operating system wouldn't know what application the connection belongs to. 客户端发送的第一个TCP数据包必须包含应用程序侦听的服务器端口,否则操作系统将不知道该连接属于哪个应用程序。

Further on, there is no incentive to switch the server TCP port to another number. 此外,没有动力将服务器TCP端口切换到另一个号码。 It doesn't help the server machine OR the client machine, it needs some overhead to perform (you need to send the new and the old TCP port together), and there's additional overhead, since the server OS can no longer identify the application by a single port - it needs to associate the application with all server ports it uses (the clients still needs to do it, but a typical client has less connections than a typical server) 它对服务器机器或客户端机器没有帮助,它需要一些开销来执行(你需要将新的和旧的TCP端口一起发送),并且还有额外的开销,因为服务器操作系统无法再通过以下方式识别应用程序单个端口 - 它需要将应用程序与其使用的所有服务器端口相关联(客户端仍需要执行此操作,但典型客户端的连接数少于典型服务器)


What you see is 你看到的是什么

  • two inbound connections, belonging to the server (local port:1234). 两个入站连接,属于服务器(本地端口:1234)。 Each has its own Socket in the server application. 每个服务器应用程序中都有自己的Socket
  • two outbound connections, belonging to the client (remote port:1234). 两个出站连接,属于客户端(远程端口:1234)。 Each has its own Socket in the client application. 每个客户端应用程序中都有自己的Socket
  • one listening connection, belonging to the server. 一个监听连接,属于服务器。 This corresponds to the single ServerSocket that accepts connections. 这对应于接受连接的单个ServerSocket

Since they are loopback connections, you can see both endpoints mixed together on a single machine. 由于它们是环回连接,因此您可以在一台计算机上看到两个端点混合在一起。 You can also see two distinct client ports (52506 and 52511), both on the local side and on the remote side. 您还可以在本地端和远程端看到两个不同的客户端端口(52506和52511)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM