5

I'm writing a custom TCP server and client and on doing a ton of requests (60,000 to be exact) I begin to get this socket error of 10048, which should mean "the address is already in use."

The error keeps happening unless I pause the process for like 2 or 3 minutes and then begin it again, and then it begins to bring up the same error a short while after restarting it. If I pause the client process and restart the server process, I still get the same error on the client. So it is a complete client side problem.

This does not make sense though, this error only usually occurs when binding and this error happens on the client and not the server. What could be the possible reasons for it?

A small excerpt of my initialization:

TcpClient client = new TcpClient();
client.Connect("XXXXX -- some ip", 25000);
client.NoDelay = true;
NetworkStream clientStream = client.GetStream();

Also, everything else seems to be working fine(including the amount of time it takes to send back and forth) and this works perfectly when using 127.0.0.1 but when putting it on another LAN computer I begin to get the 10048 error.

Is there something wrong with how I initialize it? What else could cause this error on the client side?

Earlz
  • 62,085
  • 98
  • 303
  • 499

2 Answers2

9

See http://msdn.microsoft.com/en-us/library/e160993d%28v=VS.90%29.aspx SetSocketOption. You need DontLinger or ReuseAddr, or both, I'm not sure. Basically your sockets are stuck in TIME_WAIT state for a while after you tear down the TCP connection, once you get enough of them, you won't be able to create any new client connections. Verify this with netstat -na program output.

You can also reduce the time that socket stays in TIME_WAIT state by changing it in the registry: http://msdn.microsoft.com/en-us/library/aa560610%28BTS.20%29.aspx Default is 4 minutes which can probably be reduced to 1 or 2 minutes safely, especially for testing.

Disclaimer: I'm not a TCP guru by any means.

MK.
  • 33,605
  • 18
  • 74
  • 111
0

Are you running out of client ports?

http://www.gavaghan.org/blog/2010/02/17/tcpip-parameter-tuning-for-rapid-client-connections/

A possible related question: Maximum number of concurrent connections on a single port (socket) of Server

Community
  • 1
  • 1
Richard Morgan
  • 7,601
  • 9
  • 49
  • 86
  • If this is the reason, then how does it work fine whenever I use localhost? – Earlz Mar 29 '10 at 21:37
  • 1
    @Earlz: It might be because connections on 127.0.0.1 use a very low (0?) maximum segment lifetime, as we really can not get wandering packets. I wasn't able to find anything on this by googling, but it would make sense. You could try and use an interface IP and see if this makes it reproducible on the local machine - though this may be optimized as well. Also have a look at setting TcpTimedWaitDelay to 30 seconds (http://msdn.microsoft.com/en-us/library/ms819739.aspx) on the server, if this is possible – Mads Ravn Mar 29 '10 at 22:42