[erlang-questions] gen_tcp question

Joe Armstrong (TN/EAB) <>
Mon Sep 18 11:19:27 CEST 2006

Hi Jani,

My problem is that I am NOT using gen_tcp:recv at all - I need to open
socket in active mode, since I want to receive both messages from the
and send messages to the socket at the same time.

I want to write a middle-man that abstracts out the TCP transport layer
so I write a process that understands TCP and converts terms to packets

The problem occurs in the following bit of code:

      (C is the Pid of a client)

	{ok, L} = gen_tcp:listen(Port,
      {ok, S} = gen_tcp:accept(L),
      loop(S, C).

loop(S, C) ->
         {tcp, S, Bin} ->              %% <----- Is Bin of length 4
              C ! binary_to_term(Bin),   
              loop(S, C);
         {tcp_closed, S} ->
	        C ! closed;
	   {msg, Term} ->
              gen_tcp:send(S, term_to_binary(Term)),
	        loop(S, C);
         close ->
I need the active=true mode for this since loop has to handle messages
from both C and S - so I don't want to block in gen_tcp:recv

So my question is "is Bin" fragmented? - I can easily add an extra layer
for packet recombination inside loop, it is really unclear to me what
purpose of saying {packet,4} is in the listen call if it is not to be
used for

Now if the answer to this question is in the documentation I can't seem
to find it
the setopts documentation says that a 4 byte header is appended to the
TCP data - that's

The behaviour together with recv *is* documented (as you say) but not
when you
don't call recv and process data in active mode


> -----Original Message-----
> From: Jani Hakala [mailto:] 
> Sent: den 14 september 2006 15:33
> To: 
> Cc: Joe Armstrong (TN/EAB)
> Subject: Re: [erlang-questions] gen_tcp question
> "Joe Armstrong (TN/EAB)" <> writes:
> > The behaviour appears not to be documented
> >
> Meaning of {packet,N} is explained in man inet, inet:setopts 
> The behaviour of gen_tcp:recv is explained in man gen_tcp
> Jani Hakala

More information about the erlang-questions mailing list