in gen_tcp:recv(Socket, 0)you ask the kernel: "Give me all the data that is available right now in the receive buffer." The kernel can also give you less. Even for a fairly quick link, you are likely to run into a slow start of the TCP connection, so you wonβt get much data at first.
The solution is to do your own buffering. You will have to eat data from the main socket until you have enough to create the message. Because of this, it often happens that binary protocols implement their own messaging on top of a stream.
For long-term recording: the general message format is to encode the message as:
decode(Bin) when is_binary(Bin) ->
<<Len:32/integer, R/binary>> = Bin,
<<Payload:Len/binary, Remain/binary>>,
{msg, {Len, Payload}, Remaining}.
, 4 , 32- , , . , , , Erlang C-. , inet/setops/2, {packet, 4}. , {active, once} . , {active, once} . gen_tcp (erl -man gen_tcp ), man- Erlang).
asn.1 http- (!).
, , . Erlang , .