While writing a Java program, I noticed that a SINGLE TCP PORT (ServerSocket) can connect to MANY Connections (Sockets). Does this mean that a Single TCP PORT can THEORETICALLY handle infinite connections?
The TCP tuple is composed of:
- source IP
- source port
- destination IP
- destination port
Since your sever socket is listening to one port and I presume one IP address (although it could be more) the remaining factors are the IP address and port on the remote end, i.e. the client.
For IPv4 there are 2^32 addresses, out of which roughly 3.5 billion are usable. There are 2^16 ports, most of which won't be used by clients as most TCP stacks use only a high range but let's say we are dealing with a client that could use any port, so that gives you a potential number of connections around 3.5*65000 = 229*10^12. That's more than you could ever hope to realistically handle on a single server.
There only exist 65536 ports (from 0 to 65535), one of then located by the IANA into well known ports(lower to 1024), registered ports(from 1024 to 49151, that can be used by any application) and private or dynamic ports (from 49152 to 65535 assigned dynamically to a cliento who iniciates the connection). If you, THEORETICALLY, will be a lot of CPU and RAM on your equipment, from one port, you only will be able to entablish 65536 sockets.