socket and exception

Gordon McMillan gmcm at
Fri Mar 10 05:20:34 CET 2000

Jerome Chan wrote:
> I'm using a select with a non blocking sock_stream tcp socket. And I'm
> sending data. The other side suddenly closes its socket. How do I determine
> that the socket is closed? I get an exception (which is different for each
> os). 

Depending on OS and timing, you could get it on the send, or 
select could tell you "bad fd" or the err list in select could 
detect it. Paranoia helps. FWIW, *nix usually detects this in 
the select, and Windows in the send.

> But I also get an exception when the outgoing buffer is full, 
in the
> case that the other socket is processing too slowly or is swapping memory or
> some other event. Is there a portable way of distinguishing between the two
> exceptions? 

I don't think so. You should either find select telling you the 
socket is not writable (which really just means the network 
buffers are full), or the send may not send everything you 
asked it to (check the length returned from send). But if select 
told you it was writable, and the other end is still alive, then 
send should send at least one byte. Well, assuming that you 
use select before every send, like God intended.

- Gordon

More information about the Python-list mailing list