<div dir="ltr">Hi,<div><br></div><div>OMG. Finally..... I know and understand how this code flowing. It takes days for me to understand. </div><div>It turns out this code below:<br><br></div><div><span style="font-family:"courier new",monospace">{ok, AcceptorSocket} = gen_tcp:accept(ListenSocket),</span><br></div><div><br></div><div>is locking / blocking the execution of these lines:</div><div><br></div><div><span style="font-family:"courier new",monospace">1. spawn(fun() -> acceptor(ListenSocket) end),</span><br style="font-family:"courier new",monospace"><span style="font-family:"courier new",monospace">2. handle(AcceptorSocket).</span><br></div><div><br></div><div>Then, after we doing a connection to the port via telnet, the locking / blocking become released / unlocked / unblocked, then executing lines: 1 and create new process,</div><div>lines: 2 listen / waiting the incoming message. Then make a new locking at the new created process for the next telnet connection. Yes this is an infinity recursive. </div><div>The base case is killing main process that started from <font face="courier new, monospace">start_server/1</font> . <font face="courier new, monospace">exit(Pid, killed)</font></div><div><br></div><div>Thank you :)</div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Jum, 14 Jun 2019 pukul 11.45 I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com">okaprinarjaya@gmail.com</a>> menulis:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>I learn gen_tcp from learnyousomeerlangg book. I try to understand flow of code below. </div><div>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>From my understanding it should be an infinity recursive.<br><div><br></div><div><font face="courier new, monospace">-module(naive_tcp).<br>-compile(export_all).<br><br>start_server(Port) -><br> Pid = spawn_link(<br> fun() -><br> io:format("Spawned at start_server()~n"),<br> {ok, ListenSocket} = gen_tcp:listen(Port, [binary, {active, false}]),<br> spawn(fun() -> acceptor(ListenSocket) end),<br> timer:sleep(infinity)<br> end<br> ),<br> {ok, Pid}.<br><br>acceptor(ListenSocket) -><br> io:format("I am acceptor~n"),<br> {ok, AcceptorSocket} = gen_tcp:accept(ListenSocket),<br> spawn(fun() -> acceptor(ListenSocket) end),<br> handle(AcceptorSocket).<br><br>handle(AcceptorSocket) -><br> inet:setopts(AcceptorSocket, [{active, once}]),<br> receive<br> {tcp, AcceptorSocket, <<"quit", _/binary>>} -><br> gen_tcp:close(AcceptorSocket);<br> {tcp, AcceptorSocket, Message} -><br> gen_tcp:send(AcceptorSocket, Message),<br> handle(AcceptorSocket)<br> end.</font><br></div><div><br></div><div>Please enlightenment </div></div><div><br></div><div>Thank you </div><div><br></div><div><br></div></div>
</blockquote></div>