Wednesday, 10 April 2019

Program To Demonstrate Name Resolution Using Java

import java.net.*;
import java.*;
import java.util.*;


class GFG {
public static void main(String args[])
throws UnknownHostException
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the URL for which IP address needs to be fetched : ");
// The URL for which IP address needs to be fetched
String s = sc.next();
System.out.print("Enter the Ip for which address needs to be fetched : ");
// The IP address for which Domain Name needs to be fetched
String ips = sc.next();
try {
// Fetch IP address by getByName()
InetAddress ip = InetAddress.getByName(new URL(s).getHost());


// Print the IP address
System.out.println("Public IP Address of: " + ip);


//Fetch Domain Name/Server Name by getHostName()
System.out.println("Public Address of IP : " + InetAddress.getByName(ips).getHostName());
}
catch (MalformedURLException e) {
// It means the URL is invalid
System.out.println("Invalid URL");
}
}
}
Output:
Enter the URL for which IP address needs to be fetched : https://www.google.com
Public IP Address of: www.google.com/172.217.26.228
Enter the Ip for which address needs to be fetched : 172.217.26.228
Public Address of IP : bom05s09-in-f4.1e100.net

No comments:

Post a Comment