java resolves a domain name to an IP address

Source: Internet
Author: User
Tags ip number
familiar with InetAddress class: The InetAddress class is used primarily to get the specified network address, InetAddress class does not have a direct explicit constructor。 To generate a InetAddress object, you must use a factory method that is available. Factory methods (Factory method) are only a static method in a class that returns a convention for an instance of that class. This is done in an overloaded constructor with a variety of argument lists, which makes the results clearer when you hold the unique method name.

InetAddress has three methods that can be used to create an instance of InetAddress

1.static inetaddress Getlocalhost () throws Unknownhostexception

2.static inetaddress getbyname (String hostName) throws Unknownhostexception

3.static inetaddress[] Getallbyname (String hostName) throws Unknownhostexception

non-static methods for the InetAddress class:

Boolean equals (Object other)
Returns True if the object has an Internet address that is the same as other.
Byte[] GetAddress ()
Returns the original IP address of this InetAddress object.
String gethostaddress ()
Returns a string of host addresses associated with the InetAddress object.
String GetHostName ()
Returns a string of host names associated with the InetAddress object.
int Hashcode ()
Returns the hash code of the calling object.
Boolean ismulticastaddress ()
Returns true if the Internet address is a multicast address, otherwise it returns false.
String toString ()
Returns the host name string and IP address.

Below we write a program to resolve the domain name:

package com.ip; import java.net.InetAddress; import java.net.UnknownHostException; import
Java.util.Scanner; public class IP {public static void main (String[]args) {try {System.out.print ("Enter the domain name to resolve:")
            ;
            Scanner in=new Scanner (system.in);

                String domainname=in.nextline ()//Enter the domain name to resolve System.out.println ("Use the InetAddress class method to get the IP address of the Web site" +domainname+ ... "); SYSTEM.OUT.PRINTLN ("Total IP number:" + inetaddress.getallbyname (domainname). length);//Receive
                Number of IP inetaddress[] inetadd = inetaddress.getallbyname (domainname);  Traverse all IP and output for (int i = 0; i < inetadd.length; i++) {System.out.println ("a" + (i)
                + 1) + "IP:" + inetadd[i]); The catch (Unknownhostexception e) {System.out.println ("Get the IP address of the Web site Www.csdn.net failed.") No corresponding IP.
        "); }
    }

}

Results:


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.