Search code examples
erlangerlang-shell

Client and server don't communicate if spawned on separate Eshell?


I have these 2 Erlang modules:

Server:

-module(server).

-export([start/0, loop/0]).

books(Cc) ->
  string:concat("Works ", Cc).

loans(Name) ->
  string:concat("Works too ", Name).

loop() ->
  receive
    {ClientPID, books, Info} ->
      ClientPID ! books(Info),
      loop();
    {ClientPID, loans, Info} ->
      ClientPID ! loans(Info),
      loop();
    _ ->
      io:fwrite("Invalid request received!~n"),
      loop()
  end.


start() ->
  spawn(server, loop, []).

and Client:

-module(client).

-export([start/1, client/1]).

start(Server_Address) ->
  spawn(client, client, [Server_Address]).

client(Server_Address) ->
  Server_Address ! {self(), books, "potato"},
  receive
    Response ->
      io:format("CLIENT ~w: ~w~n", [self(), Response])
  end.

I call Pid = server:start() it gives me a correct Pid without any errors, but when I call either client:start(Pid) or client:client(Pid) in a different Eshell it just doesn't communicate(It works if called in the same Eshell, obviously).

I know I'm just doing something wrong, but what is it? Thanks


Solution

  • Most likely both nodes are not clustered, you may check which nodes belong to the cluster with nodes()

    In order to start nodes that are reachable, you must name them:

    erl -sname client@localhost
    

    and ping the other node:

    $> erl -sname server@localhost                   
    Erlang/OTP 23 [erts-11.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
    
    Eshell V11.1  (abort with ^G)
    (server@localhost)1> nodes().
    []
    (server@localhost)2> net_adm:ping('client@localhost').
    pong
    (server@localhost)3> nodes().                         
    [client@localhost]