[erlang-questions] Get a large string by socket

Pablo Polvorin <>
Thu Apr 23 01:43:48 CEST 2009


Hi,
for your description it is difficult to see if you are having problems
with erlang, or with the TCP semantics in general.

Supposing that your problems are with erlang, the question is,
 basically, how large is too large?.

if you know in advance how long each string is going to be,
then use  gen_tcp in passive mode and gen_tcp:recv specifying the
desired length might be an option for you.

If each of the those "large" strings are preceded by its size,
then check if one of the packet types in inet fit your needs. If not,
implement the packet buffering by yourself.

If your "string" use some other way for framing, you have to detect it
yourself.

If your protocol doesn't implement any form of packet framing, and
still you want
to read an "entire" string at a time,  you are hitting against TCP, not erlang.

cheers,
pablo.

2009/4/22 Jose Enrique Benitez Jimenez <>:
> Hello friends,
>
> This code receive a string by socket ( {tcp, Socket_, Bin} ), but the string
> is too large so it just get a piece of it, but I need get it all, Do any of
> you have an idea for do that in a simple way?
>
>
>
> net_loop(Socket, Function, State, From)->
>
>                 receive
>
>                                {connect, Host, Port}->
>
>                                                {ok, Socket_} =
> gen_tcp:connect(Host, Port, [list, {packet, 0}]),
>
>                                                net_loop(Socket_, Function,
> connected, From);
>
>                                {send, Data, From_}->
>
>                                                gen_tcp:send(Socket, Data),
>
>                                                net_loop(Socket, Function,
> State, From_);
>
>                                {tcp, Socket_, Bin} ->
>
>                                                From ! {respuesta,Bin},
>
>                                                net_loop(Socket_, Function,
> State, From);
>
>                                {tcp_closed, Socket_}->
>
>
> gen_tcp:close(Socket_)
>
>                 end.
>
>
>
> % Thanks
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


More information about the erlang-questions mailing list