简体   繁体   中英

Sending Erlang message from one node to another node returns badarg

I am trying to learn Erlang but I have a problem when sending a message to another node. The program is just a very simple ping pong that should work like this. I start a program on node m1@ASUS-N55SF. It creates a process and it successfuly spawns a process on node m2. So I get Pids that contains {PidPing, PidPong} tuple. PidPing and PidPong are process id's like <0.X.0> and < YZ0>.
After that I call ps5:start_pinging(Pids) function and I get an error:

Pinging started
Ping received 0 on <0.53.0>
{ping,{<0.53.0>,<6807.43.0>},0}
Nodes: 'm1@ASUS-N55SF' 'm2@ASUS-N55SF'
(m1@ASUS-N55SF)8> 
=ERROR REPORT==== 1-Dec-2012::15:27:18 ===
Error in process <0.53.0> on node 'm1@ASUS-N55SF' with exit
value: {badarg,[{ps5,loop_ping,0,[{file,"ps5.erl"},{line,33}]}]}

So the initial message gets to PidPing and to loop_ping receive and is successfuly matched but I can't send a message to another node even though they are visible to one another. This suceeds because this initial message is sent on the same node:

PidPing ! {pong, {PidPing, PidPong}, 0}.

As I understood to send a message to another node I should send a message like:

{PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},

but it returns badarg. The nodes are connected, started with the same cookie, running on same machine and can ping eachother and visible. They are started in the same folder so they look on same ps5.erl file. I tried in different folders also but doesn't work either. It works if the code is run on the seme node, by just removing spawn on another node and making processes on the same node.

Here is the code:

-module(ps5).
-define(NODE_AS, 'm1@ASUS-N55SF').
-define(NODE_TP, 'm2@ASUS-N55SF').
%-define(NODE_TP, 'm2@ThinkPad-Z60m').

-compile(export_all).
start() ->
  PidPing = ps5:start_ping(),
  PidPong = ps5:start_pong(),
  Pids = {PidPing, PidPong}.

start_ping() ->
  Pid = spawn(?NODE_AS, ps5, loop_ping, []).

start_pong() ->
  Pid = spawn(?NODE_TP, ps5, loop_pong, []).

start_pinging({PidPing, PidPong}) ->
  io:format("Pinging started ~n", []),
   PidPing ! {ping, {PidPing, PidPong}, 0}.

stop({PidPing, PidPong}) ->
  exit(PidPing, normal),
  exit(PidPong, normal).

loop_ping() ->
  receive
    {ping, {PidPing, PidPong}, Number} when Number < 10  ->
      io:format("Ping received ~p on ~p~n", [Number, PidPing]),
      io:format("Nodes: ~p ~p ~n", [?NODE_AS, ?NODE_TP]),
      {PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},
      io:format("Ping sent ping ~p to ~p~n", [Number, PidPong]),
      ps5:loop_ping();
    {ping, {PidPing, PidPong}, Number} when Number >= 10->
      io:format("Ping received ~p on ~p~n and closing.~n", [Number, PidPing]);
    Oops ->
      io:format("Oops received: ~p~n", [Oops]),
      ps5:loop_ping()
  end.

loop_pong() ->
  receive
   {pong, {PidPing, PidPong}, Number} when Number < 10  ->
     io:format("Pong received ~p on ~p~n", [Number, PidPong]),
     io:format("Nodes: ~p ~p ~n", [?NODE_AS, ?NODE_TP]),
     {PidPing, ?NODE_AS} ! {ping, {PidPing, PidPong}, Number + 1},
     io:format("Pong sent ping ~p to ~p~n", [Number, PidPing]),
     ps5:loop_pong();
   {pong, {PidPing, PidPong}, Number} when Number >= 10->
     io:format("Pong received ~p on ~p~n and closing.~n", [Number, PidPing]);
   Oops ->
     io:format("Oops received: ~p~n", [Oops]),
     ps5:loop_pong()
  end.

There are some warnings during compile but they are irrelevant for this problem (at least I believe so).

The construction {Registered_process_name, Erlang_node} ! Some_message {Registered_process_name, Erlang_node} ! Some_message is used for sending messages to remote registered processes whose PID you don't know.

In your case you know the pids (and the processes are not registered) which means that you mustn't use {name, node} ! message {name, node} ! message but rather PID ! message PID ! message ie change:

{PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},

to

PidPong ! {pong, {PidPing, PidPong}, Number + 1},

and

{PidPing, ?NODE_AS} ! {ping, {PidPing, PidPong}, Number + 1}, 

to

PidPing ! {ping, {PidPing, PidPong}, Number + 1},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM