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.
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.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:
Above all methods throws an SocketException if an I/O error occurs.
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:
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> |
Ads