Java Network Programming from entry to mastery (2): Four static methods for creating inetaddress objects
Source: Author: Release Date: 20:42:39
The inetaddress class is the class used to describe IP addresses in Java. It is in the java.net package. In Java, the inet4address and inet6address classes are used to describe IPv4 and IPv6 addresses. Both classes are subclasses of inetaddress. Because inetaddress does not have a public constructor, four static methods must be used to create an inetaddress object. The inetaddress method can be used to obtain the inetaddress object of the local machine, or the inetaddress object of the remote host can be obtained through getbyname, getallbyname, and getbyaddress.
1. getlocalhost Method
Use getlocalhost to obtain the inetaddress object that describes the local IP address. The method is defined as follows:
public static InetAddress getLocalHost() throws UnknownHostException
This method throws an unknownhostexception. Therefore, you must capture or throw this exception in the program that calls this method. The following code demonstrates how to use getlocalhost to obtain the IP address and computer name of the local machine.
package inet;
import java.net.*;
public class MyInetAddress1
{
public static void main(String[] args) throws Exception
{
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println(localAddress);
}
}
Running result:
ComputerName/192.168.18.10
The inetaddress class overwrites the tostring method of the object class. The implementation is as follows:
public String toString()
{
return ((hostName != null) ? hostName : "") + "/" + getHostAddress();
}
The code above shows that the tostring method in the inetaddress method returns the Host Name and IP address separated. Therefore, in the above Code, the local computer name and IP address are directly output through the localaddress object (after the object parameter is passed into the println method, the println method will call the tostring method of the object parameter to output the result ).
When the local machine is bound with multiple IP addresses, getlocalhost returns only the first IP address. If you want to return all the IP addresses of the local machine, you can use the getallbyname method.
2. getbyname Method
This method is the most common method of the inetaddress class. It can get the corresponding IP address from DNS by specifying the domain name. Getbyname: A string type parameter. You can use this parameter to specify the Domain Name of the remote host. Its definition is as follows:
public static InetAddress getByName(String host) throws UnknownHostException
If the domain name specified by the host corresponds to multiple IP addresses, getbyname returns the first IP address. If the local name is known, you can use the getbyname method instead of getlocalhost. When the host value is localhost, the returned IP address is 127.0.0.1. If the host is a domain name that does not exist, getbyname will throw an unknownhostexception. If the host is an IP address, the getbyname method returns this IP address regardless of whether the IP address exists (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
Run the following command:
java inet.MyInetAddress2 www.csdn.net
Running result:
www.csdn.net/211.100.26.124
Test 2: local name computername
Run the following command:
java inet.MyInetAddress2 ComputerName
Running result:
ComputerName/192.168.18.10
Test 3: indicates the local localhost
Run the following command:
java inet.MyInetAddress2 localhost
Running result:
localhost/127.0.0.1
In addition to the local name or localhost, you can also map the local IP/domain name in the hosts file (in Windows operating system ). This file is in C: windowssystem32driversetc. Open these two files and add the following string to the last line:
192.168.18.100 www.mysite.com
Test 4: Local Domain Name www.mysite.com
Run the following command:
java inet.MyInetAddress2 www.mysite.com
Running result:
www.mysite.com/192.168.18.100
In addition to using domain names as parameters, the getbyname method can also directly use IP addresses as parameters. If the IP address is used as the parameter, the time domain name of the output inetaddress object is null (unless the gethostname method is called, The inetaddress object is output again. The gethostname method will be described below ). You can use 129.42.58.212 as the command line parameter of myinetaddress2 (this is the IP address of www.ibm.com) to see what the result will be.
3. getallbyname Method
You can use the getallbyname method to obtain all IP addresses corresponding to the domain name from DNS. This method returns an inetaddress array. The method is defined as follows:
Public static inetaddress [] getallbyname (string host) throws unknownhostexception
Like the getbyname method, if the host does not exist, the getallbyname will also throw an unknowhostexception. getallbyname will not verify whether the IP address exists. The following code demonstrates the usage 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
Run the following command:
java inet.MyInetAddress3 www.csdn.net
Running 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
Compare the preceding running result with the running result of test 1 of the routine 3-2, and draw a conclusion that the IP address returned by the getbyname method is the first IP address returned by the getallbyname method. In fact, getbyname is implemented in this way. The implementation code of getbyname is as follows:
public static InetAddress getByName(String host) throws UnknownHostException
{
return InetAddress.getAllByName(host)[0];
}
Test 2: Use an IP address of www.csdn.net
Run the following command:
java inet.MyInetAddress3 211.100.26.122
Running result:
/211.100.26.122
Iv. getbyaddress Method
This method must use an IP address to create an inetaddress object, and the IP address must be in the byte array format. The getbyaddress method has two reloads, which are defined as follows:
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException
The first overload method only needs to pass an IP address in the byte array format. The getbyaddress method does not verify whether the IP address exists, but simply creates an inetaddress object. The length of the ADDR array must be 4 (IPv4) or 16 (IPv6). If it is a byte array of other lengths, getbyaddress throws an unknownhostexception. The second overload mode has a host, which has different meanings from the host in the getbyname and getallbyname methods. The getbyaddress method does not use the host to find the IP address on the DNS, this host is just an alias used to represent ADDR. The following code demonstrates the usage of getbyaddress in two overloaded forms:
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 running result of the above Code is as follows:
/141.146.8.66
Official Oracle website/141.146.8.66
As shown in the preceding running results, getbyaddress simply puts the host parameter as the domain name before "/". Therefore, the host can be any string.
Series of articles:
Java Network Programming from entry to entry (1): Internet address Overview
Java Network Programming from entry to mastery (3): Why can't I access a website directly through an IP address?