Example 1 Below is a simple Java program which can get the hostname of a computer
From IP address. download now
Tips
1. Compile: javac GetHost
2. Run: java GetHost 111.111.111.1 (your IP or others)
Import java. io .*;
Import java.net .*;
//
//
// GetHost. java
//
//
Public class GetHost
{
Public static void main (String arg []) {
If (arg. length> = 1 ){
InetAddress [] Inet;
Int I = 1;
Try {
For (I = 1; I <= arg. length; I ++ ){
Inet = InetAddress. getAllByName (arg [I-1]);
For (int j = 1; j <= Inet. length; j ++ ){
System. out. print (Inet [J-1]. toString ());
System. out. print ("");
}
}
}
Catch (UnknownHostException e ){
System. out. print ("Unknown HostName! "+ Arg [I-1]);
}
}
Else {
System. out. print ("Usage java/jview GetIp ");
}
}
}
Example 2
Download now
// GetHTML. java
/**
* This is a program which can read information from a web server.
* @ Version 1.0 2000/01/01
* @ Author jdeveloper
**/
Import java.net .*;
Import java. io .*;
Public class GetHTML {
Public static void main (String args []) {
If (args. length <1 ){
System. out. println ("USAGE: java GetHTML httpaddress ");
System. exit (1 );
}
String sURLAddress = new String (args [0]);
URL url = null;
Try {
Url = new URL (sURLAddress );
} Catch (MalformedURLException e ){
System. err. println (e. toString ());
System. exit (1 );
}
Try {
InputStream ins = url. openStream ();
BufferedReader breader = new BufferedReader (new InputStreamReader (ins ));
String info = breader. readLine ();
While (info! = Null ){
System. out. println (info );
Info = breader. readLine ();
}
}
Catch (IOException e ){
System. err. println (e. toString ());
System. exit (1 );
}
}
}