问题 gen_tcp:接受

发布于 2024-08-13 08:27:40 字数 1092 浏览 5 评论 0原文

我已经创建了一个 TCP 服务器,它生成一个进程来侦听传入的连接。这是示例代码(从原始代码中删除了一些内容):

module a:

main([]) ->
    { ok, Pid } = b:start(),
    receive
            _ ->
                    ok
    end.

module b:

-define(TCP_OPTIONS, [binary, { active, false}, { packet, 0 } , {reuseaddr, true}]).

...

start_link(Port) ->
    Pid = spawn_link(server_listener, init, [ Port ]),
    { ok , self() }.

init(Port, Processor) ->
    case gen_tcp:listen(Port, ?TCP_OPTIONS) of
     { ok , LSocket } ->
           accept_loop(LSocket);
     { error, Reason } ->
        { stop, Reason }
     end.

accept_loop( LSocket) ->
  ?LOG("Current socket acceptor PID [~w]~n", [self()]),
  case gen_tcp:accept(LSocket) of
      { ok, Socket } ->   
      %do stuff here
         spawn(server_listener , accept_loop, [ LSocket ]);
      { error, Reason }  ->
         ?LOG("Error accepting socket! [ ~s ]~n", [ Reason ])   
  end.

问题是:每次我尝试通过此端口上的 telnet 连接时,我都会收到错误 { error, close }在 gen_tcp 上:接受。这已经让我抓狂,想弄清楚到底发生了什么。

谢谢,

I've made a tcp server witch spawns a process to listen incoming connections. Here is the sample code (removed a few things from my original code):

module a:

main([]) ->
    { ok, Pid } = b:start(),
    receive
            _ ->
                    ok
    end.

module b:

-define(TCP_OPTIONS, [binary, { active, false}, { packet, 0 } , {reuseaddr, true}]).

...

start_link(Port) ->
    Pid = spawn_link(server_listener, init, [ Port ]),
    { ok , self() }.

init(Port, Processor) ->
    case gen_tcp:listen(Port, ?TCP_OPTIONS) of
     { ok , LSocket } ->
           accept_loop(LSocket);
     { error, Reason } ->
        { stop, Reason }
     end.

accept_loop( LSocket) ->
  ?LOG("Current socket acceptor PID [~w]~n", [self()]),
  case gen_tcp:accept(LSocket) of
      { ok, Socket } ->   
      %do stuff here
         spawn(server_listener , accept_loop, [ LSocket ]);
      { error, Reason }  ->
         ?LOG("Error accepting socket! [ ~s ]~n", [ Reason ])   
  end.

The problem is: EVERY time that I try to connect from telnet on this port, I'm receiving an error { error, closed } on gen_tcp:accept. This is already driving me nuts trying to figure out what is happening.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-08-20 08:27:40

你的“接受循环”并不是真正的循环......而且它是人为的。

您可能需要“do_accept_loop”和适当的“server_loop”来处理连接。看看这个

你想要一些类似的东西:

% Call echo:listen(Port) to start the service.
listen(Port) ->
    {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
    accept(LSocket).

% Wait for incoming connections and spawn the echo loop when we get one.
accept(LSocket) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    spawn(fun() -> loop(Socket) end),
    accept(LSocket).

% Echo back whatever data we receive on Socket.
loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Data} ->
            gen_tcp:send(Socket, Data),
            loop(Socket);
        {error, closed} ->
            ok
    end.

Your "accept loop" isn't really a loop... and it is contrived.

You probably want "do_accept_loop" and a proper "server_loop" for handling a connection. Have a look at this.

You want something along the lines of:

% Call echo:listen(Port) to start the service.
listen(Port) ->
    {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
    accept(LSocket).

% Wait for incoming connections and spawn the echo loop when we get one.
accept(LSocket) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    spawn(fun() -> loop(Socket) end),
    accept(LSocket).

% Echo back whatever data we receive on Socket.
loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Data} ->
            gen_tcp:send(Socket, Data),
            loop(Socket);
        {error, closed} ->
            ok
    end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文