import java.io.*;
import java.net.*;
public class InetDemo {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("www.TechSarvam.com");
System.out.println("Host Name: " + ip.getHostName());
System.out.println("IP Address: " + ip.getHostAddress());
} catch (Exception e) {
System.out.println(e);
}
}
}
1. Import Statements
import java.io.*;
import java.net.*;
These are imports required for this program. Import java.net.* enables classes like InetAddress, which offers functionality to work with networking such as retrieval of IP address, whereas import java.io.* is done; however, no direct usage can be seen of these classes within the program given here.
2. Class Definition
public class InetDemo {
This declares a public class named InetDemo.
3. Main Method
public static void main(String[] args) {
The main method is the entry point of the Java program, where the program starts its execution.
4. InetAddress Object Creation
InetAddress ip = InetAddress.getByName(“www.TechSarvam.com”);
Here, the program creates an InetAddress object by calling the static method getByName(). It retrieves the IP address associated with the hostname www.TechSarvam.com. The getByName() method:
- Converts the hostname to an IP address (in other words, does a DNS lookup).
- Returns the InetAddress object for the host-www.TechSarvam.com in this instance.
5. Output Statements
System.out.println(“Host Name: ” + ip.getHostName());
System.out.println(“IP Address: ” + ip.getHostAddress());
- ip.getHostName()- returns the name of the host-in this instance, it is www.TechSarvam.com
- ip.getHostAddress()- returns the IP address linked to that particular host. The result will be the IP in string format: for example: “192.168.1.1.”
6. Exception Handling
}catch(Exception e){
System.out.println(e);
}
This try-catch block catches all the possible exceptions that may come up, like:
- In case of any problem in finding the host or an error with the host resolution, an exception will be thrown and caught.
- For all other unknown errors that may occur, the exception message will be printed out.
7. Program Output
The output of the program will be as follows:
Host Name: www.TechSarvam.com
IP Address: <IP Address of www.TechSarvam.com>
- Host Name: This will return the host name you entered, which here is www.TechSarvam.com.
- IP Address: It will return the actual IP address the hostname resolves to, such as 192.168.0.1 or some other valid IP address. The actual IP address depends on the DNS resolution of www.TechSarvam.com.
Example Output (Hypothetical)
Host Name: www.TechSarvam.com
IP Address: 93.184.216.34
Explanation
- Host Name: The domain name www.TechSarvam.com is resolved back to itself.
- IP Address: The application will print the resolved IP address of www.TechSarvam.com that depends on DNS lookup results.
- This program illustrates the process of extracting the host name along with its associated IP address of any provided domain name by using InetAddress classes in Java, doing a DNS lookup and print these values. It is able to capture exceptions that can possibly occur with it.