<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>Hi,</div>
<div> </div>
<div>Can someone explain why the code from afile_server responds only once. If I send the Pid a different message it just hangs the shell.</div>
<div> </div>
<div>Here is the code below:</div>
<div>-module(afile_server).<br/>
-export([start/1, loop/1]).</div>
<div>start(Dir) -> spawn(afile_server, loop, [Dir]).</div>
<div>loop(Dir) -><br/>
receive<br/>
{Client, list_dir} -><br/>
Client ! {self(), file:list_dir(Dir)},<br/>
loop(Dir);<br/>
{Client, {get_file, File}} -><br/>
Full = filename:join(Dir, File),<br/>
Client ! {self(), file:read_file(Full)},<br/>
loop(Dir)<br/>
end,<br/>
loop(Dir).</div>
<div> </div>
<div>We compile it and run it:</div>
<div>1> c(afile_server).<br/>
{ok,afile_server}<br/>
2> Pid = afile_server:start(".").<br/>
<0.40.0></div>
<div><br/>
We send it a list_dir message:<br/>
<pre><code><br/>
3> Pid ! {self(), list_dir}.<br/>
{<0.33.0>,list_dir}</div>
<div> </div>
<div>And receive reponse just fine:<br/>
4> receive X -> X end.<br/>
{<0.40.0>,<br/>
{ok,["afile_server.erl", "afile_server.beam"]}}</div>
<div><br/>
Send the same message again:<br/>
5> Pid ! {self(), list_dir}.<br/>
{<0.33.0>,list_dir}</div>
<div> </div>
<div>And receive response, no problem:<br/>
6> receive X -> X end. <br/>
{<0.40.0>,<br/>
{ok,["afile_server.erl", "afile_server.beam"]}}</div>
<div> </div>
<div>Now we send a get_file message:<br/>
7> Pid ! {self(), {get_file, "afile_server.erl"}}.<br/>
{<0.33.0>,{get_file,"afile_server.erl"}}</div>
<div> </div>
<div>But receive hangs:<br/>
8> receive X -> X end. </div>
<div>BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded<br/>
(v)ersion (k)ill (D)b-tables (d)istribution<br/>
a</div>
<div> </div>
<div>We restart it:<br/>
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]</div>
<div>Eshell V6.4 (abort with ^G)<br/>
1> Pid = afile_server:start(".").<br/>
<0.35.0></div>
<div> </div>
<div>And send it get_file:<br/>
2> Pid ! {self(), {get_file, "afile_server.erl"}}.<br/>
{<0.33.0>,{get_file,"afile_server.erl"}}</div>
<div> </div>
<div>This time get_file works fine:<br/>
3> receive X -> X end.<br/>
{<0.35.0>,<br/>
{ok,<<"-module(afile_server).\n-export([start/1, loop/1]).\n\nstart(Dir) -> spawn(afile_server, loop, [Dir]).\n"...>>}}</div>
<div> </div>
<div>Now we try the other list_dir message:<br/>
4> Pid ! {self(), list_dir}.<br/>
{<0.33.0>,list_dir}</div>
<div> </div>
<div>Now this one hangs:<br/>
5> receive X -> X end.</div>
<div>BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded<br/>
(v)ersion (k)ill (D)b-tables (d)istribution<br/>
a</div>
<div> </div>
<div>I'm not sure why the same message seems to work multiple times, but once we send a different request it seems that we don't get a response back.</div>
<div>I'm stumped, does anyone have any pointers?</div>
<div> </div>
<div>Thanks</div>
</div></div></body></html>