How do I use a host name to look up an IP address?
2014-10-21 03:09
459 查看
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 would 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 should 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.
If we execute DnsTest, we obtain the following console results:
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(一句顺便插), 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.
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 should 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 host Canonical Host Name:DERBIZZ Host Name:DERBIZZ Host Address:192.168.1.106 ---------------------------------------------------- Which Host:www.baidu.com Canonical Host Name:180.97.33.108 Host Name:www.baidu.com Host Address:180.97.33.108 ---------------------------------------------------- Which Host:www.baidu.com #1 Canonical Host Name:180.97.33.108 Host Name:www.baidu.com Host Address:180.97.33.108 -------------------------- Which Host:www.baidu.com #2 Canonical Host Name:61.135.169.121 Host Name:www.baidu.com Host Address:61.135.169.121 -------------------------- Which Host:www.baidu.com #3 Canonical Host Name:61.135.169.125 Host Name:www.baidu.com Host Address:61.135.169.125 -------------------------- Which Host:www.baidu.com #4 Canonical Host Name:180.97.33.107 Host Name:www.baidu.com Host 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(一句顺便插), 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)
相关文章推荐
- The host file--map a name to an IP address
- How To: Make VirtualBox Use Your Router’s DHCP to get an IP Address in Linux
- Linux: Where to set hostname and how to get IP info, as well as how to set DNS
- How do I use a modem to send an audio message?
- How do I change the FROM address to a friendly name?
- How to get local machine name and IP address?
- How-to: Resolve "Datanode denied communication with namenode because hostname cannot be resolved (ip
- [转]How to convert IP address to country name
- How To Use Cookies in an ASP Page
- How to use an Outlook Object Model from Visual C++ by using a #import statement
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- How to use Vim like an IDE
- How to use the "Enties Generator" tool to create an entities project.
- How To Set Up a TCP/IP Peer-to-Peer Network Connection
- Using IP Instances with VLANs or How to Make a Few NICs Look Like Many
- how to sign an assembly with a strong name & how to create a pair key怎样通过强名给程序集签名以及如何创建钥匙对?
- How do I authenticate to send an email
- how to get url and hostname and url text in one page
- How To Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, Word 2002, or Word 2003
- How To Use Address Lists to Organize Recipients in Exchange 2003