3

I want to use tcp to catch BERT-encoded messages, and I'm confused a little. binary_to_term(X) decodes only first term, without giving me back the rest of binary, nor saying how much did it use. Encode back with term_to_binary(X) to see it's size is not an option, because BERT-encoding methods differs(for example, it encodes all ints as long ints).

So, how do I decode the messages? The only valid option is passing the binary size too?

Jonas
  • 121,568
  • 97
  • 310
  • 388
desudesudesu
  • 2,185
  • 1
  • 15
  • 20
  • probably some code will reveal an error, if you provide it? But at first glance it looks like you mix erlang serialization with BERT related staff. – danechkin Jan 16 '13 at 18:47
  • @danechkin, thanks for pointing out, I mixed it. But this don't solve the original problem. It looks like this: bert:decode(list_to_binary([bert:encode([1,2]), bert:encode([3,4])])). And just like there, I get binary from TCP, decode first term, don't know how to decode the second. – desudesudesu Jan 16 '13 at 20:55
  • perhaps you need something like: E = term_to_binary([bert:encode([1,2]), bert:encode([3,4])]), %% send it over TCP [E1, E2] = binary_to_term(Data), [bert:decode(E1), bert:decode(E2)] – danechkin Jan 17 '13 at 18:52
  • @danechkin, It's right, with the assumption I do pass [, ], but I don't. I just pass two or more binaries, for example, with 1sec interval. Then I have client {active, false} socket. And I want to retrieve two terms somehow. – desudesudesu Jan 18 '13 at 11:49

1 Answers1

1

Send size of the encoded term before the term itself. You can even use {packet, 2} option to read packets like this.

Dmitry Belyaev
  • 2,573
  • 12
  • 17
  • {packet, 2} would make hard to read the rest of the binary. It's better to match to <> or something like this. But again, question is if this is indeed needed, and the only true way to make this. – desudesudesu Jan 21 '13 at 07:41