Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Programmatic Access to Network Parameters

Mustang provides programmatic access to network parameters via 10 new methods in the java.net.NetworkInterface class and the new java.net.InterfaceAddress class. The new NetworkInterface methods include the following:

  • public byte[] getHardwareAddress() returns the hardware address (usually the Machine Address Code, or MAC—also known as the Ethernet Address) of this network interface in a byte array. If the interface doesn’t have a hardware address, or if this address cannot be accessed because the user doesn’t have sufficient privilege, null returns. A java.net.SocketException is thrown if an I/O error occurs.
  • public List<InterfaceAddress> getInterfaceAddresses() returns a java.util.List that contains all or a subset of this network interface’s interface addresses as InterfaceAddress instances. If a security manager exists, one of its checkConnect methods is called with the java.net.InetAddress for each InterfaceAddress. Only InterfaceAddresses where checkConnect doesn’t throw a SecurityException will be returned in the List.
  • public int getMTU() returns this network interface’s Maximum Transmission Unit (MTU). The MTU refers to the size (in bytes) of the largest packet that a given layer of a communications protocol can pass to another layer. For example, the largest MTU allowed by Ethernet is 1500 bytes. The MTU can be set to a fixed size as dictated by a standard (such as Ethernet) or decided upon at connect time (which is usually the case with point-to-point serial links). A SocketException is thrown if an I/O error occurs.
  • public NetworkInterface getParent() returns this network interface’s parent network interface as a NetworkInterface instance if this network interface is a subinterface. However, if this network interface is a physical (nonvirtual) interface or if this network interface is virtual and has no parent, null returns.
  • public Enumeration<NetworkInterface> getSubInterfaces() returns a java.util.Enumeration containing all the subinterfaces (also known as virtual interfaces)—as NetworkInterface instances—that are attached to this network interface. For example, eth0:1 is a subinterface of eth0—an Ethernet network interface name.
  • public boolean isLoopback() returns true if this network interface is a loopback interface—a network interface in which outgoing data is immediately reflected back to the interface as incoming data. This method throws a SocketException if an I/O error occurs.
  • public boolean isPointToPoint() returns true if this network interface is a point-to-point interface (such as a PPP connection through a modem). If an I/O error occurs, this method throws a SocketException.
  • public boolean isUp() returns true if this network interface is up and running. Up indicates that routing entries have been set up for this interface. Running indicates that required system resources have been allocated. A SocketException is thrown if an I/O error occurs.
  • public boolean isVirtual() returns true if this network interface is a virtual interface (also known as a subinterface). A SocketException is thrown if there is a problem with I/O.
  • public boolean supportsMulticast() returns true if this network interface supports multicasting (the capability of a server program to send one copy of a message to multiple client programs). This method throws a SocketException in response to an I/O error.

The InterfaceAddress class represents a network interface address. In addition to the usual methods for determining equality, obtaining a hashcode, and obtaining a string representation, this class presents the following three methods:

  • public InetAddress getAddress() returns this interface address—an IP address, a subnet mask, and a broadcast address (when the address is IPv4), or an IP address and a network prefix length (for an IPv6 address)—as an InetAddress.

  • public InetAddress getBroadcast() returns this interface address’s broadcast address as an InetAddress on IPv4 networks; null returns on IPv6 networks, which don’t have broadcast addresses.

  • public short getNetworkPrefixLength() returns this interface address’s network prefix length for IPv6 networks or the subnet mask for IPv4 networks. The value is returned as a short integer.

    Typical IPv6 values are 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10). Values typical for IPv4 are 8 (, 16 (, or 24 (

I created a simple NetParmsDemo application that demonstrates many of the new methods in the NetworkInterface and InterfaceAddress classes. Listing 3 presents this application’s source code.

Listing 3 NetParmsDemo.java

// NetParmsDemo.java

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class NetParmsDemo
   public static void main (String [] args) throws SocketException
      Enumeration nis;
      nis = NetworkInterface.getNetworkInterfaces ();

      while (nis.hasMoreElements ())
         NetworkInterface ni = nis.nextElement ();

         System.out.println ("Network interface: " + ni.getName ());

         System.out.println ("  Display name: " + ni.getDisplayName ());

         System.out.println ("  Virtual interface: " + ni.isVirtual ());

         System.out.println ("  Maximum transmission unit: " + ni.getMTU ());

         System.out.println ("  Supports multicasting: " +
                             ni.supportsMulticast ());

         System.out.println ("  Point to point interface: " +
                             ni.isPointToPoint ());

         System.out.println ("  Loopback interface: " + ni.isLoopback ());

         System.out.println ("  Interface up and running: " + ni.isUp ());

         byte [] hardwareAddress = ni.getHardwareAddress ();
         if (hardwareAddress != null)
             System.out.print ("  Hardware address = ");
             for (int i = 0; i < hardwareAddress.length; i++)
                  System.out.printf ("%02X%c", hardwareAddress [i],
                                     (i != hardwareAddress.length-1) ? '-' :
             System.out.println ();

         List ias = ni.getInterfaceAddresses ();

         Iterator iias = ias.iterator ();
         while (iias.hasNext ())
            InterfaceAddress ia = iias.next ();
            if (ia == null) // It is possible for getInterfaceAddresses() to
                break;      // return a list containing a single null element.

            System.out.println ("  Interface Address");
            System.out.println ("    Address: " + ia.getAddress ());
            System.out.println ("    Broadcast: " + ia.getBroadcast ());
            System.out.println ("    Prefix length: " +
                                ia.getNetworkPrefixLength ());

         Enumeration inas = ni.getInetAddresses ();
         while (inas.hasMoreElements ())
            InetAddress ia = inas.nextElement ();

            System.out.println ("  Internet address = " + ia);

         System.out.println ();
  • + Share This
  • 🔖 Save To Your Account