Socket handles/connections lingering?

J

J. Dudgeon

Hello,

I've written a server that acts as a proxy between the PC and the mainframe
worlds. This server service is writting in C# w/.NET 1.1. The service is
TCP/IP based and makes heavy use of sockets.

The "client" to the service is a C# class library that also uses sockets.
I've managed to get the performance of the service up to a decent speed but
I've hit a strange problem.

My test program (that uses the C# class library) runs a series of
transactions on multiple threads for a configurable number of iterations.
Everything runs fine up until around 3600-3900 transactions (total) at which
point my C# class library throws a SocketException from the Socket.Connect
call:

System.Net.Sockets.SocketException: Only one usage of each socket address
(protocol/network address/port) is normally permitted.

I understand what this message is saying but I don't know why I'm getting
it. The C# class library guarantees, in its Dispose() method that
Socket.Close() is called (to free the underlying socket handle.) The test
program wraps the C# class in a "using" block to ensure the Dispose() method
is called on my C# class (which inherits from IDisposable.) I even tried
setting the DontLinger option to true but the problem remains. Doing a
"NETSTAT -a" in the console shows numerous TCP/IP connections from the test
program to the service with a TIME_WAIT status; so, apparently all the
sockets are being consumed and not released fast enough.

If I terminate the process (or even wait a while) I can run another 3600
transactions before getting this error. The problem with this is that I can
reach 3600 transactions in about 1 minute.

Is what I'm seeing a bug in System.dll library? Peeking into the source
shows that the Socket class calls OSSOCK.closesocket in its Dispose()
method, so I'm not sure why things aren't getting closed. Eventually the
socket handles are freed but it seems to take just long enough to cause
issues.

Is there a socket option that I can set to alleviate this issue? Can
Windows just not keep up with freeing the socket handles?

Any help would be greatly appreciated.

Thank you in advance,

Joel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top