Practical 14
1.
import java.net.InetAddress; import
java.net.UnknownHostException;
public class InetAddressExample {
public static void main(String[] args)
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("Local Host Name: " + localHost.getHostName());
System.out.println("Local Host IP Address: " + localHost.getHostAddress());
String hostname = "google.com";
InetAddress remoteHost = InetAddress.getByName(hostname);
System.out.println("Host Name: " + remoteHost.getHostName());
System.out.println("IP Address: " + remoteHost.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Host not found: " + e.getMessage());
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
Output :-
2.
import java.io.*;
import java.net.*;
public class InetDemo {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("www.google.com");
System.out.println("Host Name: " + ip.getHostName());
System.out.println("IP Address: " + ip.getHostAddress());
} catch (Exception e) {
System.out.println(e);
}
}
}
Output :-