Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows limitation on number of simultaneously opened sockets/connections per machine

Let's say I have Windows 7 with one real network interface and few loopback interfaces. I have IOCP enabled server that accepts connections from clients. I'm trying to simulate as much as possible real client connections to the server.

My client code simply establishes X amount of socket connections (note that client binds to a given interface):

        const Int32 remotePort = 12345;
        const Int32 MaxSockets = 60000;

        Socket[] s = new Socket[MaxSockets];
        IPEndPoint bindEndpoint = new IPEndPoint(IPAddress.Parse(args[0]), 0);
        for (Int32 i = 0; i < MaxSockets; i++)
        {
            s[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s[i].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            s[i].Bind(bindEndpoint);
            s[i].Connect(args[1], remotePort);

            IPEndPoint socketInfo = (IPEndPoint)s[i].LocalEndPoint;
            Console.WriteLine(String.Format("Connected socket {0} {1} : {2}", i, socketInfo.Address, socketInfo.Port));
        }

On a loopback interface I have several IPs that I use for binding. In addition, I also use real interface to bind on. I ran into a problem when amount of opened sockets is around 64K per machine:

Unhandled Exception: System.Net.Sockets.SocketException: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full

I've tried several helpless things like: - setting MaxUserPort to max value and some other recommended TCPIP settings in the registry. - trying to run two servers on different interfaces (real interfaces and loopback) and using several clients.

Is it a known limitation in Windows or its possible to overcome it somehow?

Thanks for the help!

like image 315
a_m Avatar asked Feb 28 '12 18:02

a_m


People also ask

How many sockets can be opened at once?

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

How many connections a concurrent server can handle at a time?

On the TCP level the tuple (source ip, source port, destination ip, destination port) must be unique for each simultaneous connection. That means a single client cannot open more than 65535 simultaneous connections to a single server. But a server can (theoretically) serve 65535 simultaneous connections per client.

How many TCP connections can Windows handle?

Increasing the number of available (ephemeral) ports. By default, Windows only allocates ephemeral ports in the range 1024 through 4999. To increase the upper range of ephemeral ports that are dynamically allocated to client TCP/IP socket connections perform the following.

How many sockets can Windows have?

The theoretical maximum for Windows is approximately 25,000 socket handles; however, in practical terms, it is safe to estimate that the Windows Server platforms can allocate approximately 16,000 handles on a system with 2G or more of RAM.


Video Answer


1 Answers

I have found on some Microsoft page that:

... HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort registry subkey is defined as the maximum port up to which ports may be allocated for wildcard binds. The value of the MaxUserPort registry entry defines the dynamic port range...

So, if I force the endpoint to use a certain port, e.g.

IPEndPoint bindEndpoint = new IPEndPoint(IPAddress.Parse(args[0]), 54321);

Then I can open more than 64K simultaneous sockets in the system.

like image 87
a_m Avatar answered Sep 30 '22 23:09

a_m