I'm receiving messages over TCP in Erlang using gen_tcp
. The stream is divided into packets by using a 4 byte length header, as specified by the {packet, 4}
option. This is how I call gen_tcp:listen/2
:
gen_tcp:listen(Port, [binary, inet, {active, once}, {packet, 4}]).
As you can see, I use the {active, once}
option so I can fetch my packets from the process mailbox without flooding it. This works fine as long as the length header is correct. If it's not, anything can happen. So I want to deal with the possibility of erroneous packets somehow.
This is a bit tricky as I'm actually dealing with a stream. Ignoring erroneous packets would be okay, but how do I get Erlang to skip these packets and recognize following packets? How is this problem usually dealt with?
Is it better to use a delimiter? I've looked at some other packet
options for gen_tcp
. In particular the following:
asn1 | cdr | sunrm | fcgi | tpkt | line
The only one I really understand the meaning of is line
, but I don't think that would be a good option. I'm expecting to recieve packages sent and constructed in Objective C, which I'm not familiar with, containing many different kinds of data, not only strings.