The InetAddress class is the class used in Java to describe IP addresses. It is in the java.net package. In Java, the inet4address and inet6address classes are used to describe the addresses of IPv4 and IPV6 respectively. These two classes are inetaddress subclasses of a class. Since inetaddress does not have a public construction method, it is necessary to rely on its four static methods to create a InetAddress object. InetAddress can get the InetAddress object of native by Getlocalhost method, also can get the Getallbyname object of remote host through Getbyname, Getbyaddress and InetAddress.
First, Getlocalhost Method
Use Getlocalhost to get the InetAddress object that describes the native IP . This method is defined as follows:
public static inetaddress Getlocalhost () throws Unknownhostexception
This method throws a Unknownhostexception exception, so the exception must be caught or thrown in the program that called the method. The following code shows how to use Getlocalhost to get the native IP and computer name.
Package inet;
Import java.net.*;
public class MyInetAddress1
{
public static void Main (string[] args) throws Exception
{
InetAddress localaddress = Inetaddress.getlocalhost ();
System.out.println (localaddress);
}
}
Operation Result:
computername/192.168.18.10
The tostring method of the object class is overridden in the InetAddress class , and is implemented as follows:
Public String toString ()
{
Return ((HostName! = null)? HostName: "") + "/" + gethostaddress ();
}
As you can see from the code above, the ToString method in the InetAddress method returns the hostname and IP address separated by "/". Therefore, by outputting the native computer name and IP address directly through the LocalAddress object in the preceding code (after passing the object parameter to the Println method, the Println method invokes the ToString method of the object parameter to output the result).
Getlocalhost only returns the first IP when the computer is bound to multiple IPs. You can use the Getallbyname method if you want to return all the IP for this machine.
Second, Getbyname Method
This method is the most commonly used method of the InetAddress class. It can obtain the corresponding IP address from DNS by specifying the domain name . Getbyname A string parameter that can be used to specify the domain name of the remote host, which is defined as follows:
public static inetaddress Getbyname (String host) throws Unknownhostexception
If the domain name that the host refers to corresponds to multiple ip,getbyname, the first IP is returned. If the native name is known, you can use the Getbyname method instead of Getlocalhost. When the value of host is localhost, the returned IP is typically 127.0.0.1. If host is a non-existent domain name, Getbyname will throw an unknownhostexception exception if host is an IP address, regardless of whether the IP address exists, The Getbyname method returns this IP address (so getbyname does not verify the correctness of the IP address). The following code demonstrates how to use the Getbyname method:
Package inet;
Import java.net.*;
public class MyInetAddress2
{
public static void Main (string[] args) throws Exception
{
if (Args.length = = 0)
Return
String host = Args[0];
InetAddress address = inetaddress.Getbyname (host);
SYSTEM.OUT.PRINTLN (address);
}
}
- Test 1: remote host www.csdn.net
Execute the following command:
Java inet. MyInetAddress2 www.csdn.net
Operation Result:
www.csdn.net/211.100.26.124
- Test 2 : The name of the machine ComputerName
Execute the following command:
Java inet. MyInetAddress2 ComputerName
Operation Result:
computername/192.168.18.10
- Test 3 : Represents the native localhost
Execute the following command:
Java inet. MyInetAddress2 localhost
Operation Result:
localhost/127.0.0.1
For this machine, you can also map the "ip/Domain Name" (under the Windows operating system) to this machine in the Hosts file, in addition to the use of the native name or localhost. This file is in C:\WINDOWS\system32\drivers\etc. Open these two files, and at the last add a line with the following string as follows:
192.168.18.100 www.mysite.com
- Test 4 : Native domain name www.mysite.com
Execute the following command:
Java inet. MyInetAddress2 www.mysite.com
Operation Result:
www.mysite.com/192.168.18.100
The Getbyname method can use the IP address as an argument in addition to using the domain name as a parameter. If the IP address is used as a parameter, the domain name is empty when the InetAddress object is output (unless the GetHostName method is called, then the InetAddress object is output. The GetHostName method will be described in the following sections). Readers can use 129.42.58.212 as the command line argument for MyInetAddress2 (this is the IP of www.ibm.com) and see what results will be obtained.
Third, Getallbyname Method
Use the Getallbyname method to get all the IP addresses for the domain name from DNS. This method returns an array of type inetaddress . This method is defined as follows:
public static inetaddress[] Getallbyname (String host) throws Unknownhostexception
As with the Getbyname method, Getallbyname throws an Unknowhostexception exception when host does not exist, and Getallbyname does not verify that the IP address exists. The following code demonstrates the use of Getallbyname:
Package inet;
Import java.net.*;
public class MYINETADDRESS3
{
public static void Main (string[] args) throws Exception
{
if (Args.length = = 0)
Return
String host = Args[0];
InetAddress addresses[] = inetaddress.getallbyname (host);
for (inetaddress address:addresses)
SYSTEM.OUT.PRINTLN (address);
}
}
- Test 1 : Remote host www.csdn.net
Execute the following command:
Java inet. MYINETADDRESS3 www.csdn.net
Operation Result:
www.csdn.net/211.100.26.124
www.csdn.net/211.100.26.121
www.csdn.net/211.100.26.122
Www.csdn.net/211.100.26.123
Comparing the running results of the above running results with that of test 1 of routine 3-2, we can conclude that the IP address returned by the Getbyname method is the first IP address returned by the Getallbyname method. In fact, Getbyname does so, and the implementation code for Getbyname is as follows:
public static inetaddress Getbyname (String host) throws Unknownhostexception
{
return Inetaddress.getallbyname (host) [0];
}
- Test 2 : Using www.csdn.net of a IP
Execute the following command:
Java inet. MYINETADDRESS3 211.100.26.122
Operation Result:
/211.100.26.122
Four, getbyaddress Method
This method must create the InetAddress object through an IP address, and the IP address must be in the form of a byte array. The Getbyaddress method has two overloaded forms, defined as follows:
public static inetaddress getbyaddress (byte[] addr) throws Unknownhostexception
public static inetaddress getbyaddress (String host, byte[] addr) throws Unknownhostexception
The first overloaded form only needs to pass an IP address in the form of a byte array, and the Getbyaddress method does not verify that the IP address exists, but simply creates a InetAddress object. The length of the addr array must be 4 (IPV4) or (IPV6), and if it is a byte array of other lengths, Getbyaddress throws a Unknownhostexception exception. The second overloaded form more than one host, this host and Getbyname, Getallbyname method in the meaning of the host, the Getbyaddress method does not use host to find the IP address on DNS, This host is just an alias to represent the addr. The following code demonstrates the use of the two overloaded forms of getbyaddress:
Package inet;
Import java.net.*;
public class MyInetAddress4
{
public static void Main (string[] args) throws Exception
{
byte ip[] = new byte[] {(byte) 141, (byte) 146, 8, 66};
InetAddress Address1 = inetaddress.getbyaddress (IP);
InetAddress address2 = inetaddress.getbyaddress ("Oracle official website", IP);
System.out.println (ADDRESS1);
System.out.println (ADDRESS2);
}
}
The result of the above code is as follows:
/141.146.8.66
Oracle Official website/141.146.8.66
As you can see from the running results above, getbyaddress simply places the host parameter in front of "/" as the domain name, so that host can be any string.
Java network programming from beginner to Proficient (2): Four static methods for creating InetAddress objects