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

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.

Resolution[edit]

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class Resolution
{
	public static void main (String[] arguments)
	{
		resolve ("www.info.uvt.ro");
		
		resolve ("www.google.com");
		
		resolve ("194.102.63.38");
		
		resolve ("this_name_does_not_exist.internet");
	}
	
	public static void resolve (String name)
	{
		// Printing the query.
		System.err.println ("resolving --> " + name);
		
		InetAddress address;
		InetAddress[] allAddresses;
		
		try {
			
			// Obtaining one address for the name.
			address = InetAddress.getByName (name);
			
			// Obtaining all addresses for the name.
			allAddresses = InetAddress.getAllByName (name);
			
		} catch (UnknownHostException exception) {
			
			// The address could not be resolved.
			System.err.println ("    could not resolve !!!");
			exception.printStackTrace (System.err);
			return;
		}
		
		// Printing the one (any) address.
		System.err.println ("one (any) address -->");
		printInetAddress (address);
		
		// Printing the alternative addresses.
		for (InetAddress someAddress : allAddresses) {
			System.err.println ("alternative address -->");
			if (!someAddress.equals (address))
				printInetAddress (someAddress);
			else
				System.err.println ("    skipping");
		}
		System.err.println ();
	}
	
	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 ();
		
		// Obtaining the reverse host name.
		String reverseName = address.getCanonicalHostName ();
		
		// 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 ();
		
		// Printing the reverse host name.
		System.err.println ("    reverse name = " + reverseName);
		
		// Checking for reachability.
		checkInetAddressReachable (address);
	}
	
	public static void checkInetAddressReachable (InetAddress address)
	{
		try {
			
			// Setting the timeout to 1 second (that is 1000 milliseconds).
			int timeout = 1 * 1000;
			
			// Determining the reachability.
			boolean reachable = address.isReachable (timeout);
			
			// Printing the result:
			if (reachable)
				System.err.println ("    reachable");
			else
				System.err.println ("    unreachable (or timedout) !!!");
			
		} catch (IOException exception) {
			// An unknown / unexpected network error was encountered. (Not a normal situation.)
			exception.printStackTrace (System.err);
			return;
		}
	}
}