Multicasting with NIO

A long standing issue for many developers is that the java.nio.channels package has lacked support for Internet Protocol (IP) multicasting. In the NIO.2 early review draft specification you will see that we have addressed this issue by adding multicast support to DatagramChannel. Here's a small example that opens a DatagramChannel, binds the channel's socket to a local port, sets the network interface for multicast datagrams sent via the channel, and then joins a multicast group on the same interface:

NetworkInterface interf = NetworkInterface.getByName("eth0");
InetAddress group = InetAddress.getByName("");

DatagramChannel dc =
    .setOption(SocketOption.SO_REUSEADDR, true)
    .bind(new InetSocketAddress(5000))
    .setOption(SocketOption.IP_MULTICAST_IF, interf);

MembershipKey key = dc.join(group, interf); 

Once the channel has joined the group then it reads or receives multicast datagrams in the same manner that it reads or receives unicast datagrams.

The most significant thing in this example is the static factory method to open the channel specifies a protocol family. In this example, the channel is to an IPv4 socket. In the existing APIs, the protocol family is transparent and all sockets created by the or java.nio.channels packages are either all IPv4 or all IPv6. For IP multicasting it is important that the protocol family corresponds to the address type of the multicast groups that the socket joins; otherwise it is highly operating system specific if the socket can join the group, configure options, or receive multicast datagrams. Legacy has suffered greatly from problems in this area. Another interesting thing to point out is that the channel's socket is bound and socket options are configured directly. The awkward and counterintuitive socket adaptor isn't required so it isn't necessary to mix socket APIs when configuring the channel's socket.

In the example, the MembershipKey that is returned by the join method is a token to represent membership of the group. It defines methods to query information about the membership and defines the "drop" method to drop membership of the group.

Developers tracking multicast standards will know that the RFCs in this area have been updated in recent years to add source filtering and this is now supported by almost all modern operating systems. In the NIO.2 draft specification we have included basic support for source filtering. The following code fragment shows a channel joining a multicast group to only receive multicast datagrams sent by a specific IP source address (otherwise known as "include-mode" filtering):

MembershipKey key = dc.join(group, interf, source);

"Exclusive-mode" filtering is where a group is joined to receive all multicast datagrams except those from specific IP source address:

MembershipKey key = dc.join(group, interf).block(source1).block(source2);

So that's a brief introduction to the multicast support that we propose to add to the java.nio.channels package. There's a lot more detail in the draft specification for those interested in this topic.


is this spec going to address AIO?

Posted by Denis Sandojsky on April 29, 2007 at 07:16 PM PDT #

Overall, NIO.2 sounds great. I might not be the one to use every detail, but I expect I'll use some of it, and I feel it's (one of the things) needed to make Java a serious contender for general development.

Posted by Tom Palmer on April 30, 2007 at 01:44 AM PDT #

Java already \*is\* an actual contender for, and basis of, many live production systems that I'd barely dare do in (say) C++ for security and/or threading reasons!

However, having these edge cases 'closer to the metal' dealt with in a portable and safe way is excellent, indeed will probably allow me to play with things such as multicasting that always felt too crufty to do before.

How well is multicasting handled in the IPv6 stack I wonder? B\^)



Posted by Damon Hart-Davis on April 30, 2007 at 01:58 AM PDT #

Denis, if you examine the spec you will see there is an asynchronous I/O API for both files and sockets.

Damon, for IPv6 the channel is created with the ProtocolFamily.INET6 family and then the channel can do IPv6 multicasting on any interface that has IPv6 configured.

Posted by Alan on May 01, 2007 at 02:52 AM PDT #

I am \*very\* interested in the MulticastChannel support. I see that the JSR now says this will be in Dolphin. After checking the latest Java 7 docs, I see some of the proposed API, but it doesn't quite have all the pieces in your example. Any ideas when this will be available in the Java 7 snapshots?

Posted by Greg Rabil on February 06, 2008 at 12:06 PM PST #


I'm writing a framework in which I would like to rely a lot on java.nio stuff, to support event based handling of data received from the network, where IP multicast is one of the possible ways of sending/receiving datas, so this support is \*very\* important for the architecture.

So I would like to know if eventually:
A) I can use some early preview of the IP multicast support in NIO.2 (or in a separate add-on library), or
B) there is some workaround I can do to manage to register an IP multicast datagram channel in NIO.1

Thanks a lot in advance,

Posted by Alessio Pace on March 24, 2008 at 07:22 PM PDT #

Post a Comment:
Comments are closed for this entry.



Top Tags
« July 2016

No bookmarks in folder