13

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 
1
  • "If you're asking about the upper limits of well designed systems, you're almost certainly Doing It Wrong™"
    – Chris S
    Nov 2, 2011 at 14:10

5 Answers 5

18

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.

1
  • 2
    Thanks, that answers the theoretical part of my question completely! I'll just have to try for every TCP implementation, I guess.
    – lxgr
    Nov 2, 2011 at 9:52
5

That's the maximum in practice it's usually lower. For example Linux uses the net.ipv4.ip_local_port kernel parameter to define the ports that are used for outbound connections. This is usually something like

sysctl net.ipv4.ip_local_port_range 32768 to 61000

You can increase the number available with sysctl e.g.

sysctl -w net.ipv4.ip_local_port_range="10000 64000"

or you can edit /etc/sysctl.conf with the same information

net.ipv4.ip_local_port_range = 10000 65535

All of the examples I've found show the minimum value to be 1024 too.

2

In addition to Iain's answer (above), that there might only be 10,000 ports allowed for outgoing connections by your kernel, in theory your at least limited to one set of XX,XXX ports per IP address on the adapter. Since 127.1 isn't available to the outside world, being on the local network, then for each other IP address (external ) you have a set of outgoing ports within your 65K port range.

So the outgoing limit is really :

  With 1 IP Address: XX,XXX (or 2 x XX,XXX on internal network)
  With 2 IP addresses: 2 x XX,XXX (or 3 x XX,XXX on internal network)
  With 3 IP addresses: 3 x XX,XXX (or 4 x XX,XXX on internal network)
  etc.

To make this work, you would need to read the answer to this thread.

1

Yes. This is true.

Ports are to bind and the apps with the network.

You cannot have more than 65553 application connected by TCP and 65535 connect by UDP on the same host. Operating Systems tipically dinamycally managed the ports an assign one for each app that connects to the network.

If you have two applications listeing on the same port when a network package arrives the computer couldn't know to what app is going to deliver the data. As example if you have Messenger and Skype on the same binded to the same port your messenger message will appear in skype and vice versa :)

1
  • I think the question was not about the upper limit on the number of listening applications — which, as you point out, is limited by the number of unique port numbers — but about the number of sockets operating at a time. Web servers often have dozens of sockets all addressed to their single port 80 or 443. If many servers on a host did that, then there is no reason that the number of open sockets could not exceed 2^32. Jan 15, 2013 at 2:34
0

Although a system may have a limit on the number of open TCP connections, it normally has no restriction regarding the port numbers used. A good TCP implementation must however prevent to use the same pair of sockets twice. (socket = IP address + port). A port however is assigned to a process to prevent stealing connections, and the usual method is to request a free port for a listening port or for an outgoing one. This prevents duplicate outgoing sockets and hence duplicate connections. Failing to use that method, the application itself must prevent creating duplicate connections.

1
  • 3
    How does this add anything new the the Answers already posted on this old Question??
    – Chris S
    Mar 12, 2013 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .