Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use')

Discussion in 'Python' started by Jean-Paul Calderone, Jan 27, 2009.

  1. On Tue, 27 Jan 2009 10:49:03 +0100, Laszlo Nagy <> wrote:
    >I have a program that uses socket.bind() and socket.listen() frequently.
    >After that program stops, it is not able to bind() again for a while:
    >
    >File "/home/gandalf/Python/Lib/orb/accesspoints/srvtcp.py", line 27, in
    >__init__
    > self.serversocket.bind((self.listen_address,self.port))
    > File "<string>", line 1, in bind
    >socket.error: (48, 'Address already in use')
    >
    >
    >The problem with this, is that this server program SOMETIMES need to be
    >restarted very quickly. I tried to find the solution in the socket module.
    >But there is no "socket.unbind" or "socket.unlisten". How can I tell the OS
    >that I do not want to listen on that address anymore, so other programs can
    >bind on it immediatelly?
    >
    >(Yes I know that I can use setsockopt to allow listening multiple sockets on
    >the same address, but this is NOT what I need...)


    Actually, SO_REUSEADDR is probably just what you want. Since I can't see
    your code and I don't know under what situations it fails, I can only guess
    at the problem, but my guess is that you have connections from the first run
    of your app left in the TIME_WAIT state and they are preventing you from
    binding to the address again in the second run of your app. Setting the
    SO_REUSEADDR flag on POSIX fixes this problem (don't set it on Windows,
    though).

    Jean-Paul
    Jean-Paul Calderone, Jan 27, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,769
    Mark Wooding
    Jan 27, 2009
  2. Laszlo Nagy
    Replies:
    0
    Views:
    532
    Laszlo Nagy
    Feb 1, 2009
  3. Steve Holden
    Replies:
    0
    Views:
    648
    Steve Holden
    Feb 1, 2009
  4. Steve Holden
    Replies:
    1
    Views:
    703
  5. Hendrik van Rooyen
    Replies:
    0
    Views:
    562
    Hendrik van Rooyen
    Feb 2, 2009
Loading...

Share This Page