[Haskell-cafe] Re: `Expect'-like lazy reading/Parsec matching on
TCP sockets
Ian Lynagh
igloo at earth.li
Mon Apr 16 15:04:55 EDT 2007
Hi Scott,
On Mon, Apr 09, 2007 at 10:03:55AM -0600, Scott Bell wrote:
> >Have you got a complete (but preferably small) program showing the
> >problem?
Great example, thanks!
Sorry for the delay in tracking it down.
> main :: IO ()
> main = do (_, h, _, p) <- runInteractiveCommand "telnet nyx.nyx.net"
> t <- hGetContentsTimeout h 15000
> print t >> terminateProcess p
The input handle is being garbage collected and closed, so telnet is
exiting. Try:
main :: IO ()
main = do (hin, h, _, p) <- runInteractiveCommand "telnet nyx.nyx.net"
t <- hGetContentsTimeout h 15000
print t
hClose hin
terminateProcess p
Note that you can't do either the hClose or terminateProcess before you
have forced the whole string (which print does here). You might prefer
to pass hin and p to hGetContentsTimeout, and have it close/terminate
them just before the return "".
> hGetContentsTimeout :: Handle -> Int -> IO String
> hGetContentsTimeout h t = do
> hSetBuffering stdin NoBuffering
> ready <- hWaitForInput h t; eof <- hIsEOF h
You'll also need to remove the hIsEOF call from your code, or having
decided that nothing is ready it will then block, waiting to see if
there is an end of file or not.
Thanks
Ian
More information about the Haskell-Cafe
mailing list