How do I retrieve network interface information using NetworkInterface in Java?

To retrieve network interface information using the NetworkInterface class in Java, you can use the java.net package which provides the NetworkInterface class. This class allows you to get information about network interfaces such as IP addresses, display names, names, hardware (MAC) addresses, and more.

Here is an example illustrating how to retrieve information about all network interfaces available on the machine:

Code Example

package org.kodejava.net;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NetworkInterfaceInfo {
   public static void main(String[] args) {
      try {
         // Get all network interfaces
         Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

         while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();

            // Display interface name and display name
            System.out.println("Interface Name: " + networkInterface.getName());
            System.out.println("Display Name: " + networkInterface.getDisplayName());

            // Display MAC address
            byte[] mac = networkInterface.getHardwareAddress();
            if (mac != null) {
               StringBuilder macAddress = new StringBuilder();
               for (int i = 0; i < mac.length; i++) {
                  macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
               }
               System.out.println("MAC Address: " + macAddress.toString());
            } else {
               System.out.println("MAC Address: Not available");
            }

            // Display the IP addresses associated with this interface
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
               InetAddress inetAddress = inetAddresses.nextElement();
               System.out.println("IP Address: " + inetAddress.getHostAddress());
            }

            // Display other information
            System.out.println("MTU: " + networkInterface.getMTU());
            System.out.println("Is Loopback: " + networkInterface.isLoopback());
            System.out.println("Is Up: " + networkInterface.isUp());
            System.out.println("Is Virtual: " + networkInterface.isVirtual());
            System.out.println("Supports Multicast: " + networkInterface.supportsMulticast());
            System.out.println("--------------------------------------------");
         }
      } catch (SocketException e) {
         e.printStackTrace();
      }
   }
}

Explanation:

  1. Getting All Network Interfaces:
    The NetworkInterface.getNetworkInterfaces() method retrieves an enumeration of all network interfaces available on the machine.

  2. Retrieving Basic Information:

    • getName(): Returns the name of the network interface.
    • getDisplayName(): Returns a user-readable name for the network interface.
  3. Retrieving the MAC Address:
    • getHardwareAddress(): Returns the hardware (MAC) address of the network interface as an array of bytes.
  4. Retrieving IP Addresses:
    • getInetAddresses(): Returns all assigned IP addresses (both IPv4 and IPv6) for this interface.
  5. Flags and Features of the Interface:
    • isUp: Checks if the network interface is up and operational.
    • isLoopback: Checks if the interface is a loopback interface.
    • isVirtual: Checks if it is a virtual interface.
    • supportsMulticast: Indicates whether the interface supports multicasting.
    • getMTU: Returns the Maximum Transmission Unit (MTU) of the interface.
  6. Handling SocketException:
    Accessing network interfaces and their properties might throw a SocketException, so wrap the logic in a try-catch block.

Output Example (Depending on Your Machine’s Network Setup)

Interface Name: ethernet_32768
Display Name: Intel(R) Ethernet Connection (18) I219-LM
MAC Address: C4-EF-BB-80-01-D0
IP Address: fe80:0:0:0:8e43:dfa1:eb66:b106%ethernet_32768
MTU: 1500
Is Loopback: false
Is Up: false
Is Virtual: false
Supports Multicast: true
--------------------------------------------
Interface Name: loopback_0
Display Name: Software Loopback Interface 1
MAC Address: Not available
IP Address: 0:0:0:0:0:0:0:1
IP Address: 127.0.0.1
MTU: 1500
Is Loopback: true
Is Up: true
Is Virtual: false
Supports Multicast: true
--------------------------------------------

This code allows you to inspect all network interfaces, their properties, and associated information in Java.

Wayan

Leave a Reply

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