找到的Galaxy Tab 10.1的应用程序的IP地址应用程序、地址、Tab、Galaxy

2023-09-09 21:02:37 作者:举杯敬风尘

我已经写了HTC渴望一个应用程序,它获得设备的IP地址,并将其打印到屏幕上。当我安装这个程序,我的卡10.1 IP地址的信件和电话号码在一个陌生的格式出来?

I've written an app for HTC desire and it gets the devices IP address and prints it to the screen. When I install this app to my tab 10.1 the IP address come out in letter and numbers in a strange format?

private String getIpAddress()
    {
        try
        {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
            {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
                {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                }
            }
        }
        catch(SocketException ex)
        {
            Log.e(TAG , ex.toString());
        }
        return null;
    }

什么是标签10.1不同?

What is different on the tab 10.1?

(IP返回 - FE80 :: be47:60ff:FEFF:21e2)

( IP returned - fe80::be47:60ff:feff:21e2)

需要有一个IP地址,因为这是我通过在PC上的终端连接到我的应用程序。

Needs to be an IP address as this is what I pass to the terminal on PC to connect to my app.

推荐答案

您的Galaxy Tab 10.1接收到的IPv6地址,在这种情况下, FE80 :: be47:60ff:FEFF:21e2 。这可能是关于IPv6的不同preferences或您的HTC Desire的能力和Galaxy Tab的造成的。

Your Galaxy Tab 10.1 receives an IPv6 address, in this case fe80::be47:60ff:feff:21e2. This might be caused by different preferences or capabilities of your HTC Desire and Galaxy Tab regarding IPv6.

有什么不妥之处,越来越多的设备会(有)使用IPv6地址,当IPv4地址空间耗尽。

There's nothing wrong with that, more and more devices will (have to) use IPv6 addresses when the IPv4 address space runs out.

由于在尼西姆他对这个问题的评论指出,IPv6地址开头 FE80: 链路本地地址的,即范围解决了设备自我分配,如果它没有连接到转手出去的地址的任何网络,例如:通过DHCP。

As Nesim points out in his comment on the question, IPv6 addresses starting with fe80: are link-local addresses, i.e. a range of addresses a device self-assigns if it has no connectivity to any network that hands out addresses, e.g. via DHCP.

所以看起来像你的Galaxy Tab未连接到任何WiFi网络或通过DHCP接收不到任何地址。

So it seems like your Galaxy Tab isn't connected to any wifi network or isn't receiving any address via DHCP.

在问题上的code段返回它找到并不会过滤掉链路本地地址(而不是从外部连接有用)的首地址。下面code给你既不是环回也不链路本地所有地址。你怎么许多人之间进行选择是由你 - 我真的不知道:

The code snippet in the question returns the first address it finds and doesn't filter out link-local addresses (which aren't useful to connect from the outside). The following code gives you all addresses that are neither loopback nor link-local. How you select between many of them is up to you -- I honestly don't know:

private static List<InetAddress> getIpAddress() {
  try {
    List<InetAddress> result = new ArrayList<InetAddress>();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
      NetworkInterface intf = interfaces.nextElement();
      Enumeration<InetAddress> addresses = intf.getInetAddresses();
      while (addresses.hasMoreElements()) {
        InetAddress address = addresses.nextElement();
        if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) {
          result.add(address);
        }
      }
    }
    return result;
  } catch (SocketException ex) {
    Log.e(TAG, "Failed to list network interfaces.", ex);
    return null;
  }
}

为了便于比较,我的Windows系统列出23网络接口(其中大多数虚拟)共10个地址,其中2个是回送地址(本地主机),4链路本地地址的 - 留下4不会忽略的上述code将返回 - 采摘第一个似乎过于乐观

For comparison, my Windows system lists 23 network interfaces (most of them virtual) with a total of 10 addresses, 2 of which are loop back addresses (localhost), 4 link-local addresses -- that leaves 4 adresses that the above code would return -- picking the first one seems overly optimistic.