How to Find Host Web Application in Java?

The Find Host Application uses the InetAddress class to find information related to the hosts (Web Addresses). The InetAddress class uses a Domain Name System (DNS) to determine the host information, such as – IP address, hostname, etc.

The Find Host Application uses the following functions of the InetAddress class:
1. getByName(): It returns the IP address of the host.
2. getAllByName(): It returns all the IP addresses of the host.
3. getLocalHost(): It returns the name and IP address of the local host.

Find Host Program in Java:

import java.utill.Scanner;
import java.net.*;
class FindHost
{
	public static void main(String args[])
	{
		char choice;
		String str="";
		Scanner input=ne Scanner(System.in);
		while(true)
		{
			System.out.println("Select your choice");
			System.out.println("I -> Find IP address for a given host name");
			System.out.println("A -> Find all IP address for a given host name");
			System.out.println("L -> Find the local host");
			System.out.println("E -> Exit");
			System.out.println("Your choice: ");
			System.out.flush();
			try
			{
				switch(choice=(char) System.in.read())
				{
					case 'I':
					case 'i': System.out.println("Enter host name:");
					str=input.next();
					try
					{ 
					  InetAddress addr=Inet4Address.getByName(str);
					  System.out.println("IP Addresso the site is: " +addr);
					}
					catch(UnknownHostException e)
					{
						System.out.println("There is an exception: " +e.getMessage());
					}
					break;
					
			case 'A':
			case 'a': System.out.println("Enter host name:");
			str=input.next();
			try
			{
				InetAddress[] addrs=InetAddress.getAllByName(str);
				System.out.println("IP Addresses of the site are:");
				for(int i=0; i<addrs.length; i++)
				{
					System.out.println(addrs[i]);
				}
				catch(UnknownHostException e)
				{
					System.out.println("There is an exception: " +e.getMessage());
				}
				break;
			case 'L':
			case 'l': try
			{
				InetAddress name=InetAddress.getLocalHost();
				System.out.println("Local host:"+name.toString());
			}
			catch(UnknownHostException e)
			{
				System.out.println("There is an exception: "+e.getMessage());
			}
			break;
			case 'E':
			case 'e': System.exit(0);
			default: System.out.println("Invalid Choice");
			}
				}
		             catch(Exception e)
				{
					System.out.println("I/O Error");
				}
			}
		}
	}
}

Running the application:

To run the Find Host application, we need to perform the following steps:
1. Run the following command at the command prompt:

javac findhost.java

2. After a successful compilation, run the following command:

java findhost

Output:

Select your choice
I -> Find the IP address for a given hostname
A -> Find all IP addresses for a given hostname
L -> Find the Localhost
E -> Exit
Your choices: I
Enter hostname: www.google.com
IP Address of the site is: 210.230.50.99

Select your choice
I -> Find IP address for a given hostname
A -> Find all IP addresses for a given hostname
L -> Find the Localhost
E -> Exit

Your choice: A
Enter hostname: www.google.com
IP Addresses of the site are:
www.google.com/72.14.253.147
www.google.com/72.14.253.103
www.google.com/72.14.253.105
www.google.com/72.14.253.115
Select your choice
I -> Find IP address for a given hostname
A -> Find all IP addresses for a given hostname
L -> Find the Localhost
E -> Exit
Your choices: L
Localhost: default/115.88.3.22

Select your choice
I -> Find IP address for a given hostname
A -> Find all IP addresses for a given hostname
L -> Find the Localhost
E -> Exit
Your choice: E