title: Erlang client-server error
body: <p>I'm reading the book Programming Erlang and on page 253 there is an example for running a client/server connection. The code below suggests to run the following commands in two separate erlang console windows from which I get the following error.</p>
  
  <pre><code>2&gt; socket_examples:nano_client_eval("list_to_tuple([2+3*4,10+20])").
  ** exception error: no match of right hand side value {error,econnrefused}
       in function  socket_examples:nano_client_eval/1 (socket_examples.erl, line 28)
  </code></pre>
  
  <p>the command for the server side:
   <code>socket_examples:start_nano_server().</code></p>
  
  <p>the command for the client side:
   <code>socket_examples:nano_client_eval("list_to_tuple([2+3*4,10+20])").</code></p>
  
  <p>I don't have my firewall on but that shouldn't be an issue since its not online.</p>
  
  <pre><code>    %% ---
  %%  Excerpted from "Programming Erlang",   
  %%---
  -module(socket_examples).
  -compile(export_all).
  -import(lists, [reverse/1]).
  
  
  nano_get_url() -&gt;
      nano_get_url("www.google.com").
  
  nano_get_url(Host) -&gt;
      {ok,Socket} = gen_tcp:connect(Host,80,[binary, {packet, 0}]), %% (1)
      ok = gen_tcp:send(Socket, "GET / HTTP/1.0\r\n\r\n"),  %% (2)
      receive_data(Socket, []).
  
  receive_data(Socket, SoFar) -&gt;
      receive
      {tcp,Socket,Bin} -&gt;    %% (3)
          receive_data(Socket, [Bin|SoFar]);
      {tcp_closed,Socket} -&gt; %% (4)
          list_to_binary(reverse(SoFar)) %% (5)
      end.
  
  
  
  nano_client_eval(Str) -&gt;
      {ok, Socket} = 
      gen_tcp:connect("localhost", 2345,
              [binary, {packet, 4}]),
      ok = gen_tcp:send(Socket, term_to_binary(Str)),
      receive
      {tcp,Socket,Bin} -&gt;
          io:format("Client received binary = ~p~n",[Bin]),
          Val = binary_to_term(Bin),
          io:format("Client result = ~p~n",[Val]),
          gen_tcp:close(Socket)
      end.
  
  
  
  start_nano_server() -&gt;
      {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4},  %% (6)
                       {reuseaddr, true},
                       {active, true}]),
      {ok, Socket} = gen_tcp:accept(Listen),  %% (7)
      gen_tcp:close(Listen),  %% (8)
      loop(Socket).
  
  loop(Socket) -&gt;
      receive
      {tcp, Socket, Bin} -&gt;
          io:format("Server received binary = ~p~n",[Bin]),
          Str = binary_to_term(Bin),  %% (9)
          io:format("Server (unpacked)  ~p~n",[Str]),
          Reply = lib_misc:string2value(Str),  %% (10)
          io:format("Server replying = ~p~n",[Reply]),
          gen_tcp:send(Socket, term_to_binary(Reply)),  %% (11)
          loop(Socket);
      {tcp_closed, Socket} -&gt;
          io:format("Server socket closed~n")
      end.
  
  
  
  error_test() -&gt;
      spawn(fun() -&gt; error_test_server() end),
      lib_misc:sleep(2000),
      {ok,Socket} = gen_tcp:connect("localhost",4321,[binary, {packet, 2}]),
      io:format("connected to:~p~n",[Socket]),
      gen_tcp:send(Socket, &lt;&lt;"123"&gt;&gt;),
      receive
      Any -&gt;
          io:format("Any=~p~n",[Any])
      end.
  
  error_test_server() -&gt;
      {ok, Listen} = gen_tcp:listen(4321, [binary,{packet,2}]),
      {ok, Socket} = gen_tcp:accept(Listen),
      error_test_server_loop(Socket).
  
  error_test_server_loop(Socket) -&gt;
      receive
      {tcp, Socket, Data} -&gt;
          io:format("received:~p~n",[Data]),
          atom_to_list(Data),
          error_test_server_loop(Socket)
      end.
  </code></pre>