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
- 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.
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
- 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 address = inetaddress. getbyname ("1.2.3.4"); // the IP address does not exist.
- 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
- Public static void gethostaddresstest () throws unknownhostexception {
- // Output the IPv4 address
- Inetaddress ipv4address1 = inetaddress. getbyname ("1.2.3.4 ");
- System. Out. println ("ipv4address1:" + ipv4address1. gethostaddress ());
- // Ipv4address1: 1.2.3.4
- Inetaddress ipv4address2 = inetaddress. getbyname ("www.ibm.com ");
- System. Out. println ("ipv4address2:" + ipv4address2. gethostaddress ());
- // Ipv4address2: 129.42.60.216
- Inetaddress ipv4address3 = inetaddress. getbyname ("zzq ");
- System. Out. println ("ipv4address3:" + ipv4address3. gethostaddress ());
- // Ipv4address3: 192.168.1.105
- // Output the IPv6 address
- Inetaddress ipv6address1 = inetaddress. getbyname ("ABCD: 123: 22ff ");
- System. Out. println ("ipv6address1:" + ipv6address1. gethostaddress ());
- // Ipv6address1: ABCD: 123: 0: 0: 0: 0: 0: 22ff
- Inetaddress ipv6address2 = inetaddress. getbyname ("www.neu6.edu.cn ");
- System. Out. println ("ipv6address2:" + ipv6address2. gethostaddress ());
- // Ipv6address2: 2001: da8: 9000: b255: 210: 5cff: fef5: ac49
- // Output all IP addresses of the Local Machine
- Inetaddress addresses [] = inetaddress. getallbyname ("zzq ");
- For (inetaddress address: addresses)
- System. Out. println ("Local Address:" + address. gethostaddress ());
- // Local address: 192.168.1.105
- // Local address: 0.1.0.4
- // Local address: 0: 0: 0: 0: 0: 0: 0: 0: 1
- }
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
- 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 arrays are 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
- }
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}