[erlang-questions] tcp connections dropped in gen_server

George Catalin Serbanut <>
Mon Sep 5 23:40:51 CEST 2011


Hi,

I've been testing one code found on internet and it works smoothly. The code
can be found at:

http://20bits.com/articles/erlang-a-generalized-tcp-server/

Take a look at the code and compare it with your code. It will help in
solving your problem.

About your code, it may be your code gets a bit confused by which connection
belongs to which spawned thread and then releases an error which generates
tcp_closed message. Nevertheless, I might be wrong. Better try to dump your
tcp traffic and see where is the problem in the connections.

Cheers,
CGS



On Mon, Sep 5, 2011 at 6:59 PM, Reynaldo Baquerizo <>wrote:

> I have a running application that consist in a supervisor and two
> generic servers, one of them wraps around odbc and the other handles
> tcp connections, a fragment of the relevant code is:
>
>
> init([]) ->
>    process_flag(trap_exit, true),
>    {ok, ListenSocket} = gen_tcp:listen(Port, [binary, {packet, 0},
>
> {reuseaddr, true},
>
> {active, true}]),
>    proc_lib:spawn_link(?MODULE, acceptor, [ListenSocket])
>
> acceptor(ListenSocket) ->
>    {ok, Socket} = gen_tcp:accept(ListenSocket),
>    error_logger:info_msg("New connection from ~p~n", [Socket]),
>    _Pid = proc_lib:spawn(?MODULE, acceptor, [ListenSocket]),
>    inet:setopts(Socket, [binary, {nodelay, true}, {active, true}]),
>    loop(Socket).
>
> loop(Socket) ->
>    receive
>        {tcp, Socket, Data} ->
>            error_logger:info_msg("Messaged received from ~p: ~p~n",
> [Socket, Data]),
>            comm_lib:handle_message(Socket, Data),
>            loop(Socket);
>        {tcp_closed, Socket} ->
>            error_logger:info_msg("Device at ~p disconnected~n", [Socket]);
>        _Any ->
>            %% skip this
>            loop(Socket)
>    end.
>
> So, I basically start a new  unlinked process for every new tcp
> connection. It works just fine for a couple hours but  then every tcp
> connection is dropped gradually with message "Device at ~p
> disconnected". The client will try to reconnect if connection is
> closed. The tcp connection should only terminate if remote end closes
> it or spawned proccess in the server crashes.
>
> After all connections were dropped, I can see with inet:i() that there
> are established connections but no logging!
>
> Can anyone give some insight or point to the right direction to debug this?
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110905/d2bc75ca/attachment.html>


More information about the erlang-questions mailing list