Java Java类
此类表示网络接口,包括软件和硬件、其名称、分配给它的 IP 地址列表以及所有相关信息。当我们想专门使用特定接口在具有多个 NIC 的系统上传输数据包时,可以使用它。
什么是网络接口?
网络接口可以被认为是您的计算机连接到网络的一个点。它不一定是硬件,也可以用软件实现。例如,用于测试目的的环回接口。
方法 :
1.getName() :返回此网络接口的名称。
Syntax : public String getName()
2.getInetAddresses() :如果安全管理器允许,则返回绑定到此网络接口的所有 Inetaddresses 的枚举。
Syntax :public Enumeration getInetAddresses()
3.getInterfaceAddresses() :返回此接口上所有接口地址的列表。
Syntax :public List getInterfaceAddresses()
4.getSubInterfaces() :返回此网络接口的所有子接口或虚拟接口的枚举。例如 eth0:2 是 eth0 的子接口。
Syntax :public Enumeration getSubInterfaces()
5.getParent() :如果是子接口,该方法返回父接口。如果这不是子接口,则此方法将返回 null。
Syntax :public NetworkInterface getParent()
6.getIndex() :返回系统分配给这个网络接口的索引。可以使用索引代替长名称来引用设备上的任何接口。
Syntax :public int getIndex()
7.getDisplayName() :该方法以可读的字符串格式返回网络接口的名称。
Syntax :public String getDisplayName()
8.getByName() :查找并返回具有指定名称的网络接口,如果不存在则返回null。
Syntax :public static NetworkInterface getByName(String name)
throws SocketException
Parameters :
name : name of network interface to search for.
Throws :
SocketException : if I/O error occurs.
9.getByIndex() :执行与前一个函数,索引用作搜索参数而不是名称。
Syntax :public static NetworkInterface getByIndex(int index)
throws SocketException
Parameters :
index : index of network interface to search for.
Throws :
SocketException : if I/O error occurs.
10.getByInetAddress() :此方法被广泛使用,因为它返回指定的inetaddress绑定到的网络接口。如果一个 InetAddress 绑定到多个接口,则可以返回任何一个接口。
Syntax : public static NetworkInterface getByInetAddress(InetAddress addr)
throws SocketException
Parameters :
addr : address to search for
Throws :
SocketException : If IO error occurs
11.getNetworkInterfaces() :返回系统上的所有网络接口。
Syntax :public static Enumeration getNetworkInterfaces()
throws SocketException
Throws :
SocketException : If IO error occurs
12.isUp() :返回一个布尔值,指示此网络接口是否已启动并正在运行。
Syntax : public boolean isUp()
13.isLoopback() :返回一个布尔值,指示此接口是否为环回接口。
Syntax : public boolean isLoopback()
14.isPointToPoint() :返回一个布尔值,指示此接口是否为点对点接口。
Syntax : public boolean isPointToPoint()
15.supportsMulticast() :返回一个布尔值,指示此接口是否支持多播。
Syntax : public boolean supportsMulticast()
16.getHardwareAddress() :返回一个字节数组,其中包含该接口的硬件地址(MAC)地址。调用者在调用此方法之前必须具有适当的权限。
public byte[] getHardwareAddress()
17.getMTU() :返回此接口的最大传输单元。 MTU 是可以在基于数据包的网络中发送的数据包或帧的最大大小。
Syntax :public int getMTU()
18.isVirtual() :返回一个布尔值,指示此接口是否为虚拟接口。虚拟接口与物理接口结合使用以提供附加值,例如地址和 MTU。
Syntax : public boolean isVirtual()
19.equals() :此方法用于比较两个网络接口是否相等。如果两个网络接口绑定了相同的名称和地址,则它们是相等的。
Syntax :public boolean equals(Object obj)
Parameters :
obj : Object to compare this network interface for equality
20.hashCode() :返回此对象的哈希码值。
Syntax :public int hashCode()
21.toString() :返回此对象的文本描述。
Syntax :public String toString()
Java实现:
Java
//Java program to illustrate various
//networkInterface class methods.
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
public class NetworkInterfaceEx
{
public static void main(String[] args) throws SocketException,
UnknownHostException
{
// getNetworkInterfaces() returns a list of all interfaces
// present in the system.
ArrayList interfaces = Collections.list(
NetworkInterface.getNetworkInterfaces());
System.out.println("Information about present Network Interfaces...\n");
for (NetworkInterface iface : interfaces)
{
// isUp() method used for checking whether the interface in process
// is up and running or not.
if (iface.isUp())
{
// getName() method
System.out.println("Interface Name: " + iface.getName());
// getDisplayName() method
System.out.println("Interface display name: " + iface.getDisplayName());
// gethardwareaddress() method
System.out.println("Hardware Address: " +
Arrays.toString(iface.getHardwareAddress()));
// getParent() method
System.out.println("Parent: " + iface.getParent());
// getIndex() method
System.out.println("Index: " + iface.getIndex());
// Interface addresses of the network interface
System.out.println("\tInterface addresses: ");
// getInterfaceAddresses() method
for (InterfaceAddress addr : iface.getInterfaceAddresses())
{
System.out.println("\t\t" + addr.getAddress().toString());
}
// Interface addresses of the network interface
System.out.println("\tInetAddresses associated with this interface: ");
// getInetAddresses() method returns list of all
// addresses currently bound to this interface
Enumeration en = iface.getInetAddresses();
while (en.hasMoreElements())
{
System.out.println("\t\t" + en.nextElement().toString());
}
// getMTU() method
System.out.println("\tMTU: " + iface.getMTU());
// getSubInterfaces() method
System.out.println("\tSubinterfaces: " +
Collections.list(iface.getSubInterfaces()));
// isLoopback() method
System.out.println("\this loopback: " + iface.isLoopback());
// isVirtual() method
System.out.println("\this virtual: " + iface.isVirtual());
// isPointToPoint() method
System.out.println("\this point to point: " + iface.isPointToPoint());
// supportsMulticast() method
System.out.println("Supports Multicast: " + iface.supportsMulticast());
}
}
// getByIndex() method returns network interface
// with the specified index
NetworkInterface nif = NetworkInterface.getByIndex(1);
// toString() method is used to display textual
// information about this network interface
System.out.println("Network interface 1: " + nif.toString());
// getByName() method returns network interface
// with the specified name
NetworkInterface nif2 = NetworkInterface.getByName("eth0");
InetAddress ip = InetAddress.getByName("localhost");
// getbyInetAddress() method
NetworkInterface nif3 = NetworkInterface.getByInetAddress(ip);
System.out.println("\nlocalhost associated with: " + nif3);
// equals() method
boolean eq = nif.equals(nif2);
System.out.println("nif==nif2: " + eq);
// hashCode() method
System.out.println("Hashcode : " + nif.hashCode());
}
}
输出 :
Information about present Network Interfaces...
Interface Name: lo
Interface display name: Software Loopback Interface 1
Hardware Address: null
Parent: null
Index: 1
Interface addresses:
/127.0.0.1
/0:0:0:0:0:0:0:1
InetAddresses associated with this interface:
/127.0.0.1
/0:0:0:0:0:0:0:1
MTU: -1
Subinterfaces: []
is loopback: true
is virtual: false
is point to point: false
Supports Multicast: true
Interface Name: wlan5
Interface display name: Dell Wireless 1705 802.11b|g|n (2.4GHZ)
Hardware Address: [100, 90, 4, -90, 2, 15]
Parent: null
Index: 16
Interface addresses:
/192.168.43.96
/2405:205:1486:9a1b:e567:b46f:198a:fe0c
/2405:205:1486:9a1b:8c93:9f82:6dd2:350c
/fe80:0:0:0:e567:b46f:198a:fe0c%wlan5
InetAddresses associated with this interface:
/192.168.43.96
/2405:205:1486:9a1b:e567:b46f:198a:fe0c
/2405:205:1486:9a1b:8c93:9f82:6dd2:350c
/fe80:0:0:0:e567:b46f:198a:fe0c%wlan5
MTU: 1500
Subinterfaces: []
is loopback: false
is virtual: false
is point to point: false
Supports Multicast: true
Network interface 1: name:lo (Software Loopback Interface 1)
loclhost associated with: name:lo (Software Loopback Interface 1)
nif==nif2: false
HashCode : 2544