Practical 14: Write a program to demonstrate the use of lnetAddress class and its factory methods.

1) Develop a program using InetAddress class to retrieve IP address of computer when hostname is entered by the user.
Ans:
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class RetriveIP
{
    public static void main(String[] args) {
         
         Scanner sc = new Scanner(System.in);
        
         System.out.println("Enter Host Name: "); 
         String host = sc.nextLine();

         try
         {
             InetAddress ip = InetAddress.getByName(host);

             System.out.println("IP Adress of Computer is:"+ip.getHostAddress());
         }
         catch(UnknownHostException e)
         {
             System.out.print(e);
         }
         
    }
}

Output:
Retrive IP adress using Java

Previous
Next Post »