[Haskell-beginners] Exception: bind: resource busy (Address already in use)

Miro Karpis miroslav.karpis at gmail.com
Tue Sep 17 00:17:57 CEST 2013


Hi, I'm trying to work on following code below (code is a copy from
here<http://cogsandlevers.blogspot.no/2013/05/a-tcp-server-haskell-example.html>).
Problem is that when I close the server with ctrl+c and try to run it again
I get: *** Exception: bind: resource busy (Address already in use).

In documentation of listenOn is written: NOTE: To avoid the "Address
already in use" problems popped up several times on the GHC-Users mailing
list we set the
ReuseAddr<http://hackage.haskell.org/packages/archive/network/2.3.0.14/doc/html/Network-Socket.html#v:ReuseAddr>socket
option on the listening socket. If you don't want this behaviour, please
use the lower level
listen<http://hackage.haskell.org/packages/archive/network/2.3.0.14/doc/html/Network-Socket.html#v:listen>
instead.
Please how can I fix this? (ghci version 7.6.3)

cheers m.


import Network (listenOn, accept, PortID(..), Socket)
import System.IO (hSetBuffering, hGetLine, hPutStrLn, BufferMode(..),
Handle)

import Control.Concurrent (forkIO)

echoImpl :: Handle -> IO ()
echoImpl client = do
  line <- hGetLine client
  hPutStrLn client line
  echoImpl client

clientHandler :: Socket -> IO ()
clientHandler sock = do

  (client, _, _) <- accept sock
  hSetBuffering client NoBuffering
  forkIO $ echoImpl client
  clientHandler sock

felix :: IO ()
felix = do
   sock <- listenOn $ PortNumber 5002
   putStrLn $ "Echo server started .."
   clientHandler sock
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130917/52a49aa3/attachment.htm>


More information about the Beginners mailing list