2006-09-13 14:40:39

by pledr

[permalink] [raw]
Subject: Error binding socket: address already in use


Hi,
killing a server task that is operating on a UDP socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state. A subsequently started task, that wants to use the same port, gets from bind above error message.This is, in my opinion, wrong behavior, because of the connectionless nature of UDP. Only reboot solves this situation. It looks, as if in net/socket.c, TCP and UDP are handled in the same way without taking into account the different nature of the protocols?!
How can I overcome this problem ?

kind regards

peter lezoch


2006-09-13 14:47:00

by David M. Lloyd

[permalink] [raw]
Subject: Re: Error binding socket: address already in use

On Wed, 2006-09-13 at 14:41 +0000, Peter Lezoch wrote:
> Hi,
> killing a server task that is operating on a UDP socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state. A subsequently started task, that wants to use the same port, gets from bind above error message.This is, in my opinion, wrong behavior, because of the connectionless nature of UDP. Only reboot solves this situation. It looks, as if in net/socket.c, TCP and UDP are handled in the same way without taking into account the different nature of the protocols?!
> How can I overcome this problem ?

Perhaps SO_REUSEADDR is what you're looking for?

man 7 socket

- DML

2006-09-13 14:46:55

by Jan Engelhardt

[permalink] [raw]
Subject: Re: Error binding socket: address already in use


>Hi,
>killing a server task that is operating on a UDP socket( AF_INET,
>SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state. A
>subsequently started task, that wants to use the same port, gets from
>bind above error message.This is, in my opinion, wrong behavior,

man setsockopt
Look for SO_REUSEADDR
It is all correct behavior.

>because of the connectionless nature of UDP. Only reboot solves this

Waiting a while should also solve this.

>situation. It looks, as if in net/socket.c, TCP and UDP are handled in
>the same way without taking into account the different nature of the
>protocols?!
>How can I overcome this problem ?

Jan Engelhardt
--

2006-09-13 15:00:06

by Alan

[permalink] [raw]
Subject: Re: Error binding socket: address already in use

Ar Mer, 2006-09-13 am 14:41 +0000, ysgrifennodd Peter Lezoch:
> Hi,
> killing a server task that is operating on a UDP socket( AF_INET,
> SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state.

For UDP the socket closes at the point the last user of the socket
closes. For TCP there is a time delay mandated by the specification.

If you are seeing UDP sockets remain open when you kill a server make
sure it hasn't forked other processes and passed them the file handle.

2006-09-13 15:33:16

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: Error binding socket: address already in use


On Wed, 13 Sep 2006, Peter Lezoch wrote:

>
> Hi,
> killing a server task that is operating on a UDP socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state. A subsequently started task, that wants to use the same port, gets from bind above error message.This is, in my opinion, wrong behavior, because of the connectionless nature of UDP. Only reboot solves this situation. It looks, as if in net/socket.c, TCP and UDP are handled in the same way without taking into account the different nature of the protocols?!
> How can I overcome this problem ?
>
> kind regards
>
> peter lezoch


Try:
int opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.66 BogoMips).
New book: http://www.AbominableFirebug.com/
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

2006-09-14 07:15:48

by Colin Hirsch

[permalink] [raw]
Subject: Re: Error binding socket: address already in use

On Wed, Sep 13, 2006 at 04:23:35PM +0100, Alan Cox wrote:
> Ar Mer, 2006-09-13 am 14:41 +0000, ysgrifennodd Peter Lezoch:
> > Hi,
> > killing a server task that is operating on a UDP socket( AF_INET,
> > SOCK_DGRAM, IPPROTO_UDP ), leaves the socket in an unclosed state.
>
> For UDP the socket closes at the point the last user of the socket
> closes. For TCP there is a time delay mandated by the specification.
>
> If you are seeing UDP sockets remain open when you kill a server make
> sure it hasn't forked other processes and passed them the file handle.

Additional note on REUSEADDR: The standard semantics of REUSEADDR on a
UDP socket is to allow several sockets to bind to the same address
simultaneously (!), i.e. if your server uses it you can start it several
times on the same socket, which is not what one normally wants.

Regards, Colin