Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 09:12 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
An nslookup clone in Java
This Java tip shows how to create an nslookup clone in Java.

Code:
import java.io.DataInputStream; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; /** * An "nslookup" clone in Java. * * @author Elliot Rusty Harold, O'Reilly & Associates */ public class JavaLookup { public static void main(String args[]) { if (args.length > 0) { // use command line for (int i = 0; i < args.length; i++) { lookup(args[i]); } } else { DataInputStream myInputStream = new DataInputStream(System.in); System.out .println("Enter names and IP addresses. Enter \"exit\" to quit."); while (true) { String s; try { s = myInputStream.readLine(); } catch (IOException e) { break; } if (s.equals("exit")) break; if (s.equals("quit")) break; if (s.charAt(0) == '\004') break; // unix ^D lookup(s); } } } /* end main */ private static void lookup(String s) { InetAddress thisComputer; byte[] address; // get the bytes of the IP address try { thisComputer = InetAddress.getByName(s); address = thisComputer.getAddress(); } catch (UnknownHostException ue) { System.out.println("Cannot find host " + s); return; } if (isHostname(s)) { // Print the IP address for (int i = 0; i < address.length; i++) { int unsignedByte = address[i] < 0 ? address[i] + 256 : address[i]; System.out.print(unsignedByte + "."); } System.out.println(); } else { // this is an IP address try { System.out.println(InetAddress.getByName(s)); } catch (UnknownHostException e) { System.out.println("Could not lookup the address " + s); } } } // end lookup private static boolean isHostname(String s) { char[] ca = s.toCharArray(); // if we see a character that is neither a digit nor a period // then s is probably a hostname for (int i = 0; i < ca.length; i++) { if (!Character.isDigit(ca[i])) { if (ca[i] != '.') { return true; } } } // Everything was either a digit or a period // so s looks like an IP address in dotted quad format return false; } // end isHostName } // end javalookup
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clone an Array Java Tip java.lang 0 04-14-2008 09:46 PM
clone method javaplus New To Java 2 01-30-2008 10:47 AM
clone method gapper New To Java 1 01-20-2008 09:46 AM
clone problem feniger New To Java 13 01-13-2008 11:55 AM
How to clone a JAXB object ? simon Advanced Java 1 07-15-2007 12:56 AM


All times are GMT +3. The time now is 10:51 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org