如何在 Erlang 中使用 gen_udp 进行多播?

发布于 2024-07-04 19:39:43 字数 211 浏览 10 评论 0原文

如何在 Erlang 中使用 gen_udp 进行多播? 我知道它在代码中,只是后面没有文档。 发送数据是显而易见且简单的。 我想知道如何添加会员资格。 不仅在启动时添加成员资格,而且在运行时添加成员资格也很有用。

How do you use gen_udp in Erlang to do multicasting? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箜明 2024-07-11 19:39:43

以下是有关如何侦听 Bonjour / Zeroconf 流量的示例代码。

-module(zcclient).

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

open(Addr,Port) ->
   {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({224,0,0,251},5353),
   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 example code on how to listen in on Bonjour / Zeroconf traffic.

-module(zcclient).

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

open(Addr,Port) ->
   {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({224,0,0,251},5353),
   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. 
何必那么矫情 2024-07-11 19:39:43

组播发送已应答,接收需要订阅组播组。

它(仍然)似乎没有记录,但之前已经在 erlang-questions 邮件列表中介绍过。 http://www.erlang.org/pipermail/erlang- questions/2003-March/008071.html

    {ok, Socket} = gen_udp:open(Port, [binary, {active, false},
                                       {reuseaddr, true},{ip, Addr}, 
                                       {add_membership, {Addr, LAddr}}]).

其中 Addr 是多播组,LAddr 是本地接口。 (代码由 mog 提供)

上面使用的相同选项可以传递到 < code>inet:setopts 包括 {drop_membership, {Addr, LAddr}} 停止监听该组。

Multicast sending has been answered, receipt requires subscription to the multicast group.

It (still) seems undocumented, but has been covered on the erlang-questions mailing list before. http://www.erlang.org/pipermail/erlang-questions/2003-March/008071.html

    {ok, Socket} = gen_udp:open(Port, [binary, {active, false},
                                       {reuseaddr, true},{ip, Addr}, 
                                       {add_membership, {Addr, LAddr}}]).

where the Addr is the multicast group, and LAddr is a local interface. (code courtesy of mog)

The same options used above can be passed to inet:setopts including {drop_membership, {Addr, LAddr}} to stop listening to the group.

筱果果 2024-07-11 19:39:43

我尝试在我的电脑上运行这个示例。 如果我通过打开接收套接字总是收到消息 {error,eaddrnotavail} ,会发生什么?

示例 1:这有效:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?SERVER_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

示例 2:获取运行时错误:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?MULTICAST_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

% --> {错误,eaddrnotavail}

-define(SERVER_IP, {10,31,123,123}). % The IP of the current computer
-define(PORT, 5353).
-define(MULTICAST_IP, {224,0,0,251}). 

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?

Example 1: This works:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?SERVER_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

Example 2: Getting an runtime Error:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?MULTICAST_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

% --> {error,eaddrnotavail}

-define(SERVER_IP, {10,31,123,123}). % The IP of the current computer
-define(PORT, 5353).
-define(MULTICAST_IP, {224,0,0,251}). 
﹏雨一样淡蓝的深情 2024-07-11 19:39:43

多播由 IP 地址指定

这在 erlang 中与所有语言都是相同的。 IP 地址 224.0.0.0 到 239.255.255.255 是多播地址。

选择该范围内的地址,检查是否与已分配的地址重叠,然后就可以开始了。

http://www.iana.org/assignments/multicast-addresses

Multicast is specified by IP Address

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.

Pick an address in that range, check that you're not overlapping an already assigned address, and you are good to go.

http://www.iana.org/assignments/multicast-addresses

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文