-module(tut).
-export([start_server/0]).
-export([start_client/0]).
%开启服务器%
start_server()-> spawn(fun() -> server_loop() end).
server_loop()->
receive
{update_task,Client,TaskData} ->
io:format("server receive: update_task -> TaskData:~p ~n",[TaskData]),
Client!{task_data,TaskData},
server_loop();
{stop} -> io:format("server stop")
end.
%开启客户端%
start_client() -> spawn(fun() -> client_loop() end).
client_loop() ->
receive
{task_data,TaskData} -> io:format("client receive: task_data -> TaskData:~p ~n", [TaskData]),
client_loop()
end.
%死亡处理%
deal_with(Request) -> io:format("deal_with ~p~n", [Request]).
(emacs@b51997ad18ca401)2> Svr = tut:start_server().
<0.45.0>
(emacs@b51997ad18ca401)3> Clt = tut:start_client().
<0.47.0>
(emacs@b51997ad18ca401)4> Svr!{update_task,Clt,"Hello Erlang Server"}.
server receive: update_task -> TaskData:"Hello Erlang Server"
client receive: task_data -> TaskData:"Hello Erlang Server"
{update_task,<0.47.0>,"Hello Erlang Server"}
(emacs@b51997ad18ca401)5> Svr!{stop}.
server stop{stop}
(emacs@b51997ad18ca401)6> Svr!{stop}.
{stop}
(emacs@b51997ad18ca401)7> Svr!{stop}.
{stop}
(emacs@b51997ad18ca401)8>