Checking if server is online from Java code

前端 未结 4 1481
甜味超标
甜味超标 2020-12-25 13:21

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:

<         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 14:09

    First check if server is running and the server accepts the connection.

       public static boolean hostAvailabilityCheck()
    { 
        s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
        boolean available = true; 
        try {               
            if (s.isConnected())
            { s.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;   
    } 
    

提交回复
热议问题