Windows 对每台机器同时打开的套接字/连接数的限制 [英] Windows limitation on number of simultaneously opened sockets/connections per machine

查看:44
本文介绍了Windows 对每台机器同时打开的套接字/连接数的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有一个真实网络接口和几个环回接口的 Windows 7.我有启用 IOCP 的服务器,它接受来自客户端的连接.我正在尝试尽可能多地模拟与服务器的真实客户端连接.

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.

我的客户端代码只是建立了 X 个套接字连接(注意客户端绑定到给定的接口):

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));
        }

在环回接口上,我有几个用于绑定的 IP.另外,我也是用真实的接口来绑定的.当每台机器打开的套接字数量约为 64K 时,我遇到了一个问题:

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:

未处理的异常:System.Net.Sockets.SocketException:无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满

我尝试了一些无助的事情,例如:- 在注册表中将 MaxUserPort 设置为最大值和一些其他推荐的 TCPIP 设置.- 尝试在不同的接口(真实接口和环回)上运行两个服务器并使用多个客户端.

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.

这是 Windows 中的一个已知限制还是有可能以某种方式克服它?

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

感谢您的帮助!

推荐答案

我在一些 Microsoft 页面上发现:

I have found on some Microsoft page that:

... HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort注册表子项被定义为可以为通配符绑定分配端口的最大端口.MaxUserPort 注册表项的值定义了动态端口范围...

... 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);

然后我可以在系统中同时打开超过 64K 个套接字.

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

这篇关于Windows 对每台机器同时打开的套接字/连接数的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆