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

Oliver Korpilla Oliver.Korpilla@REDACTED
Mon Apr 11 09:51:12 CEST 2016


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}).

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}]),
    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



More information about the erlang-questions mailing list