[erlang-questions] gen_tcp question
Jani Hakala
jahakala@REDACTED
Mon Sep 18 16:17:08 CEST 2006
"Joe Armstrong (TN/EAB)" <joe.armstrong@REDACTED> writes:
> 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 didn't notice that the value {active, true} was being used
implicitly (it being a default value).
> 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}]),
>
I assume length is supposed to be packet
> {ok, S} = gen_tcp:accept(L),
> loop(S, C).
>
> loop(S, C) ->
> receive
> {tcp, S, Bin} -> %% <----- Is Bin of length 4
> here?????????????
>
Length of Bin can be something between 1 byte and several
megabytes. The four extra bytes inserted by using {packet,4} option
are silently removed by underlying inet_drv.
<clip>
> 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.
>
When using {packet,PacketType}, PacketType=1,2 or 4 inet_drv receives
(sends) 1, 2 or 4 byte header before the message you are receiving
(sending). Those bytes correspond to one 8bit, 16bit or 32bit integer
numbers that tell how long the message is going to be. The C-code in
inet_drv then receives (sends) the header and the message in one or
fragments which should happen under the hood.
> 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.
>
>From inet(3erl) manpage
{packet, PacketType} (TCP/IP sockets):
Defines the type of packets to use for a socket. The fol-
lowing values are valid:
raw | 0:
No packaging is done.
1 | 2 | 4:
Packets consist of a header specifying the number of
bytes in the packet, followed by that number of
bytes. The length of header can be one, two, or four
bytes; the order of the bytes is big-endian. Each
send operation will generate the header, and the
header will be stripped off on each receive opera-
tion.
asn1 | cdr | sunrm | fcgi | tpkt | line:
...
Jani Hakala
More information about the erlang-questions
mailing list