Home : Course Map : Chapter 13 :
InetAddress Class
JavaTech
Course Map
Chapter 13

Network Overview
Internet Basics
IP - Datagrams
  TCP - UDP
  Application Layer
Ports
Java Networking
URL
  Demo 1
Read From URL
  Demo 2
InetAddress
  Demo 3   Demo 4
Sockets
  Demo 5
Client-Server

RMI
Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The java.net.InetAddress class represents IP addresses. It works with either the hostname (e.g. www.kth.se) or the numerical IP address (e.g. 64.124.81.56) of an Internet host. InetAddress class offers a number of useful methods for dealing with hostnames and IP addresses.

The demo programs below illustrate some of the capabilities of the InetAddress class.

The applet LocalAddress displays the local host and IP address. Note that the SecurityManager in the browser JVM may block access to this information. You can try it in the appletviewer tool or run it as an application.

import java.applet.*;
import java.awt.*;
import java.net.*;

/** Show how InetAddress can provide the local IP address.**/
public class LocalAddress extends Applet
{
  String fMessage = "";

  /**
    * Create an instance of InetAddress for the
    * local host and display the local IP address.
   **/
  public void init () {
    try {
      InetAddress local_Address =
             InetAddress.getLocalHost ();
      fMessage ="Local IP address = " +
             local_Address.toString ();
    }
    catch  (UnknownHostException e) {
      fMessage ="Unable to obtain local IP address";
    }
    System.out.println (fMessage);
  } // init

  /** Paint IP info in Applet window. **/
  public void paint (Graphics g) {
     g.drawString (fMessage,20,20);
  }

  /** Print out the local address in app mode.**/
  public static void main (String [] args) {
    LocalAddress applet =
       new LocalAddress ();
    applet.init ();
  } // main
  
} // Class LocalAddress

 

This application returns the IP address if given a host name, or returns the host name if given the IP address:

TranslateAddress

import java.net.*;

/** Translate an IP argument to a host name or
  * a host name to an IP address.
 **/
class TranslateAddress {

  public static void main  (String args[])
  {

    // Look for command line argument
    if (args.length != 1) {
      System.out.println ("Error! No IP or host name address");
      System.out.println ("Usage: java TranslateAddress java.sun.com");
      System.out.println ("    or java TranslateAddress 209.249.116.143");
      System.exit (0);
    }

    try  {
      // When the argument passed is a host name  (e.g. sun.com),
      // the corresponding IP address is returned. If passed
      // an IP address, then only the IP address is returned.
      InetAddress address = InetAddress.getByName (args[0]);
      System.out.println ("Address " + args[0] + " = " + address);

      // To get the hostname when passed an IP address use
      // getHostName (), which will return the host name string.
      System.out.println ("Name of " + args[0] + " = " +
                            address.getHostName ());
    } catch  (UnknownHostException e) {
      System.out.println ("Unable to translate the address.");
    }
  } // main
  
} // class TranslateAddress

 

Example runs of this program:

c:\> java TranslateAddress gluon.particle.kth.com
Address gluon.particle.kth.se = gluon.particle.kth.se/130.237.34.133
Name of gluon.particle.kth.se = gluon.particle.kth.se

c:\> java TranslateAddress 130.237.34.133
Address 130.237.34.133 = /130.237.34.133
Name of 130.237.34.133 = gluon.particle.kth.se

Note that getByName(String str) returns an instance of InetAddress when given a host name. Then when this objects converts to a string it displays the HostName/IP_Address pair.

However, when given an IP address the resulting InetAddress object does not show the host name. Instead you must use the the getHostName() method to obtain the hostname.

References and Web Resources

Latest update: Dec. 8, 2004

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.