在我的桌面应用程序中,我通过网络服务连接到服务器。 使用下面获取客户端机器Tomcat状态的代码,我可以获得服务器IP地址和端口号,但是如何找到服务器Tomcat状态?
InetAddress locIP = InetAddress.getByName("127.0.0.1");
serverSocket = new ServerSocket(8080, 0, locIP);
最佳答案
您可以按如下方式使用 Java 的 URLConnection
:
try {
URL url = new URL("http://youserver.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(2000); //Connection timeout
urlConnection.connect();
System.out.println("Server is up and running");
} catch (Exception e) {
System.out.println("Server is not yet up");
}
如果 urlConnection.connect()
在 2 秒内静默返回,则表示服务器已启动并正在运行,否则将抛出异常,表明服务器未启动或 URL 不正确。
关于java - 查找服务器tomcat是否启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235188/