InetAddress对域名进行解析是使用本地机器配置或者网络命名服务(如域名系统(Domain Name System,DNS)和网络信息服务(Network Information Service,NIS))来实现。对于DNS来说,本地需要向DNS服务器发送查询的请求,然后服务器根据一系列的操作,返回对应的IP地址,为了提高效率,通常本地会缓存一些主机名与IP地址的映射,这样访问相同的地址,就不需要重复发送DNS请求了。在java.net.InetAddress类同样采用了这种策略。在默认情况下,会缓存一段有限时间的映射,对于主机名解析不成功的结果,会缓存非常短的时间(10秒)来提高性能。
1.2. InetAddress对象的获取
InetAddress的构造函数不是公开的(public),所以需要通过它提供的静态方法来获取,有以下的方法:
static InetAddress[] getAllByName(String host)
static InetAddress getByAddress(byte[] addr)
static InetAddress getByAddress(String host,byte[] addr)
static InetAddress getByName(String host)
static InetAddress getLocalHost()
在这些静态方法中,最为常用的应该是getByName(String host)方法,只需要传入目标主机的名字,InetAddress会尝试做连接DNS服务器,并且获取IP地址的操作。代码片段如下,注意我们假设以下的代码,都是默认导入了java.net中的包,在程序的开头加上import java.net.*,否则需要指定类的全名
java.net.InetAddress。 InetAddress address=InetAddress.getByName("www.baidu.com");注意到这些方法可能会抛出的异常。如果安全管理器不允许访问DNS服务器或禁止网络连接,SecurityException会抛出,如果找不到对应主机的IP地址,或者发生其他网络I/O错误,这些方法会抛出UnknowHostException。所以需要写如下的代码:
java.net.InetAddress。 InetAddress address=InetAddress.getByName("www.baidu.com");下面是一则完整的例子:
import java.net.InetAddress; /** * this example is used to show how to get InetAddress instance * @author Dakiler */ public class Example1 { public static void main(String args[])throws Exception { InetAddress address=InetAddress.getByName("www.baidu.com"); System.out.println(address); System.out.println("-----"); InetAddress[] addresses=InetAddress.getAllByName("www.baidu.com"); for(InetAddress addr:addresses) { System.out.println(addr); } } }运行结果如下:
package org.dakiler.javanet.chapter1; import java.net.InetAddress; public class Example2 { public static void main(String args[])throws Exception { InetAddress address=InetAddress.getLocalHost(); System.out.println(address); } }这个例子首先是根据InetAddress.getLocalHost()方法获取本地IP地址,然后通过System.out.println()打印出来,结果如下: