Package com. tianren. service;
Import java.net .*;
Import sun. security. krb5.internal. HostAddress;
Public class Test {
InetAddress myIPaddress = null;
InetAddress myServer = null;
String hostAddress = null;
String hostName = null;
String Address = null;
String Name = null;
Public static void main (String args []) {
Test mytool;
Mytool = new Test ();
Mytool. getAllServerIP ();
// System. out. println ("Your host IP is:" + mytool. getMyIP ());
// System. out. println ("The Server IP is:" + mytool. getServerIP ());
}
// Obtain the IP address of LOCALHOST
Public String getMyIP (){
Try {
MyIPaddress = InetAddress. getLocalHost ();
HostAddress = myIPaddress. getHostAddress (); // only obtain the IP address
HostName = myIPaddress. getHostName (); // obtain the local name
} Catch (UnknownHostException e ){
}
Return (hostName );
}
// Obtain the IP address of www.abc.com
Public String getServerIP (){
Try {
MyServer = InetAddress. getByName ("www.abc.com ");
Address = myServer. getHostAddress (); // obtain the IP Address of www.abc.com
Name = myServer. getHostName (); // obtain the Domain Name
} Catch (UnknownHostException e ){
}
Return (Name );
}
// Obtain all IP addresses under this domain name
Public void getAllServerIP (){
String name = "www.microsoft.com ";
Try {
InetAddress [] addresses = InetAddress. getAllByName (name );
For (int I = 0; I <addresses. length; I ++ ){
System. out. println (addresses [I]. getHostAddress ());
}
} Catch (Exception e ){
System. err. println ("Unable to find:" + name );
}
}
}