How do I reconnect a disconnected socket?
Bryan Olson
fakeaddress at nowhere.org
Sat Mar 29 04:40:21 EDT 2008
Grant Edwards wrote:
> Laszlo Nagy wrote:
>> What about non-blocking sockets?
>
> $ man recv
> ...
> If no messages are available at the socket, the receive
> calls wait for a message to arrive, unless the socket is
> non-blocking (see fcntl(2)), in which case the value -1
> is returned and the external variable errno set to
> EAGAIN.
As Grant showed, a successful recv() returning zero bytes
indicates the end of the data stream from the remote peer.
Other cases block or raise errors.
A few more notes:
When a socket will return zero bytes to indicate the end of the
data stream, it will select() as readable.
In the case Laszlo asked about here -- a non-blocking socket
is not shutdown but no data is immediately readable -- on some
systems, the error is EWOULDBLOCK, which may be, but is not
generally, the same code as EAGAIN. (There may be yet other
systems that have different error conventions; I don't know.)
The Python library's 'errno' module exports the error names with
their associated numeric values.
Python's socket module does not 'return' these error codes; it
raises exceptions. A non-blocking socket, or equivalently a
socket with a timeout of zero, will raise socket.error. The
errno in the exception's (errno, string) value should be
EAGAIN or EWOULDBLOCK. If a socket has a non-zero timeout,
and that timeout expires, the operation raises socket.timeout.
--
--Bryan
More information about the Python-list
mailing list