Java Program to Determine Hostname from IP Address
Last Updated :
09 Nov, 2020
IP Address stands for internet protocol address. It is an identifying number that is associated with a specific computer or computer network. IP addresses are usually written and displayed in human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address). When connected to the internet, the IP address allows the computers to send and receive information. Every site has its own host link to their own IP Address which helps to send the request by the user to the HTTP traffic and helps to collect the information which the users want This is how an IP address is necessary for every host.
There are many ways to get the IP Address of a particular host. So the method which helps to get the IP address for any Host is getHostAddress() of InetAddress class.
Syntax:
public String getHostAddress()
Returns: It returns the raw IP address in a string format.Note: It is easy to find the IP Address from a Hostname. But due to security reasons, it is difficult to get the Host Name from the IP address because all IP addresses are secured.
The following methods are used to get the Host Name.
- getHostName(): This function retrieves the standard hostname for the local computer.
- getHostByName(): This function retrieves host information corresponding to a hostname from a host database.
Example: Obtaining the IP address from the given host
Java
// Java program to obtain IP Address
// from a given host
import java.io.*;
import java.util.*;
import java.net.*;
class GFG {
public static void main(String[] args)
{
// Host
String host = "www.geeksforgeeks.org";
// Using try Logic So that if there is an error then
// easily get the error
try {
// calling the function which gives the IP
// Address from the given host
InetAddress[] iaddress
= InetAddress.getAllByName(host);
for (InetAddress ipaddresses : iaddress) {
System.out.println(ipaddresses.toString());
}
}
catch (UnknownHostException e) {
System.out.println(e);
}
}
}
Example: Obtaining the Hostname from the Given IP address. All the Server and IP are Secured for a security reason So Hostname directly from the IP address cannot be found. But the Method is similar to found any Hostname from the unsecured IP address.
Java
// Java program to get Host name
// from an ipaddress
import java.io.*;
import java.net.*;
class GFG {
public static void main(String[] args)
{
try {
// IP Address
InetAddress addr
= InetAddress.getByName("23.229.203.68");
// Host name
System.out.println("Host name is: "
+ addr.getHostName());
// Host Address
System.out.println("Ip address is: "
+ addr.getHostAddress());
}
catch (UnknownHostException e) {
System.out.println(e);
}
}
}
OutputHost name is: 23.229.203.68
Ip address is: 23.229.203.68
Similar Reads
Determining the IP Address & Hostname of Local Computer in Java IP Address Stands for Internet Protocol Address, is a numeric label that is assigned uniquely to each device connected within a computer network that uses the internet protocol. An IP address serves two principal functions: It identifies the host, or more specifically its network interface.It provi
3 min read
Java program to find IP address of your computer An IP(Internet Protocol) address is an identifier assigned to each computer and another device (e.g., router, mobile, etc) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in hu
2 min read
How to determine the user IP address using node.js ? Node.js is an open-source, back-end JavaScript runtime environment that runs on the web engine and executes JavaScript code. There are various platforms such as Windows, Linux, Mac OS Â where Node.js can run. The Domain Name System is a hierarchical and decentralized naming system for computers etc t
2 min read
Internet Address Resolution in Java Internet-Address resolution is the process of converting a domain name (like www.geeksforgeeks.org) to an IP address or vice versa. This is the first step in connecting to a network. Network devices talk to each other using IP addresses, but people tend to remember domain names better. Most of the t
4 min read
Finding IP address of a URL in Java Prerequisite: InetAddressgetByName() : Returns the InetAddress of the given host. If the host is a literal IP address, then only its validity is checked. Fetches public IP Address of the host specified. It takes the host as an argument and returns the corresponding IP address. Examples:Â Input : www
2 min read