Use the gethostaddress method to obtain the IP address

Source: Internet
Author: User

This method is used to obtain the Host IP address, which may be an IPv4 address or an IPv6 address. The gethostaddress method is defined as follows:

public String getHostAddress()

No matter which method the inetaddress object is created, the gethostaddress method does not access the DNS server. To access a remote host using an IPv6 address, you must install the IPv6 protocol on the operating system. Below is Windows
2003 to install the IPv6 protocol:
Step 1: Open the "local connection" attribute dialog box.

Step 2: click "Install". The "select network component type" dialog box appears. Select the "protocol" option and click "add". The "select Network Protocol" dialog box appears, select "Microsoft TCP/IP version 6" and click "OK.


In addition to using the graphic interface to install IPv6, you can also use the following command line to install IPv6:

netsh interface ipv6 install

The following code demonstrates how to use gethostaddress to obtain IPv4 and IPv6 addresses, and how to obtain all the local IP addresses (including IPv4 and IPv6 addresses ).

Package mynet; import java.net. *; public class myip {public static void main (string [] ARGs) throws exception {// output IPv4 address inetaddress ipv4address1 = inetaddress. getbyname ("1.2.3.4"); system. out. println ("ipv4address1:" + ipv4address1. gethostaddress (); inetaddress ipv4address2 = inetaddress. getbyname ("www.ibm.com"); system. out. println ("ipv4address2:" + ipv4address2. gethostaddress (); inetaddress ipv4address3 = inetaddress. getbyname ("myuniverse"); system. out. println ("ipv4address3:" + ipv4address3. gethostaddress (); // output IPv6 address inetaddress ipv6address1 = inetaddress. getbyname ("ABCD: 123: 22ff"); system. out. println ("ipv6address1:" + ipv6address1. gethostaddress (); inetaddress ipv6address2 = inetaddress. getbyname ("www.neu6.edu.cn"); system. out. println ("ipv6address2:" + ipv6address2. gethostaddress (); // output all the IP addresses of the Local Machine. inetaddress addresses [] = inetaddress. getallbyname ("myuniverse"); For (inetaddress address: addresses) system. out. println ("Local Address:" + address. gethostaddress ());}}


In the above Code, www.neu6.edu.cn is used as the domain name. This domain name is the domain name that Northeastern University uses to test the IPv6 address.

Before accessing the IPv6 domain name, the local machine must use the above method or command line to install IPv6. Otherwise, the getbyname method will throw an unknownhostexception.

Running result:

Listen 4address1: Failed: 192.168.18.10ipv6address1: ABCD: 123: 0: 0: 0: 0: 0: 22ff3166address2: 2001: da8: 9000: b255: 200: e8ff: feb0: 5c5e local address: 192.168.18.10 local address: 192.168.83.1 local address: 192.168.189.1 local address: 193.10.10.10 local address: 0: 0: 0: 0: 0: 0: 0: 0: 0: 1

In the preceding running results, the IP addresses 192.168.18.10 and 192.10.10.10 are the two IP addresses bound to the local Nic, while 192.168.83.1 and 192.168.189.1 are the addresses of the two virtual NICs installed on the local VM software. The last IPv6 address 0: 0: 0: 0: 0: 0: 0: 1 represents the local IPv6 URL, which is equivalent to 127.0.0.1 of the IPv4 address. You can use the following command line to add or delete an IPv6 address:

1) Add an IPv6 address

Netsh interface IPv6 add address "Local Connection" AA: BB: CC

2) delete an IPv6 address

netsh interface ipv6 uninstall

Note: To install IPv6, you do not need to restart the computer. However, after adding an IPv6 address or deleting an IPv6 address, you must restart the computer to take effect.

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.