简体   繁体   English

UDP多播:我如何知道要发送到哪个组?

[英]UDP multicasting: how do I know which group to send to?

I've got a little program that needs to communicate between two computers on the same LAN. 我有一个小程序,需要在同一LAN上的两台计算机之间进行通信。 I'm rather new to networking, but from what I've read it sounds like I want UDP multicasting so that the two computers can discover each other, after which I can establish a TCP connection for actual data communication. 我对网络还很陌生,但是从我读到的内容来看,这听起来像是我想要UDP多播,以便两台计算机可以互相发现,然后我可以为实际的数据通信建立TCP连接。 I've found this little example for UDP multicasting which I can follow. 我已经找到了这个有关UDP多播的小示例,可以遵循。 However, I'm wondering about the multicast group ( HELLO_GROUP in that example, which is 225.0.0.37 ). 但是,我想知道多播组(在该示例中为HELLO_GROUP ,即225.0.0.37 )。

How can I know the group I should use? 我怎么知道我应该使用的小组? This program will be running on various networks, so I can't hard code one (as far as I know). 该程序将在各种网络上运行,因此我无法对其中的一个进行硬编码(据我所知)。 Do I get the group from the router, and if so, how do I do that? 我要从路由器获取组吗?如果是,该怎么办?

You can choose any multicast address (224.0.0.0 to 239.255.255.255) that isn't listed as reserved by IANA. 您可以选择未列为 IANA 保留的任何多播地址(224.0.0.0至239.255.255.255)。

Its possible (if unlikely) that another program will also be using the same address. 另一个程序也可能使用相同的地址(如果不太可能)。 You can minimise the chances of this causing any confusion by making the announcement messages your program sends out suitably specific. 您可以通过适当地使程序发送的公告消息最小化,从而将引起混乱的可能性降到最低。 eg 例如

CORNSTALKS-DISCOVERY
HOST: {address:port}
[newline]

This would inform your recipients of the address to use for their TCP connection but should find its first line rejected by any other recipients. 这将通知您的接收者要用于其TCP连接的地址,但应该找到其第一行被任何其他接收者拒绝。

You understood wrong. 你理解错了

What are you talking about is broadcasting . 你说的是广播 A broadcast UDP datagram is sent to every computer in the subnet. 广播UDP数据报发送到子网中的每台计算机。 (Technically you send a datagram to the address 255.255.255.255.) (技术上,您将数据报发送到地址255.255.255.255。)

UDP broadcast work inside a specific subnet, but don't cross the subnet boundaties. UDP广播在特定子网内工作,但不要跨越子网边界。 That is, most of the routers are configured not to route broadcast datagrams (reduce spamming). 也就是说,大多数路由器配置为不路由广播数据报(减少垃圾邮件)。

OTOH multicast is something completely different. OTOH多播完全不同。 The purpose of multicast is to avoid using TCP (or any other unicast) for data transmission. 多播的目的是避免使用TCP(或任何其他单播)进行数据传输。 It's good when you need to send something to many other recipients "at once". 当您需要“一次”向其他许多收件人发送邮件时,这是很好的。 Those machine agree preliminary on a specific multicast address (like 225.0.0.37 in your example), and "join" this multicast group. 这些机器在特定的多播地址(例如您的示例中为225.0.0.37)上初步达成一致,然后“加入”该多播组。 Within a specific subnet everything works pretty similar to broadcast, however in contrast to broadcast the multicast may also cross the subnet boundaries. 在特定子网内,一切工作都与广播非常相似,但是与广播相反,多播也可能跨越子网边界。 This is due to the fact that when machines join a multicast group the appropriate routers are notified, and they are capable to route multicast datagrams appropriately. 这是由于以下事实:当计算机加入多播组时,会通知相应的路由器,并且它们能够适当地路由多播数据报。

EDIT: 编辑:

Conclusion (for clarification). 结论(为澄清)。

  1. In order to use a multicast one has to pick a multicast address. 为了使用多播,必须选择一个多播地址。 This is like choosing a port for the application. 这就像为应用程序选择端口。
  2. The main purpose of multicast is to deliver content (transmit data) to a number of recipients. 多播的主要目的是将内容(传输数据)传递给许多接收者。 It's more efficient than unicast in this case. 在这种情况下,它比单播效率更高。
  3. A "network discovery" is usually done via broadcast. 通常通过广播来进行“网络发现”。 A multicast can theoretically be used for this as well, but this is like killing a fly with a cannon (because routers should also track the lifetime of the multicast session). 从理论上讲,也可以使用多播,但这就像用大炮杀死苍蝇(因为路由器也应该跟踪多播会话的生存期)。

I would suggest you don't use multicast directly. 我建议您不要直接使用多播。

Rather, use zero-configuration networking . 而是使用零配置网络 This, in its mDNS/ DNS-SD incarnation, is available through Apple's Bonjour library on OS X and Windows, and Avahi on unices (and possibly on OS X and Windows too, not sure). 它以mDNS / DNS-SD的形式出现,可通过OS X和Windows上的Apple Bonjour库以及在unices上的Avahi (以及不确定的OS X和Windows上)获得。

With DNS-SD, you define a name for your service, then use the library to advertise its availability on a given host, or to browse for hosts where it's available. 使用DNS-SD,您可以为服务定义一个名称,然后使用该库在给定的主机上公布其可用性,或者浏览可用的主机。 This is how Macs discover printers, file shares, etc - exactly your use case, i believe. Mac就是通过这种方式发现打印机,文件共享等的-我相信这正是您的用例。 It's a simple but very effective technology. 这是一种简单但非常有效的技术。 And it's an open standard with a good open source implementation, so it's not some proprietary Apple scarytime. 这是一个具有良好开源实现的开放标准,因此这并不是苹果公司的专有技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM