[Haskell-cafe] Heterogenous network stream

Antoine Latter aslatter at gmail.com
Sun Oct 17 11:49:18 EDT 2010


Forwarding to list.


---------- Forwarded message ----------
From: Antoine Latter <aslatter at gmail.com>
Date: Sun, Oct 17, 2010 at 10:48 AM
Subject: Re: [Haskell-cafe] Heterogenous network stream
To: Charles-Pierre Astolfi <cpa at crans.org>


On Sun, Oct 17, 2010 at 7:31 AM, Charles-Pierre Astolfi <cpa at crans.org> wrote:
> Hi cafe,
>
> I'd like to implement a toy-protocol and I'm looking for the sanest
> way (for me) to do so.
>
> Here's what it looks like:
> The server receives, in that order:
> - Ascii string (4 bytes)
> - 16 bits int (2 bytes)
> - 8 bits int (1 byte)
> - UTF8 string (128 bytes)
>
> I tried to use hGetBuf, but I don't know if it's the way to go and I
> did not understood what to do with the buf::Ptr a argument. I'm not
> very concerned about efficiency.
>
> I don't know how I should convert my 16 and 8 bits int (read as Chars,
> I guess) to an Int. Is there something for that in the standard
> library?
>

I recommend reading the data in as one bytestring[1] (since you have a
fixed length) and then parsing it using one of the many nice
bytestring parsing libraries:

* binary[2]
* cereal[3]

Others may have other suggestions.

For the UTF8 parsing, I think the text[4] package has support for
decoding UTF8 from a bytestring.

Antoine

[1] http://hackage.haskell.org/packages/archive/bytestring/0.9.1.7/doc/html/Data-ByteString.html#29
[2] http://hackage.haskell.org/package/binary
[3] http://hackage.haskell.org/package/cereal
[4] http://hackage.haskell.org/package/text


More information about the Haskell-Cafe mailing list