[erlang-questions] Writing platform independent Socket based binary parsing code

Arun Muralidharan arun11299@REDACTED
Sat May 12 11:13:21 CEST 2012


Thanks Dmitry for the quick response.
I am aware of the fact that from network we get data in our application in
Big Endian format and in c/c++ I would use ntohs/ntohl functions for
converting it into a format that the OS supports.
But, the same in Erlang is confusing me (no idea why !!).
As per your last statement *"So, the parsing of protocol primitive shall
not be dependent on CPU architecture. E.g. If you protocol say that version
is serialized into big-endian then your parsing code shall be same and it
is platform independent. "     *do you want to say that, as I am parsing
data from network socket I should use  "<<Version :
4/big-unsigned-integer:8,_Rest/binary>> = Sock_Data." as the data in
network is serialized in big endian format ?

Really sorry if I am sounding naive..but somehow i am having trouble
understanding this in Erlang.

Thanks,
Arun


On Sat, May 12, 2012 at 1:52 PM, Dmitry Kolesnikov
<dmkolesnikov@REDACTED>wrote:

> Hello,
>
> I'd like to quote one statement here: "Network stacks and communication
> protocols must also define their endianness. Otherwise, two nodes of
> different endianness would be unable to communicate."
> As an example, TCP/IP protocol suite is defined to be a big-endian.
>
> So, the parsing of protocol primitive shall not be dependent on CPU
> architecture. E.g. If you protocol say that version is serialized into
> big-endian then your parsing code shall be same and it is platform
> independent.
>
> - Dmitry
>
> P.S: ;-)
> http://geekandpoke.typepad.com/geekandpoke/2011/09/simply-explained-1.html
>
>
> On May 12, 2012, at 10:34 AM, Arun Muralidharan wrote:
>
> Hi Folks,
>
> I have an erlang application which includes parsing of binary data from
> socket (both TCP and UDP). The binary data is as per some specific
> protocol, so my options for TCP and UDP sock is as per below:
>
> TCP sock :
> Opts = [binary, {packet, 0}, {reuseaddr, true},
>             {keepalive, true}, {backlog, 30}, {active, false}],
>
> UDP sock :
> [binary,{active,once},{recbuf,2097152}]
>
>
> Now, when i parse the data I get from socket, I do like this (On UNIX):
>
>     << Version:4/big-unsigned-integer-unit:8,
>        Length:2/big-unsigned-integer:8,
>        _Rest/binary >> = Bin_event_From_Socket.
>
>
> Now, this would give me a problem when run on LINUX as LINUX is little
> endian based. So, the option here for me is to convert 'big' to 'little' in
> the above code.
>
> But, Erlang being a VM based language, I must be having some facility to
> write platform independent code. I searched the NEt but couldnt find much
> on it.
> So, the question here is how can i write a platform independent sockaet
> data parsing code ?
>
> Thanks.
> -Arun
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120512/525f38e6/attachment.htm>


More information about the erlang-questions mailing list