Java Reference
In-Depth Information
Make use of datagram multicasting using the DatagramChannel class. The Data-
gramChannel class enables more than one client to connect to a group and listen for
datagrams that have been broadcasted from a server. The following sets of code
demonstrate this technique using a client/server approach. The class demonstrates a
multicast client.
package org.java8recipes.chapter21.recipe21_4;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
public class MulticastClient {
public MulticastClient() {
}
public static void main(String[] args) {
try {
// Obtain Supported network Interface
NetworkInterface networkInterface = null;
java.util.Enumeration<NetworkInterface>
enumNI = NetworkInterface.getNetworkInterfaces();
java.util.Enumeration<InetAddress> enumIA;
NetworkInterface ni;
InetAddress ia;
ILOOP:
while (enumNI.hasMoreElements()) {
ni = enumNI.nextElement();
enumIA = ni.getInetAddresses();
while (enumIA.hasMoreElements()) {
ia = enumIA.nextElement();
Search WWH ::




Custom Search