Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Windows-Phone-7

InetAddress Java Class

5.00/5 (1 vote)
3 Mar 2013CPOL2 min read 25.9K  
The article describes the use of InetAddress class with short and easy-to-understand examples. I have tried to focus on practical examples showing how we can obtain information about a host. The examples provided in this article are provided from the academic point of view.

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 host
  • getAllByName(host) - the method will return an array of IPs about the host
  • getLocalHost() - 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:

  1. 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.
  2. 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.
  3. 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:

Java
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)

Java
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)

Java
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()

Java
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)