Multicast UDP sending question

Jarrod Roberson <>
Tue Nov 17 03:35:24 CET 2009


I have gotten a good start on my program, my first REAL Erlang program.
I have it listening for messages, reading them and parsing them. I also have
it sending them.
The one little thing that is bothering me is I can't SEND on Port 5353, I
have tried everything.
All the other applications on my machine and listen AND send on port 5353,
SubEthaEdit, iTunes, iChat.
The all report Port: 5353 when sending multicast messages.
I really want my application to play nice and do the same thing, send on
Port 5353.
Here is my module as it stands now.

-module(zeroconf).

-include("zeroconf.hrl").

-export([open/0,start/0]).
-export([stop/1,receiver/0]).
-export([send/1]).

-define(ADDR, {224,0,0,251}).
-define(PORT, 5353).

send(Domain) ->
    {ok,S} = gen_udp:open(0,[{broadcast,true}]), % I really want this Port
to be 5353 :-(
    %{ok,S} = gen_udp:open(?PORT,[{reuseaddr,true}, {ip,?ADDR},
{broadcast,true}, {multicast_ttl,4}, {multicast_loop,false}, binary]), %
this doesn't complain or throw errors but it also doesn't work :-(
    P =
#dns_rec{header=#dns_header{},qdlist=[#dns_query{domain=Domain,type=ptr,class=in}]},
    gen_udp:send(S,?ADDR,?PORT,inet_dns:encode(P)),
    gen_udp:close(S).

open() ->
   {ok,S} = gen_udp:open(?PORT,[{reuseaddr,true}, {ip,?ADDR},
{multicast_ttl,4}, {multicast_loop,false}, binary]),
   inet:setopts(S,[{add_membership,{?ADDR,{0,0,0,0}}}]),
   S.

close(S) -> gen_udp:close(S).

start() ->
   S=open(),
   Pid=spawn(?MODULE,receiver,[]),
   gen_udp:controlling_process(S,Pid),
   {S,Pid}.

stop({S,Pid}) ->
   close(S),
   Pid ! stop.

receiver() ->
   receive
       {udp, _Socket, IP, InPortNo, Packet} ->
           io:format("~n~nFrom: ~p~nPort: ~p~nData:
~p~n",[IP,InPortNo,inet_dns:decode(Packet)]),
           receiver();
       stop -> true;
       AnythingElse -> io:format("RECEIVED: ~p~n",[AnythingElse]),
           receiver()
   end.

Here is what some output looks like.

This is a QUERY from SubEthaEdit looking for other instances on the local
network, notice that it says Port: 5353
From: {192,168,0,105}
Port: 5353
Data:
{ok,{dns_rec,{dns_header,0,true,'query',true,false,false,false,false,0},
                   [],
                   [{dns_rr,"_see._tcp.local",ptr,in,0,0,
                            "",undefined,[],
                            false}],
                   [],[]}}

Now here is a QUERY from my module looking for instances of iTunes on the
local network, notice it says Port: 59795
With the code the way it is now, that port is random. I really want it to be
5353.

From: {192,168,0,105}
Port: 59795
Data:
{ok,{dns_rec,{dns_header,0,false,'query',false,false,false,false,false,
                               0},
                   [{dns_query,"_daap._tcp.local",ptr,in}],
                   [],[],[]}}

Does anyone have any arcane insight in to UDP multicast at all?


-- 
Jarrod Roberson
678.551.2852


More information about the erlang-questions mailing list