Fast I/O with sockets

Simon Marlow simonmar at
Tue Sep 16 14:45:47 EDT 2003

> I was wondering how to read and write from a Socket in the most
> performant way. According to the library documentation from GHC,
> 'hGetBuf' and 'hPutBuf' are the way to go. But my problem with these
> functions is that they expect to read exactly the number of bytes I
> gave them!
> The IO computation
>     hGetBuf sock ptr 1024
> wants to read 1024 bytes, no less. This doesn't work for me, because I
> need to drive an interactive line-based protocol session. I need the
> read operation to consume as much data as is available and then to
> return immediately, so that I can process the command right away.
> As an alternative, I looked at 'hGetLine', but this doesn't work for
> me either, at least not very well, because in my protocol, lines are
> terminated by "\r\n" -- not just "\n", like hGetLine expects it.

hGetLine should work fine, indeed that's what I used in the Haskell Web
Server.  Each line will have an extra '\r' at the end, but you just need
to allow for that.

We really should have non-blocking versions of hGetBuf and hPutBuf, and
in fact I have the code sitting in my tree.  I'll take a look and see if
it can be committed.


More information about the Haskell-Cafe mailing list