This class represents network interface, both software as well as hardware, its name, list of IP addresses assigned to it, and all related information. It can be used in cases when we want to specifically use a particular interface for transmitting our packet on a system with multiple NICs.
What is a Network Interface?
A network interface can be thought of as a point at which your computer connects to the network. It is not necessarily a piece of hardware but can also be implemented in software. For example, a loopback interface which is used for testing purposes.
Methods :
1.getName() : Returns the name of this network interface.
Syntax : public String getName()
2.getInetAddresses() : Returns an enumeration of all Inetaddresses bound to this network interface, if security manager allows it.
Syntax :public Enumeration getInetAddresses()
3.getInterfaceAddresses() : Returns a list of all interface addresses on this interface.
Syntax :public List getInterfaceAddresses()
4.getSubInterfaces() : Returns an enumeration of all the sub or virtual interfaces of this network interface. For example, eth0:2 is a sub interface of eth0.
Syntax :public Enumeration getSubInterfaces()
5.getParent() : In case of a sub interface, this method returns the parent interface. If this is not a subinterface, this method will return null.
Syntax :public NetworkInterface getParent()
6.getIndex() : Returns the index assigned to this network interface by the system. Indexes can be used in place of long names to refer to any interface on the device.
Syntax :public int getIndex()
7.getDisplayName() : This method returns the name of network interface in a readable string format.
Syntax :public String getDisplayName()
8.getByName() : Finds and returns the network interface with the specified name, or null if none exists.
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() : Performs similar function as the previous function with index used as search parameter instead of name.
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() : This method is widely used as it returns the network interface the specified inetaddress is bound to. If an InetAddress is bound to multiple interfaces, any one of the interfaces may be returned.
Syntax : public static NetworkInterface getByInetAddress(InetAddress addr)
throws SocketException
Parameters :
addr : address to search for
Throws :
SocketException : If IO error occurs
11.getNetworkInterfaces() : Returns all the network interfaces on the system.
Syntax :public static Enumeration getNetworkInterfaces()
throws SocketException
Throws :
SocketException : If IO error occurs
12.isUp() : Returns a boolean value indicating if this network interface is up and running.
Syntax : public boolean isUp()
13.isLoopback() : Returns a boolean value indicating if this interface is a loopback interface or not.
Syntax : public boolean isLoopback()
14.isPointToPoint() : Returns a boolean value indicating if this interface is a point to point interface or not.
Syntax : public boolean isPointToPoint()
15.supportsMulticast() : Returns a boolean value indicating if this interface supports multicasting or not.
Syntax : public boolean supportsMulticast()
16.getHardwareAddress() : Returns a byte array containing the hardware address(MAC) address of this interface. The caller must have appropriate permissions before calling this method.
public byte[] getHardwareAddress()
17.getMTU() : Returns the maximum transmission unit of this interface. An MTU is the largest size of the packet or frame that can be sent in packet based network.
Syntax :public int getMTU()
18.isVirtual() : Returns a boolean value indicating whether this interface is a virtual interface or not. Virtual interfaces are used in conjunction physical interfaces to provide additional values such as addresses and MTU.
Syntax : public boolean isVirtual()
19.equals() : This method is used to compare two network interfaces for equality. Two network interfaces are equal if they have same name and addresses bound to them.
Syntax :public boolean equals(Object obj)
Parameters :
obj : Object to compare this network interface for equality
20.hashCode() : Returns the hashcode value for this object.
Syntax :public int hashCode()
21.toString() : Returns a textual description of this object.
Syntax :public String toString()
Java Implementation :
Java
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
{
ArrayList<NetworkInterface> interfaces = Collections.list(
NetworkInterface.getNetworkInterfaces());
System.out.println( "Information about present Network Interfaces...\n" );
for (NetworkInterface iface : interfaces)
{
if (iface.isUp())
{
System.out.println( "Interface Name: " + iface.getName());
System.out.println( "Interface display name: " + iface.getDisplayName());
System.out.println( "Hardware Address: " +
Arrays.toString(iface.getHardwareAddress()));
System.out.println( "Parent: " + iface.getParent());
System.out.println( "Index: " + iface.getIndex());
System.out.println( "\tInterface addresses: " );
for (InterfaceAddress addr : iface.getInterfaceAddresses())
{
System.out.println( "\t\t" + addr.getAddress().toString());
}
System.out.println( "\tInetAddresses associated with this interface: " );
Enumeration<InetAddress> en = iface.getInetAddresses();
while (en.hasMoreElements())
{
System.out.println( "\t\t" + en.nextElement().toString());
}
System.out.println( "\tMTU: " + iface.getMTU());
System.out.println( "\tSubinterfaces: " +
Collections.list(iface.getSubInterfaces()));
System.out.println( "\this loopback: " + iface.isLoopback());
System.out.println( "\this virtual: " + iface.isVirtual());
System.out.println( "\this point to point: " + iface.isPointToPoint());
System.out.println( "Supports Multicast: " + iface.supportsMulticast());
}
}
NetworkInterface nif = NetworkInterface.getByIndex( 1 );
System.out.println( "Network interface 1: " + nif.toString());
NetworkInterface nif2 = NetworkInterface.getByName( "eth0" );
InetAddress ip = InetAddress.getByName( "localhost" );
NetworkInterface nif3 = NetworkInterface.getByInetAddress(ip);
System.out.println( "\nlocalhost associated with: " + nif3);
boolean eq = nif.equals(nif2);
System.out.println( "nif==nif2: " + eq);
System.out.println( "Hashcode : " + nif.hashCode());
}
}
|
Output :
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)
localhost associated with: name:lo (Software Loopback Interface 1)
nif==nif2: false
HashCode : 2544
The output of the above program will differ if you run it on your system as than it will display information about your network interfaces.
References :
Official Java Documentation
Share your thoughts in the comments
Please Login to comment...