如何连接使用短名称的 Erlang 节点?

6
当我运行erl -sname foo时,创建的节点名称使用主机名而不是"localhost",因此生成的节点名称为例如:foo@roger-pc
然后我执行register(shell, self()),并且可以从另一个节点(erl -sname bar)发送消息给它,方法如下:
{shell, 'foo@roger-pc'} ! {hello, world}.

但如果我使用{shell, foo} ! {knock, knock}就无法正常工作。消息从未被接收到。
如何连接到同一台计算机上使用短名称的Erlang节点?或者说,如何推导出目标节点名称中的"@roger-pc"部分?或者应该只使用erl -name foo@localhost注册一个长名称?
背景:我正在编写一个escript,它生成一个erl进程,并且我希望能够从该OS进程向原始脚本发送消息。

嗨,罗杰,你可能会查看这个答案:https://dev59.com/TmQn5IYBdhLWcg3wVF7i - oscarftoro
3个回答

9
你可以明确指定'sname'为'localhost'。
第一个shell。
erl -sname ax@localhost
register(rcvr, self()).

第二个 Shell

erl -sname bx@localhost
net_kernel:connect_node(ax@localhost).
{rcvr, ax@localhost} ! hello.

再次进入第一个shell

(ax@localhost)7> flush().
Shell got hello
ok

是的,这是一个打字错误:那是进程的注册名称;它必须匹配。已经修复了。 - Roger Lipscombe

0

sname仍需要'@'符号。当发送消息时,您无需输入完整的URI。

c:\>erl -sname short
(short@hostname)1> {process, 'short@hostname'} ! Message.

c:\>erl -name short
(short@hostname.more.stuff)1> {process, 'short@hostname.more.stuff'} ! Message.

short不是完整的名称。sname和name仅决定路径的其余部分所需的程度。


-2
一个节点是一个执行Erlang运行时系统,使用命令行标志-name(长名称)或-sname(短名称)给定名称。
节点名称的格式是一个原子名称@主机名,其中名称是用户指定的名称,主机名是完整的主机名(如果使用长名称),或主机名的第一部分(如果使用短名称)。node()返回节点的名称。例如:
%erl -name dilbert (dilbert@uab.ericsson.se)1> node()。 'dilbert@uab.ericsson.se'
%erl -sname dilbert (dilbert@uab)1> node()。 dilbert@uab

1
不回答问题:“当我想连接到该节点时,如何确定“-sname”实际使用的名称?” - Roger Lipscombe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接