11

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.

videoguy
  • 1,523
  • 2
  • 21
  • 44
  • I think those system properties are in the OS itself, and changing them programatically will not stick (because all apps depend on the same system properties). – Brill Pappin Nov 15 '12 at 16:09
  • 2
    (Since this older thread was referenced elsewhere, I think some clarification is needed.) `::1` is the IPv6 localhost address. `::ffff:127.0.0.1` is not, it is the IPv4 localhost address in IPv6 notation (a so-called "IPv4-mapped" IPv6 address). It is used for dual-stacked sockets that can accept both IPv4 and IPv6 packets. (Although a socket should be labeled `tcp46`, then.) – Dubu Jul 25 '13 at 08:59

1 Answers1

2

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.

epx
  • 1,025
  • 9
  • 16