Sockets that dont know when to leave...

Michael Paddon,,, mwp at ubeaut.enet.dec.com
Tue Apr 2 12:09:29 AEST 1991


>From article <7817 at jhunix.HCF.JHU.EDU>, by andy at jhunix.HCF.JHU.EDU (Andy S Poling):
> When certain daemons (UREP's SNA-over-UDP daemon for instance) are killed,
> the UDP sockets that they opened don't die with them.  This is a BIG problem
> because the UDP socket these daemons use must be bound to a certain local
> address (port).  So once a daemon has run and has either exited or has been
> killed that particular port is no longer available for binding to a socket
> (we get EADDRINUSE - errno 48).  When one uses "netstat -a" to see what
> sockets are in use there will indeed be a UDP socket open with that address
> assigned.  Furthermore netstat usually shows that there is data in the recv
> queue for that socket.

I havn't seen this happen for UDP sockets, but this is common happenstance
under TCP (ie. SOCK_STREAM) type sockets. In the TCP world there is good
reason for this as the connection should be shut down in an orderly fashion;
however there is no good reason for keeping the socket hanging around in
a datagram paradigm.

Anyway, there is a workaround. Set the socket level option SO_REUSEADDR
with setsockopt(2). This will allow multiple sockets to be bound to the
same local <host,port> tuple.

What does it mean when two sockets have the same address? In the TCP domain,
connections are specified by the tuple
	<local_host,local_port,remote_host,remote_port>
so there is no ambiguity introduced. If both sockets are marked passive
(via listen(2)) it is arbitrary which socket will accept an incoming
connection.

In the UDP domain, incoming datagrams addressed to that <host,port> will
be assigned to an arbitrary socket. Actually, the choice of socket isn't
really at random, but that is as reasonable model as any for a programmer
using sockets to use. In practice, the more server processes using the
same UDP address there are, the better throughput you can get. This
technique is used by nfsd(8), among others.

So there are some good reasons for reusing addresses. The only thing you
lose when you set SO_REUSEADDR is the mutual exclusion of not being able
to start up a second server if there is already one running (because the
bind fails). Since this functionality can be provided in other ways, it is
no real problem.

					Michael

-------------------------------------------------------------------
|                     |     Internet: mwp at ubeaut.enet.dec.com     |
|   Michael Paddon    |     ACSnet:   mwp at munnari.oz.au           |
|                     |     Voice:    +61 3 895 9392              |
-------------------------------------------------------------------



More information about the Comp.unix.ultrix mailing list