0

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
J.R.
  • 2,335
  • 2
  • 19
  • 21
  • That somewhat works for me. May be your port just busy? – Lol4t0 Jul 28 '15 at 08:54
  • No, I tried with different ports. The listener and immediately got down ! – J.R. Jul 28 '15 at 13:06
  • Well, how you understand that listener is down? – Lol4t0 Jul 28 '15 at 13:07
  • @Lol4t0 I edited the answer ! thank you! – J.R. Jul 28 '15 at 13:14
  • Hm, may be then you receive smth, because your code will crash immediately after receiving any network message (because you return `Socket` instead of `#state{}` from cast handler – Lol4t0 Jul 28 '15 at 15:00
  • What you're trying to do won't work anyway, because `gen_tcp:accept` blocks, which means your `handle_cast` function will block the entire gen_server process until a connection arrives. – Steve Vinoski Jul 28 '15 at 19:07

1 Answers1

0

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

J.R.
  • 2,335
  • 2
  • 19
  • 21