Étude 11-3: Independent Server and Client
In the previous études, the client and server have been running in the same shell. In this étude, you will make the server available to clients running in other shells.
To make a node available to other nodes, you need to name the node by using
the -name
option when starting erl
. It looks like this:
michele@localhost $ erl -name serverNode
Erlang R15B02 (erts-5.9.2) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.2 (abort with ^G)
(serverNode@localhost.gateway.2wire.net)1>
This is a long name. You can also set up a node with a short name by using
the -sname
option:
michele@localhost $ erl -sname serverNode
Erlang R15B02 (erts-5.9.2) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.2 (abort with ^G)
(serverNode@localhost)1>
Warning
If you set up a node in this way, any other node can connect
to it and do any shell commands at all. In order to prevent this,
you may use the -setcookie
when starting Cookie
erl
. Then,
only nodes that have the same Cookie (which is an atom) can
connect to your node.
To connect to a node, use the net_adm:ping/1
function, and give it
the name of the server you want to connect to as its argument. If you
connect succesfully, the function will return the atom pong
; otherwise,
it will return pang
.
Here is an example. First, start a shell with a (very bad) secret cookie:
michele@localhost $ erl -sname serverNode -setcookie chocolateChip
Erlang R15B02 (erts-5.9.2) [source] ...
Get Études for Erlang now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.