tags:

views:

75

answers:

1

hi,

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 start 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.

+4  A: 

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.

Daniel Stutzbach
thanks a lot :)
davidshen84