NetworkInterface class

Article directory

    • 1 Introduction
    • 2. Factory method
    • 3. Obtaining method

1. Introduction

The NetworkInterface class represents a local IP address. This can be a physical interface, such as an extra Ethernet card (commonly found in firewalls and routers), or it can be a virtual interface, bound to the same physical hardware as the machine’s other IP addresses. The NetworkInterface class provides methods to enumerate all local addresses (regardless of the interface), and create InetAddress objects from them, and then these InetAddress objects (this blog) can be used to create Sockets, server Sockets, etc. Using the NetworkInterface class, you can do the following:

  • Get all network interfaces on the system: You can use the getNetworkInterfaces() method to get all network interfaces on the current system. This returns an Enumeration object that you can use to iterate over each network interface on the system.

  • Get properties of network interface: For each network interface, you can use methods of NetworkInterface class to get various properties such as interface name, hardware address (MAC address), IP address, subnet mask, broadcast address, etc.

  • Configuring a Network Interface: You can use the methods of the NetworkInterface class to configure a network interface. You can enable or disable the interface, set the IP address, subnet mask, broadcast address, etc.

  • Monitoring Network Interfaces: The NetworkInterface class also provides methods for monitoring the status and statistics of network interfaces. You can obtain the transmission speed of the interface, the number of packets sent and received, and more.

Note that the NetworkInterface class is part of the Java programming language and can be found in Java’s java.net package. You can use and manipulate the NetworkInterface class using Java’s network programming capabilities.

2. Factory method

Since the NetworkInterface object represents physical hardware and virtual addresses, it cannot be constructed arbitrarily. As with the InetAddress class, there are static factory methods that return the NetworkInterface object associated with a network interface. A NetworkInterface can be requested by IP address, name or enumeration

  • public static NetworkInterface getByname(String name) throws SocketException

This method returns a NetworkInterface object representing the network interface with the specified name. Returns null if there is no such interface. If the underlying network stack encounters a problem finding the relevant network interface, a SocketException will be thrown, but this is unlikely to happen. The format of the name is platform-dependent. On a typical UNIX system, an Ethernet interface name has the form eth0, eth1, etc. A local loopback address might have a name like “lo”. On Windows, the names are similar to the strings “CE31” and “ELX100”, taken from the vendor name and hardware model name of this particular network interface.

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
           //Get the network interface of the mac
          NetworkInterface ni=NetworkInterface. getByName("lo0");
            System.out.println(ni);
          if(ni==null){<!-- -->
              System.err.println("No sucn interface:ehto");
          }
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        }
    }
}

  • public static NetworkInterface getByInetAddress(InetAddress address) throws SocketException

This method returns a NetworkInterface object representing the network interface bound to the specified IP address. Returns null if no network interface on localhost is bound to this ip address. If an error occurs, a SocketException is thrown.

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
          InetAddress local=InetAddress.getByName("192.168.31.41");
          NetworkInterface ni=NetworkInterface. getByInetAddress(local);
          System.out.println(ni);
          if(ni==null){<!-- -->
              System.err.println("No sucn interface:ehto");
          }
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        } catch (UnknownHostException e) {<!-- -->
            throw new RuntimeException(e);
        }
    }
}


  • public static Enumeration getNetworkInterfaces() throws SocketException

This method returns an enumeration object, which lists all network interfaces on the local host

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
          Enumeration<NetworkInterface> ni=NetworkInterface. getNetworkInterfaces();
          System.out.println();
          while(ni.hasMoreElements()){<!-- -->
              NetworkInterface ni2=ni.nextElement();
              System.out.println(ni2);
          }
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        }
    }
}

3. Obtaining method

With the NetworkInterface object, you can query its IP address and name. That’s pretty much the only thing these objects can do.

  • public Enumertion geInetAddresses()

A network interface can be bound to multiple IP addresses. It’s less common these days, but it does exist. The getInetAddresses() method returns a java.util.Enumeration containing an InetAddress for each IP address bound to this interface.

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
          NetworkInterface ehtho=NetworkInterface. getByName("utun4");
          Enumeration addresses = ehtho. getInetAddresses();
          while (addresses.hasMoreElements()){<!-- -->
              System.out.println(addresses.nextElement());
          }
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        }
    }
}
  • public String getName()

This method returns the name of a specific NetworkInterface object, such as eth0 or lo

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
          NetworkInterface ehtho=NetworkInterface. getByName("utun4");
          String addresses = ehtho. getName();
          System.out.println(addresses);
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        }
    }
}

  • public String getDisplayName()

This method claims to return a friendlier name for the specified NetworkInterface, something like “Ethernet Card 0”.

public class QuizCardBuilder {<!-- -->
    public static void main(String[] args) {<!-- -->
        try {<!-- -->
          NetworkInterface ehtho=NetworkInterface. getByName("utun4");
          String addresses = ehtho. getDisplayName();
          System.out.println(addresses);
        } catch (SocketException e) {<!-- -->
            e.printStackTrace();
        }
    }
}