Package java.net
Class NetworkInterface
java.lang.Object
java.net.NetworkInterface
public final class NetworkInterface extends Object
This class is used to represent a network interface of the local device. An
interface is defined by its address and a platform dependent name. The class
provides methods to get all information about the available interfaces of the
system or to identify the local interface of a joined multicast group.
-
Method Summary
Modifier and Type Method Description booleanequals(Object obj)Compares the specified object to thisNetworkInterfaceand returns whether they are equal or not.static NetworkInterfacegetByIndex(int index)Returns the NetworkInterface corresponding to the given interface index, or null if no interface has this index.static NetworkInterfacegetByInetAddress(InetAddress address)Returns theNetworkInterfacecorresponding to the given address, or null if no interface has this address.static NetworkInterfacegetByName(String interfaceName)Returns theNetworkInterfacecorresponding to the named network interface, or null if no interface has this name.StringgetDisplayName()Returns a human-readable name for this network interface.byte[]getHardwareAddress()Returns the hardware address of the interface, if it has one, or null otherwise.intgetIndex()Returns the index for the network interface, or -1 if unknown.Enumeration<InetAddress>getInetAddresses()Returns an enumeration of the addresses bound to this network interface.List<InterfaceAddress>getInterfaceAddresses()Returns a List of the InterfaceAddresses for this network interface.intgetMTU()Returns the Maximum Transmission Unit (MTU) of this interface.StringgetName()Returns the name of this network interface (such as "eth0" or "lo").static Enumeration<NetworkInterface>getNetworkInterfaces()Gets a list of all network interfaces available on the local system ornullif no interface is available.NetworkInterfacegetParent()Returns the parent NetworkInterface of this interface if this is a sub-interface, or null if it's a physical (non virtual) interface.Enumeration<NetworkInterface>getSubInterfaces()Returns an enumeration of all the sub-interfaces of this network interface.inthashCode()Returns the hash code for thisNetworkInterface.booleanisLoopback()Returns true if this network interface is a loopback interface.booleanisPointToPoint()Returns true if this network interface is a point-to-point interface.booleanisUp()Returns true if this network interface is up.booleanisVirtual()Returns true if this interface is a virtual interface (also called a sub-interface).booleansupportsMulticast()Returns true if this network interface supports multicast.StringtoString()Returns a string containing details of this network interface.
-
Method Details
-
getIndex
public int getIndex()Returns the index for the network interface, or -1 if unknown.- Since:
- 1.7
-
getName
Returns the name of this network interface (such as "eth0" or "lo"). -
getInetAddresses
Returns an enumeration of the addresses bound to this network interface. -
getDisplayName
Returns a human-readable name for this network interface. On Android, this is the same string as returned bygetName(). -
getByName
Returns theNetworkInterfacecorresponding to the named network interface, or null if no interface has this name.- Throws:
SocketException- if an error occurs.NullPointerException- ifinterfaceName == null.
-
getByInetAddress
Returns theNetworkInterfacecorresponding to the given address, or null if no interface has this address.- Throws:
SocketException- if an error occurs.NullPointerException- ifaddress == null.
-
getByIndex
Returns the NetworkInterface corresponding to the given interface index, or null if no interface has this index.- Throws:
SocketException- if an error occurs.- Since:
- 1.7
-
getNetworkInterfaces
Gets a list of all network interfaces available on the local system ornullif no interface is available.- Returns:
- the list of
NetworkInterfaceinstances representing the available interfaces. - Throws:
SocketException- if an error occurs while getting the network interface information.
-
equals
Compares the specified object to thisNetworkInterfaceand returns whether they are equal or not. The object must be an instance ofNetworkInterfacewith the same name, display name, and list of interface addresses.- Overrides:
equalsin classObject- Parameters:
obj- the object to compare with this instance.- Returns:
trueif the specified object is equal to thisNetworkInterface,falseotherwise.- See Also:
hashCode()
-
hashCode
public int hashCode()Returns the hash code for thisNetworkInterface. Since the name should be unique for each network interface the hash code is generated using the name.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
Returns a string containing details of this network interface. The exact format is deliberately unspecified. Callers that require a specific format should build a string themselves, using this class' accessor methods. -
getInterfaceAddresses
Returns a List of the InterfaceAddresses for this network interface.- Since:
- 1.6
-
getSubInterfaces
Returns an enumeration of all the sub-interfaces of this network interface. Sub-interfaces are also known as virtual interfaces.For example,
eth0:1would be a sub-interface ofeth0.- Returns:
- an Enumeration of all the sub-interfaces of this network interface
- Since:
- 1.6
-
getParent
Returns the parent NetworkInterface of this interface if this is a sub-interface, or null if it's a physical (non virtual) interface.- Returns:
- the NetworkInterface this interface is attached to.
- Since:
- 1.6
-
isUp
Returns true if this network interface is up.- Returns:
- true if the interface is up.
- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
isLoopback
Returns true if this network interface is a loopback interface.- Returns:
- true if the interface is a loopback interface.
- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
isPointToPoint
Returns true if this network interface is a point-to-point interface. (For example, a PPP connection using a modem.)- Returns:
- true if the interface is point-to-point.
- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
supportsMulticast
Returns true if this network interface supports multicast.- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
getHardwareAddress
Returns the hardware address of the interface, if it has one, or null otherwise.- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
getMTU
Returns the Maximum Transmission Unit (MTU) of this interface.- Returns:
- the value of the MTU for the interface.
- Throws:
SocketException- if an I/O error occurs.- Since:
- 1.6
-
isVirtual
public boolean isVirtual()Returns true if this interface is a virtual interface (also called a sub-interface). Virtual interfaces are, on some systems, interfaces created as a child of a physical interface and given different settings (like address or MTU). Usually the name of the interface will the name of the parent followed by a colon (:) and a number identifying the child, since there can be several virtual interfaces attached to a single physical interface.- Returns:
- true if this interface is a virtual interface.
- Since:
- 1.6
-