[newbie] accept()-ing process under supervision

Dietmar Schaefer dietmar@REDACTED
Thu Jan 27 15:50:08 CET 2005


OOPS !

Sorry Joe ->  this message was not only for you !


Sorry Folks  I don 't get it



I am having the same problem :

   keeping the server running(accecpting connections) even when my 
client (written in C) dies.

I found this in btt_srv.erl

and slightly adapted it :




getMessages(Port) when integer(Port) ->
   case gen_tcp:listen(Port, [ {reuseaddr, true},
                                        {packet, 0}, {active, false}]) of
         {ok, ListenSocket} ->
            io:format(" I listen to socket ~p~n", [Port]),
           process_flag(trap_exit, true),                    %% get 
informed when server failed                acceptLS(ListenSocket);
                 Error -> exit(error)       end.

acceptLS(Socket) ->
   Pid = erlang:spawn_link( fun() -> rec(Socket,self()) end),
   waitForAcceptLS(Socket,Pid).

waitForAcceptLS(Socket,Pid) ->
   receive
       {Pid,ok}        -> ok;              %%       
?MODULE:acceptLS(Socket);
       {'EXIT',From,_} ->  ?MODULE:acceptLS(Socket);
       _Error          -> waitForAcceptLS(Socket,Pid)
   end.           
rec(Socket,Pid) ->
   case gen_tcp:accept(Socket) of
       {ok,Sock} -> Pid ! {self(),ok},
                    doRecv(Sock);
       Error     -> exit(Error)
   end.
                        %% @doc
%%   this is the receiving routine
%%
doRecv(Sock) ->      case gen_tcp:recv(Sock,0) of
       {ok,Bin}         ->  io:format("o.k. I got:  ~p~n", 
[decode(Bin)]),                                             
                            doRecv(Sock);
       {'EXIT',From,_} -> ?MODULE:acceptLS(Sock);
       _Other       ->  other
   end.



When I  kill the client I get:

exited: {undef,[{cmmc_com,acceptLS,[#Port<0.620>]},
                  {cmmc_com,startComm,2},
                  {erl_eval,do_apply,5},
                  {shell,exprs,6},
                  {shell,eval_loop,3}]}


Any hints  ??


regards


Dietmar




More information about the erlang-questions mailing list