简体   繁体   English

服务器套接字的工作原理是什么?

[英]How exactly does a Server Socket work?

How exactly does a Server Socket work? 服务器套接字的工作原理是什么? When I create a java server socket and accept connection at port 1234. Does the server actually uses the port 1234 for all clients? 当我创建一个java服务器套接字并在端口1234接受连接时。服务器是否实际使用端口1234用于所有客户端? I've read that when you write a network server the socket actually opens another port once the connection is accepted. 我已经读过,当您编写网络服务器时,套接字实际上会在接受连接后打开另一个端口。

Is this true? 这是真的? If so, why am I not seeing it in netstat? 如果是这样,为什么我没有在netstat中看到它? I see a lot of connections like this 我看到很多像这样的连接

tcp        0      0 ::ffff:MY_IP:1234 ::ffff:97.37.134.95:39236   ESTABLISHED 
tcp        0      0 ::ffff:MY_IP:1234 ::ffff:89.204.153.101:26117 ESTABLISHED 
tcp        0      0 ::ffff:MY_IP:1234 ::ffff:195.240.16.70:26193  ESTABLISHED 
tcp        0      0 ::ffff:MY_IP:1234 ::ffff:80.187.98.116:15012  ESTABLISHED 
tcp        0      0 ::ffff:MY_IP:1234 ::ffff:218.78.248.190:30794 ESTABLISHED 

So are they really all connected to my server at 1234? 所以他们真的都在1234连接到我的服务器吗? If so, doesn't that mean you the server will be able to accept infinite number of connections? 如果是这样,这是不是意味着您的服务器将能够接受无限数量的连接?

So are they really all connected to my server at 1234? 所以他们真的都在1234连接到我的服务器吗?

Yes

If so, doesn't that mean you the server will be able to accept infinite number of connections? 如果是这样,这是不是意味着您的服务器将能够接受无限数量的连接?

You can have 2^32-2-1 (IP4) addresses (leave one free to have another host on the same network), and 2^16 remote socket ports. 您可以拥有2 ^ 32-2-1(IP4)地址(保留一个可以在同一网络上拥有另一台主机)和2 ^ 16个远程套接字端口。 That is a lot, but not infinite. 这很多,但不是无限的。 Anyway you will run out of memory before. 无论如何,你之前会耗尽内存。

TCP/IP Sockets are uniquely identified by the tuple (local Address, local port, remote address, remote port). TCP / IP套接字由元组(本地地址,本地端口,远程地址,远程端口)唯一标识。

This will provide for a very large number of sockets, but not infinite. 这将提供非常多的套接字,但不是无限的。

Yes, you are basically right. 是的,你基本上是对的。

The server is listening on some port (the one you set) but when you accept a connection it will attribute a new connected socket number. 服务器正在侦听某个端口(您设置的端口),但是当您接受连接时,它将归属新的连接套接字号。

If you do not see connected sockets using netstat, it's probably because you do not call it with the right options. 如果您没有使用netstat看到连接的套接字,可能是因为您没有使用正确的选项调用它。 You should have one LISTEN connection on the server port, and one ESTABLISHED connection with an allocated local port for each active remote connection. 您应该在服务器端口上有一个LISTEN连接,并且每个活动远程连接都有一个ESTABLISHED连接和一个分配的本地端口。 You could also have some remains of terminated connection (poorly terminated) with the TIME WAIT state. 您还可以使用TIME WAIT状态保留一些终止连接(端接不良)。

Below is some extract from my system current status (got with netstat -anlp on Linux) 下面是我系统当前状态的一些摘录(在Linux上使用netstat -anlp

tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN      27002/rdpproxy  
tcp        0      0 10.10.4.185:3389        10.10.4.13:36725        ESTABLISHED 27233/rdpproxy  

The server is 10.10.4.185, and is listening on port 3389. Any remote IP and remote port is allowed to connect. 服务器是10.10.4.185,正在侦听端口3389.允许任何远程IP和远程端口连接。

The second line show a connected session. 第二行显示连接的会话。 The remote address is 10.10.4.13 and reserve the port 36725 for this address. 远程地址是10.10.4.13,并为此地址保留端口36725。 Hence you can open plenty connection from 10.10.4.185 (tenth of thousands) and still more from other systems. 因此,您可以从10.10.4.185(成千上万)开始大量连接,而更多来自其他系统。

And, no, that does not mean your server will be able to accept infinite number of connection, your system can go out of ressources and will fail to open new connections well before that limit. 并且,不,这并不意味着您的服务器将能够接受无限数量的连接,您的系统可能会耗尽资源,并且无法在该限制之前打开新连接。

yes, server can accept any number of connections on single port. 是的,服务器可以接受单个端口上的任意数量的连接。 That is difference between server and client socket, client socket can have only one connection per port. 这是服务器和客户端套接字之间的区别,客户端套接字每个端口只能有一个连接。

It's not infinite. 这不是无限的。 There is a limit. 有一个限制。 On Unix based operating systems, the ulimit command will tell you the maximum number of "open files" a process can have and will also allow you to change it. 在基于Unix的操作系统上, ulimit命令将告诉您进程可以拥有的“打开文件”的最大数量,并且还允许您更改它。 If you exceed this limit, you will start seeing IOExceptions relating to "Too many open files". 如果超出此限制,您将开始看到与“打开的文件太多”相关的IOExceptions。

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

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