Java Network Programming from entry to mastery (7): Get the IP address using the getHostAddress Method

Source: Internet
Author: User

This method is used to obtainIPAddress.IPThe address may beIPv4OrIPv6.GetHostAddressThe method is defined as follows:
PublicString getHostAddress ()

RegardlessInetAddressWhich method is used to create an object,GetHostAddressMethod will not be accessedDNSServer. If you want to accessIPv6Address remote host, which must be installed on the Operating SystemIPv6Protocol. Below isWindows 2003Install OnIPv6Procedure:

Step 1:Open"Local Connection"Properties dialog box. 1..

Figure 1 "Local Connection" attribute dialog box


 Step 2:Click"Install"Button appears"Select network component type"Dialog box, select"Protocol"Click"Add"Button appears2Of"Select network protocol"Dialog box, select"Microsoft TCP/IPVersion6"And click"OK"Button.

Figure 2 "select Network Protocol" dialog box

In addition to the graphical interface for InstallationIPv6You can also use the following command line to installIPv6:

Netsh interface ipv6 install

The following codeDemonstrate how to useGetHostAddressGetIPv4AndIPv6Address, and how to obtain allIPAddress (includingIPv4AndIPv6Address ).

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: "  

Related Article

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.