Android - 检查外部服务器是否在线
我想在使用在线组件启动任何活动之前检查我的服务器是否在线。
我尝试过使用 ping
runtime.exec("ping -c 1 google.com");
proc.waitFor();
int exit = proc.exitValue();
,但这总是会给出退出代码 1 或 2,而不是 0,即使我知道服务器在线。
我也尝试过
Online=InetAddress.getByName("www.google.com").isReachable(10000);
,但显然这个函数对于外部服务器来说是不稳定的,因此它也不行。
像检查服务器是否在线这样的基本功能肯定是非常简单的吧?有人有我还没有尝试过的想法吗?
I want to check if my server is online before launching any activity with an online component.
I have tried using ping
runtime.exec("ping -c 1 google.com");
proc.waitFor();
int exit = proc.exitValue();
but this will always give either exit code 1 or 2, never 0, even when I know the server is online..
I have also tried
Online=InetAddress.getByName("www.google.com").isReachable(10000);
But apparently this function is flakey as hell with external servers, and as such it doesn't work either.
Surely such a basic function as checking if a server is online should be pretty straightforward? does anyone have any ideas I haven't tried yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用您要“真正”使用的任何协议连接到服务器。仅仅因为服务器响应
ping
并不意味着服务器正在按照您的需要运行。因此,例如,如果您要发出 Web 服务请求,请执行一些简单的 HTTP 操作。Try connecting to the server, using whatever protocol you are going to use "for real". Just because a server responds to a
ping
does not mean that the server is running for what you need it for. So, for example, if you are going to make requests of a Web service, perform some simple HTTP operation.