golang TCPConn.SetWriteDeadline doesn't seem to work as expected

前端 未结 1 1602
夕颜
夕颜 2021-02-10 12:05

I\'m trying to detect sending failures by inspecting the error returned by golang TCPConn.Write, but it\'s nil. I also tried using TCPConn.SetWriteDeadline without success.

1条回答
  •  深忆病人
    2021-02-10 13:04

    This isn't Go specific, and is a artifact of the underlying TCP socket showing through.

    A decent diagram of the TCP termination steps is at the bottom of this page: http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm

    The simple version is that when the client closes its socket, it sends a FIN, and receives an ACK from the server. It then waits for the server to do the same. Instead of sending a FIN though, you're sending more data, which is discarded, and the client socket now assumes that any more data coming from you is invalid, so the next time you send you get an RST, which is what bubbles up into the error you see.

    Going back to your program, you need to handle this somehow. Generally you can think of whomever is in charge of initiating a send, is also in charge of initiating termination, hence your server should assume that it can continue to send until it closes the connection, or encounters an error. If you need to more reliably detect the client closing, you need to have some sort of client response in the protocol. That way recv can be called on the socket and return 0, which alerts you to the closed connection.

    In go, this will return an EOF error from the connection's Read method (or from within the Copy in your case). SetWriteDeadline doesn't work because a small write will go though and get dropped silently, or the client will eventually respond with an RST, giving you an error.

    0 讨论(0)
提交回复
热议问题