how to know if socket is still connected

Grant Edwards grante at visi.com
Mon Jul 17 15:05:32 EDT 2006


On 2006-07-17, nephish at xit.net <nephish at xit.net> wrote:

> oh, sorry, what i mean by dropped is that the server i am
> connecting to can close the connection.

Then recv() will return "" and send() will raise an exception.

> If that happens, i need to know about it. i also need to know
> about it if the server i am connecting to just dies.

Again, you have to define what you mean by "server just dies".

If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "".  If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].

> if recv() returns "" is that the same as NONE ?

No.  It's the empty string (also spelt '').

> I mean can the value be tested true or false?

Yes.  The empty string is false, all non-empty strings are
true.

[1] Unless you've enabled the TCP Keepalive feature, in which
    case the socket will timeout in a couple hours and recv()
    will return "".

-- 
Grant Edwards                   grante             Yow!  Now I am depressed...
                                  at               
                               visi.com            



More information about the Python-list mailing list