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 a 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 enviorments.
code
import java.net.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class DemoNetworkInterface
{
private static final String NL = System.getProperty("line.separator");
private static final String NL_TAB = NL + " ";
private static final String IPV4 = "IPv4";
private static final String IPV6 = "IPv6";
private static class MyInterface
{
public String displayName;
public String name;
public int mtu;
public boolean isUp;
public boolean isLoopback;
public boolean isPointToPoint; // e.g. a PPP modem interface
public boolean isVirtual; // a sub-interface
public boolean supportsMulticast;
public byte[] macAddress;
public List<MyIpAddress> ipAddresses;
public List<MyInterface> subInterfaces;
public String toString()
{
StringBuilder sb = new StringBuilder(NL);
sb.append("*** Interface [" + name + "] ***").append(NL);
sb.append(NL).append("Name : " + displayName);
sb.append(NL).append("MTU : " + mtu);
sb.append(NL).append("Loop back : " + isLoopback);
sb.append(NL).append("Point to Point: " + isPointToPoint);
sb.append(NL).append(" InterFace is up : " + isUp);
sb.append(NL).append("virtual : " + isVirtual);
sb.append(NL).append("multicast : " + supportsMulticast);
sb.append(NL).append("MAC address : ");
if (macAddress != null)
{
for (byte b : macAddress)
{
sb.append(String.format("%1$02X ", b));
}
}
else
{
sb.append("n/a");
}
for (MyIpAddress ipAddr: ipAddresses)
{
sb.append(ipAddr);
}
for (MyInterface subInfo: subInterfaces)
{
sb.append(subInfo);
}
return sb.toString();
}
}
private static class MyIpAddress
{
public String ipAddress;
public String ipVersion = "unknown";
public String hostName;
public String canonicalHostName;
public boolean isLoopback;
public boolean isSiteLocal; // private IP address
public boolean isAnyLocal; // wildcard address
public boolean isLinkLocal;
public boolean isMulticast;
public boolean isReachable;
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(NL).append("INET address ("+ ipVersion + "): " + ipAddress);
sb.append(NL_TAB).append("host name : " + hostName);
sb.append(NL_TAB).append("canonical host name : " + canonicalHostName);
sb.append(NL_TAB).append("loopback : " + isLoopback);
sb.append(NL_TAB).append("site local : " + isSiteLocal);
sb.append(NL_TAB).append("any local : " + isAnyLocal);
sb.append(NL_TAB).append("link local : " + isLinkLocal);
sb.append(NL_TAB).append("multicast : " + isMulticast);
sb.append(NL_TAB).append("reachable : " + isReachable);
return sb.toString();
}
}
private static MyInterface getMyInterface(NetworkInterface nif) throws IOException
{
// get interface information
MyInterface info = new MyInterface();
info.displayName = nif.getDisplayName();
info.name = nif.getName();
info.mtu = nif.getMTU();
info.isUp = nif.isUp();
info.isLoopback = nif.isLoopback();
info.isPointToPoint = nif.isPointToPoint();
info.isVirtual = nif.isVirtual();
info.supportsMulticast = nif.supportsMulticast();
info.macAddress = nif.getHardwareAddress();
info.ipAddresses = new ArrayList<MyIpAddress>();
info.subInterfaces = new ArrayList<MyInterface>();
// get IP address information
Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
while (inetAddresses.hasMoreElements())
{
InetAddress inetAddr = inetAddresses.nextElement();
MyIpAddress ipInfo = new MyIpAddress();
if (inetAddr instanceof Inet4Address)
{
ipInfo.ipVersion = IPV4;
}
else if (inetAddr instanceof Inet6Address)
{
ipInfo.ipVersion = IPV6;
}
ipInfo.ipAddress = inetAddr.getHostAddress();
ipInfo.hostName = inetAddr.getHostName();
ipInfo.canonicalHostName = inetAddr.getCanonicalHostName();
ipInfo.isAnyLocal = inetAddr.isAnyLocalAddress();
ipInfo.isLinkLocal = inetAddr.isLinkLocalAddress();
ipInfo.isSiteLocal = inetAddr.isSiteLocalAddress();
ipInfo.isLoopback = inetAddr.isLoopbackAddress();
ipInfo.isMulticast = inetAddr.isMulticastAddress();
ipInfo.isReachable = inetAddr.isReachable(5000);
info.ipAddresses.add(ipInfo);
}
// get virtual interface information
Enumeration<NetworkInterface> subIfs = nif.getSubInterfaces();
while (subIfs.hasMoreElements())
{
NetworkInterface subIf = subIfs.nextElement();
MyInterface subInfo = getMyInterface(subIf);
info.subInterfaces.add(subInfo);
}
return info;
}
public static void main(String[] args) throws IOException
{
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements())
{
NetworkInterface nif = interfaces.nextElement();
System.out.println(getMyInterface(nif));
}
}
}
Output
The following output provides many information from the NIC (Network Interface Card) of your system.
Resource