简体   繁体   English

serversocket类如何在同一端口上提供多个客户端连接?

[英]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.

相关问题 Java ServerSocket如何在接受客户端后获得绑定到同一本地端口的新套接字? - How does Java ServerSocket get a new socket bound to the same local port after accept the client? 多个ServerSocket连接Java - Multiple ServerSocket Connections Java 如何使ServerSocket接受来自同一IP的多个连接? - How can I make ServerSocket accept multiple connections from same IP? java serverSocket使用相同的端口,而客户端使用不同的端口 - java serverSocket use same port whereas client uses different 绑定到同一端口时,java ServerSocket不会引发IOException - java ServerSocket does not throw IOException when bind to the same port 使用始终相同的端口进行多个非同时的Java客户端-服务器连接 - Multiple non-simultaneous Java client-server connections using always the same port java serverSocket和客户端套接字如何在同一台PC中交互? - How java serverSocket and client socket interacts within the same pc? 在Java中,如何创建到同一特定客户端的多个套接字连接? - In Java, how to create multiple socket connections to the same specific client? 为什么Java ServerSocket accept()返回一个与ServerSocket具有相同端口的套​​接字? - Why Java ServerSocket accept() returns a socket with the same port as ServerSocket? 接受来自serversocket上的客户端的多个连接 - Accepting multiple connections from clients on a serversocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM