python socket.recv/sendall call blocking
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: brea开发者_高级运维k
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!
More like, the sendall()
call does nothing (since there's no data to send), and thus the recv()
call on the client blocks waiting for data, but since nothing was sent to the server, the server never sends any data back since it's also blocked on its initial recv()
, and thus both processes are blocked.
精彩评论