A method in Java programming that USES the InetAddress class to determine a particular IP address

  • 2020-04-01 04:22:29
  • OfStack

InetAddress class

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

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

The InetAddress class hides address Numbers inside.

 
The factory method in the InetAddress class
The InetAddress class has no obvious constructor. To generate an InetAddress object, an available factory method must be applied.

A factory method is simply 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 () simply returns the InetAddress object that represents the local host.

The local address is also localhost,127.0.0.1, all three of which are the same.

 

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

If these methods cannot resolve the hostname, they throw a UnknownHostException exception.

 

On the Internet, it is common to have one name for multiple machines.

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

When a name cannot be decomposed into at least one address, it throws a UnknownHostException.

 

Application instance
InetAddressTest01


package com.example.network;

import java.net.InetAddress;

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

    System.out.println(address);
    //Output: machine name /IP address
    //Such as the username - PC / 10.4.16.131

    //Get the IP address by domain name
    address = InetAddress.getByName("www.sohu.com");
    System.out.println(address);
    //Output: domain name /IP address
  }

}

Use the InetAddress class to determine special IP addresses
IP addresses are divided into ordinary addresses and special addresses. While most of the previous articles used plain IP addresses, this article shows you how to use the ten methods provided by the InetAddress class to determine whether an IP address is a special IP address.
      I. isAnyLocalAddress method
      Returns true if the IP address is a wildcard address, or false if it is not. This wildcard address is very common on computers that have multiple network interfaces, such as two network CARDS. Using a wildcard address allows you to accept client connections from any network interface on the server host. The IPv4 wildcard address is 0.0.0.0. The IPv6 wildcard address is 0:0:0:0:0:0:0, which can also be abbreviated as: :.
      The isLoopbackAddress method
      Returns true when the IP address is a loopback address, or false. The loopback address represents the native IP address. The IPv4 loopback address range is 127.0.0.0 to 127.255.255.255, which means that as long as the first byte is 127, the lookback address is. For example, 127.1.2.3 and 127.0.200.200 are loopback addresses. The loopback address for IPv6 is 0:0:0:0:0:0:0:1, or simply: : 1. We can use the ping command to test the lookback address. As shown on the following command line:


ping 127.200.200.200 

Operation results:


Reply from 127.0.0.1: bytes=32 time<1ms TTL=128  ( Note: win7 Next is 127.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 is not ping under Windows. This is because 127.255.255.255 is the broadcast address, and under Windows there is no response to a request to the broadcast address, while on other operating systems you may get different results depending on the Settings.
      Iii. IsLinkLocalAddress method
      IPv4 local connection addresses range from 169.254.0.0 to 169.254.255.255. The first 12 bits of IPv6 local connection addresses are FE8, and other bits can be arbitrary values, such as FE88: :, FE80: : ABCD: : are local connection addresses.
      Fourth, isSiteLocalAddress method
      IPv4 addresses are divided into three segments: 10.0.0.0 ~ 10.255.255.255, 172.16.0.0 ~ 172.31.255.255, and 192.168.0.0 ~ 192.168.255.255. All are local addresses.
      5. IsMulticastAddress method
      Returns true when the IP address is broadcast address, or false otherwise. The broadcast address is used to send information to all computers in the network, rather than to a specific computer. The IPv4 broadcast address range is 224.0.0.0 to 239.255.255.255. The first byte of IPv6 broadcast address is FF, and the rest can be any value. The details of the broadcast address will be discussed in a later section.
      Vi. IsMCGlobal method
      Returns true when the IP address is a worldwide broadcast address, or false otherwise. A worldwide broadcast address can send information to all computers on the Internet. IPv4 broadcast addresses are global except for 224.0.0.0 and the IP address with the first byte of 239. The first byte in IPv6's global broadcast address is FF, the second byte's range is 0E ~ FE, and the other bytes can be any value, such as FFBE: :, FF0E: :, are all global broadcast addresses.
      Seven, isMCLinkLocal method
      Returns true when the IP address is a subnet broadcast address, or false otherwise. IPv4 subnet broadcast address range is 224.0.0.0 ~ 224.0.0.255. The first byte of IPv6 subnet broadcast address is FF, the second byte range is 02 ~ F2, other bytes can be any value, such as FFB2: :, FF02: ABCD: : are subnet broadcast address.
      IsMCNodeLocal method
      Returns true when the IP address is the local interface broadcast address, or false otherwise. The local interface broadcast address cannot send the broadcast message to the network interface that generated the broadcast message, even if it is another network interface on the same computer. All IPv4 broadcast addresses are not local interface broadcast addresses. The first byte of IPv6 local interface broadcast address is FF, the second byte range is 01 ~ F1, the other bytes can be any value, such as FFB1: :, FF01: A123: : are local interface broadcast address.
      IsMCOrgLocal method
      Returns true when the IP address is an organization-wide broadcast address, or false otherwise. Use the organization-wide broadcast address to send broadcast messages to all computers in a company or enterprise. The first byte of IPv4 organization-wide broadcast address is 239, the second byte is no less than 192, and the third byte is no more than 195. For example, 239.193.100.200 and 239.192.195.0 are both organization-wide broadcast addresses. The first byte of IPv6's organization-wide broadcast address is FF, the second byte ranges from 08 to F8, and the other bytes can be any value, such as FF08: :, FF48: :, are organization-wide broadcast addresses.
      X. isMCSiteLocal method
      Returns true when the IP address is a site-wide broadcast address, or false otherwise. Using a site-wide broadcast address, you can send broadcast information to a site-wide computer. IPv4 site-wide broadcast addresses range from 239.255.0.0 to 239.255.255.255. For example, 239.255.1.1 and 239.255.0.0 are site-wide broadcast addresses. The first byte of IPv6 site-wide broadcast addresses is FF, the second byte ranges from 05 to F5, and the other bytes can be any value, such as FF05: :, FF45: :, are 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 java.net.*;
import java.lang.reflect.*;

public class MyNet
{
  public static void main(String[] args) throws Exception
  {
    if (args.length == 0)
      return;
    InetAddress address = InetAddress.getByName(args[0]);
    Method methods[] = InetAddress.class.getMethods();
    //A method that starts with is and takes no arguments
    for (Method method : methods)
    {
      if (method.getName().matches("is.*") && method.getParameterTypes().length == 0)
      {
        if (Boolean.parseBoolean(method.invoke(address).toString()))
          System.out.println(method.getName() + " = true");
      }
    }
  }
}

Test 1
      Execute the following command:


java test.MyNet 224.0.0.1

Operation results:


isMCLinkLocal = true
isMulticastAddress = true

Test 2
      Execute the following command:


java test.MyNet FFB1::

Operation results:


isMCNodeLocal = true 
isMulticastAddress = true 


Related articles: