开发者

Sending binaries in Erlang over TCP

Using the code below to send. The gen_tcp:send call returns {error,einval} but I can't figure out why...

-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 = g开发者_运维知识库en_tcp:close(My_Socket)
end.


You're trying to send {message,B_Message}, which is a tuple, but gen_tcp:send only accepts a list of characters or a binary as its second argument. You probably wanted:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜