Hello!

On Mon, Jan 31, 2000 at 09:11:14PM +0100, Wojciech Moczydlowski, Jr wrote:
> [...]

> main = do
>       socket <- listenOn (PortNumber (mkPortNumber 6665))
>       (handle, hn) <- accept socket
>       putStrLn hn" - it's just supposed to say who called and exit.
> Nevertheless, when I try to run it again, it says:

> "Fail: resource busy
> Action: bindSocket
> Reason: Address already in use".

I believe, bindSocket is called from listenOn:

listenOn :: PortID      -- Port Identifier
         -> IO Socket   -- Connected Socket

listenOn (Service serv) = [...]

listenOn (PortNumber port) = do
    proto <- getProtocolNumber "tcp"
    sock  <- socket AF_INET Stream proto
    bindSocket sock (SockAddrInet port iNADDR_ANY)
    listen sock maxListenQueue
    return sock

[...]

There should be a call to
    SocketPrim.setSocketOption sock SocketPrim.ReuseAddr 1
before bindSocket.

*wink* to the GHC team :-)

There's an analogous idiom in C/C++ which is almost never wrong to
use, and often forgotten/not thought of in the beginning of writing
a network server.

> [...]

Regards, Hannah.

Reply via email to