[erlang-questions] UDP Multicast - how does it work?

Alexandre Snarskii snar@REDACTED
Mon Apr 11 11:38:58 CEST 2016


On Mon, Apr 11, 2016 at 09:51:12AM +0200, Oliver Korpilla wrote:
> Hello.
> 
> I'm lost on the basics here. Sorry if this seems like a fishing expedition.
> 
> What I want to do:
> 
> Step 1) Reach several processes on the same machine with one send.
> Step 2) Reach several processes on possibly different machines with one send.
> 
> I've tried the following:
> 
> -module(test_multicast).
> -export([start/0, receiver/1]).
> -define(MULTICAST_ADDRESS, {127,0,10,1}).

127.0.10.1 is not a valid multicast address. Multicast addresses are
in the range 224.0.0.0 - 239.255.255.255. 
More than that, rfc1122 section 3.2.1.3 clearly says that 127.x.x.x is an 
Internal host loopback address.  Addresses of this form MUST NOT appear 
outside a host. 
and so no OS will accept packets addresses from/to 127.x.x.x on ethernet
interfaces.

> 
> receiver(Id) ->
>     io:format("Started multicast receiver ~p~n", [Id]),
>     {ok, Socket} = gen_udp:open(3333, [binary, {active, true}, {reuseaddr, true}, {multicast_if, ?MULTICAST_ADDRESS}, {multicast_loop, true}]),

In order to receive multicast packets you must explicitly join this socket
to a multicast group using

                {add_membership, {MultiAddress, InterfaceAddress}}:
                  Join a multicast group.

Example of working multicast between two servers in the same subnet:

1> {ok, Sock} = gen_udp:open(3333, [binary, {active, true}, {add_membership,     {{239,0,10,1}, {0,0,0,0}}}]).
{ok,#Port<0.481>}
2> gen_udp:send(Sock, {239,0,10,1}, 3333, <<"hello, world">>).
ok
3> flush().
Shell got {udp,#Port<0.481>,{xx,xxx,xxx,2},3333,<<"hello, world">>}
ok

on another machine:

1> {ok, Sock} = gen_udp:open(3333, [binary, {active, true}, {add_membership,     {{239,0,10,1}, {0,0,0,0}}}]).
{ok,#Port<0.568>}
2> flush().
Shell got {udp,#Port<0.568>,{xx,xxx,xxx,2},3333,<<"hello, world">>}
ok
3> 


>     receiver_loop(Id, Socket).
> 
> receiver_loop(Id, Socket) ->
>     receive
>         {udp, _RemoteSocket, _RemoteHost, _RemotePort, Payload } ->
>             io:format("Receiver ~p received message ~p~n", [Id, Payload]),
>             receiver_loop(Id, Socket)
>     end.
> 
> send() ->
>     io:format("Sending...~n"),
>     {ok, Socket} = gen_udp:open(0),
>     ok = gen_udp:send(Socket, ?MULTICAST_ADDRESS, 3333, << "Hello, World." >>).
> 
> start() ->
>     spawn_link(?MODULE, receiver, [1]),
>     spawn_link(?MODULE, receiver, [2]),
> 
>     timer:sleep(100),
>     send().
> 
> But the only one to receive the message is receiver #2. It's as if it takes the port away from receiver #1... I thought reuseaddr was meant to bind several sockets to the same port?
> 
> Thank you for any advice,
> Oliver
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list