Java.net.NetworkInterface class in Java
Last Updated :
31 Oct, 2022
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
//Java program to illustrate various
//networkInterface class methods.
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
{
// getNetworkInterfaces() returns a list of all interfaces
// present in the system.
ArrayList<NetworkInterface> interfaces = Collections.list(
NetworkInterface.getNetworkInterfaces());
System.out.println("Information about present Network Interfaces...\n");
for (NetworkInterface iface : interfaces)
{
// isUp() method used for checking whether the interface in process
// is up and running or not.
if (iface.isUp())
{
// getName() method
System.out.println("Interface Name: " + iface.getName());
// getDisplayName() method
System.out.println("Interface display name: " + iface.getDisplayName());
// gethardwareaddress() method
System.out.println("Hardware Address: " +
Arrays.toString(iface.getHardwareAddress()));
// getParent() method
System.out.println("Parent: " + iface.getParent());
// getIndex() method
System.out.println("Index: " + iface.getIndex());
// Interface addresses of the network interface
System.out.println("\tInterface addresses: ");
// getInterfaceAddresses() method
for (InterfaceAddress addr : iface.getInterfaceAddresses())
{
System.out.println("\t\t" + addr.getAddress().toString());
}
// Interface addresses of the network interface
System.out.println("\tInetAddresses associated with this interface: ");
// getInetAddresses() method returns list of all
// addresses currently bound to this interface
Enumeration<InetAddress> en = iface.getInetAddresses();
while (en.hasMoreElements())
{
System.out.println("\t\t" + en.nextElement().toString());
}
// getMTU() method
System.out.println("\tMTU: " + iface.getMTU());
// getSubInterfaces() method
System.out.println("\tSubinterfaces: " +
Collections.list(iface.getSubInterfaces()));
// isLoopback() method
System.out.println("\this loopback: " + iface.isLoopback());
// isVirtual() method
System.out.println("\this virtual: " + iface.isVirtual());
// isPointToPoint() method
System.out.println("\this point to point: " + iface.isPointToPoint());
// supportsMulticast() method
System.out.println("Supports Multicast: " + iface.supportsMulticast());
}
}
// getByIndex() method returns network interface
// with the specified index
NetworkInterface nif = NetworkInterface.getByIndex(1);
// toString() method is used to display textual
// information about this network interface
System.out.println("Network interface 1: " + nif.toString());
// getByName() method returns network interface
// with the specified name
NetworkInterface nif2 = NetworkInterface.getByName("eth0");
InetAddress ip = InetAddress.getByName("localhost");
// getbyInetAddress() method
NetworkInterface nif3 = NetworkInterface.getByInetAddress(ip);
System.out.println("\nlocalhost associated with: " + nif3);
// equals() method
boolean eq = nif.equals(nif2);
System.out.println("nif==nif2: " + eq);
// hashCode() method
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
Similar Reads
Java.net.URI class in Java
This class provides methods for creating URI instances from its components or by parsing the string form of those components, for accessing and retrieving different components of a URI instance. What is URI? URI stands for Uniform Resource Identifier. A Uniform Resource Identifier is a sequence of c
11 min read
Java.net.Inet4Address class in Java
This class extends the InetAddress class and represents an IPv4 address. It provides methods to interpret and display useful information about IP addresses. Methods of this class take input in 4 formats: d.d.d.d: When this format is used as input, each of the given values are assigned to 4 bytes of
3 min read
Java.net.Inet6Address class in Java
This class represents IPv6 address and extends the InetAddress class. Methods of this class provide facility to represent and interpret IPv6 addresses. Methods of this class takes input in the following formats: x:x:x:x:x:x:x:x -This is the general form of IPv6 address where each x can be replaced w
5 min read
Java.net.InetSocketAddress class in Java
This class implements IP socket address( combination of IP address and port number). The objects of this class are immutable and can be used for binding, connecting purposes. Constructors : 1. InetSocketAddress(InetAddress addr, int port) : This constructor is similar to the general structure of a s
4 min read
Advanced Java Tutorial | Mastery in Java Programming
In this Advanced Java Tutorial , we will dive into more complex topics and features of Advanced Java that will elevate your coding abilities in Java programming. From advanced data structures to multithreading and networking, this tutorial will provide you with the knowledge and skills needed to bec
14 min read
private vs private-final injection of dependency
Dependency Injection (DI) is essential for improving code modularity, maintainability, and testability in the context of sophisticated Java programming. Choosing between private and private-final injection for dependencies is a typical DI concern. We will examine the distinctions between these two s
5 min read
Spring - Understanding Inversion of Control with Example
Spring IoC (Inversion of Control) Container is the core of the Spring Framework. It creates objects (beans), configures them, injects dependencies, and manages their life cycles. The container uses Dependency Injection (DI) to manage application components. It retrieves object configuration from XML
7 min read
How to Retrieve Information about a Network Interface in Java?
In Java, a network interface is known as NIC (Network Interface Card). It is a hardware component, or we can say a software interface which enables a system to connect with a computer network. It allows the device, i.e. computer, router, etc. to communicate with other devices on the same network or
2 min read
Class getInterfaces() method in Java with Examples
The getInterfaces() method of java.lang.Class class is used to get the interfaces directly implemented by this entity. This entity can be a class or an interface. The method returns an array of interfaces directly implemented by this entity.Syntax: public Class<T>[] getInterfaces() Parameter:
2 min read
Java Networking Programs - Basic to Advanced
Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Ne
3 min read