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

Steve Holden steve at holdenweb.com
Sun Feb 1 07:58:58 EST 2009


Laszlo Nagy wrote:
> 
>> 8<------------------------------
>>
>>  
>>> .......  Setting the
>>> SO_REUSEADDR flag on POSIX fixes this problem (don't set it on Windows,
>>> though).
>>>     
>>
>> Why not?  I have been merrily setting it, and I have not noticed
>> anything weird.
>> (yet)
>>   
> Please see my original post. I specifically stated that I do not want to
> use setsockopt and be able to listen on the same port from many
> processes. I knew that I could use SO_REUSEADDR, but I'm heistating to
> do so. I must guarantee that only one process listens on a given port at
> the same time.

My previous reply assumed you are running some UNIX-like operating
system. If you are on Windows then Jean-Paul's advice stands, as Windows
*does* allow several processes to listen on the same port and randomly
delivers incoming connections to one of the listening processes.

I believe this is because Microsoft failed to understand the original
meaning of SO_REUSEADDR for their early TCP implementations, and
persisted with this ghastly error in the name of backwards
compatibility, justifying it by suggesting that listener pools could be
created. Or some such nonsense. Perhaps someone with more insight into
the development process could comment. It seems to me it's completely
bizarre.

However, under Windows 2000 and later you should find there's an
SO_EXCLUSIVEADDRUSE flag which you can use to ensure a single listener -
see http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx. No
need for separate locks.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list