-2

I've created a server-client (well I created the client, my coworker created the server in a different language) application. When I try to connect 900+ clients (all individual TCP clients) to the server I get the following excpetion:

No connection could be made because the target computer actively refused it.

Socket error: 10061

WSAECONNREFUSED

Connection refused. No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.

Eventually, if I wait long enough they will all connect (because we've made our own reconnect/keep alive) on top of the TCP socket. So if it fails it will simply try again till it succeeds.

Do I get this error because the server is 'overloaded' and can't handle all the requests all at once (i'm creating 900 clients each in a separate thread so it's pretty much all trying to connect simultaneously).

Is there a way to counter act this? can we tweak a TCP Socket option so it can handle more clients all at once? It might also be good to note that i'm running the server and client on the same machine, running it on different machines seems to reduce the number of error messages. Which is why I think this is some sort of capacity problem because the server can't handle them all that fast.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
Vincent
  • 1,497
  • 1
  • 21
  • 44
  • You can try increasing the `backlog` parameter to [Socket.Listen()](https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.listen(v=vs.110).aspx). Keep in mind though, that the OS also has an upper limit for that value. [This answer](http://stackoverflow.com/a/15886010/1141432) lists some of those values. – itsme86 Aug 29 '16 at 15:05

1 Answers1

1

Do I get this error because the server is 'overloaded' and can't handle all the requests all at once (i'm creating 900 clients each in a separate thread so it's pretty much all trying to connect simultaneously).

Yes, that is insane(creating 900 individual threads, you should create thread pool using ConcurrentQueue and limit the queue)!

You can increase the number of backlogs using Socket.Listen(backlog);, where backlog is the maximum length of the pending connections queue. The backlog parameter is limited to different values depending on the Operating System. You may specify a higher value, but the backlog will be limited based on the Operating System.

Is there a way to counter act this? can we tweak a TCP Socket option so it can handle more clients all at once?

Not in this case(here it is 900 request already); but, in general - YES, provide more backlog in the Socket.Listen(backlog) for other cases having lesser backlog. Also, use a connection pool(already managed by the .NET runtime environment) and a ConcurrentQueue for handling threads in order. But, remember, you can't provide more backlog than the number which is limited by the OS. 900 is way too much for simpler machines!

It might also be good to note that i'm running the server and client on the same machine, running it on different machines seems to reduce the number of error messages.

It'll, because the load has been distributed on 2 different machines. But, still you shouldn't try what you're doing. Create a ConcurrentQueue to manage the threads(in order).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Can you perhaps explain what Socket.Listen(0) would do? because that's what we are using now. My colleague thought 0 would not be actually 0 but the max a system could handle. The only problem is we cannot find any documentation if 0 is actually a backlog of 0 (which i don't believe it is) – Vincent Aug 30 '16 at 08:47
  • 1
    @VincentAdvocaat - For Linux, *a backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value.*; I have no idea what is the exact length of the listen queue in Windows systems. But, for sure, the connections would be accepted here in Windows too. Also, in MSDN, they have listed that `To determine the maximum number of connections you can specify, retrieve the MaxConnections value. `; but when propagated to Max Connections, it says : `Not supported; will throw a SocketException if used.` – Am_I_Helpful Aug 30 '16 at 08:56