Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / Java / Java SE / J2SE 5.0
Tip/Trick

Protohost Scanner

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Jul 2013CPOL 7.2K   72   3  
Prohost Scanner

Introduction

The application that I would like to present here is called prohost scanner.

This source is just a simple idea. I read some network programming ebooks, especially Java networking. I am not an expert, but sometimes it's not as difficult as it seems. This is source code about TCP/IP concept, basic TCP/IP concept, let me explain the code.

There are 3 methods in this source.

1. Main Method

Java
public static void main(String args[]){
    
    if(args.length>0){
    for ( int i=0;i<args.length;++i){
                String lookup = "host";
    System.out.println(lookup+(args[i]));
    }
    }else
    {
    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter  Hostname [ IP Address], type \"exit\" to quit");
    try{
    while(true){
    String host=in.readLine();
    if(host.equalsIgnoreCase("exit")||host.equalsIgnoreCase("quit")){
    break;
    }
   System.out.println(lookup(host));
    }
    }
    catch(IOException ex){
    System.out.println(ex);
    }
    }
    }

2. Lookup Method

Java
private static String lookup(String host){
   
        InetAddress node;
        // get bytes from ip address
        try{
        node=InetAddress.getByName(host);
        
    
        }
        catch(UnknownHostException ex){
        return "tidak ditemukan host:"+host;
        }
   if(isHostname(host)){
   return node.getHostAddress();
   
   }
   else {
   return node.getHostName();
   }
    }

3. isHostname Method

Java
private static boolean isHostname(String host){
    // check if the host using ipv6
        if(host.indexOf(':')!=-1) return 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;
    }
Java
// simple input with bufferedReader
 BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter  Hostname [ IP Address], type \"exit\" to quit"// print out th 
Java
// gathering information about hostname
if(args.length>0){
    for ( int i=0;i<args.length;++i){
                String lookup = "host";
    System.out.println(lookup+(args[i])); 
Java
//  check hostname using ip46 or  not 
if(host.indexOf(':')!=-1) return false;
        char [] ca=host.toCharArray();
        
        for(int i=0;i<ca.length;++i){
        if(!Character.isDigit(ca[i])){
        if(ca[i]!='.') return true; 

Full Source Code

Java
import java.net.*;
import java.io.*;
/**
 *
 * @author Xz
 * contact : silentboy70203@gmail.com
 */
public class nslookup {
    
    public static void main(String args[]){
    
    if(args.length>0){
    for ( int i=0;i<args.length;++i){
                String lookup = "host";
    System.out.println(lookup+(args[i]));
    }
    }else
    {
    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Hostname [ IP Address], type \"exit\" to quit");
    try{
    while(true){
    String host=in.readLine();
    if(host.equalsIgnoreCase("exit")||host.equalsIgnoreCase("quit")){
    break;
    }
   System.out.println(lookup(host));
    }
    }
    catch(IOException ex){
    System.out.println(ex);
    }
    }
    }
    // method main selesai    
    
    private static String lookup(String host){
   
        InetAddress node;
       
        try{
        node=InetAddress.getByName(host);       
    
        }
        catch(UnknownHostException ex){
        return "tidak ditemukan host:"+host;
        }
   if(isHostname(host)){
   return node.getHostAddress();
   
   }
   else {
   return node.getHostName();
   }
    } 
    
    private static boolean isHostname(String host){
    
        if(host.indexOf(':')!=-1) return 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;
    }    
}

The function of this source is to trace the hostname and nslookup a hostname.

I know that this is just a simple source. Next time, I will be using a different Graphical User Interface. For now, I think this is enough.

License

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


Written By
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Collaborative Group (No members)


Comments and Discussions

 
-- There are no messages in this forum --