i have a simple server/client. and i am using the netcat as the client to test the server. if i stop the server before the client exit, i will not be able to sta开发者_StackOverflowrt the server again for a while and i go this error: " [Errno 98] Address already in use "
but if i close the client first, then the server stops, i will not have this issue.
my server socket works like this:
try:
s=socket
s.bind(..)
s.listen(1)
conn,addr=s.accept()
finally:
conn.close()
s.close()
it feels to me that the server did not close the socket properly. but i do not know how to fix this.
You're closing the socket just fine. However, the socket continues to use resources for a few minutes after the socket closes, so that if the remote end missed a packet the packet can be re-sent.
You should be able to work around it by calling the following before you call bind
:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
That will tell the operating system that you want to allow multiple bindings of the socket.
精彩评论