Linux 加入多播组时是否需要端口或仅需要 IP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9423820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need a PORT when joining a multicast group or just the IP?
提问by chrisapotek
I would like to learn that once and for all. What is the procedure to connect a multicast socket? I know you have to bind to a local interface (do you need IP and port for that?) then I know you have to join a group (do you need IP:PORT for the address you want to join and the network interface again!!!??) and then finally you can leave the group.
我想一劳永逸地学习这一点。连接多播套接字的过程是什么?我知道您必须绑定到本地接口(您需要 IP 和端口吗?)然后我知道您必须加入一个组(您是否需要 IP:PORT 作为您要加入的地址和网络接口! !!??) 然后你终于可以离开小组了。
Can someone with experience clarify what is the whole of those many addresses? I will list below:
有经验的人可以解释一下这么多地址的全部内容吗?我将在下面列出:
- BindAddress (IP:PORT)
- NetworkAddress (IP:PORT)
- MulticastAddress (IP:PORT)
- 绑定地址 (IP:PORT)
- 网络地址 (IP:PORT)
- 多播地址 (IP:PORT)
Where and what is the multicast grouphere?
这里的组播组在哪里,什么是组播组?
采纳答案by user207421
A multicast group is a special IP address. You join it via setsockopt()
using the socket option IP_ADDMEMBERSHIP, or e.g. in Java via MulticastSocket.joinGroup()
. No port number here. If you want to join via a specific local address, use the overload that specifies a local address, or call setNetworkInterface()
first.
组播组是一个特殊的 IP 地址。您可以通过setsockopt()
使用套接字选项 IP_ADDMEMBERSHIP 或例如在 Java 中通过MulticastSocket.joinGroup()
. 这里没有端口号。如果要通过特定本地地址加入,请使用指定本地地址的重载,或setNetworkInterface()
先调用。
Binding to a local address is a separate operation, which primarily determines which local addresses the socket can send and receive data on: one, or all of them: either one local address, which determines which of your available subnets you are listening to and can send via, or a port, or both. Normally it is best to use INADDR_ANY as the bind-address, unless your application magically knows about the network topology.
绑定到本地地址是一项单独的操作,主要确定套接字可以在哪些本地地址上发送和接收数据: 一个或全部: 一个本地地址,这决定了您正在侦听哪些可用子网并且可以通过或端口发送,或两者兼而有之。通常最好使用 INADDR_ANY 作为绑定地址,除非您的应用程序神奇地了解网络拓扑。
This is muddied by the fact that you can bind to a multicast address in Linux, but this appears to be a misunderstanding that will now always be with us.
由于您可以在 Linux 中绑定到多播地址这一事实使这一点变得混乱,但这似乎是一种误解,现在将永远伴随着我们。
You send to a multicast group by sending to the multicast address.
您可以通过发送到多播地址来发送到多播组。
回答by ribram
Yes, you must define both the address and the port for sending/receiving multicast messages. These are UDP packets, so they require both address and port for the networking stack to be able to properly deliver the messages to participating processes. So to listen for a particular set of multicast messages, your application needs to bind to a particular multicast ip address and port combination (and obviously for a set or all interfaces on the machine). The group is defined by the address/port combination.
是的,您必须定义用于发送/接收多播消息的地址和端口。这些是 UDP 数据包,因此它们需要网络堆栈的地址和端口才能将消息正确传递到参与进程。因此,要侦听一组特定的多播消息,您的应用程序需要绑定到特定的多播 ip 地址和端口组合(显然是机器上的一组或所有接口)。该组由地址/端口组合定义。