4

当一台服务器向多个客户端广播时,哪种实现更快/更有效:MulticastSocket 还是 DatagramSocket?

也请讨论解释,谢谢!

传递的消息涉及字符串和浮点数。

4

4 回答 4

6

The deciding factor is usually whether the clients are on the same, or otherwise multicast enabled/linked networks. In general, Multicast is going to be MUCH more efficient than any form of unicasting, however, multicasting is not reliable, and does not work across heterogeneous networks like the internet, where the operators tend to disable multicast traffic.

If the data needs be reliable, then you really do need to use TCP unicasting, or alternatively add some form of FEC to the multicast to impart a semblance of reliability to the data stream, and if the traffic needs to travel across the internet, then you MUST use unicast TCP or UDP.

Short version: If your data is small, needs to be reliable, traverses the internet or is sent infrequently, use unicast. If your data is large, delivered to a large number of clients, can tolerate some lossiness, and only traverses networks you control or which are multicast enabled, use multicast. Multicast is really a one trick pony, (unreliable data broadcast over a homogenous network) whereas unicast can do most anything, but with higher overhead.

Note: TCP beyond a certain amount of data loss ceases to be reliable as well, (causing disconnects) and the added traffic from unicasting can push that limit down as it multiplies the amount of data flow. FEC adds a relatively fixed overhead for even a very large number of clients, but there's a point where neither FEC nor unicasting help anymore, and you simply need to reengineer the network to achieve a workable solution.

于 2011-12-19T09:02:23.057 回答
2

如果您在多个子网上有客户端,多播是最佳选择。如果您只向一个子网络发送数据,广播的效率会稍微高一些。但是,通常使用多播,因为差异非常微弱。

数据包含什么并不重要。

您可能会发现,如果您需要可靠的传递,使用 TCP 更简单,在某些情况下甚至可以更快(因为路由器倾向于针对 TCP 进行优化)如果传递不需要可靠,则使用多播。

于 2011-12-19T08:44:26.223 回答
2

你的问题不清楚。如果您正在广播,无论您使用DatagramSocket还是MulticastSocket. 如果您询问多播是否比广播更有效,(a)答案是“是”,并且(b)您必须用于MulticastSocket接收多播;再次发送它们,您可以使用DatagramSocketor MulticastSocket,并且效率没有差异。

于 2011-12-19T09:03:27.503 回答
0

多播比数据报套接字更有效,但是,它也使用 UDP,因此不能保证数据包将被所有接收者接收。除非您在网络中有一个托管交换机,它会优先处理您的数据包,否则无论您的网络使用情况如何,您最有可能以任何随机顺序丢失数据包。

如果您的网络中需要侦听多播的设备数量有限,我建议对每个设备进行 TCP 单播,并使用某种网络服务发现来查找您的设备。

于 2016-09-22T13:17:14.067 回答