从java检查服务可用性 [英] Check service availability from java

查看:84
本文介绍了从java检查服务可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用java检查服务?我找到了这篇文章,但它仅用于检查主机名。

How can I check service using java? I found this article, but it is only for checking hostname.

我的问题是如何检查端口x运行的服务,例如: myhostname:8080 myhostname: 8099 ,我可能在这些端口上运行服务n或p但是如果我手动执行它会通过web看到,我怎样才能在java中实现相同的效果?

My question is how can I check is service on port x running, ex: myhostname:8080 or myhostname:8099 , I might be running service n or p on those ports but it would be visible trough web if I do it manually, how can I achieve same effect in java?

推荐答案

该片段发送ping,您无法操作以实现您想要的效果。只需打开套接字并捕获任何例外情况。

That snippet sends a ping, which you can't manipulate to achieve what you want. Just open a socket and catch any exceptions.

bool success = true;
try {
  (new Socket(host, port)).close();
} catch (UnknownHostException e) {
  // unknown host
  success = false;
} catch (IOException e) {
  // io exception, service probably not running
  success = false;
}

如果您需要检测正在运行的服务,那么您需要阅读发送的第一个字节并将它们与您知道每个服务应发送的内容进行比较这可能需要来回几次。

If you need to detect which service is running, then you need to read the first bytes sent and compare them to what you know each service should send. This might require a few passes back and forth.

这篇关于从java检查服务可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆