September 2013
Intermediate to advanced
548 pages
12h 25m
English
Finally we’ll see how to set up a broadcast channel. This code is simple.
| broadcast.erl | |
| | -module(broadcast). |
| | -compile(export_all). |
| | |
| | send(IoList) -> |
| | case inet:ifget("eth0", [broadaddr]) of |
| | {ok, [{broadaddr, Ip}]} -> |
| | {ok, S} = gen_udp:open(5010, [{broadcast, true}]), |
| | gen_udp:send(S, Ip, 6000, IoList), |
| | gen_udp:close(S); |
| | _ -> |
| | io:format("Bad interface name, or\n" |
| | "broadcasting not supported\n") |
| | end. |
| | |
| | listen() -> |
| | {ok, _} = gen_udp:open(6000), |
| | loop(). |
| | loop() -> |
| | receive |
| | Any -> |
| | io:format("received:~p~n", [Any]), |
| | loop() |
| | end. |
Here we need two ports, one to send the broadcast and the other to listen for answers. We’ve chosen port 5010 to send the broadcast request ...
Read now
Unlock full access