问题
I'm using blocking sockets and I'm quite confused about recv() because I didn't find any decent documentation about it. The official one seems restricted to me. The other thing that is confusing me is that people on the internet(and also on this site) says that it doesn't raise any exception for blocking sockets, but it simply keeps waiting. If I brutally close the server it throws an Errno 10054(socket.error) instead.
回答1:
To put it simply, recv
will throw an exception whenever the underlying socket operation fail. The problem is that it depends on the OS you are on. Here I can guess that you are on Windows, because 10054 is a Windows error code for connection reset. Fortunately, when and why socket operations fail is roughly consistent between OSes (check the Windows documentation for details), and python has a portable solution to identify errors :
import errno
...
if err == errno.ECONNRESET :
print "connection reset"
来源:https://stackoverflow.com/questions/27568995/when-does-socket-recv-raise-an-exception