NetWorkInterface类常用方法

本文深入讲解Java中的NetworkInterface类,涵盖获取网络接口信息、IP地址、硬件地址等关键方法,适用于网络编程初学者和进阶者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NetWorkInterface类常用方法

NetworkInterface类表示一个由名称和分配给此接口的IP地址列表组成的网络接口,也就是NetworkInterface类包含网络接口名称与IP地址列表。该类提供访问网卡设备的相关信息,如可以获取网卡名称、IP地址和子网掩码等。

想要取得NetworkInterface对象,就必须要通过NetworkInterface类的public staticEnumeration<NetworkInterface> getNetworkInterfaces()方法,该方法的返回值是泛型Enumeration<NetworkInterface>,作用是返回此机器上的所有接口。

在这里插入图片描述

获得网络接口的基本信息

public String getName()

取得网络设备在操作系统中的名称。该名称并不能得知具体设备的相关信息,仅仅就是一个代号,多数都以eth开头,后面跟着数字序号,如eth0、eth1、eth2 和eth3等这样的格式,但序号并不一定是连续的。

public int getIndex()

取得设备在操作系统中的显示名称。此方法返回的字符串包含厂商名称和网卡具体型号等相关信息,此方法返回的信息是对getName()返回信息的丰富化。

public int getIndex()
获得网络接口的索引。此索引值在不同的操作系统中有可能不一样。索引是大于或等于0的整数,索引未知时,值就是-1。

public boolean isUp()
判断网络接口是否已经开启并正常工作。

public boolean isLoopback()

判断该网络接口是否为localhost 回调/回环接口。什么是回调/回环接口?如果一个网络设备是一个回环/回调网络接口,那么它永远工作,并且还是虚拟的,也就是计算机上并不存在这样的硬件网络设备,那么它存在的意义是什么呢?如果某一台计算机没有安装物理硬件网卡,但安装了Tomcat 后想访问Tomcat, 就可以使用地址localhost或127.0.0.1 进行访问。这里的localhost和127.0.0.1 就是回调/回环地址,这时回调地址的作用就体现出来了:没有网卡,使用回调/回环地址就能访问Tomcat。

public class NetworkInterfaceDemo {
    public static void main(String[] args) throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        int count = 0;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface next = networkInterfaces.nextElement();
            System.out.println("getName获得网络设备名称=" + next.getName());
            System.out.println("getDisplayName获得网络设备显示名称=" + next.getDisplayName());
            System.out.println("getIndex获得网络接口的索引=" + next.getIndex());
            System.out.println("isUp是否已经开启并运行=" + next.isUp());
            System.out.println("isBoopback是否为回调接口=" + next.isLoopback());
            System.out.println("**********************" + count++);
        }
    }
}

输出:

getName获得网络设备名称=utun1
getDisplayName获得网络设备显示名称=utun1
getIndex获得网络接口的索引=16
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************0
getName获得网络设备名称=utun0
getDisplayName获得网络设备显示名称=utun0
getIndex获得网络接口的索引=15
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************1
getName获得网络设备名称=llw0
getDisplayName获得网络设备显示名称=llw0
getIndex获得网络接口的索引=9
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************2
getName获得网络设备名称=awdl0
getDisplayName获得网络设备显示名称=awdl0
getIndex获得网络接口的索引=8
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************3
getName获得网络设备名称=en5
getDisplayName获得网络设备显示名称=en5
getIndex获得网络接口的索引=4
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************4
getName获得网络设备名称=en0
getDisplayName获得网络设备显示名称=en0
getIndex获得网络接口的索引=6
isUp是否已经开启并运行=true
isBoopback是否为回调接口=false
**********************5
getName获得网络设备名称=lo0
getDisplayName获得网络设备显示名称=lo0
getIndex获得网络接口的索引=1
isUp是否已经开启并运行=true
isBoopback是否为回调接口=true
**********************6

在这里插入图片描述

结论:

  1. 网络设备的索引有可能不连续
  2. isLoopback()方法针对lo设备返回true,其它均为false,因为系统中只有一个回调/回环地址
  3. isUp()方法返回都是true(因为都在正常工作,拔掉网线就只剩lo设备为true了)
  4. getDisplayName(0的返回值是有据可查的

获取MTU的大小

public int getMTU()

返回MTU的大小。在网络传输中是以数据包为基本传输单位,可以使用MTU(Maximum Transmission Unit,最大传输单元)来规定网络传输最大数据包的大小,单位为字节。以太网的网卡MTU大多数默认值是1500字节,在IPv6协议中,MTU的范围是1280 ~ 65 535。MTU值设置的大小与传输效率有关,如果MTU设置大值,则传输速度很快,因为发送的数据包数量少了,但延迟很大,因为对方需要一点一点地处理数据;如果MTU设置小值,则传输速度慢,因为发送的数据包数量多了。建议不要随意更改网卡的MTU值,因为有可能造成网络传输数据故障,致使数据传输不完整,发生丢包的现象。

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        int count = 0;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface next = networkInterfaces.nextElement();
            System.out.println("getName获取网络设备名称="  + next.getName());
            System.out.println("getDisplayName获得网络设备显示名称="  + next.getDisplayName());
            System.out.println("getMTU获得最大传输单元=" + next.getMTU());
            
            System.out.println("**********************" + count++);
        }
getName获取网络设备名称=utun1
getDisplayName获得网络设备显示名称=utun1
getMTU获得最大传输单元=2000
**********************0
getName获取网络设备名称=utun0
getDisplayName获得网络设备显示名称=utun0
getMTU获得最大传输单元=1380
**********************1
getName获取网络设备名称=llw0
getDisplayName获得网络设备显示名称=llw0
getMTU获得最大传输单元=1500
**********************2
getName获取网络设备名称=awdl0
getDisplayName获得网络设备显示名称=awdl0
getMTU获得最大传输单元=1484
**********************3
getName获取网络设备名称=en5
getDisplayName获得网络设备显示名称=en5
getMTU获得最大传输单元=1500
**********************4
getName获取网络设备名称=en0
getDisplayName获得网络设备显示名称=en0
getMTU获得最大传输单元=1500
**********************5
getName获取网络设备名称=lo0
getDisplayName获得网络设备显示名称=lo0
getMTU获得最大传输单元=16384
**********************6

注意:如果MTU的值是不一定的,如果MTU值为-1会出现在网络接口禁用的情况下。

子接口处理

子接口的作用是在不添加新的物理网卡的基础上,基于原有的网络接口设备再创建出一个虚拟的网络接口设备进行通信,这个虚拟的网络接口可以理解成是一个由软件模拟的网卡。Windows 操作系统不支持子接口,而Linux支持。

public Enumeration getSubInterfaces()
返回子接口;

public boolean isVirtual()
判断当前的网络接口是否为“虚拟子接口”。在Linux操作系统上,虚拟子接口作为物理接口的子接口被创建,并给予不同的设置(如IP地址或MTU等)。

通常,虚拟子接口的名称将是父网络接口的名称加上冒号(:), 再加上标识该子接口的编号,因为一个物理网络接口可以存在多个虚拟子接口。需要注意的是,“虚拟接口”也就是非硬件类的网络设备,是由软件模拟的网络设备,这些网络设备并不一定就是“虚拟子接口”,因为有可能该虚拟网络接口没有父网络接口。

  1. 虚拟接口就是软件模拟的,没有父网络接口;
  2. 虚拟子接口也是由软件模拟的,但有父网络接口;
  3. 虚拟接口并不一定就是虚拟子接口,而虚拟子接口一定是虚拟接口。

public NetworkInterface getParent()
获得父接口。一个虚拟的子网络接口必须依赖于父网络接口,可以使用此方法来取得虛拟子网络设备所属的父接口,也就是所属的硬件网卡。

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        int count = 0;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface next = networkInterfaces.nextElement();
            System.out.println("getName获取网络设备名称="  + next.getName());
            System.out.println("getDisplayName获得网络设备显示名称="  + next.getDisplayName());
            System.out.println("isVirtual是否为虚拟接口=" + next.isVirtual());
            System.out.println("getParent获得父接口=" + next.getParent());
            System.out.println("getSubInterfaces取得子接口信息=");
            Enumeration<NetworkInterface> subInterfaces = next.getSubInterfaces();
            while (subInterfaces.hasMoreElements()) {
                NetworkInterface sub = subInterfaces.nextElement();
                System.out.println("\tgetName获取网络设备名称="  + sub.getName());
                System.out.println("\tgetDisplayName获得网络设备显示名称="  + sub.getDisplayName());
                System.out.println("\tisVirtual是否为虚拟接口=" + sub.isVirtual());
                System.out.println("\tgetParent获得父接口的hashCode=" + sub.getParent().hashCode());
                System.out.println();
            }

            System.out.println("**********************" + count++);
        }

获得硬件地址

public byte[] getHardwareAddress()
获得网卡的硬件地址。

什么是硬件地址?硬件地址也称为物理地址,或MAC (Media Access Control,媒体访问控制)。它用来定义网络设备的位置,也是网卡设备的唯一ID, 采用十六进制表示,一共48位。MAC地址包含由IEEE的注册管理机构RA负责给不同厂商分配的唯-标识,因此,正规厂商出厂的网卡的MAC地址永远不会出现重复。

物理地址、MAC地址和硬件地址,这三者的含义是一样的。

//获取mac地址
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        int count = 0;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface next = networkInterfaces.nextElement();
            System.out.println("getName获取网络设备名称="  + next.getName());
            System.out.println("getDisplayName获得网络设备显示名称="  + next.getDisplayName());
            System.out.print("getHardwareAddress获得网卡的物理地址=");

            byte[] hardwareAddress = next.getHardwareAddress();
            if (hardwareAddress != null && hardwareAddress.length != 0) {
                for (int i = 0; i < hardwareAddress.length; i++) {
                    System.out.print(hardwareAddress[i] + " ");
                }
                System.out.println();
            }
            System.out.println();
            System.out.println("**********************" + count++);
        }
getName获取网络设备名称=utun1
getDisplayName获得网络设备显示名称=utun1
getHardwareAddress获得网卡的物理地址=
**********************0
getName获取网络设备名称=utun0
getDisplayName获得网络设备显示名称=utun0
getHardwareAddress获得网卡的物理地址=
**********************1
getName获取网络设备名称=llw0
getDisplayName获得网络设备显示名称=llw0
getHardwareAddress获得网卡的物理地址=50 -100 23 18 105 -13 

**********************2
getName获取网络设备名称=awdl0
getDisplayName获得网络设备显示名称=awdl0
getHardwareAddress获得网卡的物理地址=50 -100 23 18 105 -13 

**********************3
getName获取网络设备名称=en5
getDisplayName获得网络设备显示名称=en5
getHardwareAddress获得网卡的物理地址=-84 -34 72 0 17 34 

**********************4
getName获取网络设备名称=en0
getDisplayName获得网络设备显示名称=en0
getHardwareAddress获得网卡的物理地址=-8 -1 -62 70 39 -28 

**********************5
getName获取网络设备名称=lo0
getDisplayName获得网络设备显示名称=lo0
getHardwareAddress获得网卡的物理地址=
**********************6

获得IP地址

public Enumeration getInetAddresses()
获得绑定到此网络接口的InetAddress列表,此方法返回泛型Enumeration<InetAddress>。

InetAddress类可以表示成互联网协议( IP)地址,通过使用InetAddress对象中的若干方法来获取该IP地址相关信息。一个网络接口可以使用多个IP地址。

InetAddress 类代表IP地址,它有两个子类,分别是Inet4Addressjava和Inet6Address.java,它们用来描述IPv4和IPv6的地址信息。因为InetAddress类没有公共( public)的构造方法,所以它不能直接实例化,要借助它的静态方法来实现对象的创建。

1. 获得IP地址的基本信息

类NetworkInterface中的getInetAddresses()方法的返回值是Enumeration<InetAddress>泛型。

  1. getCanonicalHostName():获取此IP地址的完全限定域名(Fully Qualified Domain Name, FQDN)。完全限定域名是指主机名加上全路径,全路径中列出了序列中所有域成员。

  2. getHostName():获取此IP地址的主机名

  3. getHostAddress():返回IP地址字符串(文本)

  4. getAddress():返回此InetAddress对象的原始IP地址,返回值是byte[]数组

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface next = networkInterfaces.nextElement();
            System.out.println("getName获得网络设备名称=" + next.getName());
            System.out.println("getDisplayName获得网络设备显示名称=" + next.getDisplayName());
            System.out.println("getInetAddresses获得网络接口的InetAddress信息:");
            Enumeration<InetAddress> inetAddresses = next.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress address = inetAddresses.nextElement();
                System.out.println("\tgetCanonicalHostName获取此IP地址的完全限定域名=" + address.getCanonicalHostName());
                System.out.println("\tgetHostName获取此IP地址的主机名=" + address.getHostName());
                System.out.println("\tgetHostAddress返回IP地址字符串=" + address.getHostAddress());
                System.out.println("\tgetAddress返回此InetAddress对象的原始IP地址=");
                byte[] arr = address.getAddress();
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + " ");
                }
                System.out.println();
            }
            System.out.println();
        }
getName获得网络设备名称=utun1
getDisplayName获得网络设备显示名称=utun1
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:d1c:16db:f38d:b4ef%utun1
	getHostName获取此IP地址的主机名=fe80:0:0:0:d1c:16db:f38d:b4ef%utun1
	getHostAddress返回IP地址字符串=fe80:0:0:0:d1c:16db:f38d:b4ef%utun1
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 13 28 22 -37 -13 -115 -76 -17 

getName获得网络设备名称=utun0
getDisplayName获得网络设备显示名称=utun0
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:c7ad:7959:ee19:2743%utun0
	getHostName获取此IP地址的主机名=fe80:0:0:0:c7ad:7959:ee19:2743%utun0
	getHostAddress返回IP地址字符串=fe80:0:0:0:c7ad:7959:ee19:2743%utun0
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 -57 -83 121 89 -18 25 39 67 

getName获得网络设备名称=llw0
getDisplayName获得网络设备显示名称=llw0
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:309c:17ff:fe12:69f3%llw0
	getHostName获取此IP地址的主机名=fe80:0:0:0:309c:17ff:fe12:69f3%llw0
	getHostAddress返回IP地址字符串=fe80:0:0:0:309c:17ff:fe12:69f3%llw0
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 48 -100 23 -1 -2 18 105 -13 

getName获得网络设备名称=awdl0
getDisplayName获得网络设备显示名称=awdl0
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:309c:17ff:fe12:69f3%awdl0
	getHostName获取此IP地址的主机名=fe80:0:0:0:309c:17ff:fe12:69f3%awdl0
	getHostAddress返回IP地址字符串=fe80:0:0:0:309c:17ff:fe12:69f3%awdl0
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 48 -100 23 -1 -2 18 105 -13 

getName获得网络设备名称=en5
getDisplayName获得网络设备显示名称=en5
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:aede:48ff:fe00:1122%en5
	getHostName获取此IP地址的主机名=fe80:0:0:0:aede:48ff:fe00:1122%en5
	getHostAddress返回IP地址字符串=fe80:0:0:0:aede:48ff:fe00:1122%en5
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 -82 -34 72 -1 -2 0 17 34 

getName获得网络设备名称=en0
getDisplayName获得网络设备显示名称=en0
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:83f:7f1d:b90d:e421%en0
	getHostName获取此IP地址的主机名=fe80:0:0:0:83f:7f1d:b90d:e421%en0
	getHostAddress返回IP地址字符串=fe80:0:0:0:83f:7f1d:b90d:e421%en0
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 8 63 127 29 -71 13 -28 33 
	getCanonicalHostName获取此IP地址的完全限定域名=192.168.0.100
	getHostName获取此IP地址的主机名=192.168.0.100
	getHostAddress返回IP地址字符串=192.168.0.100
	getAddress返回此InetAddress对象的原始IP地址=
-64 -88 0 100 

getName获得网络设备名称=lo0
getDisplayName获得网络设备显示名称=lo0
getInetAddresses获得网络接口的InetAddress信息:
	getCanonicalHostName获取此IP地址的完全限定域名=fe80:0:0:0:0:0:0:1%lo0
	getHostName获取此IP地址的主机名=fe80:0:0:0:0:0:0:1%lo0
	getHostAddress返回IP地址字符串=fe80:0:0:0:0:0:0:1%lo0
	getAddress返回此InetAddress对象的原始IP地址=
-2 -128 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
	getCanonicalHostName获取此IP地址的完全限定域名=localhost
	getHostName获取此IP地址的主机名=localhost
	getHostAddress返回IP地址字符串=0:0:0:0:0:0:0:1%lo0
	getAddress返回此InetAddress对象的原始IP地址=
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
	getCanonicalHostName获取此IP地址的完全限定域名=localhost
	getHostName获取此IP地址的主机名=localhost
	getHostAddress返回IP地址字符串=127.0.0.1
	getAddress返回此InetAddress对象的原始IP地址=
127 0 0 1 

2. 获得本地主机和回环地址的基本信息
static InetAddress getLocalHost()返回本地主机的IP地址信息。

如果本机拥有多个IP,则getLocalHost()方法只返回下标为[0]的第一个IP。如果想返回本机全部的IP,就需要使用getAllByName()方法。

在JDK源代码中,getLocalHost() 方法与getAlByName()方法调用相同的方法:

private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr),来实现取得InetAddress[]数组。

static InetAddress getLoopbackAddress():返回回环/回调的IP地址信息;

InetAddress localHost = InetAddress.getLocalHost();
        System.out.println("localhost.getAddress()地址为=");
        byte[] address = localHost.getAddress();
        for (int i = 0; i < address.length; i++) {
            System.out.print(" " + address[i] + " ");
        }
        System.out.println();
        System.out.println(" " + localHost.getClass().getName());
        System.out.println();
        System.out.println("inetAddress.getLoopvackAddress()地址为=");

        InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
        byte[] loopbackAddressAddress = loopbackAddress.getAddress();
        for (int i = 0; i < loopbackAddressAddress.length; i++) {
            System.out.print(" " + loopbackAddressAddress[i] + " ");
        }
        System.out.println();
        System.out.println(" " + localHost.getClass().getName());
localhost.getAddress()地址为=
 -64  -88  0  100 
 java.net.Inet4Address

inetAddress.getLoopvackAddress()地址为=
 127  0  0  1 
 java.net.Inet4Address

3. 根据主机名获得IP地址

static InetAddress getByName(String host):在给定主机名的情况下确定主机的IP地址。

参数可以是:计算机名、IP地址也可以是域名。

//3. 根据主机名获取ip
        InetAddress baiduAddress = InetAddress.getByName("www.baidu.com");
        InetAddress local1 = InetAddress.getByName("192.168.0.100");
        InetAddress local2 = InetAddress.getByName("localhost");
        InetAddress wrong1 = InetAddress.getByName("wwww.xxxdk2235dfs24234.com");//不存在的域名
        InetAddress wrong2 = InetAddress.getByName("192.168.0.8888");//不存在的ip

        System.out.println(baiduAddress.getClass().getName() + " " + baiduAddress.getHostAddress());
        System.out.println(local1.getClass().getName() + " " + local1.getHostAddress());
        System.out.println(local2.getClass().getName() + " " + local2.getHostAddress());
        System.out.println(wrong1.getClass().getName() + " " + wrong1.getHostAddress());
        System.out.println(wrong2.getClass().getName() + " " + wrong2.getHostAddress());

输出:

java.net.Inet4Address 220.181.38.149
java.net.Inet4Address 192.168.0.100
java.net.Inet4Address 127.0.0.1


//以下这两个不应该打印出来的,之所以不存在域名和ip能解析出结果,就是因为被万恶的中国电信劫持了...
java.net.Inet4Address 218.30.64.194
java.net.Inet4Address 218.30.64.194

4. 根据主机名获得所有的IP地址

static InetAddress[] getAllByName(String host):在给定主机名的情况下,根据系统上配置的名称服务返回其IP地址所组成的数组。

InetAddress[] baiduAddressArray = InetAddress.getAllByName("www.baidu.com");
        InetAddress[] myAddressArray = InetAddress.getAllByName("localhost");

        for (int i = 0; i <  baiduAddressArray.length; i++) {
            InetAddress inetAddress = baiduAddressArray[i];
            System.out.println(inetAddress.getHostAddress() + " " + inetAddress.getClass().getName());
        }
        System.out.println();
        for (int i = 0; i <  myAddressArray.length; i++) {
            InetAddress inetAddress = myAddressArray[i];
            System.out.println(inetAddress.getHostAddress() + " " + inetAddress.getClass().getName());
        }
220.181.38.150 java.net.Inet4Address
220.181.38.149 java.net.Inet4Address

127.0.0.1 java.net.Inet4Address
0:0:0:0:0:0:0:1 java.net.Inet6Address

5. 根据IP地址byte[]addr获得InetAddress对象

static InetAddress getByAddress(byte[] addr):在给定原始IP地址的情况下,返回InetAddress对象。

参数按网络字节顺序:地址的高位字节位于getAddress()[0]中

byte[] byteArray = new byte[] {-64, -88, 0, 102};
        InetAddress myaddress = InetAddress.getByAddress(byteArray);
        System.out.println("myAddress.getHostAddress()=" + myaddress.getHostAddress());
        System.out.println("myAddress.getHostName()=" + myaddress.getHostName());
        System.out.println("myAddress.getClass()=" + myaddress.getClass().getName());
myAddress.getHostAddress()=192.168.0.102
myAddress.getHostName()=192.168.0.102
myAddress.getClass()=java.net.Inet4Address

6. 根据主机名和IP地址byteaddr获得InetAddress对象

static InetAddress getByAddress(String host, byte[] addr)
根据提供的主机名和IP地址创建InetAddress,并不对host的有效性进行验证。

其中参数host仅仅是参数addr的一个说明及备注,代表addr这个地址所属的主机名是host。

byte[] byteArray = new byte[] {-64, -88, 0, 102};
        InetAddress myaddress = InetAddress.getByAddress("xxxxxxxx", byteArray);
        System.out.println("myAddress.getHostAddress()=" + myaddress.getHostAddress());
        System.out.println("myAddress.getHostName()=" + myaddress.getHostName());
        System.out.println("myAddress.getClass()=" + myaddress.getClass().getNam

结果与上面示例5一致。

7. 获得全限主机名和主机名

getCanonicalHostName()
取得主机完全限定域名,而getHostName()方法是取得主机别名。

//全限主机名和主机名
        //使用getLocalHost创建InetAddress
        //getCanonicalHostName()和getHostName()都是本地名称
        InetAddress address1 = InetAddress.getLocalHost();
        System.out.println("A1 " + address1.getCanonicalHostName());//mac环境返回ip
        System.out.println("A1 " + address1.getHostName());
        System.out.println();

        //使用域名创建InetAddress
        InetAddress address2 = InetAddress.getByName("www.ibm.com");
        System.out.println("B1 " + address2.getCanonicalHostName());
        System.out.println("B1 " + address2.getHostName());
        System.out.println();

        //使用IP地址创建InetAddresss
        //getCanonicalHostName()和getHostName()结果都是IP地址
        InetAddress address3 = InetAddress.getByName("14.215.177.38");
        System.out.println("C1 " + address3.getCanonicalHostName());
        System.out.println("C1 " + address3.getHostName());
A1 192.168.0.100
A1 192.168.0.100

//由于DNS服务处理的原因,有时输出两个 www.ibm.com
B1 210.192.117.89
B1 www.ibm.com

C1 14.215.177.38
C1 14.215.177.38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值