Detecting socket connection failure

Ben Sizer kylotan at gmail.com
Thu Jul 20 04:55:32 EDT 2006


schwehr at gmail.com wrote:
> First, the proof that
> something is there and rejecting the connection (or is it that this
> thing actually accepts the connection and then drops it?)...

Yes, it accepts it and then drops it, or perhaps drops it after
receiving some data? It's not a failed or rejected connection from a
socket point of view, however.

> In [4]: remote.recv(200)
> Out[4]: ''

Assuming I understand the socket module, given how under-documented it
is, I assume this means the socket has now been closed.

> How do I detect this case?  The recv may
> really not have any data for a long time, so a recv of no bytes is not
> a way to test the connection status.

You already received zero bytes the first time, which I believe means
the socket is closed, and you shouldn't pass it to select a second
time. You should never get zero bytes unless the socket is closed,
otherwise it would just sit there and wait until it has some bytes to
return to you. Select doesn't tell you that there's data waiting -
obviously it can't, as how would it handle the write case? - but in
fact tells you that the socket is 'ready', and operations upon it
should return immediately. And 'ready' in this case could well just
mean it's ready to tell you that it's not connected.

-- 
Ben Sizer




More information about the Python-list mailing list