Tutorial :Cannot spawn a simple server in Erlang



Question:

I have a simple server:

-module(simple_server).  -export([loop/0]).    loop() ->      receive       {fact, N, Sender} ->          Sender ! {factResult, fact(N), self()},          loop();      {fib, N, Sender} ->          Sender ! {fibResult, fib(N), self()},          loop();      {stop, Sender} ->          Sender ! ok      end.    fact(0) -> 1;  fact(N) -> N * fact(N - 1).    fib(N) -> fib(N, 1, 0).    fib(0, _B, A) -> A;  fib(N, B, A) -> fib(N-1, A+B, B).  

Then I get this:

...\code>erl simple_server.erl    Eshell V5.7.5  (abort with ^G)  1> Server = spawn('server@myserver', fun simple_server:loop/0).    =ERROR REPORT==== 28-Jun-2010::10:46:29 ===  ** Can not start erlang:apply,[#Fun<simple_server.loop.0>,[]] on server@myserver**  <0.33.0>  

What did I miss?


Solution:1

It does not look like you have started as a distributed node. I get the same error message when my shell erlang node is started without a short name / long name using the "-sname" / "-name" flag of erl.

If you start this shell so it can participate in distribution you must also make sure the code for simple_server is loaded at the remote node, or that the remote node can autoload it from its code path.

For interactive use, you can use the nc(File) or nl(Module) commands in the shell to load on all known nodes. Use net_adm:ping(Node) to ping the node if it is not already listed when you do erlang:nodes().


Solution:2

You should read documentation first. Some tutorial would be helpful.

  1. erl with module source code doesn't anything. Read erl -man erl for more info.
  2. erlang:apply\2 has Node as first argument but Node should exists and have to be connected to current node.

You should try:

$ erl  Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]    Eshell V5.7.5  (abort with ^G)  1> c(simple_server).  {ok, simple_serevr}  2> spawn(simple_server, loop, []).  <0.33.0>  3>  

As good starting point you can look at Learn You Some Erlang For Great Good.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »