Detecting shutdown of remote socket endpoint.
mike at hobbshouse.org
Wed Jan 12 10:41:59 EST 2005
Tim Gosselin <gosselit at norwich.edu> wrote:
> I am writing a tcp tunnel but cannot find a way of detecting when a socket
> shuts down its read end without writing to the socket. For testing the
> write end of the remote endpoint I just do a:
> if not sock.recv(buffsize)
> I cannot write to the socket and check if send returns 0 though, because
> that involves sending data out of the which may not have come in yet.
> When I try polling the socket with:
> r, w, e=select(,[sock],, 0)
> w returns with the socket still writable, even if the other end was
Even at the C level, I believe there were some differences between
Unix and Windows sockets in regard to this, so this advice may be
dependent on your platform.
At any rate, on both Unix and Windows, a closed socket will continue
to report itself as writable. To detect the closed socket, I believe
that one system reports the socket as being in error whereas the
other system reports the socket as being readable (where read() will
then immediately return 0 because the socket is closed).
So in summary, instead of:
r, w, e=select(,[sock],, 0)
r, w, e=select([sock],,[sock], 0)
If r or e is non-empty, then the socket has been closed (or there's
some other error).
More information about the Python-list