gpt4 book ai didi

java - 在纯 IPv6 网络上绑定(bind)到 Java 中的 AnyLocalAddress (0.0.0.0)

转载 作者:行者123 更新时间:2023-12-02 09:38:20 28 4
gpt4 key购买 nike

在Java中,如果我写

DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress("0.0.0.0",162));

通配符也适用于纯 IPv6 网络吗?

P.S:我没有纯 IPv6 网络来测试此功能。

最佳答案

"0.0.0.0" 基本上表示所有 IP 地址。为了确保您绑定(bind)到所有已知地址,为什么不这样做呢。

DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress(162));

如果不指定 IP 地址,则会导致其绑定(bind)到该主机上可用的所有 IP 地址。

参见 - https://docs.oracle.com/javase/8/docs/api/java/net/InetSocketAddress.html

InetSocketAddress(int port)

Creates a socket address where the IP address is the wildcard address and the port number a specified value.

The wildcard is a special local IP address. It usually means "any" and can only be used for bind operations.

关于java - 在纯 IPv6 网络上绑定(bind)到 Java 中的 AnyLocalAddress (0.0.0.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324667/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com