.NET/Windows Server 中的最大传出套接字连接数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1745862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:26:58  来源:igfitidea点击:

Max Outgoing Socket Connections in .NET/Windows Server

c#.netwindowssocketswindows-server-2003

提问by Redth

I have a slightly unusual situation where I'm needing to maintain CLIENT tcp connections to another server for thousands of mobile users on my servers (basically the mobile devices connect to my middle tier server when they are able to, which maintains a more stable connection to the 3rd party server for the mobile devices).

我有一个稍微不寻常的情况,我需要为我的服务器上的数千个移动用户维护到另一台服务器的 CLIENT tcp 连接(基本上,移动设备在它们能够连接到我的中间层服务器时,保持更稳定的连接到移动设备的第 3 方服务器)。

Anyways, I've developed my server application using Async Sockets (wrapped up in a SslStream), and have got 1000 client sessions running full time on it right now. I'm quite happy with the results so far as I'm seeing about 0-10% average cpu usage on a single core processor, and about 60mb of ram being used over time.

无论如何,我已经使用异步套接字(封装在 SslStream 中)开发了我的服务器应用程序,并且现在有 1000 个客户端会话全时运行。我对结果非常满意,因为我看到单核处理器上的平均 CPU 使用率约为 0-10%,并且随着时间的推移使用了大约 60mb 的内存。

My question is, how do I scale this up so I can reach 100,000 or 200,000 or more client sessions being run on my server? Again, this is a bit untraditional, as my server isn't really acting like a server, since I'm worried about outgoing connections, not incoming.

我的问题是,我如何扩展它以便我可以在我的服务器上运行 100,000 或 200,000 或更多的客户端会话?同样,这有点不传统,因为我的服务器实际上并不像服务器,因为我担心传出连接,而不是传入连接。

I know that there's a registry setting MaxUserPort that needs to be changed to get beyond the default which seems to be 5000. However, there seems to be another hard limit of 65535, and I'm not too clear on where that limit resides. Is this a limit per network interface? Is it a global Windows limit? Is it a limit per process?

我知道有一个注册表设置 MaxUserPort 需要更改才能超过默认值,似乎是 5000。但是,似乎还有另一个硬性限制 65535,我不太清楚该限制在哪里。这是每个网络接口的限制吗?它是全球 Windows 限制吗?每个进程是否有限制?

If it is a limit per network interface, can I add multiple network interfaces and bind client session sockets to each interface (eg: 65k on interface 1, 65k on interface 2, etc.)?

如果是每个网络接口的限制,我是否可以添加多个网络接口并将客户端会话套接字绑定到每个接口(例如:接口 1 上的 65k,接口 2 上的 65k 等)?

I'm also not too sure what, if any socket options or properties I should be setting to help things out. Right now I'm not using any socket options.

我也不太确定是什么,如果我应该设置任何套接字选项或属性来帮助解决问题。现在我没有使用任何套接字选项。

I'd really appreciate any thoughts on this subject, as clear advice has been pretty hard to come by on this subject. Thanks!

我真的很感激关于这个主题的任何想法,因为在这个主题上很难得到明确的建议。谢谢!

采纳答案by nitzmahone

A Windows machine can easily scale to very high numbers of open connections. The 64k ephemeral port limit is per IP address, not per machine. If you need more ephemeral ports, increase the limits as @SuperTux suggests, but also assign more IPs to the machine. To take advantage, you'll have to manually call Bind() on your client socket and pass a source IP from your pool with free ports (this also implies you'll be responsible for keeping track of available ephemeral port counts per address). Lots of high-end appliance- type devices do this (SNAT pools on load balancers, for instance) to support hundreds of thousands of concurrent connections.

Windows 机器可以轻松扩展到非常多的开放连接。64k 临时端口限制是每个 IP 地址,而不是每个机器。如果您需要更多临时端口,请按照@SuperTux 的建议增加限制,但也要为机器分配更多 IP。要利用这一点,您必须在您的客户端套接字上手动调用 Bind() 并从您的池中传递一个带有空闲端口的源 IP(这也意味着您将负责跟踪每个地址的可用临时端口数)。许多高端设备类型的设备都这样做(例如,负载平衡器上的 SNAT 池)以支持数十万个并发连接。

Bookkeeping is a hassle, but better than throwing underutilized hardware at it for every 64k client connections.

簿记很麻烦,但总比为每 64k 个客户端连接投入未充分利用的硬件要好。

回答by Supertux

65355 is a limit of the IP protocol and more importantly is the limit the TCP/IP stacks of most operating systems impose.

65355 是 IP 协议的限制,更重要的是大多数操作系统的 TCP/IP 堆栈的限制。

To increase the maximum number of ephemeral portson Windows, follow these steps:

要增加Windows 上的最大临时端口数,请执行以下步骤:

  1. Start Registry Editor.
  2. Locate the following subkey in the registry, and then click Parameters: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
  3. On the Edit menu, click New, and then add the following registry entry:

    Value Name:MaxUserPort

    Value Type:DWORD

    Value data:65534

    Valid Range:5000-65534 (decimal)

    Default:0x1388 (5000 decimal)

    Description: This parameter controls the maximum port number that is used when a program requests any available user port from the system. Typically , ephemeral (short-lived) ports are allocated between the values of 1024 and 5000 inclusive.

  1. 启动注册表编辑器。
  2. 在注册表中找到以下子项,然后单击参数:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
  3. 在编辑菜单上,单击新建,然后添加以下注册表项:

    值名称:MaxUserPort

    值类型:DWORD

    数值数据:65534

    有效范围:5000-65534(十进制)

    默认值:0x1388(十进制 5000)

    说明:此参数控制程序从系统请求任何可用用户端口时使用的最大端口号。通常,临时(短期)端口分配在 1024 和 5000(含)之间。

Normally to scale to more than 65K portsyou would use multiple servers in a cluster.

通常要扩展到超过 65K 的端口,您会在集群中使用多个服务器。