从Java代码检查服务器是否在线 [英] Checking if server is online from Java code

查看:807
本文介绍了从Java代码检查服务器是否在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查服务器应用程序是否可用。服务器启动后,我想停止检查,直到服务器更改状态。如何使用我的代码:

  private static final String SERVER_ADDRESS =192.144.10.10; 
private static final int TCP_SERVER_PORT = 8890;
private static boolean connected = false;
静态套接字s;

public static void main(String [] args){

Timer timer = new Timer();
timer.schedule(task,01,5001); }

静态TimerTask task = new TimerTask(){

@Override
public void run(){
// TODO自动生成的方法存根
if(connected == false)
{
System.out.println(hostAvailabilityCheck());
}
}
};

public static boolean hostAvailabilityCheck()
{

boolean available = true;
try {
if(connected == false)
{(s = new Socket(SERVER_ADDRESS,TCP_SERVER_PORT))。close();
}
}
catch(UnknownHostException e)
{//未知主机
available = false;
s = null;
}
catch(IOException e){// io异常,服务可能没有运行
available = false;
s = null;
}
catch(NullPointerException e){
available = false;
s = null;
}


可退货;
}

有没有更好的方法可以解决这个问题?

解决方案

检查方法可以重写如下(Java 7及更高版本):

  public static boolean hostAvailabilityCheck(){
try(Socket s = new Socket(SERVER_ADDRESS,TCP_SERVER_PORT)){
return true;
} catch(IOException ex){
/ * ignore * /
}
返回false;
}

除了简化异常处理外,还可以消除讨厌的套接字泄漏。 (如果您担心执行此检查所花费的时间,请在尝试连接之前设置连接超时:请参阅为套接字操作设置超时



但这种方法的问题有很多种:




  • 它只测试 正在侦听连接。如果您的服务在代理服务器后面......或者由 inetd 服务管理...那么接受的连接并不意味着您的服务实际上正在运行。 / p>


  • 这将导致您的服务看到关闭而不发送请求的连接。因此,您最好对服务进行编码,以优雅地处理此问题。


  • 重复执行此操作会增加网络和服务器负载。


  • 如果你设置了一个短暂的超时,因为你不希望测试冻结,那么你就有可能将它设置得太短并且判断主机是否因为它没有被关闭。








服务器启动后我想停止检查,直到服务器更改状态


这几乎是不可能的。原因是如果不检查,您将无法判断服务器是否已更改状态。或者至少,如果没有实现精心设置的状态通知服务,您将无法执行此操作,其中服务器调用客户端以告知其正在更改状态。 (如果更改状态包括死亡或丢失网络连接,那么您将无法使 通知可靠...如果有的话。)


I want to check if server application is available. After server is started I want to stop checking until the server changes status. How to do that with my code:

 private static final String SERVER_ADDRESS = "192.144.10.10";
    private static final int TCP_SERVER_PORT = 8890;
    private static boolean connected = false;
    static Socket s;

    public static void main(String[] args) {

    Timer timer = new Timer();
    timer.schedule(task, 01, 5001); }  

static TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (connected == false)
            {
            System.out.println(hostAvailabilityCheck());
            }
        }
    };

    public static boolean hostAvailabilityCheck()
    { 

        boolean available = true; 
        try {               
            if (connected == false)
            { (s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT)).close();    
            }               
            } 
        catch (UnknownHostException e) 
            { // unknown host 
            available = false;
            s = null;
            } 
        catch (IOException e) { // io exception, service probably not running 
            available = false;
            s = null;
            } 
        catch (NullPointerException e) {
            available = false;
            s=null;
        }


        return available;   
    } 

Is there any better way to solve this?

解决方案

The check method can be rewritten as follows (Java 7 and later):

public static boolean hostAvailabilityCheck() { 
    try (Socket s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT)) {
        return true;
    } catch (IOException ex) {
        /* ignore */
    }
    return false;
}

In addition to simplifying the exception handling, this eliminates a pesky Socket leak. (If you are concerned with the time taken to do this check, then set a connection timeout before attempting to connect: see Setting a timeout for socket operations)

But the problems with this approach are many-fold:

  • It only tests that something is listening for connections. If your service is behind a proxy ... or is managed by something like the inetd service ... then the accepted connections don't mean your service is actually working.

  • This is going to cause your service to "see" connections that close down without sending a request. So you'd better code your service to deal with this "gracefully".

  • Doing this repeatedly adds to network and server load.

  • If you set a short timeout because you don't want the test to "freeze", then you risk setting it too short and judging the host to be down when it isn't.


After server is started I want to stop checking until the server changes status

That is next to impossible. The reason is that you won't be able to tell whether the server has "changed status" without checking. Or at least, you won't be able to do this without implementing an elaborate status notification service where the server calls the client to tell it is changing status. (And if "change status" includes "die" or "lost network connection", then you won't be able to make that notification reliable ... if at all.)

这篇关于从Java代码检查服务器是否在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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