Home » Android » android – Why does my service always bind to ipv6 localhost instead of ipv4?

android – Why does my service always bind to ipv6 localhost instead of ipv4?

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.