Re: Detecting when a socket has been closed.



Kurt wrote:
I have a client & server app which communicates using the socket class. If I shutdown the server closing the socket the client still thinks the socket is open. Even calling send does not throw an exception.

Thanks
Kurt



Hi,
First send will not throw exception, because there is no way for TCP stack to know that other end has actually closed its receiving side. Second send may throw or may not, depending on the timing. It takes some time for packet to reach other side and return information that server side is gone (it may take as long as 2-3 minutes). This is by design of TCP protocol.

Few things you should note:
a) if send succeeds, that doesn't mean data is received by other side. It means only that it is buffered by the OS
b) only when TCP stack timeouts on send (if foreign computer becomes unreachable due to connection going down) TCP will know something is wrong. You will not be notified until next operation (send or receive). This may take minutes.
c) in case other end has closed socket and network is still operating after first send TCP RST segment will be returned to TCP stack notifying it connection has gone down. That send will succeed however, next should fail. This takes also some time, though generally is much faster than TCP timeout mechanism.
d) on idle connection, there is no exchange of data over socket. Meaning: if you pull out the cable of server computer, you will never be notified of this unless you try to send something (read will block forever). First send will succeed, than after timeout next send/read should fail. There is a keepalive option that can prevent this.
e) if you pull cable of client computer, you may get immediate notification due to windows media sense feature.
f) Markus is right, async receive should complete when connection tear-down is detected. But when exactly it is detected, is different story.
g) to close connection gracefully, both server and client must follow some constraints. Check:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/graceful_shutdown_linger_options_and_socket_closure_2.asp


Regards,
Goran

.



Relevant Pages

  • Re: Socket Exception
    ... In this case the server most likely rejected your ... connection. ... Each command is a new socket connection that is opened and closed ... /// Required designer variable. ...
    (microsoft.public.win32.programmer.networks)
  • Re: ASP.NET 2.0 - a newbie question
    ... Is there some experienced ASP.NET 2.0 developer, ... server that comes bundled Microsoft VWDE. ... A connection attempt failed because the ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [2.6.24.3][net] bug: TCP 3rd handshake abnormal timeouts
    ... server via TCP and non-persistent connections and all application level ... "Sometimes" when establishing a TCP connection to the server, ... Here is a simplified version of what _both_ the server and the client ...
    (Linux-Kernel)
  • Re: [2.6.24.3][net] bug: TCP 3rd handshake abnormal timeouts
    ... server via TCP and non-persistent connections and all application level ... "Sometimes" when establishing a TCP connection to the server, ... Here is a simplified version of what _both_ the server and the client ...
    (Linux-Kernel)
  • error when move web service from dev machine to test server
    ... I have a simple web service that I developed on my laptop that resides ... test server, & when I try to run it, I get the error "No connection could be ... No connection could be made because the target machine actively refused it ... Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState ...
    (microsoft.public.dotnet.framework.webservices)