1) Develop a program using InetAddress class to retrieve IP address of computer when hostname is entered by the user.
Ans:
Output:
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: