Search code examples
javanetwork-programmingbroadcasting

MulticastSocket vs. DatagramSocket in Broadcasting to Multiple Clients


Which would be a faster/more efficient implementation when one server is broadcasting to multiple clients: MulticastSocket or DatagramSocket?

Please discuss an explanation too, thank you!

The messages passed involve strings and floating point numbers.


Solution

  • 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.