[erlang-questions] Two confusions??

Valentin Micic v@REDACTED
Wed Feb 10 19:05:40 CET 2010


1) I don't know how to say this, so, I am just going to say it: "It is
somewhere in documentation for sure" is not really an answer, I don't think
it is productive to make comments like this, even though the answer may just
as well be "somewhere in documentation". If you don't know the answer, don't
answer it -- there is no pressure whatsoever.

2) What is certainly worse than giving the answer above -- is giving a wrong
answer:

>>
>> 1. In socket based erlang code I have seen
>> tcp-options of the form:
>>   [binary, {packet,0},..]
>>   [binary, {packet,4},..]
>>
>> what is the difference between the two?
>>

> First pattern matches if second element of tuple is 0,
> second - if value is 4

This is really *wrong* as it does not answers the question.
 
{packet, 0} indicates that there is no header indicating the length of the
packet.

{packet, 4} means that the first 4 octets of the header should be used to
calculate the length of the packet. 

In other words, if the receiving socket Rx is configured with {packet, 4}
and the sender Tx sends the following packet:

<<0,0,0,2,13,14,0,0,0,5,14,15,16,17, 18, 0,0,0,3, 12>>

Receiver Rx shall receive two packets:

{tcp,#Port<0.101>, <<13,14>>}
{tcp,#Port<0.101>, <<5,16,17,18,19>>}

Thus leaving <<0,0,0,3, 12>> in a driver's buffer as the packet is not
considered complete.

Conversely, if the Rx was configured with {packet, 0}, it would have
received:

{tcp,#Port<0.101>, <<0,0,0,2,13,14,0,0,0,5,14,15,16,17, 18, 0,0,0,3, 12>>}


Use {packet, 4} (or 1,2) if you want driver to accumulate the whole packet
for you -- bear in mind, however, that packet header is not going to be
presented to you as a part of the received data.

Hope this clarifies things.

V/

-----Original Message-----
From: erlang-questions@REDACTED [mailto:erlang-questions@REDACTED] On
Behalf Of Vasilij Savin
Sent: 10 February 2010 07:27 PM
To: Ish Rattan
Cc: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Two confusions??

Hello,

Comments inline.

On Wed, Feb 10, 2010 at 6:08 PM, Ish Rattan
<ishwar@REDACTED>wrote:

>
> 1. In socket based erlang code I have seen
> tcp-options of the form:
>   [binary, {packet,0},..]
>   [binary, {packet,4},..]
>
> what is the difference between the two?
>

First pattern matches if second element of tuple is 0,  second - if value is
4.



> 2. A C-client sends 4-byte int request (with numeric
>   value N encoded in the 4-bytes), erlang server
>   receives the request (in binary) and prints as:
>   <<0, 0, 0, N>>
>
> what is the interpretation here?
>

It is somewhere in documentation for sure.

Regards,
Vasilij Savin



More information about the erlang-questions mailing list