Olly Lawrence
Olly Lawrence

Reputation: 3

Communicating with erlang server via c++ (Sending Binary)

Ive been developing a project using erlang, sfml, and c++ in order to create a networked game. Ive done communications from erlang - erlang with some success however im encountering some issues intergrating c++.

Previously i was able to send data in this format (from the 'client'):

 gen_tcp:send(Socket, term_to_binary({Cmd, Parameters}));
 gen_tcp:send(Socket, term_to_binary({Cmd, {P1, P2}, P3}));
 gen_tcp:send(Socket, term_to_binary({Cmd}));
 gen_tcp:send(Socket, term_to_binary({Cmd, Msg}));

And process the data using a case:

client_handler(Socket) ->
io:format("Waiting for data...~n", []),

case gen_tcp:recv(Socket, 0) of  
{ok, Bin} ->
  Cmd = binary_to_term(Bin),
  io:format("Command '~p' received.~n", [Cmd]),

case Cmd of
    {register, NewAtom} ->          
        %%Do Stuff        
    {update_transform, {X,Y}, Rot} ->
        %%Do Stuff    
    {get_transform} ->
        %%Do Stuff  
    {send_message, Msg} ->
        %%Do Stuff    
    _ ->
       %%Do Stuff
  end,

client_handler(Socket);

{error, _} ->
  io:format("Lost connection to client!~n", [])
end.

Is there any way for me to send data across like this using c++? I realize its just building binary stings but i dont know what format the data needs to look l until now its not something i've had any experience doing with c++

Thanks in advance!!!

Upvotes: 0

Views: 340

Answers (1)

Steve Vinoski
Steve Vinoski

Reputation: 20004

C++/C can use the ei library to encode data into Erlang terms to send an Erlang application over a socket, and receive and decode Erlang terms as well. Writing such a C++/C application is tedious but once you get the basic code in place, it works pretty well.

It's also possible to write a non-Erlang node, called a C node, that communicates with an Erlang node via Distributed Erlang. You can do the same for Java using the JInterface package. To the Erlang node, such a node appears as just another node in its cluster. As with ei applications, writing such nodes can be tedious, but they can be handy when you have non-Erlang code you want to seamlessly and safely integrate into an Erlang cluster.

Upvotes: 2

Related Questions