Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
To retrieve the IP address from Host Name, vice-versac 
 

Here we are going to explain the method to find out the IP address from host name and to vice verse.

 

To retrieve the IP address from Host Name, vice-versa

                         

Here we are going to explain the method to find out the IP address from host name and to vice verse. Here we are give a complete example named HostLookup.java. In which we call a InetAddress and make a object and pass the input value in it. After that we put the condition, if the user enter any host name then we call the input value with getHostAddress() which return the IP address of that Host . If the user enter any IP address then we call the input value with getHostName() which return the host name of that Host.

Apart from all of this we put the condition for wrong entry system prints "localhost" and if user want to exit from the program then he/she should need to write "exit". The complete code of the example is as under.

Here is the Code of the Example :

HostLookup.java

import java.net.*;
import java.io.*;

public class HostLookup {
  public static void main (String[] args) {
    if (args.length > 0) { 
      for (int i = 0; i < args.length; i++) {
        System.out.println(lookup(args[i]));
      }
    }
    else {  
      BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter the name and IP addresses. ");
      System.out.println("Enter \"exit\".");
      try {
        while (true) {
          String host = buffer.readLine();
          if (host.equalsIgnoreCase("exit")) {
            break;
          }
          System.out.println(lookup(host));
        }
      }
      catch (IOException ex) {
        System.err.println(ex);
      }
    }
  }
  private static String lookup(String hostname) {
    InetAddress node;
    try {
      node = InetAddress.getByName(hostname);
    }
    catch (UnknownHostException ex) {
      return "Cannot find host " + hostname;
    }
    if (isHostname(hostname)) {
      return node.getHostAddress();
    }
    else {  
      return node.getHostName();
    }
  }
  private static boolean isHostname(String host) {
    // Is this an IPv6 address?
    if (host.indexOf(':'!= -1return false;
    char[] ca = host.toCharArray();
  for (int i = 0; i < ca.length; i++) {
    if (!Character.isDigit(ca[i])) {
      if (ca[i!= '.'return true;
    }
  }
    return false;

   }  

Here is the Output of the Example :

C:\ashish\complete>javac HostLookup.java

C:\ashish\complete>java HostLookup
Enter the name and IP addresses.
Enter "exit".
roseindia
Cannot find host roseindia
roseindi
192.168.10.104
192.168.10.103
comp20

localhost

Download of this example.

 

                         

» View all related tutorials
Related Tags: c com forms server orm form network socket io sed request return opera ai for work wait to base pos

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.