<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>There are no needs for ntohs functions in erlang. Binary matching does the job and you should keep in-mind that by default it assumes "network byte order" ~ big-endian</div><div>So, like you've wrote you parse data through binary match</div><div><<Version:32, _Rest/binary>> = Data  "<span class="Apple-style-span" style="font-family: 'courier new', monospace; ">big-unsigned" </span>can be skipped (it is assumed  by default)</div><div><br></div><div>- Dmitry</div><div><br><div><div>On May 12, 2012, at 12:13 PM, Arun Muralidharan wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Thanks Dmitry for the quick response.<div>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.</div>
<div>But, the same in Erlang is confusing me (no idea why !!).</div><div>As per your last statement <i style="font-size:x-small">"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. "     </i>do you want to say that, as I am parsing data from network socket I should use <font face="'courier new', monospace"> "<<Version : 4/big-unsigned-integer:8,_Rest/binary>> = Sock_Data." </font><font face="arial, helvetica, sans-serif">as the data in network is serialized in big endian format ?</font></div>
<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Really sorry if I am sounding naive..but somehow i am having trouble understanding this in Erlang. </font></div>
<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Thanks,</font></div><div>Arun<br>
<br><br><div class="gmail_quote">On Sat, May 12, 2012 at 1:52 PM, Dmitry Kolesnikov <span dir="ltr"><<a href="mailto:dmkolesnikov@gmail.com" target="_blank">dmkolesnikov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">Hello,<div><br></div><div>I'd like to quote one statement here: "<span style="font-family:arial,sans-serif;font-size:12px">Network stacks and communication protocols must also define their endianness. Otherwise, two nodes of different endianness would be unable to communicate.</span>"</div>
<div>As an example, TCP/IP protocol suite is defined to be a big-endian. </div><div><br></div><div>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. </div>
<div><br></div><div>- Dmitry</div><div><br></div><div>P.S: ;-)  <a href="http://geekandpoke.typepad.com/geekandpoke/2011/09/simply-explained-1.html" target="_blank">http://geekandpoke.typepad.com/geekandpoke/2011/09/simply-explained-1.html</a></div>
<div><br></div><div><br><div><div><div class="h5"><div>On May 12, 2012, at 10:34 AM, Arun Muralidharan wrote:</div><br></div></div><blockquote type="cite"><div><div class="h5"><p style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

Hi Folks,</p><p style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

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:</p><div style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

<br></div><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;font-size:14px;border-collapse:collapse;line-height:18px"><pre style="margin-top:0px;margin-right:0px;margin-bottom:10px;margin-left:0px;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;width:auto;max-height:600px;background-repeat:initial initial">TCP sock :
Opts = [binary, {packet, 0}, {reuseaddr, true},
            {keepalive, true}, {backlog, 30}, {active, false}],
</pre></span><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;font-size:14px;border-collapse:collapse;line-height:18px"><pre style="margin-top:0px;margin-right:0px;margin-bottom:10px;margin-left:0px;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;width:auto;max-height:600px;background-repeat:initial initial">UDP sock :
[binary,{active,once},{recbuf,2097152}]
</pre></span><div style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

<br></div><p style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

Now, when i parse the data I get from socket, I do like this (On UNIX):</p><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;font-size:14px;border-collapse:collapse;line-height:18px"><pre style="margin-top:0px;margin-right:0px;margin-bottom:10px;margin-left:0px;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;width:auto;max-height:600px;background-repeat:initial initial">    << Version:4/big-unsigned-integer-unit:8,
       Length:2/big-unsigned-integer:8,
       _Rest/binary >> = Bin_event_From_Socket.

</pre></span><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;font-size:14px;border-collapse:collapse;line-height:18px">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.</span><div style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

<br></div><p style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

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.</p><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;font-size:14px;border-collapse:collapse;line-height:18px">So, the question here is how can i write a platform independent sockaet data parsing code ?</span><div style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

<br></div><p style="margin-top:0px;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;font-size:14px;vertical-align:baseline;background-image:initial;background-color:transparent;clear:both;word-wrap:break-word;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;border-collapse:collapse;line-height:18px">

Thanks.</p>-Arun<br></div></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div></div></blockquote></div><br></div>
</blockquote></div><br></div></body></html>