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
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
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();
}
}
3. isHostname Method
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;
}
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter Hostname [ IP Address], type \"exit\" to quit"
if(args.length>0){
for ( int i=0;i<args.length;++i){
String lookup = "host";
System.out.println(lookup+(args[i]));
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
import java.net.*;
import java.io.*;
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);
}
}
}
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.