Does the TCP source port have to be unique per host

sockettcp

I've learned that a TCP connection is identified by the tuple (source IP, source port, destination ip, destination port). Theoretically, it should thus be possible to have a client from host1:port1 connect to server1:port1 and at the same time another client (running on host1) from host1:port1 to server2:port1.

I've tested a bit in Java, and so far it seems possible.

However, I've read multiple times that the source port has to be unique for the host address, which would basically mean that there is a hard limit of at most 65536 concurrent outgoing TCP connections. Is that true?

Update:
Here is my Java code. This seems to work, and netstat -t clearly shows two active, outgoing connections from port 9990 (one to 9997, one to 9998). At least on a modern Linux, it seems to be possible?

Socket s1 = new Socket();
s1.setReuseAddress(true);
SocketAddress saremote = new InetSocketAddress("localhost",9999);
SocketAddress salocal = new InetSocketAddress("localhost",9990);
s1.bind(salocal);
s1.connect(saremote);


Socket s2 = new Socket();
s2.setReuseAddress(true);
SocketAddress saremote2 = new InetSocketAddress("localhost",9998);
SocketAddress salocal2 = new InetSocketAddress("localhost",9990);
s2.bind(salocal2);
s2.connect(saremote2);

And the netstat -t output (truncated):

tcp6       0      0 localhost:9990          localhost:9998          CONNECTED 
tcp6       0      0 localhost:9990          localhost:9999          CONNECTED 

Best Answer

It's not a TCP requirement. As far as TCP is concerned, only the combination of source IP, source port, destination IP, and destination port needs to be unique. However, in practice most TCP APIs don't provide any way to create more than one connection with the same source port, unless they have different source IP addresses.