python socket.recv/sendall call blocking

Posted by fsm on Stack Overflow See other posts from Stack Overflow or by fsm
Published on 2010-03-31T01:09:43Z Indexed on 2010/03/31 1:13 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

Hi everyone.

This post is incorrectly tagged 'send' since I cannot create new tags.

I have a very basic question about this simple echo server. Here are some code snippets.

client

while True:
 data = raw_input("Enter data: ")
 mySock.sendall(data)
 echoedData = mySock.recv(1024)
 if not echoedData: break
 print echoedData

server

while True:
 print "Waiting for connection"
 (clientSock, address) = serverSock.accept()
 print "Entering read loop"
 while True:
     print "Waiting for data"
     data = clientSock.recv(1024)
     if not data: break
     clientSock.send(data)
 clientSock.close()

Now this works alright, except when the client sends an empty string (by hitting the return key in response to "enter data: "), in which case I see some deadlock-ish behavior.

Now, what exactly happens when the user presses return on the client side? I can only imagine that the sendall call blocks waiting for some data to be added to the send buffer, causing the recv call to block in turn. What's going on here?

Thanks for reading!

© Stack Overflow or respective owner

Related posts about python

Related posts about sockets