2

this my code to receive data

-module(t).
-compile(export_all).

start() ->
  {ok, LSock} = gen_tcp:listen(5555, [binary, {packet, 0},
    {active, false}]),
  {ok, Sock} = gen_tcp:accept(LSock),
  {ok, Bin} = do_recv(Sock, []),
  ok = gen_tcp:close(Sock),
  Bin.

do_recv(Sock, Bs) ->

  io:format("(="), io:format(Bs),io:format("=)~n"),

  case gen_tcp:recv(Sock, 0) of
    {ok, B} ->
      do_recv(Sock, [Bs, B]);
    {error, closed} ->
      {ok, list_to_binary(Bs)}
  end.

i send to socket in series - 1, then 2, then 3, then 4, then 5

code is accumulates received data

it's print to screen

(=12345=)

how to modify the code to the code printed

(=1=)
(=2=)
(=3=)
(=4=)
(=5=)

that data is not accumulated

1 Answers1

3

TCP represents data as a stream with no message structure. This has nothing to do with Erlangs implementation of it.

If you need a message structure you have to encode it in band in the data stream.

Erlang helps you with a simple builtin packet structure with 1, 2 or 4 byte length followed by the data. This is what {packet N} does for N equal to 1,2 or 4

But you need to also send data conforming to this structure.

Peer Stritzinger
  • 8,232
  • 2
  • 30
  • 43