A bad file descriptor most likely means that the socket is already closed by another thread.
Here are some thoughts on common practices. For the client, one way to find out if it is disconnected is to check if the recv () value is 0. If so, it means that the remote side has closed the connection. Basically, you should use select (or poll) and pass the fds of all clients and server to select. If you get a read event on any of the fds, then depending on the type of fd, this is what happens. If fd is a server type, then a read event means that there is a pending connection, and you must send accept () to get a new connection. On the other hand, if hte fd is not a server type (which means a regular tcp connection), then a read event means there is some data, and you should throw a recv () event to read the data.
You must use a loop to select. Basically, start the loop using the select () call, and as soon as you get the event, do something with this event, and then re-run the loop and issue the next select (). You may find these links useful: http://ilab.cs.byu.edu/python/select/echoserver.html and http://docs.python.org/2/library/socket.html
source share