The code snippet below is from Schildt (Java full guide, p. 680).

import java.net.*; import java.util.Objects; class InetAddressTest { public static void main(String args[]) throws UnknownHostException { InetAddress Address = InetAddress.getLocalHost(); System.out.println(Address); Address = InetAddress.getByName("www.lurkmore.to"); System.out.println(Address); InetAddress SW[] = InetAddress.getAllByName("vedomosti.ru"); for (int i=0; i<SW.length; i ++) System.out.println(SW[i]); } } 

There is a boolean isMulticastAddress() method in the InetAddress class. It returns true if the address is a group address.

enter image description here

Colleagues, please tell me, how can I write this method into the source code to check whether the Internet address is a group address? And then I enter this way and that, and it does not work out.

    1 answer 1

    Maybe so:

     import java.net.*; import java.util.Objects; public class InetAddressTest { public static void main(String args[]) throws UnknownHostException { InetAddress Address = InetAddress.getLocalHost(); Address = InetAddress.getByName("www.lurkmore.to"); checkInetAddress(Address); InetAddress SW[] = InetAddress.getAllByName("vedomosti.ru"); for (int i = 0; i < SW.length; i++) checkInetAddress(SW[i]); } public static void checkInetAddress(InetAddress address){ if(address.isMulticastAddress()){ System.out.println("Address :" + address + "is Multicast"); }else{ System.out.println("Address :" + address + " not Multicast"); } } } 
    • Very similar to the truth. Thank. - Andrew Kachalin