-module(echo2). -export([start/0]). echo() -> receive {From, Msg} -> timer:sleep(random:uniform(100)), From ! {self(), Msg}, echo(); stop -> true end. start() -> PidB = spawn(fun echo/0), PidC = spawn(fun echo/0), % sending tokens Token = 42, PidB ! {self(), Token}, io:format("Sent ~w~n",[Token]), Token2 = 7, PidC ! {self(), Token2}, io:format("Sent ~w~n",[Token2]), % receive messages receive {PidB, Msg} -> io:format("Received from B: ~w~n", [Msg]) ; {PidC, Msg} -> io:format("Received from C: ~w~n", [Msg]) end, receive {PidB, Msg2} -> io:format("Received from B: ~w~n", [Msg2]) ; {PidC, Msg2} -> io:format("Received from C: ~w~n", [Msg2]) end, % stop echo-servers PidB ! stop, PidC ! stop.