An nslookup clone in Java : IP Address « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » IP AddressScreenshots 
An nslookup clone in Java

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? address[i256
            : 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



           
       
Related examples in the same category
1. Convert a hostname to the equivalent IP addressConvert a hostname to the equivalent IP address
2. Display multiple IP addresses
3. Looking Up the Address of a HostLooking Up the Address of a Host
4. Looking for Ports: 0 -- 1024
5. Looking for Port: 1024 -- 65536
6. Looking for Port: start from 65535
w___w___w__.j___av_a2s_._c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.