[Haskell-cafe] Simple network client

Jules Bean jules at jellybean.co.uk
Wed Jan 30 07:03:27 EST 2008


Your bug here is hGetContents.

Don't use it.

Lazy IO gremlins bite once again.

Your client is waiting for the server to close the socket before it 
prints anything. But your server is waiting for the client to close the 
socket before *it* prints anything.

Just don't use hGetContents in any serious code, or any program longer 
than 4 lines.

Jules

Timo B. Hübel wrote:
> Hello,
> 
> I am using the very simple interactTCP example from [1] to play around with 
> Haskell network programming but I just can't get a simple client for that 
> example to work (it works like a charm with my telnet client, as described in 
> the article). 
> 
> This is what I am trying to do with the client:
> 
>   main = withSocketsDo $ do
>          hdl <- connectTo "localhost" (PortNumber 1234)
>          hSetBuffering hdl NoBuffering
>          hPutStr hdl "test message"
>          res <- hGetContents hdl
>          putStrLn (show res)
> 
> The server looks like this:
> 
>   interactTCP :: Int -> (String -> IO String) -> IO ()
>   interactTCP port f = withSocketsDo $ do
>           servSock <- listenOn $ PortNumber (fromIntegral port)
>           waitLoop f servSock
> 
>   waitLoop f servSock = do
>           bracket (fmap (\(h,_,_)->h) $ accept servSock)
>                   hClose
>                   (\h -> do
>                           hSetBuffering h NoBuffering
>                           hGetContents h >>= f >>= hPutStr h)
>           waitLoop f servSock
> 
>   main = interactTCP 1234 (return . map toUpper)
> 
> But is seems as some deadlocking occurs. Both programs just hang around doing 
> nothing. By inserting some debug output I was able to make sure that the 
> client successfully connects, but the data interchange just does not start. 
> Because the whole thing works using telnet, I suspect that I am doing 
> something fundamentally wrong in the client ...
> 
> Any hints are greatly appreciated.
> 
> Thanks,
> Timo
> 
> [1] 
> http://stephan.walter.name/blog/computers/programming/haskell/interacttcp.html
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list