python socket.recv/sendall call blocking

╄→гoц情女王★ 提交于 2019-12-24 10:54:52

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/2549788/python-socket-recv-sendall-call-blocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!