erlang-erl
代码
-module(tut17).
-export([start_ping/1, start_pong/0, ping/2, pong/0]).
ping(0, Pong_Node) ->
{pong, Pong_Node} ! finished,
io:format("ping finished~n", []);
ping(N, Pong_Node) ->
{pong, Pong_Node} ! {ping, self()},
receive
pong ->
io:format("Ping received pong~n", [])
end,
ping(N - 1, Pong_Node).
pong() ->
receive
finished ->
io:format("Pong finished~n", []);
{ping, Ping_PID} ->
io:format("Pong received ping~n", []),
Ping_PID ! pong,
pong()
end.
start_pong() ->
register(pong, self()),
pong().
start_ping([Pong_Node]) ->
register(ping, self()),
ping(3, Pong_Node).
// -sname 运行
erl -noshell -sname pong -s tut17 start_pong -s init stop
erl -noshell -sname ping -s tut17 start_ping pong@coolwork -s init stop
// -name 运行
erl -noshell -name [email protected] -s tut17 start_pong -s init stop
erl -noshell -name [email protected] -s tut17 start_ping [email protected] -s init stop