Java network programming from getting started to mastering (7): Obtaining an IP address using the Gethostaddress method

Source: Internet
Author: User

This method is used to get the IP address of the host, which may be the address of IPv4 or IPv6 . The Gethostaddress method is defined as follows:

Public String gethostaddress ()

Regardless of which method the InetAddress object is created in, thegethostaddress method does not access the DNS server . If you want to access a remote host that uses an IPV6 address, you need to install the IPV6 protocol on the operating system. here are the steps to install the IPV6 protocol on Windows 2003:

First step: Open the Local Area Connection Properties dialog box. As shown in 1.

Figure 1 The Local Area Connection Properties dialog box

Second Step: click the "Install" button, the "Select Network Component Type" dialog box, select the "Protocol" option, click on the "Add" button, 2 of the "Select Network Protocol" dialog box, select "Microsoft TCP/IP version 6", and finally click the "OK" button.

Figure 2 The Select Network Protocol dialog box

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

netsh interface ipv6 install

The following code shows how to use Gethostaddress to get IPv4 and IPv6 addresses, and how to get all the IP addresses of the native (including IPV4 and IPV6 addresses).

PackageMyNet;
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 IP addresses of this machine          inetaddress addresses[] = inetaddress.getallbyname ("Myuniverse");         for  (inetaddress address : addresses)             system.out.println ("Native Address:"  +  Address.gethostaddress ());     }}

In the above code, www.neu6.edu.cn is used as the domain name, which is the domain name used by Northeastern University to test the IPV6 address. Here are some other domain names that can be used to test IPv6, and readers can use the ping command or routine 3-9 to test these domain names.

Www6.whu.edu.cn (Wuhan University) www.jlu6.edu.cn (Jilin University) www6.usst.edu.cn (Shanghai Polytechnic University) www.fudan6.edu.cn (Fudan)

Before accessing these domain names, the machine must install IPV6 using the above method or command line, otherwise the Getbyname method throws a Unknownhostexception exception.

Operation Result:

ipv4address1:1.2.3.4 ipv4address2:129.42.60.212 ipv4address3:192.168.18.10 ipv6address1:abcd:123:0:0:0:0:0:22ff ipv6address2:2001:da8:9000:b255:200:e8ff:feb0:5c5e Native Address: 192.168.18.10 Native Address: 192.168.83.1 Native Address: 192.168.189.1 Native Address: 193.10.10.10 Native Address: 0:0:0:0:0:0:0:1

The IP addresses in the results above are 192.168.18.10 and 192.10.10.10 are two IPs bound to the native NIC, while 192.168.83.1 and 192.168.189.1 are the addresses of the two virtual network cards that the VMware virtual machine software installs natively. The last IPV6 address 0:0:0:0:0:0:0:1 is the IPv6 URL representing the native, equivalent to the IPV4 address of 127.0.0.1. Readers can add IPv6 addresses and delete IPv6 using the following command line:

Add IPV6 Address

netsh interface ipv6 add address "Local Area Connection" AA:BB::CC

Delete IPv6

netsh interface ipv6 uninstall

Note: Installing IPV6 does not require a restart of the computer, but after you add the IPV6 address or remove IPV6, you must restart the computer to take effect.

Java network programming from getting started to mastering (7): Obtaining an IP address using the Gethostaddress method

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.