Get IP Address

Source: Internet
Author: User
Tags get ip

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 );

}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.