tango.net.device.Multicast

License:

BSD style: see license.txt

Version:

Jun 2004 : Initial release

Version:

Dec 2006 : South pacific version

Author:

Kris
class Multicast : Datagram
MulticastConduit sends and receives data on a multicast group, as described by a class-D address. To send data, the recipient group should be handed to the write() method. To receive, the socket is bound to an available local adapter/port as a listener and must join() the group before it becomes eligible for input from there.
While MulticastConduit is a flavour of datagram, it doesn't support being connected to a specific endpoint.

Sending and receiving via a multicast group:
1
2
3
4
5
6
7
8
9
10
11
auto group = new InternetAddress ("225.0.0.10", 8080);

// listen for datagrams on the group address (via port 8080)
auto multi = new MulticastConduit (group);

// join and broadcast to the group
multi.join.write ("hello", group);

// we are listening also ...
char[8] tmp;
auto bytes = multi.read (tmp);

Note that this example is expecting to receive its own broadcast; thus it may be necessary to enable loopback operation (see below) for successful receipt of the broadcast.

Note that class D addresses range from 225.0.0.0 to 239.255.255.255

see:

http://www.kohala.com/start/mcast.api.txt
this()
Create a writable multicast socket
this(InternetAddress group, bool reuse = false)
Create a read/write multicast socket
This flavour is necessary only for a multicast receiver (e.g. use this ctor in conjunction with SocketListener).

You should specify both a group address and a port to listen upon. The resultant socket will be bound to the specified port (locally), and listening on the class-D address. Expect this to fail without a network adapter present, as bind() will not find anything to work with.

The reuse parameter dictates how to behave when the port is already in use. Default behaviour is to throw an IO exception, and the alternate is to force usage. To become eligible for incoming group datagrams, you must also invoke the join() method
Multicast loopback(bool yes = true)
Enable/disable the receipt of multicast packets sent from the same adapter. The default state is OS specific
Multicast ttl(uint value = Subnet)
Set the number of hops (time to live) of this socket. Convenient values are
1
2
3
4
5
6
Host:           packets are restricted to the same host
Subnet:         packets are restricted to the same subnet
Site:           packets are restricted to the same site
Region:         packets are restricted to the same region
Continent:      packets are restricted to the same continent
Unrestricted:   packets are unrestricted in scope
Multicast join()
Add this socket to the listening group
Multicast leave()
Remove this socket from the listening group