套接字地址

在初始化通信的时候,客户端必须指定一个运行服务端程序的主机的IP地址。在Java中,地址可以是数字地址(如IPV4的点分法表示的地址:192.168.43.1;IPV6用冒号隔开的地址:fe20:12a0::0abc:1234)组成的字符串,也可以是由名字(如www.baidu.com)组成的字符串。用名字组成的地址时,该名字必须能被DNS解析到指定主机,不然会报异常。

Java中的java.net.InetAddress类代表了一个网络目的地,其中包含了名字和地址信息。它有两个子类Inet4AddressInet6Address,分别代表了IPV4和IPV6。InetAddress的实例是不可变的,一旦被创建只能指向同一个地址。InetAddress的实例由IP地址和可能存在的与IP绑定的域名组成(格式为域名/IP地址)。

下面的例子,打印了与本地主机相关联的所有IPV4和IPV6地址。

想要获得本地主机的地址,首先需要借用Java的网络接口类(Network Interface)java.net.NetworkInterface(是一个final类)。这个类的方法可以访问一个主机所有的接口信息,这是非常有用的,可以利用它将一个程序的地址告诉另一个程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.net.*;
import java.util.Enumeration;

public class InetAddressExample {
public static void main(String[] args) {
public class InetAddressExample {
public static void main(String[] args) {
//获取当前主机的网络接口以及对应的地址,一个接口可能有多个地址
try {
Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();
if (interfaceList == null) {
System.out.println("--- There is no INTERFACE");
} else {
while(interfaceList.hasMoreElements()){
NetworkInterface networkInterface = interfaceList.nextElement();
System.out.println("Interface " + networkInterface.getName() + ":");
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
if(!inetAddresses.hasMoreElements()){
System.out.println("Has no address bound to this interface");
}
while (inetAddresses.hasMoreElements()){
InetAddress inet = inetAddresses.nextElement();
System.out.print("\tAddress" +
((inet instanceof Inet4Address ? "(v4)" :
(inet instanceof Inet6Address ? "(v6)" : "(?)"))));
System.out.println(":" + inet.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
1
public final class NetworkInterface extends Object

此类表示由名称和分配给此接口的IP地址列表组成的网络接口。它用于标识多播组加入的本地接口。接口通常以诸如“le0”之类的名称而为人所知。

static Enumeration<NetworkInterface> getNetworkInterfaces():返回此计算机上所有的接口。

static NetworkInterface getByInetAddress(InetAddress addr):方便的查找绑定了指定IP的接口。

static NetworkInterface getByName(String name):查找绑定了指定名称的接口。

String getName() :获取此接口的名称。This generally consists of an alphabetic string followed by a numeric part, for example eth0. The loopback interface is named lo0 on many systems.

Enumeration<InetAddress> getInetAddresses() 返回绑定到此接口的全部或部分网络地址。

The first method above is quite useful, making it easy to learn an IP address of the host a program is running on: you get the list of interfaces with getNetworkInterfaces(), and use the getInetAddresses() instance method to get all the addresses of each. (the list contains all the interfaces of the host, including the loopback virtual interface, which cannot send or receive messages to the rest of the network. Similarly, the list of addresses may contain link￾local addresses that also are not globally reachable. Since the order is unspecified, you cannot simply take the first address of the first interface and assume it can be reached from the Internet; instead, use the property-checking methods of InetAddress (see below) to find one that is not loopback, not link-local, etc.)

1
2
// java.util
public interface Enumeration<E>

Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素。这种传统接口已被迭代器取代,虽然Enumeration 还未被遗弃,但在现代代码中已经被很少使用了。尽管如此,它还是使用在诸如Vector和Properties这些传统类所定义的方法中,除此之外,还用在一些API类,并且在应用程序中也广泛被使用。 下表总结了一些Enumeration声明的方法:

boolean hasMoreElements() 测试此枚举是否包含更多元素

Object nextElement() 如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。

获取到的本地接口及地址

下面的例子展示了从命令行获取的host对应的名字和地址信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.net.*;
import java.util.Enumeration;

public class InetAddressExample {
public static void main(String[] args) {
for(var host : args){
try {
System.out.println(host + ":");
InetAddress[] address = InetAddress.getAllByName(host);
for(var add : address){
System.out.println("\t" + add.getHostName()
+ "/" + add.getHostAddress());
}
} catch (UnknownHostException e) {
System.out.println("\tUnable to find address for " + host);
}
}
}
}
1
// 命令行参数:www.baidu.com 127.0.0.1 blah.blah blog.buercheng.space

通过命令行参数获取主机的名字和地址

1
2
// java.net
public class InetAddress extends Object implements Serializable

这个类代表一个IP地址,格式:hostname / literal IP address。也可能不包含主机名。

  • 创建和获取InetAddress

    • static InetAddress[] getAllByName(String host):给定一个主机域名,返回绑定在这个域名的所有IP地址。
    • static InetAddress getByName(String host):给定一个主机域名,返回其IP地址。
    • static InetAddress getLocalHost():返回本地主机的域名。
    • byte[] getAddress():返回当前对象的IP地址的字节数组格式,IPV4为四个字节,IPV6为16位。
  • 获取字符串类型的InetAddress

    • String toString():重写了toString()方法,将IP地址转换为字符串类型。返回的格式:hostname /literal IP address。如果没有机名,则不会进行反向解析,hostname字段返回空。

    • String getHostAddress():仅返回IP地址的字符串格式。

    • String getHostName():仅返回主机名。如果初始化该实例时,只给定了一个域名,则直接返回该域名,而不会进行解析。否则,该方法只会返回地址信息中的主机名。

    • String getCanonicalHostName():仅返回主机名。与上一个方法不同,该方法尝试获取该地址的绝对域名(fully qualified domain name),注意此方法返回的主机名可能与调用的对象的主机名不同。

      以上方法,如果未指定名字,则返回IP地址。

  • InetAddress还有支持检查原子特性checking for properties的方法:检查是否是特殊域名(如私有域名)及可达性reachability。

    • boolean isAnyLocalAddress():检测是否是通配符地址wildcard addres。
    • boolean isLinkLocalAddress():检测是否是链路本地地址。
    • boolean isLoopbackAddress():检测是否是回环地址。
    • boolean isMulticastAddress():检测是否是多播地址
    • the isMC...() methods check for various scopes of multicast address.The scope determines, roughly, how far packets addressed to that destination can travel from
      their origin.
      • boolean isMCGlobal():检测是否是全局范围的多播地址。
      • boolean isMCLinkLocal():检测多播地址是否包含链路本地地址
      • boolean isMCNodeLocal():检测多播地址是否包含node地址
      • boolean isMCOrgLocal():检测多播地址是否包含organization地址
      • boolean isMCSiteLocal():检测多播地址是否包含site地址
    • 检测是否能够同该InetAddress指定的主机进行包交换。Note that, unlike the other methods, which involve simple syntactic checks, these methods cause the networking system to take action, namely sending packets. The system attempts to send a packet until the specified number of milliseconds passes. The latter form is more specific: it determines whetherthe destination can be contacted by sending packets out over the specified NetworkInterface, with the specified time-to-live (TTL) value. The TTL limits the distance a packet can travel through the network. Effectiveness of these last two methods may be limited by the security manager configuration.
      • boolean isReachable(int timeout):
      • boolean isReachable(NetworkInterface netif, int ttl, int timeout):

InetAddress类常用方法演示

创建InetAddress实例的方法

  • public static InetAddress getByName(String host) throws UnknownHostException
  • static InetAddress[] getAllByName(String host) throws UnknownHostException
  • public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
  • public static InetAddress getByAddress(String host,byte[] addr)throws UnknownHostException
  • public static InetAddress getLocalHost() throws UnknownHostException
  • public static InetAddress getLoopbackAddress()

getByName(String host)

1
public static InetAddress getByName(String host)  throws UnknownHostException

给定主机名,返回该主机IP地址。主机名可以是域名,比如”www.baidu.com”;也可以是IP地址的字符串形式。如果给定的是IP地址,只检查其格式是否正确。如果给定主机名是null或者host.length()==0,那么则返回回环地址。如果给定主机无IP绑定,或者给定的是一个IPV6的范围地址( scoped address),抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetByName {
public static void main(String[] args) {
//InetAddress getByName(String host)
//给定主机名,获取其IP地址。主机名可以是域名,可以是IP地址
try {
//主机名为域名
InetAddress address1 = InetAddress.getByName("www.baidu.com");
System.out.println(address1);

//主机名为IP
InetAddress address2 = InetAddress.getByName("14.215.177.38");
System.out.println(address2);

//主机名为空
InetAddress address3 = InetAddress.getByName("");
System.out.println(address3);

//主机名为null
InetAddress address4 = InetAddress.getByName(null);
System.out.println(address4);

//主机名未绑定IP
InetAddress address5 = InetAddress.getByName("www.buercheng.space");
System.out.println(address5);

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201101193927456

getAllByName(String host)

1
static InetAddress[]	getAllByName(String host)   throws UnknownHostException

给定主机名字(可以是IP地址,可以是域名),返回与其绑定的所有IP地址组成的数组(IP地址由系统配置的域名服务提供)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class TestInetAddress {
public static void main(String[] args) {
//主机名为域名
String host = "www.baidu.com";
try {
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress address : addresses) {
System.out.println(address);
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201101100812594

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class TestInetAddress {
public static void main(String[] args) {
//主机名为IP
String host = "192.168.43.1";
try {
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress address : addresses) {
System.out.println(address);
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201101101140998

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;


public class TestInetAddress {
public static void main(String[] args) {
String host = "host";
try {
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress address : addresses) {
System.out.println(address);
}
} catch (UnknownHostException e) {
System.out.println("未知主机异常");
}

}
}

会尝试解析,所以会运行一段时间,没有找到结果,报异常

image-20201101102151753

getByAddress(byte[] addr)

1
public static InetAddress getByAddress(byte[] addr)  throws UnknownHostException

根据给定的IP地址组成的字节数组,生成一个InetAddress对象。不执行反向域名解析(不对IP地址绑定的域名进行查询)。IP地址为非法长度时,抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetByAddress {
public static void main(String[] args) {
//static InetAddress getByAddress(byte[] addr):
// 通过给定的由IP地址组成的字节数组,获取指定的主机名称
byte[] addr = {(byte)192, (byte)168, 43, 1};
try {
InetAddress address = InetAddress.getByAddress(addr);
System.out.println(address);
} catch (UnknownHostException e) {
System.out.println("未知主机地址");
}
}
}

image-20201101174953529

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetByAddress {
public static void main(String[] args) {
//static InetAddress getByAddress(byte[] addr):
// IP地址长度不对
byte[] addr = {(byte) 192, (byte) 168, 43, 1, 1};
try {
InetAddress address = InetAddress.getByAddress(addr);
System.out.println(address);
} catch (UnknownHostException e) {
System.out.println("未知主机地址");
}
}
}

image-20201101175225153

getByAddress(String host, byte[] addr)

1
public static InetAddress getByAddress(String host,byte[] addr)throws UnknownHostException

通过给定的主机名和IP地址创建一个InetAddress实例,不会检查地址的合理性。

主机名可以是域名也可以是IP地址。御冥夜不会进行合理性检查。

如果给定的是IPV4地址则返回一个Inet4Address对象,给定的是IPV6地址则返回Inet6Address对象。

当IP地址的长度不合法时,抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetByAddress2 {
public static void main(String[] args) {

try {
//域名/IPV4
byte[] add1 = {(byte) 192, (byte) 168, 43, 1};
InetAddress address1 = InetAddress.getByAddress("localhost", add1);
System.out.println(address1);

//IPV4/IPV6
byte[] add2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
InetAddress address2 = InetAddress.getByAddress("127.0.0.1", add2);
System.out.println(address2);

//长度不对时,抛出异常
byte[] add3 = {0, 0, 0, 0, 0, 0, 1};
InetAddress address3 = InetAddress.getByAddress("1localhost", add3);
System.out.println(address3);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201102192437569

getLocalHost()

1
public static InetAddress getLocalHost()  throws UnknownHostException

返回本地主机网络地址。通过系统找到主机域名,再返回一个InetAddress实例。

本地域名不能被处理为InetAddress时抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetLocalHost {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201102210510287

getLoopbackAddress()

1
public static InetAddress getLoopbackAddress()

返回环回地址。如果是IPV4地址,则返回127.*.*.*中的一个;IPV6则返回::1

1
2
3
4
5
6
7
8
9
10
11
package InetAddress;

import java.net.InetAddress;

public class GetLoopbackAddress {
public static void main(String[] args) {
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
System.out.println(loopbackAddress);
}
}

image-20201102213715865

获取InetAddress实例的信息的方法

  • byte[] getAddress()
  • String getHostAddress()
  • String getHostName()
  • getCanonicalHostName()

byte[] getAddress()

1
public byte[] getAddress()

返回调用该实例的对象的IP地址的字节数组形式。

这个方法返回的 byte数组是有符号的。在Java中byte类型的取值范围是-128〜127。如果返回的IP地址的某个字节是大于127的整数,在byte数组中就是负数。由于Java中没有无符号byte类型,因此,要想显示正常的IP地址,必须使用int或long类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetAddress {
public static void main(String[] args) {
try {
// 用getByName()方法创建一个InetAddress实例
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
// getAddress()方法获取IP地址的字节数组
byte[] addresses = inetAddress.getAddress();
// IP地址中的某一位如果大于127会得到一个负数
for (byte address : addresses) {
System.out.print(address + " ");
}
System.out.println();
// 对于IP地址中大于127的数字需要进行处理
for (byte address : addresses) {
int newAddress = (address < 0) ? 256 + address : address;
System.out.print(newAddress + " ");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}

}
}

image-20201102221728984

String getHostAddress()

1
public String getHostAddress()

返回IP地址的字符串形式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetHostAddress {
public static void main(String[] args) {
try {
// 获取实例
InetAddress host = InetAddress.getByName("www.baidu.com");
// 获取实例的IP地址
String hostAddress = host.getHostAddress();
System.out.println(hostAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}

}
}

image-20201102222228979

String getHostName()

1
public String getHostName()

返回调用此方法的对象的域名。如果此对象是用域名方式创建的,则返回创建时使用的域名。否则,会根据系统配置进行反向域名解析。

系统配置:C:\Windows\System32\drivers\etc\host中,20.20.11.2 www.test.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package InetAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetHostName {
public static void main(String[] args) {
//根据IP地址创建对象
byte[] address = {20,20,11,2};
try {
InetAddress host = InetAddress.getByAddress(address);
// 调用getHostName()获取域名
String hostName = host.getHostName();
System.out.println(hostName);

//根据域名获取对象
InetAddress localhost = InetAddress.getByName("localhost");
String hostName1 = localhost.getHostName();
System.out.println(hostName1);

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

image-20201102223809432

getCanonicalHostName()

1
public String getCanonicalHostName()

返回全限定域名。

判断是否是特殊地址的方法

  • boolean isAnyLocalAddress()
  • boolean isLinkLocalAddress()
  • boolean isLoopbackAddress()
  • boolean isMulticastAddress()
  • boolean isMCGlobal()
  • boolean isMCLinkLocal()
  • boolean isMCNodeLocal()
  • boolean isMCOrgLocal()
  • boolean isMCSiteLocal()
  • boolean isReachable(int timeout)
  • boolean isReachable(NetworkInterface netif, int ttl, int timeout)

NetworkInterface常用方法

  • static EnumerationNetworkInterface getNetworkInterfaces()
  • static NetworkInterface getByInetAddress(InetAddress addr)
  • static NetworkInterface getByName(String name)
  • EnumerationInetAddress getInetAddresses()
  • String getName()
  • String getDisplayName()

getNetworkInterfaces()

1
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException

返回此机器上的所有网络接口的枚举类集合。该枚举类集合至少包含一个元素,可能代表只支持本地实例通信的环回接口。

发生IO错误,或者此机器没有配置任何接口时抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package NetworkInterface;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetNetworkInterfaces {
public static void main(String[] args) {
try {
// 获得一个本地机器全部网络接口的枚举类集合
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
// 遍历该集合
while(networkInterfaces.hasMoreElements()){
NetworkInterface networkInterface = networkInterfaces.nextElement();
System.out.println(networkInterface);
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}

image-20201102231952283

getByInetAddress(InetAddress addr)

1
public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException

查找绑定了指定IP地址 的网络接口。

发生IO错误时,抛出SocketException异常。

指定的IP地址为null时,抛出NullPointerException异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package NetworkInterface;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class GetByInetAddress {
public static void main(String[] args) {
try {
// 创建一个InetAddress对象
byte[] ip = {127, 0, 0, 1};
InetAddress inetAddress = InetAddress.getByAddress(ip);
// 通过getByInetAddress()获取该InetAddress的IP地址绑定的接口
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress);
System.out.println(networkInterface);

InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
NetworkInterface anInterface = NetworkInterface.getByInetAddress(inetAddress1);
System.out.println(anInterface);//null NetworkInterface只能获取本地接口

NetworkInterface anInterface1 = NetworkInterface.getByInetAddress(null);
System.out.println(anInterface1);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}

image-20201102234208693

getByName(String name)

1
public static NetworkInterface getByName(String name) throws SocketException

查找指定名称对应的网络接口。如果不存在,则返回null

发生IO错误时,抛出SocketException异常。

指定的IP地址为null时,抛出NullPointerException异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package NetworkInterface;

import java.net.NetworkInterface;
import java.net.SocketException;

public class GetByName {
public static void main(String[] args) {
try {
NetworkInterface networkInterface = NetworkInterface.getByName("lo");
System.out.println(networkInterface);

// 不存在
NetworkInterface networkInterface1 = NetworkInterface.getByName("eee");
System.out.println(networkInterface1);

} catch (SocketException e) {
e.printStackTrace();
}
}
}

image-20201102234450529

getInetAddresses()

1
public Enumeration<InetAddress> getInetAddresses()

返回该网络接口绑定的全部InetAddress或者其子集的一个枚举类集合。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package NetworkInterface;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetInetAddress {
public static void main(String[] args) {
try {
// 先获得一个NetworkInterface对象
NetworkInterface anInterface = NetworkInterface.getByName("lo");
// 再通过getInetAddress()获取其InetAddress的枚举集合
Enumeration<InetAddress> inetAdds = anInterface.getInetAddresses();
// 遍历枚举集合
while(inetAdds.hasMoreElements()){
InetAddress inetAdd = inetAdds.nextElement();
System.out.println(inetAdd);
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}

image-20201102235304239

getName()

1
public String getName()

获取该网络接口的名字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package NetworkInterface;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class GetName {
public static void main(String[] args) {

try {
// 通过InetAddress对象获取一个NetworkInterface对象
InetAddress inetAddress = InetAddress.getByName("localhost");
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress);
// 通过getName方法获取接口名字
String name = networkInterface.getName();
System.out.println(name);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}

image-20201102235715820

getDisplayName()

1
public String getDisplayName()

返回该接口的描述名称。描述名称是易于人阅读的名称。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package NetworkInterface;

import java.net.NetworkInterface;
import java.net.SocketException;

public class GetDisplayName {
public static void main(String[] args) {
//
try {
NetworkInterface anInterface = NetworkInterface.getByName("lo");
String displayName = anInterface.getDisplayName();
System.out.println(displayName);
} catch (SocketException e) {
e.printStackTrace();
}
}
}

image-20201103000045568