socket.error: [Errno 98] Address already in use

Nobody nobody at nowhere.com
Sun Sep 19 13:19:19 EDT 2010


On Sun, 19 Sep 2010 18:42:51 +1200, Lawrence D'Oliveiro wrote:

>>>> That's why Stevens recommends that all TCP servers use the
>>>> SO_REUSEADDR socket option.
>>> 
>>> I don’t think I’ve ever used that. It seems to defeat a safety mechanism
>>> which was put in for a reason.
>> 
>> It was put in for the benefit of clients, to prevent them from selecting
>> a port which they won't be able to use.
> 
> But clients typically don’t care what port they use—they let the
> system pick a port for them, so this kind of option is unnecessary.

If they use an ephemeral port, the kernel won't pick a port which has any
connections in the TIME_WAIT state.

However, some clients choose their own source ports. E.g. rlogin/rsh use
privileged (low-numbered) ports, and you can't get the kernel to choose a
random privileged port for you.

If you're writing a server which listens on a known port, you *should* be
using SO_REUSEADDR to avoid unnecessary delays in start-up. The kernel
will automatically reject packets relating to stale connections, and your
server should be accepting any new connections ASAP.




More information about the Python-list mailing list