//return, the, raw, IP, address, in, a, string, format
InetAddress.getLocalHost ().GetHostAddress ();
//return, the, raw, IP, address, of, this, object
InetAddress.getLocalHost ().GetAddress ();
a running result of this machine:
InetAddress.getLocalHost () exception handling:
uses: InetAddress.getLocalHost () an exception:
"Reason:, java.net.UnknownHostException, zw_65_43.":... ..
problem is due to configuring zw_65_43 in the system's /etc/Hostname as host name,
There is no corresponding "zw_65_43" in the /etc/hosts file.
Simple solution is to match the relationship can be good, or even delete /etc/Hostname, this document can also.
deep reason: in most Linux operating systems, the host name is found in the configuration in /etc/hosts,
But the Detian based system uses the configuration in the /etc/Hostname file as the host name.
And Java's InetAddress.java calls InetAddressImpl.java's
Public native, String, getLocalHostName (), throws, UnknownHostException; to get the local hostname,
This method of Java is native, and is an implementation of the local system, so the Java code has a problem when the local configuration problem occurs,.
relies on the machine name of the machine to query the IP address, which is the same as the IP address returned by Ping
hostname, not the eth0 IP address.
hostname to see the native name
a. if the native name is not an IP address, such as "Sohu", you must configure the Sohu in the /etc/hosts to correspond to the native IP,
Otherwise, java.net.InetAddress.getLocalHost throws the java.net.UnknownHostException exception.
b. if the machine name is a IP address, and must be a local network card IP,
c. third cases are more troublesome. If the name of this machine is a IP address, but not a local card IP, or just a part of the IP address,
For example, 192.168 (which I have encountered), then java.net.InetAddress.getLocalHost ().GetHostAddress () will get 188.8.131.52,
Java.net.InetAddress.getLocalHost ().GetHostAddress () does not throw an exception, but the associated access stagnates for a long time,
The reason is that he is looking for the address of 184.108.40.206. If the address does not exist in the network, the Connection timed out exception will eventually be thrown.
If this address exists in the network, the exception Connection refused to host will be thrown, which is obviously not the desired
- sets the local name: hostname mName XXX, preferably not in the form of an IP address, but if written, it must be in the full IP form of the machine (do not only write half)
- adds a local IP mName in /etc/hosts
- tests with InetAddress.getLocalHost ().GetHostAddress () whether the result is consistent with the native IP