简体   繁体   中英

Accepting many tcp connections at one time using c#

I want to accept about 5000 tcp client that are trying to connect exactly at one time.
when i test the program many of client can connect succusfully but many of them cant by giving "No connection could be made because the target machine actively refused it" error.
i increased backlog parameter of listen method of my socket but it didn't help the code i used is the example of msdn with this link. can anybody help me?

It is ok for underlying stack to refuse connections while busy accepting other connections(nothing is really parallel inside). If you really need to connect that many clients at a time, you can change client logic a bit: reconnect on failing (like proposed in comments). Or you can start multiple listeners on different threads on different ports and choose which port to connect by fair dice on clientside.

You may want to look into using the SocketAsyncEventArgs object. Then you can have accept sockets to handle the initial connections and once the connection is established the accept socket will hand it off to a worker SocketAsyncEventArgs object.

Check out this project to get started with it. http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

What I have found is that using this technique works really well, but you will run up against the limitations of the OS and hardware. I tested my tcp server (running on windows server 2008 R2), which uses SocketAsyncEventArgs object, with a few thousand connections and it worked successfully without the clients getting rejected (Had to increase the backlog for this). The problem was that the time between the client and server establishing the connection, and the client getting a response, grew as the number of simultaneous connection requests grew.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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