[erlang-questions] Erlang get_tcp:recv data length

Mazen Harake <>
Fri Feb 25 09:16:40 CET 2011


When you create your socket use {active, false} and {packet, line} as
options. This will first of all stop all the receiving messages until
you are ready to accept them and second it will "split" all incoming
data based on lines (I.e. on \r\n or \n etc.).

When you are ready to receive data you do inet:setopts(Socket,
[{active, once}]) which will send you 1 message with the first line of
the message.
If the socket has closed you will get a {tcp_closed, Socket} message.
If the line is too long it will be truncated to max of the packet
buffer (and the ending chars will be absent from the message)

In your case it seems that the lines are too long. If they are, then
the alternative is to recv the data and continously scan what you got
until you reach the delimiter, when you do you keep your own buffer of
received data so that the "extra" data you get after the delimiter
becomes the start of the next. Also as Max pointed out, recv() blocks
so consider using a timeout and recursion to give time inbetween for
updates etc.

Hope this is more clear.

/M



On 24 February 2011 14:25, info <> wrote:
> Be explicit ! How to detect the last message of a packet ?
>
> J-Ph. Constantin
> ITS3 Genève
> www.its3.ch


More information about the erlang-questions mailing list