我将 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.