Windows 对每台机器同时打开的套接字/连接数的限制

标签 windows sockets networking iocp

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

我的客户端代码简单地建立了 X 数量的套接字连接 (注意客户端绑定(bind)到给定的接口(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));
        }

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

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

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

这是 Windows 中的已知限制还是可以通过某种方式克服它?

感谢您的帮助!

最佳答案

我在某些 Microsoft 页面上发现:

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

因此,如果我强制端点使用某个端口,例如

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

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

关于Windows 对每台机器同时打开的套接字/连接数的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487569/

相关文章:

安卓套接字异常 "socket is closed"

javascript - Express js : Router. 中的 Socket.io 使用需要中间件函数但未定义

networking - 如何在 Fortran 中连接到网络?

windows - 栈和栈基地址

c# - 在 Windows 10 和 7 上部署 WPF c#

windows - 您如何以编程方式从 Windows 中检测双引导(尤其是 Linux)?

python - 在 Windows 10 上安装 gtk3 + glade

windows - Windows 8 应用商店应用程序可以通过 UDP/TCP 套接字与 Windows 7 桌面应用程序通信吗?

networking - P2P 应用程序中的直接 TCP/IP 连接

docker - Docker容器网络-Kafka Producer