[Haskell] Parsing binary data

Alistair Bayley abayley at gmail.com
Sat Aug 27 15:46:44 EDT 2005

> I'm trying to parse chunks of binary data that arrive through a
> socket. There's a defined format of these chunks where the first 4
> bytes are an id, then could come a Pascal (size first) string, then
> some more data.
> I'm trying to figure out how to read and write Haskell data in this
> format and using a socket.
> Would someone have examples?

If you're not doing anything too fancy, you might get away with using
the Network module (for listening on ports) and System.IO (e.g.
hGetBuf and hPutBuf).

Peter Simons wrote an MTA, which might contain useful examples.

Check out his block-io library too:

There's also Simon Marlow's HWS (http server), which is a bit dated
(w.r.t. the current libraries), but still shows useful examples:

John Goerzen's MissingH library contains a variety of networking code;
the most relevant of this I think would be:

I also had a go at writing an http server (just to see how easy it
was). There's a multi-threaded generic socket-server (very similar to

and an http request handler:

(disclaimer: my own code likely contains a number of school-boy errors
w.r.t networking practices.Networking's not my strong point.)


More information about the Haskell mailing list