Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Enhancements in Networking Features 
 

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>

Download this example

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.