从Android查找IP地址

时间:2015-04-22 23:04:20

标签: android networking wifi ip-address hostname

我正在尝试显示连接到当前wifi网络的所有设备的列表。

我面临的问题是,当我在Java上尝试以下代码(使用DNS查找)时,我看到了几个设备。 (哪个好);但它在我的Android设备或模拟器上不起作用。

有谁知道我做错了什么?我正在开发一款连接到WiFi的Lolipop手机。找到的唯一IP地址是本地主机。

byte[] ip = localhost.getAddress();
                for (int i = 0; i <= 254; i++)
                {
                    ip[3] = (byte)i;
                    InetAddress address = InetAddress.getByAddress(ip);
                    if (!address.getHostAddress().equals(address.getHostName() ) )
                    {
                        // machine is known in a DNS lookup
                           AvailableHostList.add(address);
                    }
                    else
                    {

                    }

1 个答案:

答案 0 :(得分:0)

所有,我终于得到了这个工作。 问题是我无法正确获取IP地址。所以我首先找到了我的本地IP ......下面......

                 for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces()))
                 {
                     for (InetAddress addr : Collections.list(intf.getInetAddresses()))
                     {

                         if (!addr.isLoopbackAddress())
                         {
                             int ipAddress = 0;
                             ipAddress = wifiInfo.getIpAddress();
                             ip = "IP:   " + String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
                             localIP =  String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
                         }
                     }
                 }

localIP是String格式的IP地址。 (1.2.3.4)..然后我只用IP地址创建了一个字节,并扫描了每一个..我将每个在DNS服务器中的IP保存或者有一个响应,保存到ArrayList。

byte[] ip = new byte[4];
    int ipInt = parseNumericAddress(localIP);
    ip[3] = (byte) (ipInt & 0xFF);
    ip[2] = (byte) ((ipInt >> 8) & 0xFF);
    ip[1] = (byte) ((ipInt >> 16) & 0xFF);
    ip[0] = (byte) ((ipInt >> 24) & 0xFF);


              progressDialog.incrementProgressBy(1);
              for (int i = 1; i <= 255; i++)
                {
                    progressDialog.incrementProgressBy(1);
                    ip[3] = (byte)i;

                    try {
                         InetAddress address = InetAddress.getByAddress(ip);
                        if (address.isReachable(100))
                        {
                            // machine is turned on and can be pinged
                            System.out.println("***Can Be Pinged*** Host Name " + address.getHostName() + address.getAddress().toString());
                            AvailableHostList.add(address.getHostName().toString());
                        }
                        else 
                        if (!address.getHostAddress().equals(address.getHostName() ) )
                        {
                            // machine is known in a DNS lookup
                            System.out.println("***Can Be Found in DNS Look Up*** HostName:" + address.getHostName() + "Address: "+address.getAddress().toString());
                            AvailableHostList.add(address.getHostName().toString());
                        }
                        else
                        {
                            System.out.println("Can Not Be Found" + address.toString());

                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }