【问题标题】:Sending binaries in Erlang over TCP通过 TCP 在 Erlang 中发送二进制文件
【发布时间】:2010-12-20 02:17:16
【问题描述】:

使用下面的代码发送。 gen_tcp:send 调用返回 {error,einval} 但我不知道为什么...

-define(TCP_OPTIONS,[binary, {active, false}, {reuseaddr, true}]).

client() ->
  case gen_tcp:connect(to_Server(), 8080, ?TCP_OPTIONS) of
    {error, Reason} ->
      io:format("~p~n",[Reason]);
    {ok,My_Socket} ->
      Message = {"stuff", hello, "data"},
      B_Message = term_to_binary(Message),
      OK = gen_tcp:send(My_Socket, {message,B_Message}),  % error here
      OK = gen_tcp:close(My_Socket)
end.

【问题讨论】:

    标签: erlang tcp


    【解决方案1】:

    您正在尝试发送 {message,B_Message},它是一个元组,但 gen_tcp:send 只接受字符列表或二进制作为其第二个参数。你可能想要:

    % ...
    OK = gen_tcp:send(My_Socket, B_Message),
    % ...
    

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多