[erlang-questions] gen_tcp question

Serge Aleynikov <>
Thu Sep 14 14:26:15 CEST 2006

Joe Armstrong (TN/EAB) wrote:
> I have a question about gen_tcp
> A client does this:
> 	{ok, Port} = gen_tcp:connect(Host, Post, [binary,{packet,N}])
> 	get_tcp:send(Port, <<Bin>>)
> The server
> 	calls gen_tcp:listen(Port, [binary,{packet,N}])
> Then goes into a receive loop to receive messages
> Questions:
>      1) is <<Bin>> delivered as a single message to the server program?
>         or is the packet fragmented.

I would say that since the tcp_recv call in inet_drv.c is aware of the 
{packet,N} option set on the socket, gen_tcp:recv(Sock, 0) will (memory 
permitting) deliver a complete packet of N bytes to the caller.  However 
the packet may get fragmented in the transport layer because of the MTU 
size excess, though this should be transparent to the caller.

>      2) Does fragmentation or not depend upon N (the packet size)
>          must N be > 0 for the receiver to be able to reconstruct the
>          fragments (if fragmented?)

I think this depends on which socket option is set.  For {packet,N} 
doing gen_tcp:recv(Sock, M), where M != 0 will return an einval (if 
memory serves) error.  If you are using raw socket option, there is a 
16M limit on the size of M.  You'll get an enomem error in that case.

> The behaviour appears not to be documented
> /Joe



More information about the erlang-questions mailing list