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

如何连接到使用短名称的Erlang节点?,erlang,Erlang,当我运行erl-sname foo时,创建的节点名使用主机名,而不是“localhost”,因此它被生成为,例如foo@roger-电脑 然后我注册(shell,self()),我可以从另一个节点(erl-sname bar)向它发送消息,如下所示: {shell, 'foo@roger-pc'} ! {hello, world}. 但是如果我使用{shell,foo},它就不起作用了!{knock,knock}。消息从未收到 如何连接到使用短名称的同一台PC上的Erlang节点?或者:如何派

当我运行
erl-sname foo
时,创建的节点名使用主机名,而不是“localhost”,因此它被生成为,例如
foo@roger-电脑

然后我注册(shell,self()),我可以从另一个节点(
erl-sname bar
)向它发送消息,如下所示:

{shell, 'foo@roger-pc'} ! {hello, world}.
但是如果我使用
{shell,foo},它就不起作用了!{knock,knock}
。消息从未收到

如何连接到使用短名称的同一台PC上的Erlang节点?或者:如何派生目标节点名称的“@roger pc”部分?或者:我应该只使用
erl-name吗foo@localhost
要注册一个长名称吗


一些背景:我正在编写一个escript,它生成一个
erl
进程,我希望能够将该操作系统进程的消息发送回原始脚本。

您可以为sname显式指定“localhost”

第一炮弹

erl -sname ax@localhost
register(rcvr, self()).
第二炮弹

erl -sname bx@localhost
net_kernel:connect_node(ax@localhost).
{rcvr, ax@localhost} ! hello.
又是第一个贝壳

(ax@localhost)7> flush().
Shell got hello
ok
节点是使用命令行标志-name(长名称)或-sname(短名称)指定名称的正在执行的Erlang运行时系统

节点名称的格式是atomname@host其中,name是用户给定的名称,host是完整的主机名(如果使用长名称),或者是主机名的第一部分(如果使用短名称)。node()返回节点的名称。例如:

%名字叫迪尔伯特 (dilbert@uab.ericsson.se)1> node()。 'dilbert@uab.ericsson.se"

%厄尔斯奈姆·迪尔伯特 (dilbert@uab)1> node()。
dilbert@uab

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仅仅决定了路径的其余部分需要多少。

嘿,罗杰,你可能会检查这个答案:没有回答问题:“当我想连接到那个节点时,我如何计算
-sname
实际使用的名称?”是的,这是一个输入错误:这是进程的注册名称;它必须匹配。修好了。