使用 INADDR_ANY 的目的和结果是什么? [英] What is the purpose and result of using INADDR_ANY?

查看:63
本文介绍了使用 INADDR_ANY 的目的和结果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,当我们想要制作一个侦听多播流量的脚本时,我们将套接字的 IP_ADD_MEMBERSHIP 选项设置为一个由多播组地址和本地接口地址组成的值它将监听群组的流量.

In Python when we want to make a script that listens for multicast traffic we set the IP_ADD_MEMBERSHIP option of the socket with a value that consists of the multicast group address and the address of a local interface on which it will listen for the group's traffic.

Internet 上的许多示例将 INADDR_ANY 通配符地址传递给 IP_ADD_MEMBERSHIP 作为本地接口,并且一些 声明这将使在所有接口上侦听多播数据包的套接字.然而,Linux ip(7) 手册页指出,当使用 INADDR_ANY

Many examples on the Internet pass to IP_ADD_MEMBERSHIP the INADDR_ANY wildcard address as the local interface, and some of them state that this will make the socket to listen on all interfaces for multicast packets. However the Linux ip(7) man page states that when using INADDR_ANY

系统选择合适的接口"

并且 freebsd 手册页说它将选择默认界面".

and the freebsd man page says that it will choose the "default interface".

所以要么网上的一些答案是错误的,要么我在这里遗漏了一些东西.我认为 INADDR_ANY 在用作 IP_ADD_MEMBERSHIP 中的参数时会与 INADDR_ANY 混淆,而在 bind() 中用作参数时会与 INADDR_ANY 混淆 (由空字符串表示)但我不太确定.有人可以澄清在 IP_ADD_MEMBERSHIP 中使用时 INADDR_ANY0.0.0.0 发生了什么(即它选择默认接口或所有接口)和与 bind 一起使用时是否表现不同?

So either some answers online are wrong, or there's something that I'm missing here. I believe there's a confusion with INADDR_ANY when used as a parameter in IP_ADD_MEMBERSHIP, and INADDR_ANY when used as a parameter in bind() (represented by an empty string) but I'm not really sure. Can someone please clarify what is happening with INADDR_ANY or 0.0.0.0 when used in IP_ADD_MEMBERSHIP (i.e. it chooses the default interface or all interfaces) and if it behaves differently when used with bind?

推荐答案

INADDR_ANY 被指定为 bind 调用中的地址时,这会导致套接字监听任何网络接口的给定端口.

When INADDR_ANY is given as the address in a bind call, this causes the socket to listen on the given port for any network interface.

以这种方式调用 bind 后,您将在 netstat 命令的输出中看到这样的条目:

After calling bind in this way, you'll see an entry like this in the output of the netstat command:

udp        0      0 0.0.0.0:46162           0.0.0.0:*  

这是一个绑定到 INADDR_ANY 端口 46162 的 UDP 套接字.

This is a UDP socket that was bound to INADDR_ANY with port 46162.

当设置IP_ADD_MEMBERSHIP选项时用作接口地址时,INADDR_ANY表示操作系统将选择一个接口加入给定的多播组,即默认"" 网络接口.

When used as the interface address when setting the IP_ADD_MEMBERSHIP option, INADDR_ANY indicates that the OS will chose an interface to join the given multicast group on, i.e. the "default" network interface.

意味着它将在所有界面上加入该组.为此,您需要遍历所有网络接口并在每个网络接口上设置 IP_ADD_MEMBERSHIP 选项.

It does not mean that it will join the group on all interfaces. To do that, you would need to itereate over all network interfaces and set the IP_ADD_MEMBERSHIP option on each one.

我使用过 Linux、Solaris、FreeBSD 和 Windows,当使用 INADDR_ANY 时,它们都没有加入所有接口上的多播组.

I've worked with Linux, Solaris, FreeBSD, and Windows, and none of them joins a multicast group on all interfaces when using INADDR_ANY.

这篇关于使用 INADDR_ANY 的目的和结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆