I have a service that creates a ServerSocket and binds to localhost:7060
. When I did "netstat -an" on my android device, I see it is using ipV6 localhost instead of ipv4 localhost interface.
The output is like this:
tcp6 0 0 ::ffff:127.0.0.1:7060 :::* LISTEN
The ports that use ipV4 are listed like this:
tcp 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN
What is the trick to force it to use IPv4 always? I am setting up a port forward rule using iptables. The version I have supports ipv4 destination addresses.
This is how I am creating my Java code for listening on the port.
InetAddress localAddress = Inet4Address.getByName("localhost");
//InetAddress localAddress = Inet4Address.getLocalHost();
sockServer = new ServerSocket(nPort, 20, localAddress);
I followed other advice like setting system property to prefer ipV4 in the startup of my service. That didn't make any difference.
System.setProperty("java.net.preferIPv4Stack", "true");
I am running this on Android 2.3 built for an embedded device.
Update:
I checked InetAddress.java sources in android tree. It is reading the above flag with a line like below.
static boolean preferIPv6Addresses() {
String propertyName = "java.net.preferIPv6Addresses";
String propertyValue = AccessController.doPrivileged(new PriviAction<String>(propertyName));
return Boolean.parseBoolean(propertyValue);
}
Now I am not sure System.setProperty() call is really changing the value read by above code.
The loopback address, also called localhost, is probably familiar to you. It is an internal address that routes back to the local system. The loopback address in IPv4 is 127.0. 01.
The name localhost normally resolves to the IPv4 loopback address 127.0. 0.1, and to the IPv6 loopback address ::1.
The IPv6 equivalent of IPv4's 0.0. 0.0 is ::/0 . Save this answer.
For IPv4, this class A network address is 127.0. 0.1 . For IPv6, the reserved loopback address is ::1 . You can also specify loopback as the host name.
In theory a IPv6 server listens to IPv4 as well, since IPv4 address space is a subset of IPv6, is this causing real problems to you?
A trick that might work is to use "127.0.0.1" instead of "localhost", which has IPv4 and IPv6 addresses associated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With