Fast I/O with sockets
simons at cryp.to
Tue Sep 16 14:37:48 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
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.
Finally, I considered 'hGetContents', which has the nice property of
returning a lazy-evaluated list of the data read from the socket. But
unfortunately, I don't see how I could implement something like a
"read timeout" on the socket when using this function.
Any ideas, anybody?
More information about the Haskell-Cafe