public final class

NetworkInterface

extends Object
java.lang.Object
   ↳ java.net.NetworkInterface

Class Overview

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.

Summary

Public Methods
boolean equals(Object obj)
Compares the specified object to this NetworkInterface and returns whether they are equal or not.
static NetworkInterface getByInetAddress(InetAddress address)
Gets the specific network interface according to the given address.
static NetworkInterface getByName(String interfaceName)
Gets the specific network interface according to a given name.
String getDisplayName()
Gets the human-readable name associated with this network interface.
byte[] getHardwareAddress()
Returns the hardware address of the interface, if it has one, and the user has the necessary privileges to access the address.
Enumeration<InetAddress> getInetAddresses()
Gets a list of addresses bound to this network interface.
List<InterfaceAddress> getInterfaceAddresses()
Returns a List the InterfaceAddresses for this network interface.
int getMTU()
Returns the Maximum Transmission Unit (MTU) of this interface.
String getName()
Gets the name associated with this network interface.
static Enumeration<NetworkInterface> getNetworkInterfaces()
Gets a list of all network interfaces available on the local system or null if no interface is available.
NetworkInterface getParent()
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.
int hashCode()
Returns the hash code for this NetworkInterface.
boolean isLoopback()
Returns true if this network interface is a loopback interface.
boolean isPointToPoint()
Returns true if this network interface is a point-to-point interface.
boolean isUp()
Returns true if this network interface is up.
boolean isVirtual()
Returns true if this interface is a virtual interface (also called a sub-interface).
boolean supportsMulticast()
Returns true if this network interface supports multicast.
String toString()
Gets a string containing a concise, human-readable description of this network interface.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public boolean equals (Object obj)

Since: API Level 1

Compares the specified object to this NetworkInterface and returns whether they are equal or not. The object must be an instance of NetworkInterface with the same name, displayName and list of network interfaces to be equal.

Parameters
obj the object to compare with this instance.
Returns
  • true if the specified object is equal to this NetworkInterface, false otherwise.
See Also

public static NetworkInterface getByInetAddress (InetAddress address)

Since: API Level 1

Gets the specific network interface according to the given address.

Parameters
address the address to identify the searched network interface.
Returns
  • the network interface with the specified address if one exists or null otherwise.
Throws
SocketException if an error occurs while getting the network interface information.
NullPointerException if the given interface address is invalid.

public static NetworkInterface getByName (String interfaceName)

Since: API Level 1

Gets the specific network interface according to a given name.

Parameters
interfaceName the name to identify the searched network interface.
Returns
  • the network interface with the specified name if one exists or null otherwise.
Throws
SocketException if an error occurs while getting the network interface information.
NullPointerException if the given interface's name is null.

public String getDisplayName ()

Since: API Level 1

Gets the human-readable name associated with this network interface.

Returns
  • the display name of this network interface or the name if the display name is not available.

public byte[] getHardwareAddress ()

Since: API Level 9

Returns the hardware address of the interface, if it has one, and the user has the necessary privileges to access the address.

Returns
  • a byte array containing the address or null if the address doesn't exist or is not accessible.
Throws
SocketException if an I/O error occurs.

public Enumeration<InetAddress> getInetAddresses ()

Since: API Level 1

Gets a list of addresses bound to this network interface.

Returns
  • the address list of the represented network interface.

public List<InterfaceAddress> getInterfaceAddresses ()

Since: API Level 9

Returns a List the InterfaceAddresses for this network interface.

If there is a security manager, its checkConnect method is called with the InetAddress for each InterfaceAddress. Only InterfaceAddresses where the checkConnect doesn't throw a SecurityException will be returned.

Returns
  • a List of the InterfaceAddresses for this network interface.

public int getMTU ()

Since: API Level 9

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.

public String getName ()

Since: API Level 1

Gets the name associated with this network interface.

Returns
  • the name of this NetworkInterface instance.

public static Enumeration<NetworkInterface> getNetworkInterfaces ()

Since: API Level 1

Gets a list of all network interfaces available on the local system or null if no interface is available.

Returns
  • the list of NetworkInterface instances representing the available interfaces.
Throws
SocketException if an error occurs while getting the network interface information.

public NetworkInterface getParent ()

Since: API Level 9

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.

public Enumeration<NetworkInterface> getSubInterfaces ()

Since: API Level 9

Returns an Enumeration of all the sub-interfaces of this network interface. Sub-interfaces are also known as virtual interfaces.

For example, eth0:1 would be a sub-interface of eth0.

Returns
  • an Enumeration of all the sub-interfaces of this network interface

public int hashCode ()

Since: API Level 1

Returns the hash code for this NetworkInterface. Since the name should be unique for each network interface the hash code is generated using this name.

Returns
  • this object's hash code.

public boolean isLoopback ()

Since: API Level 9

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.

public boolean isPointToPoint ()

Since: API Level 9

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.

public boolean isUp ()

Since: API Level 9

Returns true if this network interface is up.

Returns
  • true if the interface is up.
Throws
SocketException if an I/O error occurs.

public boolean isVirtual ()

Since: API Level 9

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.

public boolean supportsMulticast ()

Since: API Level 9

Returns true if this network interface supports multicast.

Throws
SocketException if an I/O error occurs.

public String toString ()

Since: API Level 1

Gets a string containing a concise, human-readable description of this network interface.

Returns
  • the textual representation for this network interface.