本文介绍了为什么我的服务始终绑定到本地主机的IPv6代替IPv4的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个ServerSocket,并结合服务本地主机:7060 。当我做的netstat -an我的Andr​​oid设备上,我看到它是使用IPv6本地主机,而不是IPv4的本地主机接口。

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.

输出结果是这样的:
TCP6 0 0 :: FFFF:127.0.0.1:7060 ::: * LISTEN

The output is like this:
tcp6 0 0 ::ffff:127.0.0.1:7060 :::* LISTEN

这是使用IPv4的端口列如下:
TCP 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN

The ports that use ipV4 are listed like this:
tcp 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN

什么把戏,迫使它使用IPv4永远?我设置了使用iptables端口转发规则。该版本我都支持IPv4目的地址。

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.

这是我如何创建我的Java $ C $下侦听端口。

This is how I am creating my Java code for listening on the port.

    InetAddress类将localAddress = Inet4Address.getByName(localhost的);    // InetAddress类将localAddress = Inet4Address.getLocalHost();    sockServer =新的ServerSocket(NPORT,20,将localAddress);

InetAddress localAddress = Inet4Address.getByName("localhost"); //InetAddress localAddress = Inet4Address.getLocalHost(); sockServer = new ServerSocket(nPort, 20, localAddress);

我跟其他咨询类的系统属性设置为preFER IPV4在我服务的启动。这并没有什么差别。

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。,真);

System.setProperty("java.net.preferIPv4Stack", "true");

我是专为嵌入式设备的Andr​​oid 2.3运行此。

I am running this on Android 2.3 built for an embedded device.

更新:我的android树检查InetAddress.java来源。这是看完上面的标记中包含的一条线。

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);
    }

现在我不知道System.setProperty()调用真正的改变由上述code读出的值。

Now I am not sure System.setProperty() call is really changing the value read by above code.

推荐答案

在理论上一个IPv6的服务器侦听的IPv4为好,IPv4地址空间,因为是IPv6的一个子集,这是导致实际问题吗?

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?

这可能工作的技巧是使用127.0.0.1,而不是本地主机,它有关联的IPv4和IPv6地址。

A trick that might work is to use "127.0.0.1" instead of "localhost", which has IPv4 and IPv6 addresses associated.

这篇关于为什么我的服务始终绑定到本地主机的IPv6代替IPv4的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:03