A network card (not too standard, should be called a Web interface, a NIC can have more than one network interface, such as SOFTAP) has a set of network configuration: IP address, subnet mask, gateway, DNS and so on.
Starting with Java 1.6, there are some interfaces for accessing the network configuration:
Java.net.NetworkInterface (starting from 1.4)
This class represents a network interface that consists of a name and a list of IP addresses assigned to this interface. It is used to identify the local interface that joins the multicast group. Interfaces are typically differentiated by name (such as "Le0").
Java.net.InterfaceAddress (starting from 1.6)
This class represents the network interface address. In short, for an IPv4 address, the IP address, subnet mask, and broadcast address are referred to. For IPV6 addresses, the IP address and network prefix length.
Java.net.InetAddress and subclasses inet4address, inet6address
The address is divided into multicast address, loopback address, unicast address (this is used when actually taking native IP)
The method is detailed:java.net.NetworkInterface
boolean |
equals(Object obj) Compares this object to a specified object. |
※※※ static NetworkInterface |
getByInetAddress(InetAddress addr) A convenient way to search for a network interface that is bound to a specified Internet Protocol (IP) address. |
※※※ static NetworkInterface |
getByName(String name) Searches for a network interface with the specified name. |
String |
getDisplayName() Gets the display name of this network interface. |
byte[] |
getHardwareAddress() If there is a hardware address and can be accessed with the given current permission, the hardware address (usually MAC) is returned. |
※※※ Enumeration<InetAddress> |
getInetAddresses() A convenient way to return a enumeration that has all or part of the inetaddress bound to this network interface. |
※※※ List<InterfaceAddress> |
getInterfaceAddresses() Gets a list of all or part of this network interface InterfaceAddresses . |
int |
getMTU() Returns the maximum transmission unit (Maximum transmission UNIT,MTU) for this interface. |
String |
getName() Gets the name of this network interface. |
※※※
static Enumeration<NetworkInterface>
|
getNetworkInterfaces() Returns all the interfaces on this machine. |
NetworkInterface |
getParent() If this interface is a sub-interface, it returns the parent networkinterface if it is a physical (non-virtual) interface or has no parent interface null . |
※※※ Enumeration<NetworkInterface> |
getSubInterfaces() Gets the enumeration that has all sub-interfaces (also known as virtual interfaces) connected to this network interface. |
int |
hashCode() Returns the hash code value of the object. |
boolean |
isLoopback() Returns whether the network interface is a loopback interface. |
boolean |
isPointToPoint() Returns whether the network interface is a point-to-point interface. |
boolean |
isUp() Returns whether the network interface is turned on and running. |
boolean |
isVirtual() Returns whether this interface is a virtual interface (also known as a sub-interface). |
boolean |
supportsMulticast() Returns whether the network interface supports multicast. |
String |
toString() Returns the string representation of the object. |
Special attention needs to be paid to:
1.getNetworkInterfaces(),此静态方法用去获取所有本机的NetworkInterface,这个是获取地址的第一步,通过遍历此返回值的枚举,来获取本机所有网卡的地址。
2, the rest of the non-static method, is a networkinterface instance of the method.
3, take the instance, you can determine whether it is a loopback address, is the case can be excluded. Whether it is a virtual interface, is the case can be excluded. Whether it is turned on and running, no case can be excluded.
4, a physical network card, there can be more than one virtual interface, there are two ways to get these virtual interfaces.
5. Get the name of the NIC (e.g. eth0 under Linux) to getDisplayName(),getName().
get the MAC address: getHardwareAddress()
6, take interfaceaddress instance. getInterfaceAddresses()
, return a list of interfaceaddress, which is all the interfaceaddress under the NetworkInterface.
7, if you want to skip the interfaceaddress directly take the IP address, can call the getInetAddresses()
method, return is a InetAddress enumeration.
Interfaceaddress
boolean |
equals(Object obj) Compares this object to a specified object. |
InetAddress |
getAddress() Return this address InetAddress . |
InetAddress |
getBroadcast() Returns the address of this interfaceaddress broadcast InetAddress . |
short |
getNetworkPrefixLength() Returns the network prefix length for this address. |
int |
hashCode() Returns the hash code for this interface address. |
String |
toString() Convert this interface address to a String . |
Java gets all network adapters for the on-premises environment and all networking configurations for each NIC