Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket errors of 10048 on the client? Possible causes?

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?

like image 706
Earlz Avatar asked Mar 29 '10 20:03

Earlz


1 Answers

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.

like image 97
MK. Avatar answered Sep 28 '22 08:09

MK.