InetAddress.getLocalHost (). GetHostAddress () acquired IP incorrect

Today the normal running under Windows programs under Linux to get tested, but there is a problem, after examination found InetAddress.getLocalHost (). GetHostAddress () there is a problem when taking local IP address. Because we expect the IP is 222.xxx.xxx.xx, but the actual print out the IP address is 127.0.0.1.

Check / etc / hosts file, which has a native name and corresponding IP 222.xxx.xxx.xx this record. Comment it out, and then modify the host name of the machine, and adding the hostname and 222. xx.xx.xx correspondence, problem solved in the hosts file.

By solution to the problem, we can find InetAddress.getLocalHost () getHostAddress () method to find the native IP implementation method - rely on machine name of the machine to query IP address, and ping `IP address returned is the same hostname` a not ipconfig eth0 IP address obtained by the method.

getHostAddress (): return type is String, return the IP address string (textual presentation).

getLocalHost (): return type is static InetAddress, returns the local host
Published 56 original articles · won praise 0 · Views 7782

Guess you like

Origin blog.csdn.net/chainhou/article/details/84441335