haskell network io hgetline

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-05-23T19:15:23Z Indexed on 2010/05/23 19:20 UTC
Read the original article Hit count: 574

Filed under:

I want to read all the data on a handle, and then block waiting for more data. listen1 stops when there is a '\n' character in the stream. listen2 works and could be made completely general by imitating the code for hGetNonBlocking. What is the best way to do this?

import qualified Data.ByteString as B

loop = sequence_ . repeat

listen1 :: Handle -> TChan B.ByteString -> IO()
listen1 sock chan = do
  loop ( do
         s <- B.hGetLine sock
         atomically (writeTChan chan s)
       )

listen2 :: Handle -> TChan B.ByteString -> IO()
listen2 sock chan = do
  loop ( do
         s <- B.hGet sock 1
         s1 <- B.hGetNonBlocking sock 65000
         atomically (writeTChan chan (B.append s s1))
       )

© Stack Overflow or respective owner

Related posts about haskell