【问题标题】:Erlang gen_tcp is accumulates received dataErlang gen_tcp 是累积接收到的数据
【发布时间】:2014-06-26 05:05:31
【问题描述】:

这是我接收数据的代码

-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.

我串行发送到套接字 - 1,然后 2,然后 3,然后 4,然后 5

代码是累积接收到的数据

打印到屏幕

(=12345=)

如何将代码修改为打印出来的代码

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

数据没有累积

【问题讨论】:

    标签: erlang gen-tcp


    【解决方案1】:

    TCP 将数据表示为没有消息结构的流。这与 Erlangs 的实现无关。

    如果您需要消息结构,则必须在数据流中对它进行带状编码。

    Erlang 帮助您使用简单的内置数据包结构,其长度为 1、2 或 4 字节长度,后跟数据。这就是{packet N} 在 N 等于 1,2 或 4 时所做的事情

    但是你还需要发送符合这个结构的数据。

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 2011-08-12
      • 2014-12-21
      • 2018-05-02
      • 2012-08-21
      • 2018-06-01
      • 2012-04-02
      • 2012-09-21
      • 1970-01-01
      相关资源
      最近更新 更多