如果在单独的 Eshell 上生成,客户端和服务器不通信?

时间:2021-03-20 18:00:52

标签: erlang erlang-shell

我有这两个 Erlang 模块:

服务器:

-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, []).

和客户:

-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.

我调用 Pid = server:start() 它给了我一个正确的 Pid,没有任何错误,但是当我在不同的 Eshell 中调用 client:start(Pid) 或 client:client(Pid) 时,它只是不通信(很明显,如果在同一个 Eshell 中调用它就可以工作)。

我知道我只是做错了什么,但那是什么? 谢谢

1 个答案:

答案 0 :(得分:2)

很可能两个节点都没有集群,您可以使用nodes()

检查哪些节点属于集群

为了启动可访问的节点,您必须为它们命名:

erl -sname client@localhost

并ping另一个节点:

$> 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]
相关问题