[erlang-questions] Beginner: Windows UDP multicast receive

Christoph Schüler <>
Thu Jun 26 09:20:11 CEST 2008


Scott Gregory schrieb:
> Help?!
>
> Scott
>
>  
> {add_membership,{{224,0,66,66},{192,168,6,49}}}]),
>         loop(Socket).
>
>
>   
Your Code is missing a Controlling-Process, you have to use 
'gen_udp:controlling_process/2' to get this work.
Also you should not use 'add_membership' in 'gen_udp:open', because you 
won't get any IGMP-Messages.

Try this:

%%% CODE-START
-module(udp_mcast).
-export([open/2,start/2]).
-export([stop/1,receiver/0]).

open(Ip,Port) ->
   Opts=[{reuseaddr,true},
   {multicast_loop,false},
   {multicast_if,{0,0,0,0}},
   {multicast_ttl,4}],
   {ok,S}=gen_udp:open(Port,Opts),
   inet:setopts(S,[{add_membership,{Ip,{0,0,0,0}}}]),
   S.

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

start(Ip,Port)    ->
   S=open(Ip,Port),
   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,Packet]),
           receiver();
       stop -> true;
       AnythingElse -> io:format("RECEIVED: ~p~n",[AnythingElse]),
           receiver()
   end.
%%% CODE-END

If there are any UPNP-Devices in your Network (e.g. DSL-Router), you may 
use the Module as following:
---
1> {S,Pid}=udp_mcast:start({239,255,255,250},1900).
{#Port<0.142>,<0.51.0>}


From: {192,168,0,1}
IP: 4747
Data: "NOTIFY * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nCACHE-CONTROL: 
max-age=1800\r\nLOCATION: http://192.168.0.1:80/desc.xml\r\nNT: 
upnp:rootdevice\r\nNTS: ssdp:alive\r\nSERVER: IGD-HTTP/1.1 UPnP/1.0 
UPnP-Device-Host/1.0\r\nUSN: 
uuid:00-13-46-52-83-7F-0100A8C00::upnp:rootdevice\r\n\r\n"
.
.
.
---
Note: Under Windows, disable the SSDP-Service and have some patience...


hope this helps!
Chris





More information about the erlang-questions mailing list