When I close the socket on one end of a connection, the other end gets an error the second time it sends data, but not the first time:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 12345))
server.listen(1)
client = socket.create_connection(("localhost",12345))
sock, addr = server.accept()
sock.close()
client.sendall("Hello World!") # no error
client.sendall("Goodbye World!") # error happens here
I've tried setting TCP_NODELAY, using send
instead of sendall
, checking the fileno()
, I can't find any way to get the first send to throw an error or even to detect afterwards that it failed. EDIT: calling sock.shutdown
before sock.close
doesn't help. EDIT #2: even adding a time.sleep
after closing and before writing doesn't matter. EDIT #3: checking the byte count returned by send
doesn't help, since it always returns the number of bytes in the message.
So the only solution I can come up with if I want to detect errors is to follow each sendall
with a client.sendall("")
which will raise an error. But this seems hackish. I'm on a Linux 2.6.x so even if a solution only worked for that OS I'd be happy.