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

发布于 2024-08-11 13:20:45 字数 673 浏览 2 评论 0原文

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

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

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

我知道有一个注册表设置 MaxUserPort 需要更改以超出默认值(似乎是 5000)。但是,似乎还有另一个硬限制 65535,我不太清楚该限制位于何处。 这是每个网络接口的限制吗?这是 Windows 的全球限制吗?每个进程有限制吗?

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

我也不太确定我应该设置什么(如果有的话)来帮助解决问题。现在我没有使用任何套接字选项。

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

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

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.

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.

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?

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-08-18 13:20:45

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

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

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.

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

凡尘雨 2024-08-18 13:20:45

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

要增加 Windows 上临时端口的最大数量,请按照下列步骤操作:

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

    值名称:MaxUserPort

    值类型: DWORD

    数值数据:65534

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

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

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

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

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

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

  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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文