user1002288
user1002288

Reputation: 5040

Two process communicate in two computers by Erlang

This is a 2-process communication program in Erlang.

I need to run each process in a different computer.

I need to run erl -name ping

in computer2, whose host name is linux-2.mydomain

And , I need to run

 erl -name pong

in computer1, whose host name is linux-1.mydomain

Then,

On computer 1, I run:

  tut17:start_pong().

On computer 2, I run:

 tut17:start_ping(pong@linux-1)

But, I got error:

it seems that Erlang thought that the " - " is minus operator in "linux-1".

So, how to make Erlang understand my command ?

Any help is really appreciated.

The code is below:

 -module(tut17).

 -export([start_ping/1, start_pong/0,  ping/2, pong/0]).

 ping(0, Pong_Node) ->
     {pong, Pong_Node} ! finished,
     io:format("ping finished~n", []);

 ping(N, Pong_Node) ->
     {pong, Pong_Node} ! {ping, self()},
    receive
        pong ->
             io:format("Ping received pong~n", [])
    end,
 ping(N - 1, Pong_Node).

 pong() ->
     receive
         finished ->
              io:format("Pong finished~n", []);

         {ping, Ping_PID} ->
               io:format("Pong received ping~n", []),

               Ping_PID ! pong,
           pong()
  end.

  start_pong() ->
       register(pong, spawn(tut17, pong, [])).

  start_ping(Pong_Node) ->
       spawn(tut17, ping, [3, Pong_Node]).

Upvotes: 1

Views: 586

Answers (1)

butter71
butter71

Reputation: 2723

put the node name in single quotes:

tut17:start_ping('pong@linux-1')

http://www.erlang.org/doc/reference_manual/data_types.html#id66276

An atom should be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.

Upvotes: 4

Related Questions