Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 813
erotsppa Avatar asked Sep 07 '10 14:09

erotsppa


People also ask

How do sockets communicate?

Socket addresses An application can communicate with a remote process by exchanging data with TCP/IP by knowing the combination of protocol type, IP address, and port number. This combination is often known as a socket address. It is the network-facing access handle to the network socket.

What is the difference between socket and server socket?

Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side.

How does a server socket work in Java?

ServerSocket is a java.net class that provides a system-independent implementation of the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it can't listen on the specified port (for example, the port is already being used).

How many clients can connect to a server socket?

Maximum number of sockets. For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.


2 Answers

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.

like image 184
PeterMmm Avatar answered Oct 11 '22 15:10

PeterMmm


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.

like image 30
Darron Avatar answered Oct 11 '22 13:10

Darron