如何在 erlang 中停止作为 gen_server 实现的 tcp_listener
How to stop a tcp_listener implemented as a gen_server in erlang
我将 tcp_listener 实现为 gen_server()。我有一个名为 start_link(Port) 的函数在该端口创建一个 tcp_listener。现在,我很难理解如何通过 stop() 告诉 tcp_listener 停止收听。
我试着用 stop 调用这样的函数。
stop() -> gen_server:cast(?MODULE, shutdown)
在 terminate/2 函数中我尝试关闭侦听套接字但失败了。
terminate(_Reason, State = #state{lsocket = LSocket}) ->
gen_tcp:close(LSocket),
NewState = State#state{lsocket = null},
{ok, NewState}.
如果我关闭侦听套接字,我生成的已接受连接会发生什么情况。
谢谢!
start_link(Port) when is_integer(Port) ->
State = #state{port = Port},
gen_server:start_link({local, ?MODULE}, ?MODULE, State, []).
init(State = #state{port = Port}) ->
io:format("Supervisor started me and I am listening at ~p ~n", [Port]),
case gen_tcp:listen(Port, ?TCP_OPTIONS) of
{ok, LSocket} ->
NewState = State#state{lsocket = LSocket},
spawn(fun() -> accept(NewState) end),
{ok, NewState};
{error, Reason} ->
{stop, Reason}
end.
accept(State = #state{lsocket = LSocket}) ->
case gen_tcp:accept(LSocket) of
{ok, Socket} ->
Pid = spawn(fun() ->
io:format("Connection accepted ~n"),
loop(Socket)
end),
gen_tcp:controlling_process(Socket, Pid),
accept(State);
{error, closed} -> error
end.
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
loop(Socket);
{error, closed} ->
ok
end.
我建议你看看 this chapter in the Learn You Some Erlang book,特别是:
Both sockets can send messages in the same way, and can then be closed with gen_tcp:close(Socket)
. Note that closing an accept socket will close that socket alone, and closing a listen socket will close none of the related and established accept sockets, but will interrupt currently running accept calls by returning {error, closed}
.
所以它应该只是在所有套接字上调用 gen_tcp:close(Socket)
的问题。
我将 tcp_listener 实现为 gen_server()。我有一个名为 start_link(Port) 的函数在该端口创建一个 tcp_listener。现在,我很难理解如何通过 stop() 告诉 tcp_listener 停止收听。 我试着用 stop 调用这样的函数。
stop() -> gen_server:cast(?MODULE, shutdown)
在 terminate/2 函数中我尝试关闭侦听套接字但失败了。
terminate(_Reason, State = #state{lsocket = LSocket}) ->
gen_tcp:close(LSocket),
NewState = State#state{lsocket = null},
{ok, NewState}.
如果我关闭侦听套接字,我生成的已接受连接会发生什么情况。
谢谢!
start_link(Port) when is_integer(Port) ->
State = #state{port = Port},
gen_server:start_link({local, ?MODULE}, ?MODULE, State, []).
init(State = #state{port = Port}) ->
io:format("Supervisor started me and I am listening at ~p ~n", [Port]),
case gen_tcp:listen(Port, ?TCP_OPTIONS) of
{ok, LSocket} ->
NewState = State#state{lsocket = LSocket},
spawn(fun() -> accept(NewState) end),
{ok, NewState};
{error, Reason} ->
{stop, Reason}
end.
accept(State = #state{lsocket = LSocket}) ->
case gen_tcp:accept(LSocket) of
{ok, Socket} ->
Pid = spawn(fun() ->
io:format("Connection accepted ~n"),
loop(Socket)
end),
gen_tcp:controlling_process(Socket, Pid),
accept(State);
{error, closed} -> error
end.
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
loop(Socket);
{error, closed} ->
ok
end.
我建议你看看 this chapter in the Learn You Some Erlang book,特别是:
Both sockets can send messages in the same way, and can then be closed with
gen_tcp:close(Socket)
. Note that closing an accept socket will close that socket alone, and closing a listen socket will close none of the related and established accept sockets, but will interrupt currently running accept calls by returning{error, closed}
.
所以它应该只是在所有套接字上调用 gen_tcp:close(Socket)
的问题。