[英]How does serversocket class serve multiple client connections on same port?
When using a Socket class one is establishing a TCP connection to a server on some port, but on the server the ServerSocket is capable of handling multiple client connections for each accept request and delegate it to a thread to server the request. 当使用Socket类时,一个是在某个端口上与服务器建立TCP连接,但在服务器上,ServerSocket能够为每个接受请求处理多个客户端连接,并将其委托给一个线程来为请求提供服务。 But how is it possible for a ServerSocket class to accept multiple tcp connections on the same port.
但是,如何使ServerSocket类在同一端口上接受多个tcp连接。
Does it mean that it is upto the OS to decide how many connections it allows or what is the maximum backlog allowed and can this be controlled by applications on top of OS(i mean is java restricted by the maximum backlog supported by OS) and is there any privison for backlog connections in TCP specification? 这是否意味着由操作系统来决定它允许的连接数或允许的最大积压数是多少,这可以由OS之上的应用程序控制(我的意思是java受OS支持的最大积压限制)并且是TCP规范中的积压连接是否有任何特权?
Best reagards, 最好的反馈,
Keshav 凯沙夫
A TCP connection is defined by a unique set of (source IP, source port, dest IP, dest port). TCP连接由一组唯一的(源IP,源端口,dest IP,dest端口)定义。 Since the server binds to a particular port, it defines two of those 4 variables.
由于服务器绑定到特定端口,因此它定义了这4个变量中的两个。 As long as the clients all come from different IPs and/or different ports, it won't be an issue.
只要客户端都来自不同的IP和/或不同的端口,就不会有问题。
And yes, the OS can control how many total connections are allowed, and your program can restrict that even further. 是的,操作系统可以控制允许的总连接数,并且您的程序可以进一步限制。
It serves multiple clients and you can choose how many clients you will handle a the same time. 它为多个客户提供服务,您可以选择同时处理多少客户端。
A connection (aka a Socket
between a client and a server isn't only identified by the ServerIP/ServerPort, it's identified with ClientIP/ClientPort/ServerIP/ServerPort. 连接(也称为客户端和服务器之间的
Socket
不仅由ServerIP / ServerPort标识,而是通过ClientIP / ClientPort / ServerIP / ServerPort标识。
You only have to accept connections (and usually treat them in different threads). 您只需要接受连接(并且通常在不同的线程中处理它们)。
By default the backlog size is 50, but you can set it when you create your ServerSocket
. 默认情况下,积压大小为50,但您可以在创建
ServerSocket
时进行设置。
new ServerSocket(21, 100); //Create a server socket with a backlog of 100
Resources : 资源:
运行服务器的操作系统使用远程端口号来区分与服务器的各种连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.