Search code examples
erlangerlang-otpgen-servergen-tcp

Gen_tcp over gen_server socket listen closed immediatly


I want to use gen_tcp over gen_server, here is the code:

start_link() ->
 io:format("start_link~n"),
 gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).

init([])  ->
 {ok,ListenSocket} = gen_tcp:listen(8091, [{active,true}, binary]),
 io:format("listen done ~p ~p pid: ~p ~n",[ok,ListenSocket,self()]),
 %here the listen is closed
 waitConnection(),
 {ok,#state{listenSocket = ListenSocket}}.

handle_cast(waitConnection, #state{listenSocket = ListenSocket}) ->
 io:format("cast wait connections ~p pid:~p   ~n",[ListenSocket,self()]),
 {ok,Socket} = gen_tcp:accept(ListenSocket),
 io:format("cast wait accept ~n"),
 Pid = spawn(?MODULE,get_request,[Socket,[]]),
 gen_tcp:controlling_process(Socket,Pid),
 waitConnection(),
 {noreply, ListenSocket}.

waitConnection() ->
 try gen_server:cast({global, ?MODULE}, waitConnection)
 catch
   exit:{_,_} -> io:format("errror")
 end.

Well, the tcp_listener is immediately closed before waitConnetion, and I really don’t understand why.

If I move the handle_cast code on the init section it works correctly.

Why the connection is closed? I’m spending lot of time without success.

Any help is appreciated.

Edit:

If I move the cast code after the listener:

lsof -i :8091
    COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    beam.smp 6402 JR   24u  IPv4 0x9544c4111122e613      0t0  TCP *:8091 (LISTEN)

the code I have posted:

lsof -i :8091
// is empty

Solution

  • Ok I solved:

    handle_cast(waitConnection, State = #state{listenSocket = ListenSocket}) ->
        io:format("ListenSocket ok ~n"),
        {ok,Socket} = gen_tcp:accept(ListenSocket),
        io:format("cast wait accept ~n"),
        Pid = spawn(?MODULE,get_request,[Socket,[]]),
        gen_tcp:controlling_process(Socket,Pid),
        waitConnection(),
        {noreply, State}.
    

    The problem was the "State" parameter.

    Thank you to all