<div dir="auto">Compare the receive pattern for "<span style="font-size:12.8px">{Client, {put_file, File, Bytes}}" to what you're sending</span></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 31, 2021, 08:13 David Christensen <<a href="mailto:dpchrist@holgerdanske.com">dpchrist@holgerdanske.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">erlang-questions:<br>
<br>
I am working my way through "Programming Erlang", 2 e.:<br>
<br>
2021-12-30 20:00:05 dpchrist@tinkywinky ~/sandbox/erlang<br>
$ cat /etc/debian_version ; uname -a ; dpkg-query -W erlang<br>
9.13<br>
Linux tinkywinky 4.9.0-17-amd64 #1 SMP Debian 4.9.290-1 (2021-12-12) <br>
x86_64 GNU/Linux<br>
erlang 1:19.2.1+dfsg-2+deb9u3<br>
<br>
<br>
Chapter 2, Exercise 4, requests that I add a "put_file" command to the <br>
file client and server code. RTFM I found the file:write_file() <br>
function. I can test it with the Erlang shell, and it works:<br>
<br>
2021-12-30 20:29:34 dpchrist@tinkywinky ~/sandbox/erlang<br>
$ erl<br>
Erlang/OTP 19 [erts-8.2.1] [source] [64-bit] [smp:8:8] <br>
[async-threads:10] [kernel-poll:false]<br>
<br>
Eshell V8.2.1 (abort with ^G)<br>
1> file:write_file("foo.2", "this is foo.2\n").<br>
ok<br>
2> halt().<br>
<br>
2021-12-30 20:30:09 dpchrist@tinkywinky ~/sandbox/erlang<br>
$ cat foo.2<br>
this is foo.2<br>
<br>
<br>
I have extended afile_server.erl with a "put_file" command that calls <br>
file::write_file():<br>
<br>
2021-12-30 20:34:25 dpchrist@tinkywinky ~/sandbox/erlang<br>
$ cat ex0204_server.erl<br>
-module(ex0204_server).<br>
-export([start/1, loop/1]).<br>
<br>
start(Dir) -> spawn(afile_server, loop, [Dir]).<br>
<br>
loop(Dir) -><br>
receive<br>
{Client, list_dir} -><br>
Client ! {self(), file:list_dir(Dir)};<br>
{Client, {get_file, File}} -><br>
Full = filename:join(Dir, File),<br>
Client ! {self(), file:read_file(Full)};<br>
{Client, {put_file, File, Bytes}} -><br>
Full = filename:join(Dir, File),<br>
Client ! {self(), file:write_file(Full, Bytes)}<br>
end,<br>
loop(Dir).<br>
<br>
<br>
Testing in the Erlang shell, start() works, the "list_dir" command and <br>
reply work, and the "get_file" command and reply work, but the shell <br>
hangs when I attempt to receive a reply for the "put_file" command:<br>
<br>
2021-12-30 20:35:23 dpchrist@tinkywinky ~/sandbox/erlang<br>
$ erl<br>
Erlang/OTP 19 [erts-8.2.1] [source] [64-bit] [smp:8:8] <br>
[async-threads:10] [kernel-poll:false]<br>
<br>
Eshell V8.2.1 (abort with ^G)<br>
1> c(ex0204_server).<br>
{ok,ex0204_server}<br>
2> Server = ex0204_server:start(".").<br>
<0.64.0><br>
3> Server ! {self(), list_dir}.<br>
{<0.57.0>,list_dir}<br>
4> receive A -> A end.<br>
{<0.64.0>,<br>
{ok,[".ex0204_client.erl.swp","Makefile","afile_client.erl",<br>
"afile_server.beam","ex0204_server.erl","hello.beam",<br>
"ex0204_client.erl","hello.erl","afile_server.erl","CVS",<br>
"afile_server.run","ex0204_server.beam"]}}<br>
5> Server ! {self(), {get_file, "hello.erl"}}.<br>
{<0.57.0>,{get_file,"hello.erl"}}<br>
6> receive B -> B end.<br>
{<0.64.0>,<br>
{ok,<<"-module(hello).\n-export([start/0]).\n\nstart() ->\n <br>
io:format(\"hello, world!~n\").\n">>}}<br>
7> Server ! {self(), {put_file, "foo.txt", "foo on you!\n"}}.<br>
{<0.57.0>,{put_file,"foo.txt","foo on you!\n"}}<br>
8> receive C -> C end.<br>
<br>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded<br>
(v)ersion (k)ill (D)b-tables (d)istribution<br>
a<br>
<br>
<br>
Why?<br>
<br>
<br>
David<br>
</blockquote></div>