节点1:

F:\WorkSpace\Server\src>erl -name [email protected] -setcookie 4213
consulting .erlang in "F:/WorkSpace/Server/src"
Now in:"F:/WorkSpace/Server/src"
Eshell V5.9.3.1 (abort with ^G)
([email protected])1> auth:get_cookie().
'4213'
([email protected])2> hw:start().
true
([email protected])3>

步骤
启动节点: erl -name [email protected] -setcookie 4213

启动进程: hw:start().

注意: 本地机器不同节点可以用都短名 -sname, 跨网络涉及到DNS要用-name

节点2:

F:\WorkSpace\Server\src>erl -name [email protected] -setcookie 4213
consulting .erlang in "F:/WorkSpace/Server/src"
Now in:"F:/WorkSpace/Server/src"
Eshell V5.9.3.1 (abort with ^G)
([email protected])1>
net_adm:ping([email protected]).
* 2: syntax error before: 168.1
([email protected])1> net_adm:ping([email protected]).
* 1: syntax error before: 168.1
([email protected])1> net_adm:ping('[email protected]').
pong
([email protected])2> node().
'[email protected]'
([email protected])3> auth_getcookie().
** exception error: undefined shell command auth_getcookie/0
([email protected])4> auth:get_cookie().
'4213'
([email protected])5> net_adm:ping('[email protected]').
pong
([email protected])6> node().
'[email protected]'
([email protected])7> rpc:call([email protected],hw,store,[weather,cold]).
* 1: syntax error before: 168.1
([email protected])7> rpc:call('[email protected]',hw,store,[weather,cold]).
cold
([email protected])8> rpc:call('[email protected]',hw,lookup,[weather]).
{ok,cold}
([email protected])9>

步骤

启动节点: erl -name [email protected] -setcookie 4213

启动进程: rpc:call('[email protected]',hw,store,[weather,cold]).

rpc:call('[email protected]',hw,lookup,[weather]).

注意: '[email protected]'  不能少了单引号

两个节点用的代码是同样的, 如下:

%% @author Administrator
%% @doc @todo Add description to bibo. -module(bibo). %% ====================================================================
%% API functions
%% ====================================================================
-export([]). %% ====================================================================
%% Internal functions
%% ==================================================================== %% ====================================================================
%% API functions
%% ====================================================================
%%-export([start/2, stop/1]).
-compile(export_all). %% ====================================================================
%% Internal functions
%% ==================================================================== start()->
register(kvs, spawn(fun()->
loop()
end)
). loop()->
receive
{From, {store, Key, Value}}->
put(Key, {ok, Value}),
From ! {kvs, Value},
loop();
{From, {lookup, Key}} ->
From ! {kvs, get(Key)},
loop()
end. store(Key, Value)->
rpc({store, Key, Value}). lookup(Key)->
rpc({lookup, Key}). rpc(Q)->
kvs ! {self(), Q},
receive
{kvs, Reply}->
Reply
end.
05-20 12:16