NetworkInterface

java.net.NetworkInterface Example

In this example we are going to examine the java.net.NetworkInterface class. This class allows us to programmatically gather information about our network interfaces, and it is especially useful in cases where we need to do some programming using networks and connections. We will explain some of the most important methods and usage of this class and show the retrieved information.

 

 

 

1. NetworkInterface Example

First, let’s take a look at the code. This example was implemented in a personal laptop, so the output will definitely be different if you download and run the example. That makes complete sense, as NetworkInterface gets information from the established hardware (and virtual interfaces that you may have created).

NetworkInterfaceExample.java

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;

public class NetworkInterfaceExample {

    public static void main(String[] args) throws SocketException {
        
        // NetworkInterface implements a static method that returns all the interfaces on the PC,
        // which we add on a list in order to iterate over them.
        ArrayList interfaces = Collections
                .list(NetworkInterface.getNetworkInterfaces());
        
        System.out.println("Printing information about the available interfaces...\n");
        for (NetworkInterface iface : interfaces) {
            
            // Due to the amount of the interfaces, we will only print info
            // about the interfaces that are actually online.
            if (iface.isUp()) {
                
                // Display name
                System.out.println("Interface name: " + iface.getDisplayName());
                
                // Interface addresses of the network interface
                System.out.println("\tInterface addresses: ");
                for (InterfaceAddress addr : iface.getInterfaceAddresses()) {
                    System.out.println("\t\t" + addr.getAddress().toString());
                }
                
                // MTU (Maximum Transmission Unit)
                System.out.println("\tMTU: " + iface.getMTU());
                
                // Subinterfaces
                System.out.println("\tSubinterfaces: " + Collections.list(iface.getSubInterfaces()));
                
                // Check other information regarding the interface
                System.out.println("\tis loopback: " + iface.isLoopback());
                System.out.println("\tis virtual: " + iface.isVirtual());
                System.out.println("\tis point to point: " + iface.isPointToPoint());
            }
        }
    }
}

Output

Printing information about the available interfaces...

Interface name: Software Loopback Interface 1
	Interface addresses: 
		/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
Interface name: Broadcom 802.11n Network Adapter
	Interface addresses: 
		/192.168.2.3
		/fe80:0:0:0:25d6:a917:3b9f:85c1%11
	MTU: 1500
	Subinterfaces: []
	is loopback: false
	is virtual: false
	is point to point: false
Interface name: Teredo Tunneling Pseudo-Interface
	Interface addresses: 
		/2001:0:5ef5:79fb:34dd:2f8a:fac9:a7a1
		/fe80:0:0:0:34dd:2f8a:fac9:a7a1%19
	MTU: 1280
	Subinterfaces: []
	is loopback: false
	is virtual: false
	is point to point: true

2. Method explanation

The methods that are used in the above example are the following:

  • isUp(): Checks if the interface is up and running.
  • getDisplayName(): Returnsthe display name of the interface.
  • isLoopback(): Returns a boolean, showing if it is a loopback interface.
  • isVirtual(): Returns a boolean, showing if it is a virtualinterface.
  • isPointToPoint(): Returns a boolean, showing if it is a point to point interface.
  • getSubInterfaces(): Returns a list of all the sub-interfaces of the network interface, or an empty list if no sub-interfaces are defined.
  • getMTU(): Returns the Maximum Transfer Unit of the interface.
  • getInterfaceAddresses(): Returns a list of all the interfaces addresses that belong to the network interface, most commonly an IPv4 and an IPv6 address.

3. Download the example

This was an example of the java.net.NetworkInterface class.

Download
You can download the full source code of this example here : NetworkInterfaceExample

Ilias Koutsakis

Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button