[erlang-questions] Re: about a tcp question in erlang

Joe Armstrong <>
Thu Mar 31 09:04:29 CEST 2011


On Tue, Mar 29, 2011 at 5:45 AM, zhangbo <> wrote:

> Hi,
>        Code:
>        {ok, Listen} = gen_tcp:listen(80, [{packet,0},
>                                        binary,
>                                        {active, true}])
>
>        Loop:
>                receive
>                        {tcp, S, Bin} ->
>                                ...
>
> I want to know, when a client send a message("helloworld") to server, then
> send another message("helloworld2"). How many messages will the server
> receive? (I guess 2.)  If the client send very fast, or the message is big
> enough, how the situation again?
>
> When receive {tcp, S, Bin}, Bin stands for a message or maybe just a
> fragment of message? If the client send "hello, server", Bin maybe "hello,
> server", but If the message is very big, what will Bin be? and how many
> times will the loop receive?
>

because you have said {packet,0} then data might become fragmented. The
options {packet,2}, (or {packet,4} mean "send a 2(4) byte length header at
the start of the data". If the client and server
both open a socket with {packet,4} then any fragmentation will be taken care
of by the tcp driver. In this
case any packet fragments will be recombined by the driver and only
completed messages delivered to
the application.

/Joe




>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110331/a2b5e527/attachment.html>


More information about the erlang-questions mailing list