*This is an alternative to `tut17`.* This example is an *echo server* to which you can send a `ping` message, and you will get a `pong` message in return. In this example session we start two Erlang nodes (doesn't have to be on the same machine), one named `es` for "echo server" and another one named `node2`. Start and compile/load the code on both nodes: ``` $ erl -sname es (es@biwia)1> c(echo_server). ``` ``` $ erl -sname node2 (node2@biwia)1> c(echo_server). ``` Start the server: ``` (es@biwia)2> echo_server:start(). Starting server, pid=<0.95.0> true (es@biwia)3> ``` The server uses `spawn/3` to run in the background. With the PID that `spawn` returns, it registers an `echo` name that the client will use. We can look at this process with `regs()`: ``` (es@biwia)3> regs(). ** Registered procs on node es@biwia ** Name Pid Initial Call Reds Msgs application_controlle <0.44.0> erlang:apply/2 784 0 auth <0.60.0> auth:init/1 784 0 code_server <0.50.0> erlang:apply/2 129042 0 echo <0.95.0> echo_server:loop/1 5 0 ... ``` On the client side we can now send a ping message with `echo_client:send_ping(es@biwia)`. The `es@biwia` atom names the *node* on the specific *host*. A host can run many nodes so the node name needs to be included. ``` (node2@biwia)2> echo_client:send_ping(es@biwia). Sending echo to es@biwia, self=<0.88.0> got pong from <14715.95.0> ok ``` On the server you will notice this output: ``` (es@biwia)4> <0.95.0>: Got ping req from: <14771.88.0> (es@biwia)4> ``` Both the server and client uses `self/0` to find their own PID and you see that the server has the PID `0.95.0` and the client has `0.85.0`. When the server receives the message the client pid is slightly different: `14771.88.0`, and the same for the client's reply from the server. After the server receives 10 messages, it will terminate itself: ``` <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> <0.95.0>: Got ping req from: <14771.88.0> ping finished (es@biwia)4> ``` If the client sends another message, it will time out: ``` (node2@biwia)14> echo_client:send_ping(es@biwia). Sending echo to es@biwia, self=<0.88.0> timeout ok ```