Internet Address Resolution in Java
Last Updated :
14 May, 2023
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.
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read