如何在 Erlang 中使用 gen_udp 进行多播?
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是有关如何侦听 Bonjour / Zeroconf 流量的示例代码。
Here is example code on how to listen in on Bonjour / Zeroconf traffic.
组播发送已应答,接收需要订阅组播组。
它(仍然)似乎没有记录,但之前已经在 erlang-questions 邮件列表中介绍过。 http://www.erlang.org/pipermail/erlang- questions/2003-March/008071.html
其中
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
where the
Addr
is the multicast group, andLAddr
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.我尝试在我的电脑上运行这个示例。 如果我通过打开接收套接字总是收到消息 {error,eaddrnotavail} ,会发生什么?
示例 1:这有效:
示例 2:获取运行时错误:
% --> {错误,eaddrnotavail}
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:
Example 2: Getting an runtime Error:
% --> {error,eaddrnotavail}
多播由 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