socket programming

Bryan Olson bryanjugglercryptographer at
Thu Sep 16 01:38:26 CEST 2004

Ajay wrote:
> the code is
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> the problem is when my client finished sending and waits to receive, on the
> server side, it still stays in the while loop waiting to receive more data.

SOCK_STREAM sockets have no concept record-boundary.  If you 
need to convey the end of one message, but keep the connection 
open, then you need to define or find a protocol that builds 
distinct messages on top of the byte stream.

If the client is truly done sending on that socket, it can call 
s.shutdown(1). Then, after the server has received all the sent 
data, (the socket will select as readable and) the server's next 
recv will return, but the size of the read will be zero bytes.

Remote shutdown of sending is the only case in which recv will 
succeed with a zero size.  If remote side has not shut down, and 
there is no data available, a recv on a blocking sockets will 
block, and non-blocking sockets will raise/return EWOULDBLOCK.


More information about the Python-list mailing list