[Haskell-cafe] Working with Network

Niklas Broberg n_broberg at hotmail.com
Sun May 23 19:25:21 EDT 2004



Marco Righele wrote:
>Ideally each session would work more or less as follows:
>
>do    (hdl,hn,pn) <- accept socket
>      text <- hGetContents hdl
>      inputs <- parse text
>      replies <- sequence $ map reply inputs
>      sequence $ map (hPrint hdl) replies
>      ...
>
>Where parse and reply are IO functions that produce and consume some kind 
>of higher level representation of the text that the client sent.
>Of course this won't work because the code will first try to read all the 
>input and then to write back all the replies. I think that if I had two 
>different handles for input and output I wouldn't have this problem, am I 
>right ? Is there a way to handle such situation without having to read the 
>input text in chunks ? This for example would allow me to use a library 
>like Parsec to parse the input (that looks like works on string and not on 
>handles)

It looks like the lazy read problem rears its ugly head again. =)
Try the following:

do  (hdl,hn,pn) <- accept socket
      text <- hGetContents hdl
      if text == text then return () else return ()
      inputs <- parse text
      replies <- sequence $ map reply inputs
      sequence $ map (hPrint hdl) replies
      ...

The conditional will force 'text' to be read in full, which will free the 
handle for writing.

/Niklas

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online 
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963



More information about the Haskell-Cafe mailing list