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? 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? 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? 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?
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. 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).
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. You should have one LISTEN connection on the server port, and one ESTABLISHED connection with an allocated local port for each active remote connection. You could also have some remains of terminated connection (poorly terminated) with the TIME WAIT state.
Below is some extract from my system current status (got with netstat -anlp
on Linux)
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.
The second line show a connected session. The remote address is 10.10.4.13 and reserve the port 36725 for this address. Hence you can open plenty connection from 10.10.4.185 (tenth of thousands) and still more from other systems.
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. If you exceed this limit, you will start seeing IOExceptions relating to "Too many open files".
精彩评论