【问题标题】:Get IPv4 and IPv6 address using NsdServiceInfo?使用 NsdServiceInfo 获取 IPv4 和 IPv6 地址?
【发布时间】:2016-10-05 10:01:05
【问题描述】:

我使用了 NSDServiceInfo.getHost()。 getHostAddress() 输出的是 IPv4 到 IPv6 的设备和路由器。对于设备,我已经可以使用 IPv4,所以我需要 IPv6。和路由器一样,我已经可以了,所以我需要 IPV4 IPV6。

我尝试过的解决方案。但是当他的hostaddress发出IPV4时,则退出错误“Not an IPv6 address: [xx, xx, xx, xx]

同时,当它发出主机地址 IPv6 时,错误消息“java.net.Inet6Address can't be cast to java.net.Inet4Address”

我喜欢这种编码

List <NSDServiceInfo> Data = new ArrayList<>();
InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress());
byte [] addressBytes = hostInet.getAddress();

Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet));
Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes);
Log.d ( "IP", "IPv4" + dest4.getHostAddress ());
Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());

【问题讨论】:

    标签: java android nsd


    【解决方案1】:

    这是我的错,因为我没有完整地询问。所以我使用了 NSDServiceInfo.getHost()。 getHostAddress() 输出的是 IPv4 到 IPv6 的设备和路由器。对于设备,我已经可以使用 IPv4,所以我需要 IPv6。和路由器一样,我已经可以了,所以我需要 IPV4 IPV6。

    我尝试过的解决方案。但是当他的hostaddress发出IPV4时,则退出错误“Not an IPv6 address: [xx, xx, xx, xx]

    同时,当它发出主机地址 IPv6 时,错误消息“java.net.Inet6Address can't be cast to java.net.Inet4Address”

    我喜欢这种编码

    List <NSDServiceInfo> Data = new ArrayList<>();
    InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress());
    byte [] addressBytes = hostInet.getAddress();
    
    Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet));
    Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes);
    Log.d ( "IP", "IPv4" + dest4.getHostAddress ());
    Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());
    

    对不起,如果我的英语不好 谢谢

    【讨论】:

      【解决方案2】:

      首先通过给出主机名使用静态函数获取InetAddress对象

      InetAddress hostInet= InetAddress.getByName(hostNameString);
      

      现在我们需要使用hostInet 的字节数组表示

      byte [] addressBytes = hostInet.getAddress();
      

      现在您可以使用 addressBytes 将其格式化为 IPv4 或 IPv6,使用 Inet6AddressInet4AddressgetByAddress 加上您需要使用 getByInetAddress 函数传递主机名、字节地址和网络接口

      Inet6Address dest = Inet6Address.getByAddress(hostNameString, addressBytes, NetworkInterface.getByInetAddress(hostInet));
      

      Inet4Address dest = Inet4Address.getByAddress(hostNameString, addressBytes, NetworkInterface.getByInetAddress(hostInet));
      

      现在您可以使用getAddressgetHostAddress 函数检索dest 对象的文本或字节表示形式

      【讨论】:

      • 感谢您的解决方案。我努力了。对不起,我已经发布了我的答案。我再次请求他的帮助。谢谢
      【解决方案3】:

      NsdServiceInfo 有一个方法getHostAddress(),它是java.net.InetAddress 的一种类型。 IP 地址的文本表示是特定于地址族的:

      这两个类都扩展了InetAddress。您需要一个方法 public String getHostAddress() 以文本形式返回 IP 地址字符串。

      【讨论】:

      • 感谢您的解决方案。不好意思能不能具体点他的新方法?谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2023-04-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多