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,subnet mask, hardware address (MAC addresses) and MTU size.



Some new methods are added in NetworkInterface class are as follows:

public boolean isUp()

This 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()

This method returns true if the network interface is a loopback interface.



public boolean isPointToPoint()

This method returns true if the network interface is point to point interface.



public boolean supportsMulticast()

This method is used to know the network interface is support multicasting or not. If yes then its return true.



public byte[] getHardwareAddress()

This 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()

This method returns the value of MTU (Maximum Transmission Unit) of this interface.

Above all methods throws an SocketException if an I/O error occurs.



Example:










import java.util.*;

import java.net.*;

public class NetInt

{

   public static void main(String args[])throws SocketException

   {

      Enumeration netis= NetworkInterface.getNetworkInterfaces();

      while(netis.hasMoreElements())

      {

             NetworkInterface nis=(NetworkInterface)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 iaddress=nis.getInterfaceAddresses();

           Iterator iiaddress=iaddress.iterator();

           while(iiaddress.hasNext())

          {

             InterfaceAddress iadd=(InterfaceAddress)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:- The following is the sample output for the above program.


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 :Intel(R) PRO/Wireless 2200BG Network Connection - Packet Scheduler Miniport

Network Interface is up and running :false

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-13-CE-A8-56-0D









                    

Copyright © 2012 VisualBuilder. All rights reserved