Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to an Erlang node which uses a short name?

Tags:

erlang

When I run erl -sname foo, the node name created uses the hostname, rather than "localhost", so it is generated as, for example, foo@roger-pc.

I then register(shell, self()), and I can send messages to it from another node (erl -sname bar) as follows:

{shell, 'foo@roger-pc'} ! {hello, world}.

But it doesn't work if I use {shell, foo} ! {knock, knock}. The message is never received.

How do I connect to an Erlang node on the same PC which is using a short name? Or: how do I derive the "@roger-pc" part of the destination node name? Or: should I just use erl -name foo@localhost to register a long name?

Some background: I'm writing a an escript which spawns an erl process, and I'd like to be able to send messages from that OS process back to the original script.

like image 596
Roger Lipscombe Avatar asked Mar 05 '14 15:03

Roger Lipscombe


1 Answers

you can specify 'localhost' explicitly for sname.

first shell

erl -sname ax@localhost
register(rcvr, self()).

second shell

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

and first shell again

(ax@localhost)7> flush().
Shell got hello
ok
like image 86
Odobenus Rosmarus Avatar answered Nov 12 '22 16:11

Odobenus Rosmarus