Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Advanced Java Programming

Name : ASHWIN PAWAR CO5I Roll No. : 23

Practical : 14

Q. . Execute the following code and write the output


CODE:
import java.io.*;
import java.net.*;

public class practical14Test {


public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("localhost");
System.out.println("Host Name: " + ip.getHostName());
System.out.println("IP Address: " + ip.getHostAddress());
} catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT:

Q. Develop a program using InetAddress class to retrieve IP address of


computer when hostname is entered by the user?
Advanced Java Programming

CODE:
import java.net.InetAddress;
import java.util.Scanner;

public class practical14A{


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter Hostname : ");


String hostname = sc.nextLine();

try {
InetAddress inetAddress = InetAddress.getByName(hostname);
String ipAdd = inetAddress.getHostAddress();

System.out.println("IP address for " +hostname+ " is : " +ipAdd);


} catch (Exception e) {
System.err.println("Unable to find the IP of : "+hostname);
}
finally{
sc.close();
}
}
}

OUTPUT:
Advanced Java Programming

You might also like