Hello!
On Fri, Aug 20, 1999 at 12:19:59AM +0200, Anders Henriksson wrote:
> Hello all,
> I try to make my program behave politely by releasing discarded
> server sockets. Unfortunately this never happens. With the simple
> code below, the program cannot be rerun, as it fails with an
> "Address already in use".
As Sven Panne already lined out, that's standard behaviour in BSD
compatible socket interfaces, and it's not Haskell or GHC specific.
However, there's a solution handy, and it's a rather standard one:
Set the SO_REUSEADDR socket option:
In C:
int one = 1;
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
/* error handling */
In Haskell, try
import SocketPrim -- setSocketOption, SocketOption(... ReuseAddr ...)
setSocketOption sd ReuseAddr 1
before the bind operation.
Perhaps in the Socket module, the listenOn function should do that
by default!
Regards, Hannah.