How do I use a host name to look up an IP address?

Source: Internet
Author: User
Tags domain name server

The inetaddress class can be used to perform Domain Name Server (DNS) lookups. for example, you can call the static inetaddress. getbyname ("www.teamcakes.com") to retrieve an inetaddress object for 'www .teamcakes.com '. this object woshould contain the canonical name, host name, and IP address of 'www .teamcakes.com '.

The dnstest class below demonstrates inetaddress. getlocalhost (), which obtains an Internet address for your local host. it also demonstrates inetaddress. getbyname ("www.google.com"), which gives an Internet address object for 'www .google.com '. however, it shocould be noted that a DNS name can map to multiple servers, and the inetaddress. getallbyname ("www.google.com") Call retrieves an array of inetaddress objects that represent all of the 'www .google.com 'servers retrieved from DNS.

package org.javalobby.tnt.net;import java.net.InetAddress;import java.net.UnknownHostException;public class DnsTest {    public static void main(String[] args) {        try {            InetAddress inetAddress = InetAddress.getLocalHost();            displayStuff("local host", inetAddress);            System.out.print("--------------------------");            inetAddress = InetAddress.getByName("www.baidu.com");            displayStuff("www.baidu.com", inetAddress);            System.out.print("--------------------------");            InetAddress[] inetAddressArray = InetAddress.getAllByName("www.baidu.com");            for (int i = 0; i < inetAddressArray.length; i++) {                displayStuff("www.baidu.com #" + (i + 1), inetAddressArray[i]);            }        } catch (UnknownHostException e) {            e.printStackTrace();        }    }    public static void displayStuff(String whichHost, InetAddress inetAddress) {        System.out.println("--------------------------");        System.out.println("Which Host:" + whichHost);        System.out.println("Canonical Host Name:" + inetAddress.getCanonicalHostName());        System.out.println("Host Name:" + inetAddress.getHostName());        System.out.println("Host Address:" + inetAddress.getHostAddress());    }}

If we execute dnstest, we obtain the following console results:

--------------------------Which Host:local hostCanonical Host Name:DERBIZZHost Name:DERBIZZHost Address:192.168.1.106----------------------------------------------------Which Host:www.baidu.comCanonical Host Name:180.97.33.108Host Name:www.baidu.comHost Address:180.97.33.108----------------------------------------------------Which Host:www.baidu.com #1Canonical Host Name:180.97.33.108Host Name:www.baidu.comHost Address:180.97.33.108--------------------------Which Host:www.baidu.com #2Canonical Host Name:61.135.169.121Host Name:www.baidu.comHost Address:61.135.169.121--------------------------Which Host:www.baidu.com #3Canonical Host Name:61.135.169.125Host Name:www.baidu.comHost Address:61.135.169.125--------------------------Which Host:www.baidu.com #4Canonical Host Name:180.97.33.107Host Name:www.baidu.comHost Address:180.97.33.107

As you can see, the inetaddress class allows us to perform DNS lookups and retrieve canonical host names, host names, and IP addresses. the results above show us that multiple servers can be represented by one host name, and these can be distinguished by their different IP (host) addresses and canonical host names.

Just as an aside (by the way), what happens if we try doing a lookup on a host that doesn't exist, such as: inetaddress. getbyname ("www.this-host-does-not-exist.com ")? An unknownhostexception is thrown, as shown below.

java.net.UnknownHostException: www.this-host-does-not-exist.com: www.this-host-does-not-exist.com    at java.net.InetAddress.getAllByName0(InetAddress.java:1128)    at java.net.InetAddress.getAllByName0(InetAddress.java:1098)    at java.net.InetAddress.getAllByName(InetAddress.java:1061)    at java.net.InetAddress.getByName(InetAddress.java:958)    at test.DnsTest.main(DnsTest.java:12)

 

How do I use a host name to look up an IP address?

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.