Checking if server is online from Java code

前端 未结 4 1480
甜味超标
甜味超标 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:15

    I used this method for my ServerUtil.

        public static boolean isOnline() {
        boolean b = true;
        try{
            InetSocketAddress sa = new InetSocketAddress("SERVER_IP_ADDRESS", PORT);
            Socket ss = new Socket();
            ss.connect(sa, 1);
            ss.close();
        }catch(Exception e) {
            b = false;
        }
        return b;
    }
    

提交回复
热议问题