[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.
http://postmaster.cryp.to/

Check out his block-io library too:
http://cryp.to/blockio

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:
http://cvs.sourceforge.net/viewcvs.py/haskell-libs/libs/hws-wp/

John Goerzen's MissingH library contains a variety of networking code;
the most relevant of this I think would be:
http://quux.org/devel/missingh/html/MissingH.Network.SocketServer.html

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
John's):
http://cvs.sf.net/viewcvs.py/haskell-libs/libs/kakama/src/Network/HTTP/Server.hs?rev=1.4

and an http request handler:
http://cvs.sf.net/viewcvs.py/haskell-libs/libs/kakama/src/Network/HTTP/Server/Request.hs?rev=1.4

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

Alistair.


More information about the Haskell mailing list