Skip to content

Most visited

Recently visited

navigation
Added in API level 1

NetworkInterface

public final class NetworkInterface
extends Object

java.lang.Object
   ↳ java.net.NetworkInterface


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 getByIndex(int index)

Returns the NetworkInterface corresponding to the given interface index, or null if no interface has this index.

static NetworkInterface getByInetAddress(InetAddress address)

Returns the NetworkInterface corresponding to the given address, or null if no interface has this address.

static NetworkInterface getByName(String interfaceName)

Returns the NetworkInterface corresponding to the named network interface, or null if no interface has this name.

String getDisplayName()

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.

int getIndex()

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.

int getMTU()

Returns the Maximum Transmission Unit (MTU) of this interface.

String getName()

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 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()

Returns a string containing details of this network interface.

Inherited methods

From class java.lang.Object

Public methods

equals

Added in API level 1
boolean equals (Object obj)

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, display name, and list of interface addresses.

Parameters
obj Object: the object to compare with this instance.
Returns
boolean true if the specified object is equal to this NetworkInterface, false otherwise.

See also:

getByIndex

Added in API level 19
NetworkInterface getByIndex (int index)

Returns the NetworkInterface corresponding to the given interface index, or null if no interface has this index.

Parameters
index int
Returns
NetworkInterface
Throws
SocketException if an error occurs.

getByInetAddress

Added in API level 1
NetworkInterface getByInetAddress (InetAddress address)

Returns the NetworkInterface corresponding to the given address, or null if no interface has this address.

Parameters
address InetAddress
Returns
NetworkInterface
Throws
SocketException if an error occurs.
NullPointerException if address == null.

getByName

Added in API level 1
NetworkInterface getByName (String interfaceName)

Returns the NetworkInterface corresponding to the named network interface, or null if no interface has this name.

Parameters
interfaceName String
Returns
NetworkInterface
Throws
SocketException if an error occurs.
NullPointerException if interfaceName == null.

getDisplayName

Added in API level 1
String getDisplayName ()

Returns a human-readable name for this network interface. On Android, this is the same string as returned by getName().

Returns
String

getHardwareAddress

Added in API level 9
byte[] getHardwareAddress ()

Returns the hardware address of the interface, if it has one, or null otherwise.

Returns
byte[]
Throws
SocketException if an I/O error occurs.

getIndex

Added in API level 19
int getIndex ()

Returns the index for the network interface, or -1 if unknown.

Returns
int

getInetAddresses

Added in API level 1
Enumeration<InetAddress> getInetAddresses ()

Returns an enumeration of the addresses bound to this network interface.

Returns
Enumeration<InetAddress>

getInterfaceAddresses

Added in API level 9
List<InterfaceAddress> getInterfaceAddresses ()

Returns a List of the InterfaceAddresses for this network interface.

Returns
List<InterfaceAddress>

getMTU

Added in API level 9
int getMTU ()

Returns the Maximum Transmission Unit (MTU) of this interface.

Returns
int the value of the MTU for the interface.
Throws
SocketException if an I/O error occurs.

getName

Added in API level 1
String getName ()

Returns the name of this network interface (such as "eth0" or "lo").

Returns
String

getNetworkInterfaces

Added in API level 1
Enumeration<NetworkInterface> getNetworkInterfaces ()

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

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

getParent

Added in API level 9
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.

Returns
NetworkInterface the NetworkInterface this interface is attached to.

getSubInterfaces

Added in API level 9
Enumeration<NetworkInterface> getSubInterfaces ()

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
Enumeration<NetworkInterface> an Enumeration of all the sub-interfaces of this network interface

hashCode

Added in API level 1
int hashCode ()

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

Returns
int this object's hash code.

isLoopback

Added in API level 9
boolean isLoopback ()

Returns true if this network interface is a loopback interface.

Returns
boolean true if the interface is a loopback interface.
Throws
SocketException if an I/O error occurs.

isPointToPoint

Added in API level 9
boolean isPointToPoint ()

Returns true if this network interface is a point-to-point interface. (For example, a PPP connection using a modem.)

Returns
boolean true if the interface is point-to-point.
Throws
SocketException if an I/O error occurs.

isUp

Added in API level 9
boolean isUp ()

Returns true if this network interface is up.

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

isVirtual

Added in API level 9
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
boolean true if this interface is a virtual interface.

supportsMulticast

Added in API level 9
boolean supportsMulticast ()

Returns true if this network interface supports multicast.

Returns
boolean
Throws
SocketException if an I/O error occurs.

toString

Added in API level 1
String 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.

Returns
String a printable representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.