Problem with lingering "LISTENING" sockets after failed remoting client connect

Problem with lingering "LISTENING" sockets after failed remoting client connect

Post by Per Bergla » Fri, 26 Sep 2003 14:53:13


We have found that if a Socket.Connect fails, you must call Socket.Close
or there will be lingering "LISTENING" ports on your machine with
seemingly random port numbers.

However, when the socket handling is out of our control, such as in a
remoting client (http or tcp doesn't matter), the machine will add one
more port every time the server connection cannot be setup.

(Running on XP/2K with .NET 1.1)

Looking in the CLI source (20021101), the reason is obvious:
************************
socketcache.cs:

private SocketHandler CreateNewSocket()
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);

// disable nagle delays

socket.SetSocketOption(SocketOptionLevel.Tcp,
SocketOptionName.NoDelay,
1);


InternalRemotingServices.RemotingTrace("RemoteConnection::CreateNewSocket:
connecting new socket :: " + _ipEndPoint);

socket.Connect(_ipEndPoint);

return _socketCache.CreateSocketHandler(socket,
_machineAndPort);
} // CreateNewSocket
************************

an exception handling block around "socket.Connect" that calls
socket.Close before propagating the exception would be sufficient

Any chance of this getting fixed?

/Per

PS. Why a failed socket.Connect leaves a lingering connection is not
easy to understand either - maybe it lies somewhere in WinSock.