3

我正在尝试学习 Erlang,但在向另一个节点发送消息时遇到问题。该程序只是一个非常简单的乒乓球,应该像这样工作。我在节点 m1@ASUS-N55SF 上启动一个程序。它创建了一个进程,并成功地在节点 m2 上生成了一个进程。所以我得到了包含 {PidPing, PidPong} 元组的 Pids。PidPing 和 PidPong 是进程 id,如 <0.X.0> 和 <YZ0>。
之后我调用 ps5:start_pinging(Pids) 函数,我得到一个错误:

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}]}]}

因此,初始消息到达 PidPing 和 loop_ping 接收并成功匹配,但我无法将消息发送到另一个节点,即使它们彼此可见。这成功了,因为此初始消息是在同一节点上发送的:

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

据我了解,要向另一个节点发送消息,我应该发送如下消息:

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

但它返回badarg。节点已连接,使用相同的 cookie 启动,运行在同一台机器上,并且可以互相 ping 通并且可见。它们在同一个文件夹中启动,因此它们查看相同的 ps5.erl 文件。我也在不同的文件夹中尝试过,但也不起作用。如果代码在 seme 节点上运行,它可以工作,只需删除另一个节点上的 spawn 并在同一节点上进行进程。

这是代码:

-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.

编译期间有一些警告,但它们与这个问题无关(至少我相信如此)。

4

1 回答 1

8

该构造{Registered_process_name, Erlang_node} ! Some_message用于向您不知道其 PID的远程注册进程发送消息。

在您的情况下,您知道 pids(并且进程未注册),这意味着您不能使用{name, node} ! message而是PID ! message更改:

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

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

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

PidPing ! {ping, {PidPing, PidPong}, Number + 1},
于 2012-12-01T15:44:31.117 回答