InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1
You'll need to use NetworkInterface
to enumerate network interfaces; InetAddress.getLocalHost()
always returns loopback. This doesn't explain why you get 127.0.1.1 instead of 127.0.0.1, but since that method doesn't do what you're trying to do, it doesn't seem especially pertinent. See: http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()
why does InetAddress.getLocalHost().getHostAddress() returns 127.0.0.1 in android .But works fine in JAVA program
This is expected behavior on Android. Please refer to the javadoc for getLocalHost():
Note that if the host doesn't have a hostname set – as Android devices
typically don't – this method will effectively return the loopback
address, albeit by getting the name localhost and then doing a lookup
to translate that to 127.0.0.1.
InetAddress: getHostAddress() returns 127.x.x.x instead of external IP address
The result of that function depends on your system's configuration (which host is treated as "canonical" by your OS), which can be system and configuration dependent.
To find out internet addresses for your system, use NetworkInterface::getNetworkInterfaces()
, as described eg. here: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
In your case, what you want to do is probably something like this:
InetAddress theOneAddress = null;
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
if (!netint.isLoopback()) {
theOneAddress = Collections.list(netint.getInetAddresses()).stream().findFirst().orElse(null);
if (theOneAddress != null) {
break;
}
}
}
Java. InetAddress.getLocalHost returns strange IP
The problem is that my hostname will consists only of numbers and could not be resolved.
I change my /etc/hostname with characters at first position and problem has solved.
Java InetAddress.getLocalHost().getHostAddress() returns invalid IP
Finally managed to fix it.
The issue, as @guest pointed out, is for some reason connecting to VPN messes up name resolution, so that my localhost is resolved to some remote IP.
The solution was to simply add an /etc/hosts entry, manually mapping my hostname to 127.0.0.1. That fixed it.
InetAddress.getLocalHost().getHostAddress() returns 127.0.0.1 in Android
Modified few bits and this one is working as desired for getting IPv4 addresses. !inetAddress.isLoopbackAddress() removes all the loopback address. !inetAddress.isLinkLocalAddress() and inetAddress.isSiteLocalAddress()) removes all IPv6 addresses. I hope this will help someone in here.
StringBuilder IFCONFIG=new StringBuilder();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
IFCONFIG.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException ex) {
Log.e("LOG_TAG", ex.toString());
}
servers.add(IFCONFIG.toString());
Related Topics
Issue Using Imageio.Write Jpg File: Pink Background
Changing the Shapes of Points in Scatter Plot
Is a Java Array of Primitives Stored in Stack or Heap
How Is Hashcode() Calculated in Java
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory
What Is the Significance of Load Factor in Hashmap
Leiningen - How to Add Dependencies for Local Jars
Streaming Large Files in a Java Servlet
Using == Operator in Java to Compare Wrapper Objects
Sorting a List of Points with Java
What Is a Covariant Return Type
How to Redirect to Login Page When Session Is Expired in Java Web Application
Why Do Many Collection Classes in Java Extend the Abstract Class and Implement the Interface as Well
Printing Runtime Exec() Outputstream to Console