为什么我的服务总是绑定到 ipv6 localhost 而不是 ipv4?

发布于 2024-12-18 19:40:28 字数 1178 浏览 2 评论 0原文

我有一个创建 ServerSocket 并绑定到 localhost:7060 的服务。当我在 Android 设备上执行“netstat -an”时,我看到它使用 ipV6 localhost 而不是 ipv4 localhost 接口。

输出是这样的:
tcp6 0 0 ::ffff:127.0.0.1:7060 :::* LISTEN

使用 ipV4 的端口列出如下:
tcp 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN

强制它始终使用 IPv4 的技巧是什么? 我正在使用 iptables 设置端口转发规则。我的版本支持 ipv4 目标地址。

这就是我创建用于侦听端口的 Java 代码的方式。

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

我遵循了其他建议,例如在服务启动时将系统属性设置为首选 ipV4。那没有什么区别。

<代码> System.setProperty("java.net.preferIPv4Stack", "true");

我正在为嵌入式设备构建的 Android 2.3 上运行它。

更新: 我检查了 android 树中的 InetAddress.java 源。它正在用如下所示的行读取上面的标志。

static boolean preferIPv6Addresses() {
        String propertyName = "java.net.preferIPv6Addresses";
        String propertyValue = AccessController.doPrivileged(new PriviAction<String>(propertyName));
        return Boolean.parseBoolean(propertyValue);
    }

现在我不确定 System.setProperty() 调用是否真的改变了上面代码读取的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-12-25 19:40:28

理论上,IPv6 服务器也侦听 IPv4,因为 IPv4 地址空间是 IPv6 的子集,这会给您带来真正的问题吗?

一个可能有效的技巧是使用“127.0.0.1”而不是“localhost”,它具有关联的 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?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文