Previous articleThe article says that the atoms associated with the registration process have a global scope. The global here refers to the current Erlang virtual machine. In the distributed form, it is the current distributed node. Therefore, the atoms associated with the registration process in one node cannot be used directly in another node, but must be used with the target node.
{RegName, Node} ! {messages}.
example
Start a Server node first
erl -sname server
Then operate in Erlang Shell
A brief introduction to some common functions
% View the current node
node().
% => 'server@Gentoo-PC'
% View all connected nodes
nodes().
% => [] �
% Check whether the current node is alive
is_alive().
% => true
Then go to the main topic
% Start the last program in the previous article
test:start().
% Waiting for new message.
% => true
% The current node can use the testp atom
testp ! message.
% New message: message
% Waiting for new message.
% => message
Then start another Client node
erl -sname client
In the new Erlang Shell
nodes().
% => [] �
% The current node cannot use this atom directly
testp ! {}.
% ** exception error: bad argument
% in operator !/2
% called as testp ! {}
% Need to be used with the target node
{testp, 'server@Gentoo-PC'} ! {}.
% => {}
At this time, the server node will receive the message and print it out
% New message: {}
% Waiting for new message.
After the first connection between nodes, the two nodes will remain connected
In Client node
nodes().
% => ['server@Gentoo-PC']
In Server node
nodes().
% => ['client@Gentoo-PC']
Ending
Of course, this is just a method. Since the call function is defined in the module, you can use remote calls to call the test:call method on the Server node.
Functions can be called remotely using the call/4 method in the rpc module
%Execute apply(Module, Function, Args) on Node node
% Returns Result when the call is successful, {badrpc, Reason} when the call fails
-spec rpc:call(Node, Module, Function, Args} -> Result | {badrpc, Reason}
In Client node
rpc:call('server@Gentoo-PC', test, call, ['message from other node']).