Ads

New Domain

Blog has been moved to new domain: www.it-googled.com Enjoy!

Wednesday 19 January 2011

Java IpAddressLookup simple class

import java.net.*;
import java.util.*;
public class IpAddressLookup {

public static void main(String[] args) {
try
{

System.out.print("Please enter the address : ");
Scanner Sc = new Scanner(System.in);
String host= Sc.next();
InetAddress PC = InetAddress.getByName(host);

String hostname = PC.getHostName();
byte[] signed = PC.getAddress();
System.out.println("Hostname is : " + hostname);
System.out.println("Signed address is " + signed);
int unsigned;
System.out.println();
System.out.print("IP Address is : ");

for(int i=0; i {
unsigned = signed[i] < 0 ? signed[i] + 256 : signed[i];

System.out.print(unsigned + ".");
}
System.out.println();
}
catch (UnknownHostException e){
System.out.println("Can't find the IP Address or the hostname");
}}}

No comments:

Post a Comment