Click here to Skip to main content
Click here to Skip to main content

InetAddress Java Class

By , 3 Mar 2013
 

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:

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!

License

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

About the Author

Marius Mihailescu
Instructor / Trainer ,,Titu Maiorescu'' University
Romania Romania
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 3 Mar 2013
Article Copyright 2013 by Marius Mihailescu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid