In Java 6, this class provides some new methods for accessing state and configuration information and this information is related to system's network adapters.
Enhancements in Networking Features
Changes in NetworkInterface Class
NetworkInterface is a class in java.net package. This class is used to represent a Network Interface and it is made up of a name and a IP addresses list assigned to this interface. In Java 6, this class provides some new methods for accessing state and configuration information and this information is related to system's network adapters. This includes information like broadcast address, list object with all or a subset of the InterfaceAddress, enumeration object with all of the subinterfaces, subnet mask, hardware address (MAC addresses) and MTU size.
Some new methods are added in NetworkInterface class are as follows:
- public boolean isUp()
Above method returns true if the network interface is up and running. Up specifies that routing entries have been set up for the network interface. And running specifies that required system resources have been allocated.
- public boolean isLoopback()
Above method returns true if the network interface is a loopback interface.
- public boolean isPointToPoint()
Above method returns true if the network interface is point to point interface.
- public boolean supportsMulticast()
Above method is used to know the network interface is support multicasting or not. If yes then its return true.
- public byte[] getHardwareAddress()
Above method is used to get the byte array of MAC address. It return null if the address is not accessible or doesn't exist.
- public int getMTU()
Above method returns the value of MTU (Maximum Transmission Unit) of this interface.
Above all methods throws an SocketException if an I/O error occurs.
- public boolean isVirtual()
Above method return true if the network interface is virtual interface. Virtual interfaces are those interfaces that created as child of physical interface and given different settings such as MTU or address.
- public List<InterfaceAddress>
getInterfaceAddresses()
Above method is used to get a list object of all or a subset of the InterfaceAddresses of this network interface. If security manager is available, it invoke its checkConnect() for each InterfaceAddress with the InetAddress. Only those InterfaceAddresses will be returned in the list where the checkConnect() doesn't throw a SecurityException.
- public Enumeration<NetworkInterface>
getSubInterfaces()
Above method is used to get an enumeration object with all of the virtual interfaces (subinterfaces) of this network interface.
- public NetworkInterface getParent()
Above method is used to get the parent NetworkInterface of this network interface but only when if it is a subinterface. It returns null if it has no parent or it is a physical interface.
InterfaceAddress Class
A new class InterfaceAddress is also included in java.net package. This class is used to represent a Network Interface address. And its encapsulates all information about a NetworkInterface's IP addresses including the subnet mask and broadcast address.
Following methods are included in this class:
- public InetAddress getAddress()
Above method returns an InetAddress of the given Interface Address.
- public InetAddress getBroadcast()
Above method returns an InetAddress representing the broadcast address for this interface address. It returns null if there is no broadcast address. Only IPv4 networks have broadcast address.
- public short getNetworkPrefixLength()
Above method returns the network Prefix length for the subnet mask of the interface address.
The following example demonstrates the above methods:
import java.util.*; import java.net.*; public class NetInt { public static void main(String args[])throws SocketException { Enumeration<NetworkInterface> netis= NetworkInterface.getNetworkInterfaces(); while(netis.hasMoreElements()) { NetworkInterface nis=netis.nextElement(); System.out.println("Network Interface name is :" +nis.getName()); System.out.println("Display name of network interface is :" +nis.getDisplayName()); System.out.println("Network Interface is up and running :" +nis.isUp()); System.out.println("Network Interface is loopback :" +nis.isLoopback()); System.out.println("Network Interface is point to point interface :"+nis.isPointToPoint()); System.out.println("Network Interface support multicasting :" +nis.supportsMulticast()); System.out.println("Network Interface MTU value is :" +nis.getMTU()); System.out.println("Network Interface is virtual interface :" +nis.isVirtual()); System.out.println("Network Interface has any Paren :" +nis.getParent()); byte[] haddress=nis.getHardwareAddress(); if (haddress!= null) { System.out.print (" Hardware address = "); for (int i = 0; i < haddress.length; i++) System.out.printf ("%02X%c", haddress [i], (i != haddress.length-1) ? '-' :'\0'); System.out.println(); } List<InterfaceAddress> iaddress=nis.getInterfaceAddresses(); Iterator<InterfaceAddress> iiaddress=iaddress.iterator(); while(iiaddress.hasNext()) { InterfaceAddress iadd=iiaddress.next(); System.out.println("Interface Address -"); System.out.println("InetAddress of the Interface Address :" +iadd.getAddress()); System.out.println("Broadcast Addres of the Interface Address :" +iadd.getBroadcast()); System.out.println("Network Prefix Length of the Interface Address :" +iadd.getNetworkPrefixLength()); } System.out.println(); } } } |
Output of the program is:
C:\j2se6>javac NetInt.java C:\j2se6>java NetInt Network Interface name is :lo Display name of network interface is :MS TCP Loopback interface Network Interface is up and running :true Network Interface is loopback :true Network Interface is point to point interface :false Network Interface support multicasting :true Network Interface MTU value is :1520 Network Interface is virtual interface :false Network Interface has any Paren :null Interface Address - InetAddress of the Interface Address :/127.0.0.1 Broadcast Addres of the Interface Address :/127.255.255.255 Network Prefix Length of the Interface Address :8 Network Interface name is :eth0 Display name of network interface is :3Com 3C920 Integrated Fast Ethernet Contro ller (3C905C-TX Compatible) - Packet Scheduler Miniport Network Interface is up and running :true Network Interface is loopback :false Network Interface is point to point interface :false Network Interface support multicasting :true Network Interface MTU value is :1500 Network Interface is virtual interface :false Network Interface has any Paren :null Hardware address = 00-B0-D0-3A-71-F7 Interface Address - InetAddress of the Interface Address :/192.168.10.55 Broadcast Addres of the Interface Address :/192.168.10.255 Network Prefix Length of the Interface Address :24 C:\j2se6> |