erlangerlang-otpgen-servergen-tcp

How to stop a tcp_listener implemented as a gen_server in erlang


I implemented a tcp_listener as a gen_server(). I have a function called start_link(Port) creates a tcp_listener at that port. Now, I have trouble understanding how to tell the tcp_listener to stop listening through stop(). I tried calling a function like this with stop.

 stop() -> gen_server:cast(?MODULE, shutdown) 

in the terminate/2 function I tried closing the listening socket but failed.

 terminate(_Reason, State = #state{lsocket = LSocket}) ->
 gen_tcp:close(LSocket),
 NewState = State#state{lsocket = null},
 {ok, NewState}.

If I close the listening socket, what happens to the accepted connections that I spawned.

Thank you!

   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.

Solution

  • I recommend you have a look at this chapter in the Learn You Some Erlang book, in particular:

    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}.

    So it should be just a matter of calling gen_tcp:close(Socket) on all sockets.