TcpListener Socket still active after program exits.
Posted
by lnical
on Stack Overflow
See other posts from Stack Overflow
or by lnical
Published on 2010-05-12T19:49:17Z
Indexed on
2010/05/12
19:54 UTC
Read the original article
Hit count: 279
I'm trying to stop a TCP Listener as my program is exiting. I do not care about any data that is currently active on the socket or any of the active client sockets.
The socket clean up code is essentially:
try
{
myServer.Server.Shutdown(SocketShutdown.Both)
}
catch (Exception ex)
{
LogException(ex)
}
myServer.Server.Close(0)
myServer.Stop()
myServer is a TCPListener
On some occasions, Shutdown will thrown an exception
System.Net.Sockets.SocketException: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
When this happens, the socket is never released. Even after the application exits netstat shows the socket is still in the listening state.
I have not been able to create definitive reproduction scenerio, it happens at seemingly random times.
Client Sockets are cleaned up independently.
Do you have any suggestions to help me make this socket die?
© Stack Overflow or respective owner