[Haskell-beginners] A question about network programming

Ertugrul Soeylemez es at ertes.de
Wed Jun 8 14:56:45 CEST 2011

Raphael Päbst <raphael.paebst at googlemail.com> wrote:

> This is probably a question that shows my incompetence more than
> anything else, but I'm stuck with some TCP programming.
> I have some code that looks like this:
> hdl <- open TCP connection
> message <- hGetContents hdl
> answer <- do stuff with message
> hPut hdl answer
> hClose hdl
> My problem is that the stuff that gets done with message happens, but
> only after I close the connection rom the client's side and then of
> course it is too late to get the answer out of said connection.
> What am I doing wrong?

You are using lazy I/O, which is in general unpredictable.  You have
almost no control over when the handle is closed.  If you want
predictable yet elegant I/O, I recommend an iteratee-based approach.
There is a nice online tutorial in the Yesod book [1].

[1] http://www.yesodweb.com/book/enumerator

BTW, I like your "hab dich lieb" handle. =P


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Beginners mailing list