<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr">Underscore (_) or variable (Any) works, but in this case the receiver needs to know the sender’s address to send a reply.</div><div dir="ltr"><br></div><div dir="ltr">We have to include the address of the sender in the message or register it with a name.</div><div dir="ltr"><br></div><div dir="ltr">Cheers,</div><div dir="ltr">Yao</div><div dir="ltr"><br><blockquote type="cite">在 2022年1月1日,13:36,Nalin Ranjan <ranjanified@gmail.com> 写道:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="auto">How about using an underscore(_) inside the receive ??<br><br><div data-smartmail="gmail_signature">नमस्ते।<br>नलिन रंजन</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jan 1, 2022, 8:24 AM Yao Bao <<a href="mailto:free7by@163.com">free7by@163.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
> Now that we have figured out the bug -- the shell sent a command to the server for which there was no matching receive pattern -- it begs the question: how do I detect when a process is sent a message for which there is no matching receive pattern?<br>
<br>
Perhaps we cannot put any guarantee on this.<br>
<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>
> {Client, U} -><br>
> Client ! {self(), {error, "Unknown command", U}};<br>
> {Client, U, A} -><br>
> Client ! {self(), {error, "Unknown command", U, A}};<br>
> {Client, U, A, B} -><br>
> Client ! {self(), {error, "Unknown command", U, A, B}}<br>
> end,<br>
<br>
We cannot enumerate all kinds of patterns in the limited receive expression,<br>
but we can solve this problem via properly designed message pattern.<br>
<br>
For the receiver, we can say, it can receive one kinds of message,<br>
which is: {Client, Command}, then the sender should send messages matching<br>
this pattern, or, the server has no responsibility for replying anything.<br>
<br>
With this design, the receive expression can be written as:<br>
<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>
> {Client, Command} -><br>
> Client ! {self(), {error, "Unknown command", Command}}<br>
> end,<br>
<br>
The sender can send such messages:<br>
<br>
> Server ! {self(), foo}<br>
<br>
> Server ! {self(), {foo, bar}}.<br>
> Server ! {self(), {foo, bar, baz}}.<br>
<br>
After sending, the sender say:<br>
<br>
receive C -> C end<br>
<br>
And pattern C can match those messages happily.<br>
<br>
By the way, in my understanding, in the worst case, message passing<br>
is reliable but no guarantee, so the receiver might consider that worst case and<br>
handle it properly (add timeout mechanism is an example).<br>
<br>
Cheers,<br>
Yao<br>
<br>
</blockquote></div>
</div></blockquote></body></html>