Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Monday Aug 6th 2018 by M S Sridhar
Article Image

Language: Java, Expertise: Intermediate - Use this Java API to figure out whether or not an IP address is a multicast address.

You may want to know whether an IP address is a multicast address or not in order to make a decision on how to proceed with it. Java supports this with the help of an API called isMulticastAddress().

import java.net.*;

public class MulticastAddress 
   public static void main(String args[])
      MulticastAddress multicastAddress = new MulticastAddress();

   private void proceed() 
      InetAddress addr = null;
      String ipAddress = "";

         //You can customize this by passing an argument and check for values in runtime
         addr = InetAddress.getByName(ipAddress);
      }catch(UnknownHostException uhe)
         System.out.println("UnknownHostException: " + uhe);
      //This method identifies if the given ipaddress is multicast address or not
      if (addr.isMulticastAddress()) {
         System.out.println(addr + " is multicast address.");
         System.out.println(addr + " is not multicast address.");


Expected output:

[root@mypc]# java MulticastAddress
/ is multicast address.

Mobile Site | Full Site