I need to detect that a TCP connection has been closed by a remote server. I am currently doing this (where c is a net.Conn):one := make([]byte, 1)c.SetReadDeadline(time.Now())if _, err := c.Read(one); err == io.EOF {c.Close()} else {var zero time.Timec.SetReadDeadline(zero)}Is this correct? Is there a simply or faster way to do this?John.
On Thursday, August 23, 2012 10:14:47 AM UTC-7, Kyle Lemons wrote:Are you not reading from or writing to the connection? Both of these operations should indicate an error in the socket, such as the remote side being closed.
To be more specific, a Read or a Write will detect a clean close by the remote side.If you want to detect all disconnects by the remote side, then you need to use some sort of pinging and read with a timeout.
That's TCP. The write succeeds when it's in your kernel's buffers, not when the remote end has ACKd it.
Thomas
That's TCP. The write succeeds when it's in your kernel's buffers, not when the remote end has ACKd it.