Home>

inetaddress class

The inetaddress class is used to encapsulate the numeric IP address and the domain name of the address we discussed earlier.

You interact with this class through an IP host name,An IP host name is simpler and easier to understand than its IP address.

The address number is hidden inside theaddress inetaddress class.

Factory method in inetaddress classinetaddress class has no obvious constructor.To generate an inetaddress object, you must use one of the available factory methods.

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 represents the local host.

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

The getbyname () method returns an inetaddress of the hostname passed to it.

If these methods cannot resolve the host name,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 the inetaddresses class representing all addresses resolved by a particular name.

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

Program example

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
    //e.g. username-pc/10.4.16.131
    //Get IP address by domain name
    address=inetaddress.getbyname ("www.sohu.com");
    system.out.println (address);
    //Output:domain name/ip address
  }
}

Use inetaddress class to determine special IP addressesIP addresses are divided into ordinary addresses and special addresses.Most of the previous articles used 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.

First, the isanylocaladdress methodReturns true when the IP address is a wildcard address, otherwise returns false. This wildcard address is very owned by a computer with multiple network interfaces (such as two network cards).Using wildcard addresses allows the server host to accept client connections from any network interface.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, which can also be abbreviated as ::.

Second, the isloopbackaddress methodReturns true when the IP address is a loopback address, otherwise returns false. The loopback address is the IP address representing the machine. The range of ipv4's loopback address is 127.0.0.0 ~ 127.255.255.255, that is,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:0, which can also 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

operation result:

reply from 127.0.0.1:bytes=32 time<1ms ttl=128 (Note:127.200.200.200 under win7)
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 the broadcast address,Does not respond to requests sent to broadcast addresses under Windows,On other operating systems, depending on the settings,May get different results.

Three, islinklocaladdress methodReturns true when the ip address is a local connection address (linklocaladdress), otherwise returns false. The range of the local connection address of ipv4 is 169.254.0.0 ~ 169.254.255.255.ipv6. The first 12 digits of the local connection address are fe8, The other bits can be any value,Such as fe88 ::, fe80 ::abcd ::are local connection addresses.

Fourth, issitelocaladdress methodReturns true when the ip address is a local local address (sitelocaladdress), otherwise it returns false. The local address of the IPv4 address is divided into three segments:10.0.0.0 ~ 10.255.255.255, 172.16.0.0 ~ 172.31.255.255, 192.168. The first 12 digits of the local address of 0.0 ~ 192.168.255.255.ipv6 are fec, and the other digits can be any value.Such as fed0 ::, fef1 ::are local address.

Five, ismulticastaddress methodReturns true when the IP address is a multicast address, otherwise returns false. The broadcast address can send information to all computers on the network,Rather than just sending information to a specific computer.The range of the broadcast address of ipv4 is 224.0.0.0 ~ 239.255.255.255. The first byte of the broadcast address of ipv6 is ff, and the other bytes can be any value.Details about broadcast addresses will be discussed in later chapters.

Six, ismcglobal methodReturns true when the IP address is a global broadcast address, otherwise it returns false. The global broadcast address can send information to all computers on the internet.Except for the broadcast address of ipv4, except for 224.0.0.0 and the first byte is 239, the ip address is a global broadcast address.In the global broadcast address of IPv6, the first byte is ff, and the second byte ranges from 0e to fe. The other bytes can be any value.Such as ffbe ::, ff0e ::are global broadcast addresses.

Seven, ismclinklocal methodReturns true when the IP address is a subnet broadcast address, otherwise it returns false. Using the broadcast address of the subnet can only send information to computers within the subnet.The range of the subnet broadcast address of ipv4 is 224.0.0.0 ~ 224.0.0.255. The first byte of the subnet broadcast address of ipv6 is ff, and the range of the second byte is 02 ~ f2. The other bytes can be Any value,Such as fbb2 ::, ff02:abcd ::are subnet broadcast addresses.

Eight, ismcnodelocal methodReturns true when the IP address is a local interface broadcast address, otherwise returns false. The local interface broadcast address cannot send broadcast information to the network interface that generates the broadcast information,Not even another network interface on the same computer.All IPv4 broadcast addresses are not local interface broadcast addresses.The first byte of the local interface broadcast address of IPv6 is ff, and the range of the second byte is 01 ~ f1. The other bytes can be any value.For example, ffb1 ::, ff01:a123 ::are local interface broadcast addresses.

Nine, ismcorglocal methodReturns true when the IP address is an organization-wide broadcast address, otherwise returns false. Use an organization-wide broadcast address to send broadcast information to the company or all computers within the enterprise.The first byte of the IPv4 organization-wide broadcast address 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, and the range of the second byte is 08 ~ f8. The other bytes can be any value.Such as ff08 ::, ff48 ::are all organization-wide broadcast addresses.

Ten, ismcsitelocal methodsReturns true when the IP address is a site-wide broadcast address, otherwise returns false. Use a site-wide broadcast address,Broadcast information can be sent to computers within the site.The range of site-wide broadcast addresses of ipv4 is 239.255.0.0 ~ 239.255.255.255. For example, 239.255.1.1 and 239.255.0.0 are site-wide broadcast addresses.The first byte of the site-wide broadcast address of ipv6 is ff, and the range of the second byte is 05 ~ f5. 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 ();
    //method starting with is and no parameters
    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 result:

ismclinklocal=true
ismulticastaddress=true

Test 2

Execute the following command:

java test.mynet ffb1 ::

operation result:

ismcnodelocal=true
ismulticastaddress=true
  • Previous Explain the concepts of methods and properties in Swift programming
  • Next How to implement jQuery multi-condition filtering