[erlang-questions] Erlang get_tcp:recv data length

Mazen Harake <>
Fri Feb 25 16:35:25 CET 2011


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 <> 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


More information about the erlang-questions mailing list