Since port numbers are limited to 65536, is there a limit for the connection num?
How does each connection differs from each other?
If it's by port,then there can never been more than 65536 connections at the same time?
Since port numbers are limited to 65536, is there a limit for the connection num?
How does each connection differs from each other?
If it's by port,then there can never been more than 65536 connections at the same time?
There's many different pieces in play. Since a connection is defined by (Src IP, Src Port, Dest IP, Dest Port)
tuples, you're allowed 65536 ^ 2 connections between two given peers at any given time: from 1 to 1, from 1 to 2, .. from 1 to 65535, etc. And that's just between two peers -- you can of course have many connections open to many peers simultaneously.
BUT, most operating systems limit the number of open filedescriptors / handles per process. This limit was historically low (20), but is now often higher (1024 on my system, ulimit -a
will show per-process limits in bash(1)
).
In addition to the setrlimit(3)
limits on Unix systems, there are also system-wide limits; /proc/sys/fs/file-max
on a Linux system will report the maximum number of open files allowed on the entire system. (This is 596118 on my system.) Other systems will have different limits.
And, there may be a limit to the number of open connections enforced by a stateful firewall in the middle. Since each state requires memory in the firewall tables, any will probably enforce some arbitrary limit to avoid running short on memory.
A TCP connection is actually identified by peer IP address + peer port + local IP address + local port, so you could actually have way more than 64k, but I don't know if OSs do the work to allow more than 64k per local IP address. Windows doesn't.
One thing of interest is that ports can remain reserved for a short while after they are closed. (This is done to avoid accidental or intentional crosstalk between old and new connections.) By simply creating and closing a connection on tight loop, you can actually make your machine run out of ports. See http://www.perlmonks.org/?node_id=897591 for Perl code that will hang socket connection calls (on some machines) by using up all the sockets.
UDP also has ports, but UDP doesn't have connections. The socket is therefore identified only by its local IP address + local port, so one can have a maximum of 64k UPD ports on the go per local IP address.
Update: Added paragraph on UDP.