Open In App

Internet Address Resolution in Java

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 time, an Internet address is resolved by a Domain Name System (DNS) server, which keeps a list of domain names and the IP addresses that go with them.

The InetAddress class in Java makes it possible to convert domain names to IP addresses and IP addresses to domain names. The InetAddress class’s getByName() method takes a domain name as an argument and gives back an InetAddress object that is the IP address for that domain name. In the same way, the getHostName() method of the InetAddress class takes an IP address as an input and returns a String that is the domain name for that IP address.

In Java, you use the InetAddress class, which is part of the java.net package, to figure out an internet address. This class has ways to convert between IP addresses and host names. You can use the InetAddress class’s getByName() method to convert a hostname to an IP address. For instance:

String hostName = "www.example.com";
InetAddress address = InetAddress.getByName(hostName);

This will give you back an InetAddress object with the IP address of the hostname you gave. An UnknownHostException will be thrown if the hostname can’t be found. You can use the InetAddress class’s getHostByAddress() method to turn an IP address into a hostname. For instance:

byte[] ipAddress = { 192, 0, 2, 1 };
InetAddress address = InetAddress.getByAddress(ipAddress);
String hostName = address.getHostByAddress(ipAddress);

This will return a String object with the hostname associated with the supplied IP address. An UnknownHostException will be issued if the IP address cannot be resolved. It is crucial to remember that these approaches may entail network I/O activities, which may create a delay in the execution of your software. As a result, it’s best to run them in a different thread or employ non-blocking I/O operations.

Code Example

Java




import java.net.InetAddress;
import java.net.UnknownHostException;
  
public class InternetAddressResolutionExample {
  
    public static void main(String[] args) {
        
        String hostName = "www.geeksforgeeks.org";
        
        try {
            // Resolving host name to IP address
            InetAddress address = InetAddress.getByName(hostName);
            System.out.println("IP address of " + hostName + ": " + address.getHostAddress());
  
            // Resolving IP address to host name
            byte[] ipAddress = { (byte) 172, (byte) 217, (byte) 2, (byte) 110 };
            InetAddress reverseAddress = InetAddress.getByAddress(ipAddress);
            System.out.println("Host name of " + reverseAddress.getHostAddress() + ": " + reverseAddress.getHostName());
        } catch (UnknownHostException e) {
            System.out.println("Could not resolve host: " + e.getMessage());
        }
    }
}


Output:

IP address of www.geeksforgeeks.org: 23.34.172.35
Host name of 172.217.2.110: iad23s72-in-f14.1e100.net

Code Explanation

In this example, we begin by specifying the hostname to be resolved: www.geeksforgeeks.org. The InetAddress class’s getByName() function is then used to translate this hostname to an IP address. The IP address of the hostname is included in the resultant InetAddress object, which we can access using the getHostAddress() function.

After that, we show how to resolve an IP address to a hostname by generating a byte array using the four bytes of the IP address 172.217.2.110. We then use the InetAddress class’s getByAddress() function to build an InetAddress object from this byte array. Lastly, we use the InetAddress object’s getHostName() function to acquire the hostname associated with this IP address.

We were able to resolve the hostname www.geeksforgeeks.org to its IP address, and the IP address 172.217.2.110 to its hostname, as you can see. If the hostname is unable to be resolved, the getByName() function will raise an UnknownHostException, which we will catch and manage in the example.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads