[erlang-questions] gen_tcp custom framing

Peter Lund <>
Tue Oct 9 10:20:58 CEST 2007

Joern skrev:
>> In R9, I have also forced to match each packet more than one time since
>> an initial length value read out first, cannot be used later in the same
>> binary match statement, but I am guessing that this problem now is history
>> when using R11.
> Hmm - I'm not sure I follow you on this one. Can you elaborate?

I think I was overly hopeful on what R11 can do (working daily with R9C).

1> f(), <<Z,D:Z/binary,R/binary>> = <<2,3,4,5,6,7,8,9,0 >>, {Z,D,R}.

2> f(), <<Z,D:Z+1/binary,R/binary>> = <<2,3,4,5,6,7,8,9,0 >>, {Z,D,R}.
** 1: syntax error before: '+' **

I thought/hoped that 2 above would work, but when testing it, it did not.

The size field read into Z from the first octet could be used as the 
size of next match in the binary. (1)

But the size could NOT be an expression, in this case involving Z (2).

This forces you to do sequencial binary matching:
 - read size Z,
 - assign Z2 = expression(Z)
 - read rest of the binary using Z2


More information about the erlang-questions mailing list