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. /etc/rc.local ; /etc/init.d ;/etc/profile;/etc/bashrc;~/.bash_profile;~/.bashrc;~/.bash_logout

    1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里. 把脚本程序写在/etc/rc.d/init.d/目录下也可以  在完成 run level 3 ...

  2. css reset浅谈

    我们都知道,web开发中浏览器兼容性是困扰很多开发者的一个问题.所谓兼容性问题,即不同浏览器对同一段代码有不同的解析效果.而我们的需求往往是无论用户使用何种浏览器查看我们的网站,都应该获得相同或相近的 ...

  3. 以中断方式实现1s定时

    中断方式比较特殊,需要使用单片机内部的中断处理机制,同时指定中断函数. #include <reg52.h> sbit LED = P0^; unsigned ; void main() ...

  4. appium的安装过程(图文界面)

    资料来源:http://www.cnblogs.com/fnng/p/4560298.html 1.准备安装材料

  5. iOS 9 适配

    一.iOS9 bitcode首先最大的问题就是坑货xcode7,xcode7默认是打开bitcode的,bitcode是苹果为了解决他自己以后的应用可以随意更换硬件的处理做的准备也就是arm指令集和x ...

  6. 在ajax当中使用url重写来避免url的暴露

    记得一次面试,有这样一道面试题:jsp页面当中需要用到ajax的实现,此时需要调用java的url:此时的问题是如果用户查看页面源码就能看到真是的url,这个问题如何避免.说实话,AJAX我用的只是皮 ...

  7. python学习之---匿名函数,返回函数,偏函数

    1. 返回函数: 所谓的返回函数,指的是函数作为返回值.高阶函数除了可以接受函数作为参数外,同样可以接受函数作为结果返回.以下是一个可变参数的求和例子,一般求和函数是如此这般定义的: >> ...

  8. 【BZOJ 3110】 [Zjoi2013]K大数查询(整体二分)

    [题目] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到 ...

  9. 【UVA 1395】 Slim Span (苗条树)

    [题意] 求一颗生成树,满足最大边和最小边之差最小 InputThe input consists of multiple datasets, followed by a line containin ...

  10. 基于Maven的spring_security入门

    配置文件的修改点没什么变化,可以参考:http://blog.csdn.net/ouitiken/article/details/8830505 pom.xml的依赖参考: <dependenc ...