Introduction
This article focuses on the Java class named InetAddress
. The class uses DNS (Domain Name Systems) to return different information about a host (web address).
The article is not very technical, I have tried to keep it as simple as possible and focus on the main advantages of the class.
The most useful methods from this class are:
getByName(host)
- the method will return the IP of the hostgetAllByName(host)
- the method will return an array of IPs about the hostgetLocalHost()
- the method will return the name of the computer and the IP where the application is running
The exception UnknownHostException
is thrown when the host cannot be found or reached with success. It's very useful to catch this exception and to customize it as you wish when you need it.
There are two types of addresses: unicast and multicast.
The unicast addresses are split in three general categories:
- An identifier for a single interface - where a packet is sent to a unicast address and is delivered to the interface which is identified by that address.
- The Unspecified Address - which is also called anylocal or wildcard address. It must never be assigned to any node. It indicates the absence of an address. The unspecified address must not be used as the destination address of an IP packet.
- The Loopback Addresses - which is the address assigned to the loopback interface. Anything sent to this IP address loops around and becomes IP input on the local host. This address is often used when testing a client.
The multicast addresses which represent an identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address.
Try to analyse the code below:
try {
InetAddress utopia = InetAddress.getByName("utopia.poly.edu");
InetAddress duke = InetAddress.getByName("128.238.2.92");
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
It is very simple and precise to find the IP or the name of the host, right?
The class is widely used when you want to make a client server application, a chat program or try to make a communication between a server [see] and a client [see].
Using the Class and Implementing the Methods
Method 1 - getByName(host)
public static void main(String[] args)
{
String host = "";
Scanner sc = new Scanner(System.in);
System.out.println("Host: ");
host = sc.next();
try
{
InetAddress ia = InetAddress.getByName(host);
System.out.println(ia);
}
catch(UnknownHostException uhe)
{
System.out.println(uhe.toString());
}
}
Method 2 - getAllByName(host)
public static void main(String[] args)
{
String host = "";
Scanner sc = new Scanner(System.in);
System.out.println("Host: ");
host = sc.next();
try
{
InetAddress[] ia = InetAddress.getAllByName(host);
for(int i=0; i<ia.length; i++)
{
System.out.println(ia[i].toString());
}
}
catch(UnknownHostException uhe)
{
System.out.println(uhe.toString());
}
}
Method 3 - getLocalHost()
public static void main(String[] args)
{
String host = "";
Scanner sc = new Scanner(System.in);
System.out.println("Host: ");
host = sc.next();
try
{
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia.toString());
}
catch(UnknownHostException uhe)
{
System.out.println(uhe.toString());
}
}
Conclusion
I hope you enjoyed reading this small and concise article about InetAddress
class, and that I have managed to give an idea about network programming in several easy steps. If you have any questions, don't hesitate to ask.
Happy coding!