Strange behavior with Network sockets

Alec Berryman alec at thened.net
Fri Jan 19 20:19:15 EST 2007


mrd on 2007-01-19 18:16:31 -0500:

> In GHC 6.6 (and HEAD), it seems to block unexpectedly, and this
> strange behavior is related to hIsOpen and hIsEOF, somehow.  It acts
> like hIsOpen or hIsEOF is holding up the loop until input arrives, but
> it still blocks at hGetLine.
> 
> The server ends up printing out lines that were typed into the client
> a while ago, as if it were stuck on some kind of backlog but could
> not catch up.

[...]
 
> > import Control.Concurrent
> > import Control.Concurrent.STM
> > import System.IO
> > import Network
> 
> > main = withSocketsDo $ do
> >     sock    <- listenOn $ PortNumber 9000
> >     (h,_,_) <- accept sock

From your description it sounds like you have a buffering problem.  Have
you tried adding 'hSetBuffering h LineBuffering' here?

> >     ch      <- atomically newTChan
> >     -- thread which pumps lines read 
> >     -- from socket into channel
> >     forkIO . sequence_ . repeat $ 
> >                hGetLine h >>= atomically . writeTChan ch
> >     -- main thread just waits for data from channel
> >     -- and prints it to screen
> >     sequence_ . repeat $ 
> >             do isOpen <- hIsOpen h
> >                isEOF  <- hIsEOF h 
> >                putStrLn $ "handle isOpen = " ++ show isOpen 
> >                        ++ "; isEOF = " ++ show isEOF
> >                hFlush stdout
> >                line <- atomically $ readTChan ch
> >                putStrLn line
> >                hFlush stdout
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20070119/9d9dfd3b/attachment.bin


More information about the Glasgow-haskell-users mailing list