[erlang-questions] gen_tcp question

Joe Armstrong (TN/EAB) joe.armstrong@REDACTED
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
the
socket in active mode, since I want to receive both messages from the
socket
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
etc.

The problem occurs in the following bit of code:


      (C is the Pid of a client)

	{ok, L} = gen_tcp:listen(Port,
[{length,4},binary,{active,true}]),
      {ok, S} = gen_tcp:accept(L),
      loop(S, C).

loop(S, C) ->
     receive
         {tcp, S, Bin} ->              %% <----- Is Bin of length 4
here?????????????
              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 ->
	        gen_tcp:close(S)	
	end        
       
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
the
purpose of saying {packet,4} is in the listen call if it is not to be
used for
recombination.

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
all.

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

/Joe 

> -----Original Message-----
> From: Jani Hakala [mailto:jahakala@REDACTED] 
> Sent: den 14 september 2006 15:33
> To: erlang-questions@REDACTED
> Cc: Joe Armstrong (TN/EAB)
> Subject: Re: [erlang-questions] gen_tcp question
> 
> "Joe Armstrong (TN/EAB)" <joe.armstrong@REDACTED> 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