How do I use a host name to look up an IP address?
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?的更多相关文章
- 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 ...
- 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' ...
- 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 ...
- 错误RSA host key for [ip address] has changed and you have requested strict checking.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION HAS ...
- 解决办法: RSA host key for [ip address] has changed and you have requested strict checking.
在服务器重装后想要远程连接服务器,报错如下: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE ...
- 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 ...
- 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 ...
- ansible 配置了端口在host文件但是还要走22 ip:60001 ansible_ssh_port=60001
fatal: [101.251.194.102]: UNREACHABLE! => {"changed": false, "msg": "Fai ...
- Linux环境下Jmeter 报错:Unable to get local host IP address
主要是没有在host中配置本机ip hostname查看本机名 [root@test task]# hostname test [root@test task]# 打开 [root@test task ...
随机推荐
- 【原创】Android开发之ADB及ADB SHELl命令的应用
adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.adb的工作方式比 ...
- Improved Semantic Representations From Tree-Structured Long Short-Term Memory Networks(1)
今天和陈驰,汪鑫讨论了一下,借此记录一下想法. 关于这篇论文,要弄清的地方有: 1.LSTMtree到底是从上往下还是从下往上学的,再确认一下 2.关于每个节点的标注问题 3.label的值到底该怎么 ...
- windows 远程桌面连接ubuntu xrdp 只看到墙纸其他什么都没有
用 windows 的 mstsc 连接 ubuntu 的 xrdp 时,进入后只看到墙纸,其他什么都没有,鼠标指针也不见,输入按键都无反应. 原来 Ubuntu 启动了 3d 桌面,导致 xrdp ...
- shell脚本学习积累笔记(第一篇)
(1)首先,今天在执行shell脚本./test.sh时抛出“/bin/sh^M: bad interpreter: No such file or directory”的异常,百度后,才知道这是由于 ...
- Aircrack-ng官方文档翻译[中英对照]---Aireplay-ng
Aircrack-ng官方文档翻译---Aireplay-ng[90%] Description[简介] Aireplay-ng is used to inject frames. Aireplay- ...
- python manage.py shell之后的一些错误:
1. 在执行python manage.py shell之后的一些错误: wyl@wyl:~/myobject$ python manage.py shell /usr/lib/python2.7/d ...
- Discuz!X3.1 全新安装图文教程
http://www.discuz.net/thread-3456887-1-1.html
- 中国首个 SaaS 模式的云告警平台 iOS 版 APP 上线
今天上午,国内首个 SaaS 模式的云告警平台 OneAlert 正式发布 ios 版 APP,每个 ios 用户,无需电脑,都可以通过手机全程跟踪所有告警,并且可以和每一个成员一键式电话沟通,团队协 ...
- Python还是很重要的,不能丢。学习IF和WHILE
number = 23 guess = int(input('Enter an interger : ')) if guess == number: print('Congratulations, y ...
- [topcoder]BestRoads
http://community.topcoder.com/stat?c=problem_statement&pm=10172&rd=13515 http://community.to ...