Network Interface in Java

Network Interface in Java

 
A network interface is the point of interconnection between a computer and a private or public network. A network interface is generally a network interface card (NIC). Java 1.4 introduced NetworkInterface Class to get the details of a local network interface, MAC address of the Interface, MTU (maximum transmission unit) of the Interface, list of IP address attached to the interface whether the interface up and/or down and whether it supports multicasting etc.
 
NetworkInterface is useful for a multihomed system, which is a system with multiple NICs. Using NetworkInterface, you can specify which NIC to use for particular network activity. The NetworkInterface class is given by Java in a package named java.net and the NetworkInterface class represents both types of interfaces.
 
Example
 
The loop back interface (127.0.0.1 for IPv4 and 1 for IPv6) is not a physical device but a piece of software simulating a network interface. The loop back interface is commonly used in test environments.
 
code
  1. import java.net.*;  
  2. import java.io.*;  
  3. import java.net.*;  
  4. import java.util.*;  
  5. public class DemoNetworkInterface  
  6. {  
  7.     private static final String NL = System.getProperty("line.separator");  
  8.     private static final String NL_TAB = NL + "  ";  
  9.     private static final String IPV4 = "IPv4";  
  10.     private static final String IPV6 = "IPv6";  
  11.     private static class MyInterface  
  12.     {  
  13.         public String displayName;  
  14.         public String name;  
  15.         public int mtu;  
  16.         public boolean isUp;  
  17.         public boolean isLoopback;  
  18.         public boolean isPointToPoint; // e.g. a PPP modem interface  
  19.         public boolean isVirtual; // a sub-interface  
  20.         public boolean supportsMulticast;  
  21.         public byte[] macAddress;  
  22.         public List < MyIpAddress > ipAddresses;  
  23.         public List < MyInterface > subInterfaces;  
  24.         public String toString()  
  25.         {  
  26.             StringBuilder sb = new StringBuilder(NL);  
  27.             sb.append("*** Interface [" + name + "] ***").append(NL);  
  28.             sb.append(NL).append("Name  : " + displayName);  
  29.             sb.append(NL).append("MTU           : " + mtu);  
  30.             sb.append(NL).append("Loop back     : " + isLoopback);  
  31.             sb.append(NL).append("Point to Point: " + isPointToPoint);  
  32.             sb.append(NL).append(" InterFace is up    : " + isUp);  
  33.             sb.append(NL).append("virtual       : " + isVirtual);  
  34.             sb.append(NL).append("multicast     : " + supportsMulticast);  
  35.             sb.append(NL).append("MAC address  : ");  
  36.             if (macAddress != null)  
  37.             {  
  38.                 for (byte b: macAddress)  
  39.                 {  
  40.                     sb.append(String.format("%1$02X ", b));  
  41.                 }  
  42.             } else  
  43.             {  
  44.                 sb.append("n/a");  
  45.             }  
  46.             for (MyIpAddress ipAddr: ipAddresses)  
  47.             {  
  48.                 sb.append(ipAddr);  
  49.             }  
  50.             for (MyInterface subInfo: subInterfaces)  
  51.             {  
  52.                 sb.append(subInfo);  
  53.             }  
  54.             return sb.toString();  
  55.         }  
  56.     }  
  57.     private static class MyIpAddress  
  58.     {  
  59.         public String ipAddress;  
  60.         public String ipVersion = "unknown";  
  61.         public String hostName;  
  62.         public String canonicalHostName;  
  63.         public boolean isLoopback;  
  64.         public boolean isSiteLocal; // private IP address  
  65.         public boolean isAnyLocal; // wildcard address  
  66.         public boolean isLinkLocal;  
  67.         public boolean isMulticast;  
  68.         public boolean isReachable;  
  69.         public String toString()  
  70.         {  
  71.             StringBuilder sb = new StringBuilder();  
  72.             sb.append(NL).append("INET address (" + ipVersion + "): " + ipAddress);  
  73.             sb.append(NL_TAB).append("host name           : " + hostName);  
  74.             sb.append(NL_TAB).append("canonical host name : " + canonicalHostName);  
  75.             sb.append(NL_TAB).append("loopback        : " + isLoopback);  
  76.             sb.append(NL_TAB).append("site local        : " + isSiteLocal);  
  77.             sb.append(NL_TAB).append("any local        : " + isAnyLocal);  
  78.             sb.append(NL_TAB).append("link local         : " + isLinkLocal);  
  79.             sb.append(NL_TAB).append("multicast        : " + isMulticast);  
  80.             sb.append(NL_TAB).append("reachable       : " + isReachable);  
  81.             return sb.toString();  
  82.         }  
  83.     }  
  84.     private static MyInterface getMyInterface(NetworkInterface nif) throws IOException  
  85.     {  
  86.         // get interface information  
  87.         MyInterface info = new MyInterface();  
  88.         info.displayName = nif.getDisplayName();  
  89.         info.name = nif.getName();  
  90.         info.mtu = nif.getMTU();  
  91.         info.isUp = nif.isUp();  
  92.         info.isLoopback = nif.isLoopback();  
  93.         info.isPointToPoint = nif.isPointToPoint();  
  94.         info.isVirtual = nif.isVirtual();  
  95.         info.supportsMulticast = nif.supportsMulticast();  
  96.         info.macAddress = nif.getHardwareAddress();  
  97.         info.ipAddresses = new ArrayList < MyIpAddress > ();  
  98.         info.subInterfaces = new ArrayList < MyInterface > ();  
  99.         // get IP address information  
  100.         Enumeration < InetAddress > inetAddresses = nif.getInetAddresses();  
  101.         while (inetAddresses.hasMoreElements())  
  102.         {  
  103.             InetAddress inetAddr = inetAddresses.nextElement();  
  104.             MyIpAddress ipInfo = new MyIpAddress();  
  105.             if (inetAddr instanceof Inet4Address)  
  106.             {  
  107.                 ipInfo.ipVersion = IPV4;  
  108.             } else if (inetAddr instanceof Inet6Address)  
  109.             {  
  110.                 ipInfo.ipVersion = IPV6;  
  111.             }  
  112.             ipInfo.ipAddress = inetAddr.getHostAddress();  
  113.             ipInfo.hostName = inetAddr.getHostName();  
  114.             ipInfo.canonicalHostName = inetAddr.getCanonicalHostName();  
  115.             ipInfo.isAnyLocal = inetAddr.isAnyLocalAddress();  
  116.             ipInfo.isLinkLocal = inetAddr.isLinkLocalAddress();  
  117.             ipInfo.isSiteLocal = inetAddr.isSiteLocalAddress();  
  118.             ipInfo.isLoopback = inetAddr.isLoopbackAddress();  
  119.             ipInfo.isMulticast = inetAddr.isMulticastAddress();  
  120.             ipInfo.isReachable = inetAddr.isReachable(5000);  
  121.             info.ipAddresses.add(ipInfo);  
  122.         }  
  123.         // get virtual interface information  
  124.         Enumeration < NetworkInterface > subIfs = nif.getSubInterfaces();  
  125.         while (subIfs.hasMoreElements())  
  126.         {  
  127.             NetworkInterface subIf = subIfs.nextElement();  
  128.             MyInterface subInfo = getMyInterface(subIf);  
  129.             info.subInterfaces.add(subInfo);  
  130.         }  
  131.         return info;  
  132.     }  
  133.     public static void main(String[] args) throws IOException  
  134.     {  
  135.         Enumeration < NetworkInterface > interfaces =  
  136.             NetworkInterface.getNetworkInterfaces();  
  137.         while (interfaces.hasMoreElements())  
  138.         {  
  139.             NetworkInterface nif = interfaces.nextElement();  
  140.             System.out.println(getMyInterface(nif));  
  141.         }  
  142.     }  
  143. }  
Output
 
The following output provides many information from the NIC (Network Interface Card) of your system.
 
cmd1.jpg 
 
Resource


Similar Articles