[erlang-questions] Get a large string by socket

Joe Armstrong <>
Thu Apr 23 10:24:21 CEST 2009


If both the client and server are in erlang then it's easy

open the socket at both ends with a {packet,4} option. (read the
gen_tcp manual page)

Then when you do gen_server:send(Socket, Data)
the data will be preceded by a 4 byte length header.

The receiving end will perform any necessary recombination of the
data (if fragmented) and deliver the data as a single message.

If the server is not in erlang, then you will have to read the length
header and perform recombination yourself.

If the data is really large you might have to rethink this.

For small strings this is fine -  What is small depends on your hardware.
Typically transferring up to a few tens of MBytes should not be a problem,
just send and receive them in a single call. If you're talking GB then
you should
probably rethink this - for large strings *don't* use strings use binaries!

Cheers

/Joe


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