Computer networks -- 2008-2009 -- info.uvt.ro/Sockets/Addressing/Examples/Interfaces

From Wikiversity

Quick links: front; agenda; courses 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; examination.

Important! Please note that the current work serves mainly as general guidance and discussion topics, and is by no means the reference material for the course. For further information please consult the dedicated section.

Interfaces[edit]

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;


public class Interfaces
{
	public static void main (String[] arguments)
			// Normally we should not encounter any exceptions. (If we have at least one network card...)
			throws Exception
	{
		// Obtaining the local address.
		InetAddress localAddress = InetAddress.getLocalHost ();
		
		// Printing the information available for the local address.
		System.err.println ("local -->");
		printInetAddress (localAddress);
		System.err.println ();
		
		
		// Iterating through the list of available network interfaces.
		Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces ();
		while (interfs.hasMoreElements ()) {
			NetworkInterface interf = interfs.nextElement ();
			
			// Printing the information available for the interface.
			System.err.println ("interface -->");
			printInterface (interf);
			
			// Iterating through the list of available interface addresses for the current interface.
			for (InterfaceAddress address : interf.getInterfaceAddresses ()) {
				System.err.println ("interface address -->");
				printInterfaceAddress (address);
			}
			
			System.err.println ();
		}
	}
	
	public static void printInterface (NetworkInterface interf)
	{
		// Obtaining the (operating system) identifier.
		String identifier = interf.getName ();
		
		// Obtaining a more human-readable identifier.
		String description = interf.getDisplayName ();
		
		// Printing the obtained inforation.
		System.err.println ("    identifier = " + identifier);
		System.err.println ("    description = " + description);
	}
	
	public static void printInterfaceAddress (InterfaceAddress interfaceAddress)
	{
		// Obtaining the local address.
		InetAddress localAddress = interfaceAddress.getAddress ();
		
		// Obtaining the broadcast address.
		InetAddress broadcastAddress = interfaceAddress.getBroadcast ();
		
		// Printing the local address.
		System.err.println ("local address -->");
		printInetAddress (localAddress);
		
		// Printing the broadcast address.
		System.err.println ("broadcast address -->");
		if (broadcastAddress != null)
			printInetAddress (broadcastAddress);
		else
			System.err.println ("    none");
	}
	
	public static void printInetAddress (InetAddress address)
	{
		// Obtaining the IP as a string.
		String ipAsString = address.getHostAddress ();
		
		// Obtaining the IP as an array of bytes.
		byte[] ipAsBytes = address.getAddress ();
		
		// Printing the default string representation.
		System.err.println ("    address = " + address);
		
		// Printing the IP as string.
		System.err.println ("    ip as string = " + ipAsString);
		
		// Printing the IP as bytes.
		System.err.println ("    ip as bytes length = " + ipAsBytes.length);
		System.err.print ("    ip as bytes =");
		for (byte b : ipAsBytes)
			System.err.print (" " + (b));
		System.err.println ();
	}
}