SoFunction
Updated on 2025-03-08

Methods to determine special IP addresses using InetAddress class in Java programming

InetAddress class

The InetAddress class is used to encapsulate the digital IP address and the domain name of that address we discussed earlier.

You interact with this class through an IP hostname, which is easier and easier to understand than its IP address.

The address number is hidden inside the InetAddress class.

 
Factory method in InetAddress class
The InetAddress class does not have an obvious constructor. To generate an InetAddress object, an available factory method must be used.

A factory method is just a convention for a static method in a class to return an instance of that class.

For InetAddress, three methods: getLocalHost(), getByName() and getAllByName() can be used to create an instance of InetAddress.

getLocalHost() returns only the InetAddress object that symbolizes the localhost.

The local address is also localhost, 127.0.0.1, and these three addresses are the same.

 

The getByName() method returns an InetAddress passed to its hostname.

If these methods cannot resolve hostnames, they raise an UnknownHostException exception.

 

On the Internet, it is common to use one name to represent multiple machines.

The getAllByName() factory method returns an array of InetAddresses class representing all addresses decomposed by a special name.

When the name cannot be decomposed into at least one address, it will raise an UnknownHostException exception.

 

Program example
InetAddressTest01

package ;

import ;

public class InetAddressTest01
{
  public static void main(String[] args) throws Exception
  {
    InetAddress address = ();

    (address);
    // Output: Machine name/IP address    // For example, username-PC/10.4.16.131
    // Get the IP address through the domain name    address = ("");
    (address);
    // Output: Domain name/IP address  }

}

Use the InetAddress class to determine a special IP address
IP addresses are divided into ordinary addresses and special addresses. Most of the previous articles are ordinary IP addresses. In this article, we will introduce how to use the ten methods provided by the InetAddress class to determine whether an IP address is a special IP address.
1. isAnyLocalAddress method
Returns true when the IP address is a wildcard address, otherwise it returns false. This wildcard address is very owned by computers with multiple network interfaces (such as two network cards). Using wildcard addresses allows client connections from any network interface to be accepted on the server host. The wildcard address of IPv4 is 0.0.0.0. The wildcard address of IPv6 is 0:0:0:0:0:0:0:0:0:0:0:0:0, or it can be abbreviated as::.
2. IsLoopbackAddress method
Return true when the IP address is loopback address, otherwise the return address represents the IP address of the local machine. The range of the loopback address of IPv4 is 127.0.0.0 ~ 127.255.255.255.255. That is to say, as long as the first byte is 127, it is the lookback address. For example, 127.1.2.3 and 127.0.200.200 are loopback addresses. The loopback address of IPv6 is 0:0:0:0:0:0:0:0:1, or it can be abbreviated as::1. We can use the ping command to test the lookback address. As shown in the following command line:

ping 127.200.200.200 

Running results:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128  (Note:win7Next is127.200.200.200)
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.200.200.200:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Although 127.255.255.255 is also a loopback address, 127.255.255.255 cannot be pinged under Windows. This is because 127.255.255.255 is a broadcast address. It does not respond to requests sent to the broadcast address under Windows, but on other operating systems, different results may be obtained depending on the settings.
3. isLinkLocalAddress method
Return true when the IP address is the local connection address (LinkLocalAddress), otherwise the range of the local connection address of IPv4 is 169.254.0.0 ~ 169.254.255.255.255. The first 12 bits of the local connection address of IPv6 are FE8, and the other bits can be any value, such as FE88:: and FE80::ABCD:: are all local connection addresses.
4. isSiteLocalAddress method
Return true when the IP address is a local address (SiteLocalAddress), otherwise the address that returns false. The local address of IPv4 is divided into three segments: 10.0.0.0 ~ 10.255.255.255, 172.16.0.0 ~ 172.31.255.255, 192.168.0.0 ~ 192.168.255.255. The first 12 bits of the local address of IPv6 are FEC, and the other bits can be any value, such as FED0:: and FEF1:: are both local addresses.
5. isMulticastAddress method
Returns true when the IP address is a broadcast address (MulticastAddress), otherwise returns false. Through the broadcast address, information can be sent to all computers in the network, rather than just sending information to one specific computer. The range of the broadcast address of IPv4 is 224.0.0.0 ~ 239.255.255.255.255.255. The first byte of IPv6's broadcast address is FF, and the other bytes can be any value. Details about broadcast addresses will be discussed in future chapters.
6. IsMCGlobal method
Return true when the IP address is a global broadcast address, otherwise it returns false. Global broadcast address can send information to all computers in the Internet. The IPv4 broadcast address is a global broadcast address except for the IP address 224.0.0.0 and the first byte is 239. The first byte in the global broadcast address of IPv6 is FF, the second byte has a range of 0E ~ FE, and the other bytes can be any value, such as FFBE:: and FF0E:: are all global broadcast addresses.
7. isMCLinkLocal method
Return true when the IP address is the subnet broadcast address, otherwise it returns false. The broadcast address using the subnet can only send information to the computers in the subnet. The range of the IPv4 subnet broadcast address is 224.0.0.0 ~ 224.0.0.255. The first byte of the IPv6 subnet broadcast address is FF, the second byte is 02 ~ F2, and the other bytes can be any value, such as FFB2:: and FF02:ABCD:: are all subnet broadcast addresses.
8. isMCNodeLocal method
Return true when the IP address is the local interface broadcast address, otherwise it returns false. The local interface broadcast address cannot send broadcast information to the network interface that generates the broadcast information, even another network interface of the same computer cannot do it. All IPv4 broadcast addresses are not local interface broadcast addresses. The first byte of the local interface broadcast address of IPv6 is FF, the range of the second byte is 01 ~ F1, and the other bytes can be any value, such as FFB1:: and FF01:A123:: are all local interface broadcast addresses.
9. isMCOrgLocal method
Returns ture when the IP address is an organization-wide broadcast address, otherwise it returns false. Use the organization-wide broadcast address to send broadcast information to all computers within the company or enterprise. The first byte of the organization-wide broadcast address of IPv4 is 239, the second byte is not less than 192, and the third byte is not greater than 195. For example, 239.193.100.200 and 239.192.195.0 are all organization-wide broadcast addresses. The first byte of the organization-wide broadcast address of IPv6 is FF, the range of the second byte is 08 ~ F8, and the other bytes can be any value, such as FF08:: and FF48:: are all organization-wide broadcast addresses.
10. isMCSiteLocal method
Return true when the IP address is a site-wide broadcast address, otherwise it returns false. Using the site-wide broadcast address, broadcast information can be sent to site-wide computers. The range of IPv4's site-wide broadcast address is 239.255.0.0 ~ 239.255.255.255.255. For example, 239.255.1.1 and 239.255.0.0 are all site-wide broadcast addresses. The first byte of the site-wide broadcast address of IPv6 is FF, the range of the second byte is 05 ~ F5, and the other bytes can be any value, such as FF05:: and FF45:: are all site-wide broadcast addresses.
 
The following code can determine whether an IP address is within the range of the above ten address types:

package test;

import .*;
import .*;

public class MyNet
{
  public static void main(String[] args) throws Exception
  {
    if ( == 0)
      return;
    InetAddress address = (args[0]);
    Method methods[] = ();
    // Methods that start with is and have no parameters    for (Method method : methods)
    {
      if (().matches("is.*") && ().length == 0)
      {
        if (((address).toString()))
          (() + " = true");
      }
    }
  }
}

Test 1
Execute the following command:

java  224.0.0.1

Running results:

isMCLinkLocal = true
isMulticastAddress = true

Test 2
Execute the following command:

java  FFB1::

Running results:

isMCNodeLocal = true 
isMulticastAddress = true