java Android 调试 InetAddress.isReachable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2935325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Debugging InetAddress.isReachable
提问by badMonkey
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 ).
我试图弄清楚如何在调试期间判断特定 ipaddress 在我的 android 应用程序中是否可用(我还没有在实际设备上尝试过)。
From reading it appears that InetAddress.isReachable should do this for me.
从阅读看来, InetAddress.isReachable 应该为我做这件事。
Initially I thought that I could code something like:
最初我认为我可以编写如下代码:
InetAddress address = InetAddress.getByAddress( new byte[] { (byte) 192, (byte) 168, (byte) 254, (byte) 10 ); success = address.isReachable( 3000 );
InetAddress 地址 = InetAddress.getByAddress( new byte[] { (byte) 192, (byte) 168, (byte) 254, (byte) 10 ); 成功 = address.isReachable(3000);
This returns false even though I am reasonably sure it is a reachable address.
即使我有理由确定它是一个可访问的地址,这也会返回 false。
I found that if I changed this to 127, 0, 0, 1 it returned success.
我发现如果我把它改成 127, 0, 0, 1 它返回成功。
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.
我的下一次尝试是相同的代码,但我使用了从 www.google.com ping 获得的地址(撰写本文时为 72.167.164.64)。没有成功。
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
}
}
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.
我对 Java 和 Android 比较陌生,所以我怀疑我遗漏了一些东西,但我找不到任何可以表明那是什么的东西。
回答by zed_0xff
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.
似乎 isReachable() 在 Android b/c 上运行得不好,它尝试使用 ICMP,通常需要 root 权限,然后它尝试建立到端口 7 的连接,在现代系统上通常没有正在运行的服务。
you'd better check connectivity with establishing TCP connection to ports you know should be open.
您最好通过建立与您知道应该打开的端口的 TCP 连接来检查连接性。
回答by badMonkey
Thanks Zed, I also know that the address I am trying to get will return html, so I can do more than look at the socket. Here is what I ended up doing that works reasonably for my problem. The parameter pUrl has the address of a website in it. I chose 3 seconds for the timeout.
谢谢 Zed,我也知道我试图获取的地址将返回 html,所以我可以做的不仅仅是查看套接字。这是我最终为我的问题所做的合理工作。参数 pUrl 中包含网站的地址。我为超时选择了 3 秒。
boolean result = false;
try
{
HttpGet request = new HttpGet(pUrl.toString());
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK)
{
result = true;
}
}
catch (SocketTimeoutException e)
{
result = false; // this is somewhat expected
}

