Problems with socket

Steve Holden sholden at
Fri Nov 10 17:15:07 CET 2000

Rolf Wester wrote:
> Hi,
> I have a little problem with using sockets. When I call recv(n) on a
> socketobject
> with n > (number of bytes sent by the client), recv(n) returns. When I
> choose n to be
> smaller than the number of bytes sent by the client recv(n) returns with
> n bytes read.
> When I repeat calling recv(n) until there are no more data to be read,
> recv(n) doesn't return.

This is standard behaviour for a blocking socket, the default

> Can I change this behaviour, so that recv(n) returns even when it is
> called in case that there
> are no more bytes to be read. Below you will find a simple server and
> client that demonstrate
> taht behaviour (copied from the Python-docs and changed a little bit).
If you use socket.setblocking(0) to tell the socket not to block your
process when no data is queued, you should find that you get an immediate
socket.error, which you can trap and treat as "no data waiting".  However,
this does make your socket's overall behaviour more complex to handle,
so you might want to use setblocking(1) immediately after the recv()
-- on both the error and the non-error paths!.

Helping people meet their information needs with training and technology.
703 967 0887      sholden at

More information about the Python-list mailing list