Methods of the inetaddress class

Source: Internet
Author: User

1. gethostname () method

Definition: Public String gethostname ()

Gethostname returns different values based on the method used to create an inetaddress object.

 

1. UseGetlocalhost ()Inetaddress object created by the method

 

GethostnameThe local name is returned.

Inetaddress address = inetaddress. getlocalhost ();

System. Out. println (address. gethostname (); // return the local name.

 

2.Create with Domain NameInetaddress object

 

The inetaddress object obtained by using the domain name as the parameters of the getbyname and getallbyname methods. This object will get this domain name. When you call

When gethostname is used, you do not need to access the DNS server. InsteadDirectly return this domain name.

Java code
 
  1. Inetaddress address = inetaddress. getbyname ("zzq ");
  2. System. Out. println (address. gethostname (); // you do not have to access the DNS server and directly return the domain name.
Inetaddress address = inetaddress. getbyname ("zzq"); system. Out. println (address. gethostname (); // you do not have to access the DNS server and directly return the domain name.

3.IP address usedCreate an inetaddress object

 

When you use an IP address to create an inetaddress object (the getbyname, getallbyname, and getbyaddress methods can all create an inetaddress object through an IP address), you do not need to access the DNS server. Therefore, the gethostname method is used to search for a domain name on the DNS server.

If the IP address does not exist or the DNS server does not allow IP address and domain name ing, the IP address is returned.

Java code
 
  1. Inetaddress address = inetaddress. getbyname ("141.146.8.66 ");
  2. System. Out. println (address. gethostname (); // You must access the DNS server to obtain the domain name.
  3. Inetaddress address = inetaddress. getbyname ("1.2.3.4"); // the IP address does not exist.
  4. System. Out. println (address. gethostname (); // the IP address is returned directly.
Inetaddress address = inetaddress. getbyname ("141.146.8.66"); system. out. println (address. gethostname (); // You must access the DNS server to obtain the domain name inetaddress = inetaddress. getbyname ("1.2.3.4"); // the IP address does not exist in the system. out. println (address. gethostname (); // returns the IP address directly.

2. getcanonicalhostname () method

Definition: Public String getcanonicalhostname ()

This method is the same as the gethostname method to obtain the Domain Name of the remote host. The difference is that this method gets the Host Name and gethostname gets the host alias.

1. UseGetlocalhost ()Create an inetaddress object

 

In this case, the getcanonicalhostname and gethostname MethodsThe local name is obtained.

2.Domain name usedCreate an inetaddress object

 

After the inetaddress object is created using the domain name, The gethostname method does not access the DNS server.

However, the getcanonicalhostname method does not necessarily depend on how the DNS server interprets the Host Name and host alias.

 

3.Use IP AddressCreate an inetaddress object

 

In this case, the getcanonicalhostname method and the gethostname method are identical,All returned host namesInstead of the host alias.

 

The host alias is used because sometimes the host name may be complex, such as the host name bigip-OTN-

Portal.oracle.com. Therefore, to make it easier for users to access the website, a simpler host alias is added, such
Www.oracle.com

3. gethostaddress () method

Definition: Public String gethostaddress ()

This method is used to obtain the Host IP address, which can be IPv4 or IPv6

No matter which method the inetaddress object is created, the gethostaddress method does not access the DNS server.

Java code
 
  1. Public static void gethostaddresstest () throws unknownhostexception {
  2. // Output the IPv4 address
  3. Inetaddress ipv4address1 = inetaddress. getbyname ("1.2.3.4 ");
  4. System. Out. println ("ipv4address1:" + ipv4address1. gethostaddress ());
  5. // Ipv4address1: 1.2.3.4
  6. Inetaddress ipv4address2 = inetaddress. getbyname ("www.ibm.com ");
  7. System. Out. println ("ipv4address2:" + ipv4address2. gethostaddress ());
  8. // Ipv4address2: 129.42.60.216
  9. Inetaddress ipv4address3 = inetaddress. getbyname ("zzq ");
  10. System. Out. println ("ipv4address3:" + ipv4address3. gethostaddress ());
  11. // Ipv4address3: 192.168.1.105
  12. // Output the IPv6 address
  13. Inetaddress ipv6address1 = inetaddress. getbyname ("ABCD: 123: 22ff ");
  14. System. Out. println ("ipv6address1:" + ipv6address1. gethostaddress ());
  15. // Ipv6address1: ABCD: 123: 0: 0: 0: 0: 0: 22ff
  16. Inetaddress ipv6address2 = inetaddress. getbyname ("www.neu6.edu.cn ");
  17. System. Out. println ("ipv6address2:" + ipv6address2. gethostaddress ());
  18. // Ipv6address2: 2001: da8: 9000: b255: 210: 5cff: fef5: ac49
  19. // Output all IP addresses of the Local Machine
  20. Inetaddress addresses [] = inetaddress. getallbyname ("zzq ");
  21. For (inetaddress address: addresses)
  22. System. Out. println ("Local Address:" + address. gethostaddress ());
  23. // Local address: 192.168.1.105
  24. // Local address: 0.1.0.4
  25. // Local address: 0: 0: 0: 0: 0: 0: 0: 0: 1
  26. }
Public static void gethostaddresstest () throws unknownhostexception {// output IPv4 address inetaddress ipv4address1 = inetaddress. getbyname ("1.2.3.4"); system. out. println ("ipv4address1:" + ipv4address1. gethostaddress (); // listen 4address1: 1.2.3.4inetaddress limit 4address2 = inetaddress. getbyname ("www.ibm.com"); system. out. println ("ipv4address2:" + ipv4address2. gethostaddress (); // listen 4address2: 129.42.60.216inetaddress limit 4address3 = inetaddress. getbyname ("zzq"); system. out. println ("ipv4address3:" + ipv4address3. gethostaddress (); // ipv4address3: 192.168.1.105 // output IPv6 address inetaddress ipv6address1 = inetaddress. getbyname ("ABCD: 123: 22ff"); system. out. println ("ipv6address1:" + ipv6address1. gethostaddress (); // route 6address1: ABCD: 123: 0: 0: 0: 0: 0: 22 ffinetaddress limit 6address2 = inetaddress. getbyname ("www.neu6.edu.cn"); system. out. println ("ipv6address2:" + ipv6address2. gethostaddress (); // ipv6address2: 2001: da8: 9000: b255: 210: 5cff: fef5: ac49 // output all the IP addresses of the Local Machine. inetaddress addresses [] = inetaddress. getallbyname ("zzq"); For (inetaddress address: addresses) system. out. println ("Local Address:" + address. gethostaddress (); // address of the Local Machine: 192.168.1.105 // address of the Local Machine: 0.1.0.4 // address of the Local Machine: 0: 0: 0: 0: 0: 0: 0: 1}

Iv. getaddress () method

Definition: Public byte [] getaddress ()

The only difference between this method and the gethostaddress method is that,Gethostaddress returns the character-type IP address, and getaddress returns the byte array-type IP address.

Java code
 
  1. Public static void getaddresstest () throws unknownhostexception {
  2. Inetaddress address = inetaddress. getbyname ("www.csdn.net ");
  3. Byte IP [] = address. getaddress ();
  4. For (byte ipsegment: IP)
  5. System. Out. Print (ipsegment + ".");
  6. System. Out. println ("");
  7. //-53.81.21.61
  8. /*
  9. * Byte arrays are signed. In Java, the value range of byte type is-128 ~ 127.
     
  10. * If a byte of the returned IP address is an integer greater than 127, It is a negative number in the byte array.
     
  11. * Because Java does not have an unsigned byte type, Int or long type must be used to display normal IP addresses.
     
  12. */
  13. For (byte ipsegment: IP ){
  14. Int newipsegment = (ipsegment <0 )? 256 + ipsegment: ipsegment;
  15. System. Out. Print (newipsegment + ".");
  16. }
  17. // 203.81.21.61
  18. }
Public static void getaddresstest () throws unknownhostexception {inetaddress address = inetaddress. getbyname ("www.csdn.net"); byte IP [] = address. getaddress (); For (byte ipsegment: IP) system. out. print (ipsegment + ". "); system. out. println (""); //-53.81.21.61/** byte array is signed. In Java, the value range of byte type is-128 ~ 127. * If a byte of the returned IP address is an integer greater than 127, It is a negative number in the byte array. * Because Java does not have an unsigned byte type, Int or long type must be used to display normal IP addresses. */For (byte ipsegment: IP) {int newipsegment = (ipsegment <0 )? 256 + ipsegment: ipsegment; system. Out. Print (newipsegment + ".");} // 203.81.21.61}

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.