Sending binary files to Erlang over TCP

Using the code below to send. Gen_tcp: send call returns {error, einval}, but I can’t understand 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 = gen_tcp:close(My_Socket)
end.
+3
source share
1 answer

You are trying to send {message, B_Message}, which is a tuple, but gen_tcp: send only accepts a character list or binary as the second argument. You probably wanted to:

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

Source: https://habr.com/ru/post/1723824/


All Articles