简体   繁体   English

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

[英]Max Outgoing Socket Connections in .NET/Windows Server

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连接到另一台服务器(基本上移动设备能够连接到我的中间层服务器,这样可以保持更稳定的连接到移动设备的第三方服务器)。

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

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? 我的问题是,如何扩展它以便我可以在我的服务器上运行100,000或200,000或更多客户端会话? 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. 我知道有一个注册表设置MaxUserPort需要更改,以超出默认值,似乎是5000.但是,似乎有另一个硬限制65535,我不太清楚该限制所在的位置。 Is this a limit per network interface? 这是每个网络接口的限制吗? Is it a global Windows limit? 它是全局Windows限制吗? 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.)? 如果是每个网络接口的限制,我可以添加多个网络接口并将客户端会话套接字绑定到每个接口(例如:接口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! 谢谢!

A Windows machine can easily scale to very high numbers of open connections. Windows机器可以轻松扩展到非常多的开放连接。 The 64k ephemeral port limit is per IP address, not per machine. 64k临时端口限制是每个IP地址,而不是每台机器。 If you need more ephemeral ports, increase the limits as @SuperTux suggests, but also assign more IPs to the machine. 如果您需要更多短暂端口,请按@SuperTux建议增加限制,但也要为机器分配更多IP。 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). 为了利用,您必须在客户端套接字上手动调用Bind()并从池中传递带有空闲端口的源IP(这也意味着您将负责跟踪每个地址的可用临时端口计数)。 Lots of high-end appliance- type devices do this (SNAT pools on load balancers, for instance) to support hundreds of thousands of concurrent connections. 许多高端设备类型设备执行此操作(例如,负载平衡器上的SNAT池)以支持数十万个并发连接。

Bookkeeping is a hassle, but better than throwing underutilized hardware at it for every 64k client connections. 簿记是一件麻烦事,但比为每个64k客户端连接投入未充分利用的硬件更好。

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 ports on 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 在注册表中找到以下子项,然后单击“参数”: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 值名称: MaxUserPort

    Value Type: DWORD 值类型: DWORD

    Value data: 65534 值数据: 65534

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

    Default: 0x1388 (5000 decimal) 默认值: 0x1388(小数点后5000)

    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. 通常,临时(短期)端口在1024和5000之间分配。

Normally to scale to more than 65K ports you would use multiple servers in a cluster. 通常,要扩展到超过65K端口,您将在群集中使用多个服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM