In TCP there is only one way to detect an orderly disconnect, and that is by getting zero as a return value from read()/recv()/recvXXX()
when reading.
There is also only one reliable way to detect a broken connection: by writing to it. After enough writes to a broken connection, TCP will have done enough retries and timeouts to know that it's broken and will eventually cause write()/send()/sendXXX()
to return -1 with an errno/WSAGetLastError()
value of ECONNRESET,
or in some cases 'connection timed out'. Note that the latter is different from 'connect timeout', which can occur in the connect phase.
You should also set a reasonable read timeout, and drop connections that fail it.
The answer here about ioctl()
and FIONREAD
is compete nonsense. All that does is tell you how many bytes are presently in the socket receive buffer, available to be read without blocking. If a client doesn't send you anything for five minutes that doesn't constitute a disconnect, but it does cause FIONREAD
to be zero. Not the same thing: not even close.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…