<html><head><meta http-equiv="Content-Type" content="text/html charset=gb2312"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">The acceptor is waiting for new TCP connection.</div><div class="">If a new connection arrives, it will spawn a new process waiting for future new connection first, then the current process will handle communication on the connected connection.</div><div class=""><br class=""></div><div class="">Best Regards,</div><div class="">Yao</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">在 2019年6月14日,12:45,I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" class="">okaprinarjaya@gmail.com</a>> 写道:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi,<div class=""><br class=""></div><div class="">I learn gen_tcp from learnyousomeerlangg book. I try to understand flow of code below. </div><div class="">And i got confused with acceptor/1 . Because acceptor/1 call itself but have no base case and just execute once. It's not usual. Why? <br class="">From my understanding it should be an infinity recursive.<br class=""><div class=""><br class=""></div><div class=""><font face="courier new, monospace" class="">-module(naive_tcp).<br class="">-compile(export_all).<br class=""><br class="">start_server(Port) -><br class=""> Pid = spawn_link(<br class=""> fun() -><br class=""> io:format("Spawned at start_server()~n"),<br class=""> {ok, ListenSocket} = gen_tcp:listen(Port, [binary, {active, false}]),<br class=""> spawn(fun() -> acceptor(ListenSocket) end),<br class=""> timer:sleep(infinity)<br class=""> end<br class=""> ),<br class=""> {ok, Pid}.<br class=""><br class="">acceptor(ListenSocket) -><br class=""> io:format("I am acceptor~n"),<br class=""> {ok, AcceptorSocket} = gen_tcp:accept(ListenSocket),<br class=""> spawn(fun() -> acceptor(ListenSocket) end),<br class=""> handle(AcceptorSocket).<br class=""><br class="">handle(AcceptorSocket) -><br class=""> inet:setopts(AcceptorSocket, [{active, once}]),<br class=""> receive<br class=""> {tcp, AcceptorSocket, <<"quit", _/binary>>} -><br class=""> gen_tcp:close(AcceptorSocket);<br class=""> {tcp, AcceptorSocket, Message} -><br class=""> gen_tcp:send(AcceptorSocket, Message),<br class=""> handle(AcceptorSocket)<br class=""> end.</font><br class=""></div><div class=""><br class=""></div><div class="">Please enlightenment </div></div><div class=""><br class=""></div><div class="">Thank you </div><div class=""><br class=""></div><div class=""><br class=""></div></div>
_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></div></body></html>