本文介绍了Android的调试InetAddress.isReachable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何判断某个ip地址是在我的Andr​​oid应用程序可以在调试过程中(我没有试过在实际设备上)。

I am trying to figure out how to tell if a particular ipaddress is available in my android app during debugging ( I haven't tried this on an actual device ).

从阅读似乎InetAddress.isReachable应该为我做到这一点。

From reading it appears that InetAddress.isReachable should do this for me.

起初,我以为我可以code是这样的:

Initially I thought that I could code something like:

InetAddress类地址= InetAddress.getByAddress(新的byte [] {(字节)192,(字节)168,(字节)254,(字节)10);成功= address.isReachable(3000);

InetAddress address = InetAddress.getByAddress( new byte[] { (byte) 192, (byte) 168, (byte) 254, (byte) 10 );success = address.isReachable( 3000 );

尽管我有理由相信这是一个可到达的地址。这将返回false。

This returns false even though I am reasonably sure it is a reachable address.

我发现,如果我改变了这个127,0,0,1,它返回成功。

I found that if I changed this to 127, 0, 0, 1 it returned success.

我的下一个尝试是相同的code,但我用我在www.google.com上的ping得到(72.167.164.64截至记者发稿)的地址。没有成功。

My next attempt was same code, but I used the address I got from a ping of www.google.com ( 72.167.164.64 as of this writing ). No success.

于是我尝试了另一个例子:

So then I tried a further example:

int timeout = 2000;
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress address : addresses)
{
    if ( address.isReachable(timeout))
    {
       success = true; // just set a break point here
    }
}

我是比较新的Java和Android的,所以我怀疑我失去了一些东西,但我找不到任何可以表明是什么。

I am relatively new to Java and Android so I suspect I am missing something, but I can't find anything that would indicate what that is.

推荐答案

似乎isReachable()从未工作以及在Android B / C它尝试使用ICMP,这通常需要root权限,然后尝试建立连接7号端口,这通常对现代系统没有运行的服务。

seems that isReachable() never worked well on Android b/c it tries to use ICMP, that usually needs a root privileges, and then it tries to establish connection to port 7, that's usually have no running services on modern systems.

您最好检查连通性建立TCP连接端口,您知道应该是开放的。

you'd better check connectivity with establishing TCP connection to ports you know should be open.

这篇关于Android的调试InetAddress.isReachable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:51