JavaJava Quick Tip: Check for Multicast Support

Java Quick Tip: Check for Multicast Support

There are times when a developer or programmer might need to know whether a network interface supports multicast or not. Using the Java java.net package, we can not only list all of the available network interfaces but also check their multicast support state. Here is how that looks in code:

*/

import java.net.*;
import java.util.*;

public class NetworkIntMulticastSupported {
	
	public static void main(String[] args) 
	{  
		NetworkIntMulticastSupported networkIntMulticastSupported = new NetworkIntMulticastSupported();
		networkIntMulticastSupported.proceed();
	}

	private void proceed()  
	{
		int networkInterfaceIndex = 0;
        try 
		{
            Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) 
			{
                NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
				boolean supportsMulticast = networkInterface.supportsMulticast();
				System.out.println(
						"Network interface " + ++networkInterfaceIndex + ": "+ networkInterface.getName() 
						+ " supports multicast: " + supportsMulticast 
						+ ", isVirtual: " + networkInterface.isVirtual());
            }
        }catch(SocketException socketException) 
		{
            socketException.getMessage();
        }
	}
}

/*

Running this Java code in your code editor or integrated development environment (IDE) results in the following output. Note, however, that your output may vary depending on your operating system configuration and settings:

[root@mypc]# java NetworkIntMulticastSupported
Network interface 1: lo supports multicast: true, isVirtual: false
Network interface 2: net0 supports multicast: true, isVirtual: false
Network interface 3: eth0 supports multicast: true, isVirtual: false
Network interface 4: eth1 supports multicast: true, isVirtual: false
Network interface 5: net1 supports multicast: true, isVirtual: false
Network interface 6: eth2 supports multicast: true, isVirtual: false
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories