views:

154

answers:

5

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?

A: 

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.

Andrey
So according to logic, a server will never run out of port serving clients?
erotsppa
@erotsppa nothing is infinite in work of machines. i am not sure i can tell exact number but it is very big. (integer (~2e9) probably)
Andrey
@Andrey: 2e9 is about 512, let's go up to 2^16 (65536) less some reserved value, and you'll get nearer the truth.
kriss
@kriss 2e9 stands for 2 * 10^9 (http://en.wikipedia.org/wiki/Scientific_notation), this was my rough approximation for what maximum int (actually 2^32) is.
Andrey
@Andrey: for historical reasons IPv4 socket number is not an int but a 16 bits value (and changing that means changing network packet format). But basically we agree, and this is available for each remote address and addressed opned at the same time, so not a very hard constraint. But that is not infinite and that is not the only limit, system ressources are a much harder restriction.
kriss
+2  A: 

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.

PeterMmm
A: 

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.

kriss
Well that's what others here are saying, when you accept a new connection you don't consume any addition local ports.
erotsppa
@erotspaa: you *do* consume additional local port from the high range. Server port are usually from the low range (well known ports) and reserved.
kriss
@erotspaa: but that is not a big limit, as these ports are available for each IP. The problem with server ports is that they are bound to any IP or to some restricted set of IP. Hence you will starve much faster.
kriss
@erotspaa: I rephrased my answer with some extract from netstat output. I reread your remark and yes the word 'local' was wrong.
kriss
No everyone is saying you don't consume any port at all. Your answer seems wrong.
erotsppa
@erotspaa: no "everyone" is not saying that (actually only Andrey say something like infinite number of port in his answer and he reworded it to "very big" in comments). Actually, every IP header in every network packet contains 4 values (sourceip, sourceport, targetip, targetport) to distinguish between different connections. The set of possible ports is 16 bits, so impossible to have more than that for each remote address and each server, and the TCP/IP stack has to keep track of all opened connections. The connected socket number is a handle to the opened connection, not a port number.
kriss
the sourceip and sourceport doesn't change on the server side so you can have infinite number of these 4 values (sourceip, sourceport, targetip, targetport) in other words the number of ports available on the server side is irrelevant.
erotsppa
@erotspaa: once again, replace infinite with "large" or "very big". Indeed server port and server ip are fixed, remote port is limited to 65536, that is infinite in no way, and we have already grown out of the available ipv4 addresses. However as I said, because of other restrictions in system resources the real number of possible simultaneously open sockets is significantly smaller. Typically a few tenth of thousands.Anyway opening more on the same physical server wouldn't be very useful as the bandwidth must be divided between all opened connections.
kriss
You can have 65536 remote ports *per remote address*. So in your example, where you have recieved a connection from the remote address `10.10.4.13:36725` you could simultaneously also recieve a connection from the remote address `10.55.55.55:36725`.
caf
@caf: yes, absolutely right, that's also what I'm saying.
kriss
+3  A: 

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.

Darron
Does that mean a server will not have to worry about running out of ports? I was always under the impression that a server will run out of port if you have more than 65535 connections.
erotsppa
This is complicated. Technically the limit is only 65535 connections with the same peer address, but many TCP/IP stacks pessimistically prevent conflicts by keeping the local ports unique at socket bind time. You can disable this with the REUSEADDR socket option, with a small amount of risk.
Darron
A: 

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".

dogbane