Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:27:35+00:00 2026-06-19T01:27:35+00:00

I’m reading the book Programming Erlang and on page 253 there is an example

  • 0

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.

2> 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)

the command for the server side:
socket_examples:start_nano_server().

the command for the client side:
socket_examples:nano_client_eval("list_to_tuple([2+3*4,10+20])").

I don’t have my firewall on but that shouldn’t be an issue since its not online.

    %% ---
%%  Excerpted from "Programming Erlang",   
%%---
-module(socket_examples).
-compile(export_all).
-import(lists, [reverse/1]).


nano_get_url() ->
    nano_get_url("www.google.com").

nano_get_url(Host) ->
    {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) ->
    receive
    {tcp,Socket,Bin} ->    %% (3)
        receive_data(Socket, [Bin|SoFar]);
    {tcp_closed,Socket} -> %% (4)
        list_to_binary(reverse(SoFar)) %% (5)
    end.



nano_client_eval(Str) ->
    {ok, Socket} = 
    gen_tcp:connect("localhost", 2345,
            [binary, {packet, 4}]),
    ok = gen_tcp:send(Socket, term_to_binary(Str)),
    receive
    {tcp,Socket,Bin} ->
        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() ->
    {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) ->
    receive
    {tcp, Socket, Bin} ->
        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} ->
        io:format("Server socket closed~n")
    end.



error_test() ->
    spawn(fun() -> 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, <<"123">>),
    receive
    Any ->
        io:format("Any=~p~n",[Any])
    end.

error_test_server() ->
    {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) ->
    receive
    {tcp, Socket, Data} ->
        io:format("received:~p~n",[Data]),
        atom_to_list(Data),
        error_test_server_loop(Socket)
    end.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:27:37+00:00Added an answer on June 19, 2026 at 1:27 am

    The message you receive ({error,econnrefused}) shows that the server is down.

    As Muzaaya said, the {packet,N} should be consistent in the system. If not the server will fail in the binary_to_term conversion and crash (I didn’t dig into that but i guess this is linked to the way the information is transported).

    The next requests will get then the connection refused message.

    with the change suggested by Muzaaya the server is able to execute binary_to_term, and everything works until the call to b_misc:string2value… which is not included in the post.

    I notice something weird then:

    • I launched the server using spawn(socket_examples,start_nano_server,[]).
    • the server crash on the call to an undefined module –> normal behavior.
    • But then the shell hang, and I cannot enter any new command.

    I cannot understand what is going on.

    Edit:

    Thanks Muzaaya, I forgot to look at the client code…

    I post here the code modified, in addition to the packet definition, I made some changes in the server and the client. I must say that I didn’t look deeply in the documentation, but it seems that there were some trouble in the original code with opening, closing and accepting. I am not sure that the code here is correct, and I know it misses something to close properly the socket, and above all that this code doesn’t serve multiple requests in parallel.

     -module(socket_examples).
    -compile(export_all).
    -import(lists, [reverse/1]).
    
    
    nano_get_url() ->
        nano_get_url("www.google.com").
    
    nano_get_url(Host) ->
        {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) ->
        receive
        {tcp,Socket,Bin} ->    %% (3)
            receive_data(Socket, [Bin|SoFar]);
        {tcp_closed,Socket} -> %% (4)
            list_to_binary(reverse(SoFar)) %% (5)
        end.
    
    
    
    nano_client_eval(Str) ->
        {ok, Socket} = 
        gen_tcp:connect("localhost", 2345,
                [binary, {packet, 0}]),
        ok = gen_tcp:send(Socket, term_to_binary(Str)),
        R = receive
            {tcp,Socket,Bin} ->
                io:format("Client received binary = ~p~n",[Bin]),
                Val = binary_to_term(Bin),
                io:format("Client result = ~p~n",[Val]),
                {ok,Val}
            after 5000 ->
                io:format("nano_client_eval got not answer to ~p~n",[Str]),
                {error,timeout}
        end,
    %   gen_tcp:close(Socket),
        R.
    
    
    
    start_nano_server() ->
        {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 0},  %% (6)
                         {reuseaddr, true},
                         {active, true}]),
     %   {ok, Socket} = gen_tcp:accept(Listen),  %% (7)
     %   gen_tcp:close(Listen),  %% (8)
    %    loop(Socket).
        loop(Listen).
    
    % loop(Socket) ->
    loop(Listen) ->
        {ok, Socket} = gen_tcp:accept(Listen),
        receive
        {tcp, Socket, Bin} ->
            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)
            Reply = string:to_upper(Str),  %% (10)
            io:format("Server replying = ~p~n",[Reply]),
            gen_tcp:send(Socket, term_to_binary(Reply)),  %% (11)
    %        loop(Socket);
            loop(Listen);
        {tcp_closed, Socket} ->
            io:format("Server socket closed~n")
        end.
    
    
    
    error_test() ->
        spawn(fun() -> error_test_server() end),
        lib_misc:sleep(2000),
        {ok,Socket} = gen_tcp:connect("localhost",4321,[binary, {packet, 0}]),
        io:format("connected to:~p~n",[Socket]),
        gen_tcp:send(Socket, <<"123">>),
        receive
        Any ->
            io:format("Any=~p~n",[Any])
        end.
    
    error_test_server() ->
        {ok, Listen} = gen_tcp:listen(4321, [binary,{packet,0}]),
        {ok, Socket} = gen_tcp:accept(Listen),
        error_test_server_loop(Socket).
    
    error_test_server_loop(Socket) ->
        receive
        {tcp, Socket, Data} ->
            io:format("received:~p~n",[Data]),
            atom_to_list(Data),
            error_test_server_loop(Socket)
        end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.