InetAddress类的一些方法



一、getHostName() 方法

    定义:public String getHostName()

    根据创建InetAddress对象的不同方式,getHostName的返回值是不同的。



    1. 用 getLocalHost() 方法创建的InetAddress的对象



        此时getHostName返回的是本机名

        InetAddress address=InetAddress.getLocalHost();

        System.out.println(address.getHostName());//返回本机名



    2. 用域名创建 InetAddress对象



        用域名作为getByName和getAllByName方法的参数得到的InetAddress对象,该对象会得到这个域名,当调用

        getHostName时,就无需再访问DNS服务器,而是直接将这个域名返回。



InetAddress address=InetAddress.getByName("ZZQ");

System.out.println(address.getHostName());//不必再访问DNS服务器,直接返回域名

3. 用IP地址 创建InetAddress对象



        使用IP地址创建InetAddress对象(getByName,getAllByName,getByAddress方法都可以通过IP地址创建InetAddress对象)时,并不需要访问DNS服务器。因此,通过DNS服务器查找域名的工作就由getHostName方法来完成。

        如果IP地址不存在或DNS服务器不允许进行IP地址和域名映射,就返回这个IP地址。

InetAddress address=InetAddress.getByName("141.146.8.66");

System.out.println(address.getHostName());//需要访问DNS服务器才能得到域名

InetAddress address=InetAddress.getByName("1.2.3.4");//IP地址不存在

System.out.println(address.getHostName());//直接返回IP地址


二、getCanonicalHostName()方法



    定义:public String getCanonicalHostName()

    该方法和getHostName方法一样,也是得到远程主机的域名。区别是,该方法得到的是主机名,getHostName得到的是主机别名。

    1. 使用 getLocalHost() 创建InetAddress对象



        此时getCanonicalHostName方法和getHostName方法得到的都是本机名



    2. 使用域名 创建InetAddress对象



        使用域名创建InetAddress对象后,getHostName方法不会访问DNS服务器

        但getCanonicalHostName方法就不一定了,这取决于DNS服务器如何解释主机名和主机别名



    3. 使用IP地址 创建InetAddress对象



        此时getCanonicalHostName方法和getHostName方法完全相同,返回的都是主机名,而不是主机别名。



        之所以要使用主机别名,是因为有时主机名可能比较复杂,如Oracle官方网站的主机名bigip-otn-

        portal.oracle.com,因此,为了使用户访问网站更方便,就增加了更简单的主机别名,如 www.oracle.com

三、getHostAddress()方法



    定义:public String getHostAddress()



    该方法用来得到主机的IP地址,这个IP地址可以是IPv4也可以是IPv6的



    无论InetAddress对象是用哪种方式创建,getHostAddress方法都不会访问DNS服务器。

public static void getHostAddressTest() throws UnknownHostException { // 输出IPv4地址 InetAddress ipv4Address1 = InetAddress.getByName("1.2.3.4"); System.out.println("ipv4Address1:" + ipv4Address1.getHostAddress()); //ipv4Address1:1.2.3.4 InetAddress ipv4Address2 = InetAddress.getByName("www.ibm.com"); System.out.println("ipv4Address2:" + ipv4Address2.getHostAddress()); //ipv4Address2:129.42.60.216 InetAddress ipv4Address3 = InetAddress.getByName("ZZQ"); System.out.println("ipv4Address3:" + ipv4Address3.getHostAddress()); //ipv4Address3:192.168.1.105 // 输出IPv6地址 InetAddress ipv6Address1 = InetAddress.getByName("abcd:123::22ff"); System.out.println("ipv6Address1:" + ipv6Address1.getHostAddress()); //ipv6Address1:abcd:123:0:0:0:0:0:22ff

InetAddress ipv6Address2 = InetAddress.getByName("www.neu6.edu.cn"); System.out.println("ipv6Address2:" + ipv6Address2.getHostAddress()); //ipv6Address2:2001:da8:9000:b255:210:5cff:fef5:ac49 // 输出本机全部的IP地址

InetAddress Addresses[] = InetAddress.getAllByName("ZZQ");

for (InetAddress address : Addresses)

System.out.println("本机地址:" + address.getHostAddress());

//本机地址:192.168.1.105

//本机地址:0.1.0.4

//本机地址:0:0:0:0:0:0:0:1 }

四、getAddress()方法



    定义:public byte[] getAddress()



    该方法和getHostAddress方法唯一区别是,getHostAddress返回字符形式的IP地址,getAddress返回byte数组形式的IP地址。

public static void getAddressTest() throws UnknownHostException {

InetAddress address = InetAddress.getByName("www.csdn.net");

byte ip[] = address.getAddress();

for (byte ipSegment : ip)

System.out.print(ipSegment + ".");

System.out.println("");

//-53.81.21.61

for (byte ipSegment : ip) {

int newIPSegment = (ipSegment < 0) ? 256 + ipSegment : ipSegment;

System.out.print(newIPSegment + ".");

}

//203.81.21.61

}

猜你喜欢

转载自hq369.iteye.com/blog/1928149