1

我正在阅读 Programming Erlang 这本书,在第 253 页有一个运行客户端/服务器连接的示例。下面的代码建议在两个单独的 erlang 控制台窗口中运行以下命令,我从中得到以下错误。

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)

服务器端的命令: socket_examples:start_nano_server().

客户端的命令: socket_examples:nano_client_eval("list_to_tuple([2+3*4,10+20])").

我没有打开防火墙,但这应该不是问题,因为它不在线。

    %% ---
%%  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.
4

1 回答 1

1

您收到的消息 ({error,econnrefused}) 表明服务器已关闭。

正如 Muzaaya 所说,{packet,N} 在系统中应该是一致的。如果不是,服务器将在 binary_to_term 转换中失败并崩溃(我没有深入研究,但我想这与信息的传输方式有关)。

下一个请求将收到连接被拒绝的消息。

通过 Muzaaya 建议的更改,服务器能够执行 binary_to_term,并且一切正常,直到调用 b_misc:string2value... 这未包含在帖子中。

我注意到一些奇怪的事情

  • 我使用 spawn(socket_examples,start_nano_server,[]) 启动了服务器。
  • 服务器在调用未定义模块时崩溃-> 正常行为。
  • 但是随后外壳挂起,我无法输入任何新命令。

我无法理解发生了什么。

编辑:

谢谢 Muzaaya,我忘了看客户端代码......

我这里贴出修改后的代码,除了包定义,我在服务端和客户端做了一些改动。不得不说,我没有深入查看文档,但是在打开,关闭和接受的原始代码中似乎有些麻烦。我不确定这里的代码是否正确,我知道它错过了正确关闭套接字的一些东西,最重要的是,这段代码不能并行处理多个请求。

 -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.
于 2013-02-11T10:44:10.363 回答