diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 20:37:44 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 20:37:44 +0100 |
commit | 34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5 (patch) | |
tree | 695f1b00ef163941fc20d74c8db0352b646650c6 /step-05/README.md | |
parent | aa3c25288e44bcf082680308256f9390cba50bbc (diff) | |
download | erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.gz erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.bz2 erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.tar.xz erlang-workshop-34f56f81e88b12f3aedf26bc4d9d0f55788d5bf5.zip |
wip
Diffstat (limited to 'step-05/README.md')
-rw-r--r-- | step-05/README.md | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/step-05/README.md b/step-05/README.md new file mode 100644 index 0000000..bc5f6ed --- /dev/null +++ b/step-05/README.md @@ -0,0 +1,64 @@ +*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. |