通过TCP在Erlang中发送二进制文件

通过TCP在Erlang中发送二进制文件,erlang,tcp,Erlang,Tcp,使用下面的代码发送。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])

使用下面的代码发送。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.

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

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