<p>How do you use <code>gen_udp</code> in Erlang to do <a href="https://en.wikipedia.org/wiki/Multicast" rel="nofollow noreferrer">multicasting</a>? I know its in the code, there is just no documentation behind it. Sending out data is obvious and simple. I was wondering on how to add memberships. Not only adding memberships at start-up, but adding memberships while running would be useful too.</p>

[{'answer_id': 78886, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 0, 'selected': False, 'text': '<p><strong>Multicast is specified by IP Address</strong></p>\n\n<p>It\'s the same in erlang as for all languages.  The IP addresses 224.0.0.0 through 239.255.255.255 are multicast addresses.</p>\n\n<p>Pick an address in that range, check that you\'re not overlapping an already assigned address, and you are good to go.</p>\n\n<p><a href="http://www.iana.org/assignments/multicast-addresses" rel="nofollow noreferrer">http://www.iana.org/assignments/multicast-addresses</a></p>\n'}, {'answer_id': 81149, 'author': 'Bwooce', 'author_id': 15290, 'author_profile': 'https://Stackoverflow.com/users/15290', 'pm_score': 4, 'selected': False, 'text': '<p>Multicast sending has been answered, receipt requires subscription to the multicast group. </p>\n\n<p>It (still) seems undocumented, but has been covered on the erlang-questions mailing list before. <a href="http://www.erlang.org/pipermail/erlang-questions/2003-March/008071.html" rel="nofollow noreferrer">http://www.erlang.org/pipermail/erlang-questions/2003-March/008071.html</a></p>\n\n<pre><code>    {ok, Socket} = gen_udp:open(Port, [binary, {active, false},\n                                       {reuseaddr, true},{ip, Addr}, \n                                       {add_membership, {Addr, LAddr}}]).\n</code></pre>\n\n<p>where the <code>Addr</code> is the multicast group, and <code>LAddr</code> is a local interface. (code courtesy of mog)</p>\n\n<p>The same options used above can be passed to <a href="http://www.erlang.org/doc/man/inet.html#setopts-2" rel="nofollow noreferrer"><code>inet:setopts</code></a> including <code>{drop_membership, {Addr, LAddr}}</code> to stop listening to the group. </p>\n'}, {'answer_id': 1740065, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': '<p>Here is example code on how to listen in on Bonjour / Zeroconf traffic.</p>\n\n<pre><code>-module(zcclient).\n\n-export([open/2,start/0]).\n-export([stop/1,receiver/0]).\n\nopen(Addr,Port) -&gt;\n   {ok,S} = gen_udp:open(Port,[{reuseaddr,true}, {ip,Addr}, {multicast_ttl,4}, {multicast_loop,false}, binary]),\n   inet:setopts(S,[{add_membership,{Addr,{0,0,0,0}}}]),\n   S.\n\nclose(S) -&gt; gen_udp:close(S).\n\nstart() -&gt;\n   S=open({224,0,0,251},5353),\n   Pid=spawn(?MODULE,receiver,[]),\n   gen_udp:controlling_process(S,Pid),\n   {S,Pid}.\n\nstop({S,Pid}) -&gt;\n   close(S),\n   Pid ! stop.\n\nreceiver() -&gt;\n   receive\n       {udp, _Socket, IP, InPortNo, Packet} -&gt;\n           io:format("~n~nFrom: ~p~nPort: ~p~nData: ~p~n",[IP,InPortNo,inet_dns:decode(Packet)]),\n           receiver();\n       stop -&gt; true;\n       AnythingElse -&gt; io:format("RECEIVED: ~p~n",[AnythingElse]),\n           receiver()\n   end. \n</code></pre>\n'}, {'answer_id': 1880167, 'author': 'Matthias', 'author_id': 228713, 'author_profile': 'https://Stackoverflow.com/users/228713', 'pm_score': 2, 'selected': False, 'text': '<p>I try to get this example running on my PC. What could happen, if I get always the message {error,eaddrnotavail} by opening the receive socket?</p>\n\n<p>Example 1: This works:</p>\n\n<pre><code>{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?SERVER_IP},\n               {multicast_ttl,4}, {multicast_loop,false}, binary]),\n</code></pre>\n\n<p>Example 2: Getting an runtime Error:</p>\n\n<pre><code>{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?MULTICAST_IP},\n               {multicast_ttl,4}, {multicast_loop,false}, binary]),\n</code></pre>\n\n<p>% --> {error,eaddrnotavail}</p>\n\n<pre><code>-define(SERVER_IP, {10,31,123,123}). % The IP of the current computer\n-define(PORT, 5353).\n-define(MULTICAST_IP, {224,0,0,251}). \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78826', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10432/']