如何在Erlang中加入源特定的组播

时间:2014-07-15 12:05:04

标签: erlang multicast

为了使用igmp v3运行代码并在Erlang中加入源特定的多播,我应该更改或添加到代码中

-module(udp_bin).

-compile([export_all]).

start([Host,Port]) ->
    {ok,IpAddress} = inet:parse_address(Host),
    {ok,Socket} = gen_udp:open(erlang:list_to_integer(Port),
        [
            inet,
            binary,
            {active,true},
            {reuseaddr,true},
            {multicast_ttl, 30},
            {add_membership,{IpAddress,{0,0,0,0}}}
        ]),
    io:format("Socket ~p:~n", [Socket]),
    Pid = spawn(fun() -> loop(Socket) end),
    io:format("Pid :~p~n",[Pid]),            
    ok = gen_udp:controlling_process(Socket,Pid).


loop(Socket) ->
    receive
        {udp,_Socket,_SrcAddr,_Port,Bin} ->
            io:format("Bin ~p:~n", [Bin]),           
            loop(Socket);
        Msg ->
            io:format("Msg ~p:~n", [Msg])
    end.

1 个答案:

答案 0 :(得分:0)

-module(udp_bin_ssm).

-compile([export_all])。

start([Source,Group,Port]) ->
{ok,SourceAddress} = inet:parse_address(Source),
{ok,GroupAddress} = inet:parse_address(Group),
LocalIp = ip_to_binary({0,0,0,0}),
GroupIp = ip_to_binary(GroupAddress),
SourceIp = ip_to_binary(SourceAddress),
Bin = << GroupIp/binary,LocalIp/binary,SourceIp/binary >>,
{ok,Socket} = gen_udp:open(erlang:list_to_integer(Port),
    [
        inet,
        binary,
        {active,true},
        {reuseaddr,true},
        {multicast_ttl, 30},
    {raw, 0, 39, Bin}

    ]),
io:format("Socket ~p:~n", [Socket]),
Pid = spawn(fun() -> loop(Socket) end),
io:format("Pid :~p~n",[Pid]),            
ok = gen_udp:controlling_process(Socket,Pid).

ip_to_binary(Ip) ->
list_to_binary(tuple_to_list(Ip)).

loop(Socket) ->
receive
    {udp,_Socket,_SrcAddr,_Port,Bin} ->
        io:format("Bin ~p:~n", [Bin]),          
        loop(Socket);
    Msg ->
        io:format("Msg ~p:~n", [Msg])
end.