[erlang-questions] Erlang get_tcp:recv data length
Alain O'Dea
alain.odea@REDACTED
Fri Feb 25 18:30:26 CET 2011
Mazen, your {active, once} solution supports any delimiter if used in conjuction with {packet, raw}.
J-Ph, it really doesn't matter how big or fragmented the packets are so long as you accumulate fragments until you get a message. The message boundary could easily be in the middle of a packet, but Mazen's solution makes that easy to handle :)
Here is an example of accumulating binary data:
OldAccBinary = <<"Hello ">>,
Data = <<"World!">>.
BinAcc = <<OldBinAcc/binary, Data/binary>>.
On 2011-02-25, at 12:05, Mazen Harake <mazen.harake@REDACTED> wrote:
> No I think that in that case you have to keep track of it yourself and
> so you accept the data packages and accumulate the data and as the
> same time search for the delimiter. If you find the delimiter you take
> what you have accumulated and send it somewhere and save the rest as
> an accumulator for the next data coming in. This should be fairly
> straight forward. For this you should not set the packet to line but
> rather to binary (or list if you prefer).
>
> My explanation assumes "receive {tcp,Socket,Data} -> ... end", because
> you do inet:setopts(Socket, [{active,once}]) right before like this:
>
> foo(Socket, BinAcc) ->
> inet:setopts(Socket, [{active, once}]),
> receive
> {tcp,Socket,Data} ->
> %% search for delimiter here and add to BinAcc... then recurse.
> {tcp_closed,Socket} ->
> %% something else here...
> end.
>
> The only delimiters you can accept are those that are specified in the
> packet option in setopts, you can find it here:
> http://www.erlang.org/doc/man/inet.html#setopts-2
>
>
> On 25 February 2011 16:21, info <info@REDACTED> wrote:
>> Yes, it's more clear ! Thank you. However two questions:
>> - Is it possible to define another terminator string ? {packet,line} accept
>> only <LF>. If I want to define "<CR><LF>" or "<NULL>" or "#" ?
>> - I suppose that your explanation is also valid for "receive
>> {tcp,Socket,Data} -> ... end" ?
>> Rgds,
>>
>> J-Ph. Constantin
>> ITS3 Genève
>> www.its3.ch
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
More information about the erlang-questions
mailing list