Pass Data between two Nodes erlang

90 Views Asked by At

I have recently started learning Erlang and I am trying to implement a server-client sample program. I have created a registered process and I would like to send data to it from another process. The code is as follows.

-module(mine).
-export([alice/0, bob/2, startAlice/0, startBob/1]).

alice() ->
    receive
        {message, BobNode} ->
            io:fwrite("Alice got a message \n"),
            BobNode ! message,
            alice()
        finished -> io:fwrite("Alice is finished\n")
    end.

bob(0, AliceNode) ->
    {alice, AliceNode} ! finished,
    io:fwrite("Bob is finished\n");

bob(N, AliceNode) ->
    {alice, AliceNode} ! {message, self()},
    receive
        message -> io:fwrite("Bob got a message ~w \n",[N])
    end,
    bob(N-1, AliceNode).

startAlice() ->
    register(alice, spawn(mine, alice, [])).

startBob(AliceNode) ->
    spawn(mine, bob, [30000, AliceNode]).

Here, I would like to send some value say N, from bob to alice. I tried sending the data as

{alice, AliceNode, Nvalue} ! {message, self(), N} in bob(N, AliceNode) function, but got the error variable 'Nvalue' is unbound erl. I am sure I am missing something trivial here. Any help would be appreciated. Thanks in advance.

1

There are 1 best solutions below

0
Gabor Szelei On

See changed code:

-module(mine).
-export([alice/0, bob/2, startAlice/0, startBob/1]).

alice() ->
    receive
        {X, BobNode} ->
            io:fwrite("Alice got a message ~w \n",[X]),
            BobNode ! message,
            alice();
        finished -> io:fwrite("Alice is finished\n")
    end.

bob(0, AliceNode) ->
    {alice, AliceNode} ! finished,
    io:fwrite("Bob is finished\n");

bob(N, AliceNode) ->
    {alice, AliceNode} ! {N, self()},
    receive
        message -> io:fwrite("Bob got a message ~w \n",[N])
    end,
    bob(N-1, AliceNode).

startAlice() ->
    register(alice, spawn(mine, alice, [])).

startBob(AliceNode) ->
    spawn(mine, bob, [10, AliceNode]).

I've used erlang: Erlang/OTP 23 [erts-11.1.8] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

12> c(mine).            
    {ok,mine}
13> mine:startAlice().    
    true
14> mine:startBob(node()).
Alice got a message 10 
<0.93.0>
Bob got a message 10 
Alice got a message 9 
Bob got a message 9 
Alice got a message 8 
Bob got a message 8 
Alice got a message 7 
Bob got a message 7 
Alice got a message 6 
Bob got a message 6 
Alice got a message 5 
Bob got a message 5 
Alice got a message 4 
Bob got a message 4 
Alice got a message 3 
Bob got a message 3 
Alice got a message 2 
Bob got a message 2 
Alice got a message 1 
Bob got a message 1 
Bob is finished
Alice is finished

I think send and the receive part should be in sync.

{alice, AliceNode, Nvalue} ! {message, self(), N}

needs:

alice() ->
    receive
        {message, BobNode, X} ->
            io:fwrite("Alice got a message ~w \n",[X]),
            BobNode ! message,
            alice();
        finished -> io:fwrite("Alice is finished\n")
    end.