English 中文(简体)
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).

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!

最佳回答

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.

问题回答

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签