Java Network Programming from entry to mastery (2): Four static methods for creating InetAddress objects

Source: Internet
Author: User

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 use of two reloads of getByAddress.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.