[Haskell-cafe] Wondering about networking programming in Haskell.
johan.tibell at gmail.com
Thu Jan 20 10:34:39 CET 2011
On Thu, Jan 20, 2011 at 4:19 AM, Magicloud Magiclouds
<magicloud.magiclouds at gmail.com> wrote:
> Originally, my idea for networking programming is like: read some
> bytes, judge if this is enough for a packet (defined in certain
> protocol), if not, read more until the packet is complete, then parse
> it into a message.
I'd say this is the easiest way to get started with network
programming in Haskell as well. For many applications there's a better
alternative, based on a concept called iteratees. It takes a bit
longer to grasp but deals with a number of problems you often have to
deal with in network programming (e.g. chunk boundaries). Check out
the enumerator package:
> Now we have lazy in Haskell. Can I do it as:
> buffer <- hGetContents handle
> (message1, leftData1) <- parse1 buffer
You can. There are a few pitfalls however:
* If you close the handler before consuming 'buffer' all the data in
the Handle will not have been read into 'buffer'.
* You must make sure you consume all of 'buffer' to make sure that
the Handle gets closed.
* You must under no circumstances read only parts of 'buffer' and
hold on to the result for an extended point of time as this will cause
the underlying OS resources to be held up longer than desired (i.e. a
More information about the Haskell-Cafe