[erlang-questions] How to solve the blocking accept call in atcp_listener process ?

Claes Wikstrom <>
Fri Jun 5 23:42:35 CEST 2009


No, but you'll have to live with that.


info wrote:
> ok, but it's not gen_server compatible !
>  
> James Hague wrote:
>  > 
>  > Many examples uses the async_accept primitive from the module prim_inet.
>  > 
>  > This primitive is not documented (low level) and might change or dissapear.
>  > 
>  > Do you know examples which don't use it but use a "clean" solution for the blocking accept call ?
>  > > Or could you show an elegant solution in this forum ?
>  > 
>  > You could always put the blocking call into it's own process.  When a
>  > connection occurs, change the port's owner and send the port in a
>  > message.  (And open the port in passive mode to make sure messages
>  > aren't received before switching the owner.)
>  
> The way I've always done it is to spawn the acceptor as:
>  
> L = listen(...),
> listen_loop(L).
>  
>  
> listen_loop(L) - >
>     Top = self(),
>     spawn(fun() - >
>             Sock = accept(L),
>             Top ! one_more,
>             handle_sock(Sock)
>            end),
>      receive
>         one_more - >
>            listen_loop(L)
>      end.
>  
>  
> /klacke
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list