Home » Java programs

Java program to get the IP address, Hostname based on given Hostname

Submitted by BalRam Dixit, on May 19, 2017
Learn: How to get the host ip address (website ip address), host name based on given host name (website name) in java, this program will take a host name as input and print the host name, ip address.

In this java program, we will learn how to get the IP Address and Host name, when host name is given. This program will read host name (website name) as an input and based on this input program will find and print the IP address and host name.

Consider the program

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

public class IPDemo 
{ 
	public static void main(String[] args){ 
		String host; 
		Scanner input = new Scanner(System.in); 
		System.out.print("\n Enter host name: "); 
		host = input.nextLine(); 
		try { 
			InetAddress address = InetAddress.getByName(host);
			System.out.println("IP address: " + address.getHostAddress());
			System.out.println("Host name : " + address.getHostName());  
			System.out.println("Host name and IP address: " + address.toString()); 

		} 
		catch (UnknownHostException ex) {
		     System.out.println("Could not find " + host); 
		}
	} 
}

Output

Enter host name: www.google.com
IP address: 216.58.220.196
Host name : www.google.com
Host name and IP address: www.google.com/216.58.220.196



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.