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.

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)

How do I use a host name to look up an IP address?的更多相关文章

  1. dubbo could not get local host ip address will use 127.0.0.1 instead 异常处理

    dubbo could not get local host ip address will use 127.0.0.1 instead 查看hostname localhost:spring wls ...

  2. github 遇到Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts问题解决

    刚开始使用github的时候不是很了解,新手一般的都会遇到这个问题Permanently added the RSA host key for IP address '192.30.252.128' ...

  3. Unable to construct api.Node object for kubelet: can't get ip address of node master.example.com: lookup master.example.com on : no such host

    openshift首页进不去 启动openshift时报的错,大意是: 无法为kubelet构造api.Node对象:无法获取节点master.example.com的IP地址: 所以就联想到新装的c ...

  4. 错误RSA host key for [ip address] has changed and you have requested strict checking.

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION HAS ...

  5. 解决办法: RSA host key for [ip address] has changed and you have requested strict checking.

    在服务器重装后想要远程连接服务器,报错如下: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE ...

  6. ubuntu使用git的时:Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.

    1:问题现象: hlp@hlp:~/code/github_code/catch_imooc1$ git push origin master Warning: Permanently added t ...

  7. XShell提示Connection closed by foreign host的问题 和 路由器分配IP的规则

    情况是这样的: VMware中有三个Linux机器分别是crxy99(192.168.1.99),crxy100(192.168.1.100),crxy101(192.168.1.101),crxy1 ...

  8. ansible 配置了端口在host文件但是还要走22 ip:60001 ansible_ssh_port=60001

    fatal: [101.251.194.102]: UNREACHABLE! => {"changed": false, "msg": "Fai ...

  9. Linux环境下Jmeter 报错:Unable to get local host IP address

    主要是没有在host中配置本机ip hostname查看本机名 [root@test task]# hostname test [root@test task]# 打开 [root@test task ...

随机推荐

  1. void void*

    void类型及void指针 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在使用上出现了一些错误.本文将对void关键字的深刻含义进行解说,并 详述void及void指 ...

  2. $.ajax参数备注-转转转

    jquery中的ajax方法参数总是记不住,这里记录一下.  $,ajax()方法参数详解 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为St ...

  3. Jquery div边框大全

    网址 http://jquery.malsup.com/corner/ jQuery Corner是一款jQuery的插件,最初由Dave Methvin开发,但后在Malsup同志的协助下,进行了一 ...

  4. MySql 在大数量的统计中具体的使用技巧

    一.CASE WHEN THEN ELSE END 使用用法. 在用sql语句统计某字段的某种状态的出现的次数,可以考虑用到 CASE WHEN THEN ELSE END 使用用法.当数据量过于庞大 ...

  5. python之全栈开发——————IO模型

    一:在讲IO模型之前我们首先来讲一下事件驱动模型,属于一种编程的范式,那么我们以前就是传统式编程,来看看有什么区别吧(此处为借鉴别人的) 传统的编程是如下线性模式的: 开始--->代码块A--- ...

  6. 【python】分片copy和等号的区别

    例一 >>> list1=[1,2,3,4]>>> list2=list1>>> list3=list1[:]>>> list1 ...

  7. ConfigParser---python

    # !/usr/bin/python # Filename:tcfg.py import ConfigParser config = ConfigParser.ConfigParser() confi ...

  8. 2D image convolution

    在学习cnn的过程中,对convolution的概念真的很是模糊,本来在学习图像处理的过程中,已对convolution有所了解,它与correlation是有不同的,因为convolution = ...

  9. java 常用基本数据类型的默认值

    在使用基本数据类型作为类成员的时候,有时只初始化了而没有给变量赋值,那么此时,java会给你的变量赋一个默认初始值. boolean        false char              '/ ...

  10. bzoj 3144: [Hnoi2013]切糕 最小割

    3144: [Hnoi2013]切糕 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 375[Submit][Status] ...