9

サーバーがオンラインかどうかを判断するために InetAddress を使用しています。

サーバーがオフラインの場合、サーバーを再起動します。

このプロセスは 5 分ごとにループし、サーバーがオンラインかどうかをもう一度確認します。

正常に動作しますが、サーバーのステータスをチェックするときに、デフォルトのポート 80 の代わりにポート 43594 を使用するように指定する方法を理解する必要があります。

ありがとう!これが私のコードです:

import java.net.InetAddress;
public class Test extends Thread {
    public static void main(String args[]) {
        try {
            while (true) {
                try
                {
                    InetAddress address = InetAddress.getByName("cloudnine1999.no-ip.org");
                    boolean reachable = address.isReachable(10000);
                    if(reachable){
                        System.out.println("Online");
                    }
                    else{
                        System.out.println("Offline: Restarting Server...");
                        Runtime.getRuntime().exec("cmd /c start start.bat");
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                Thread.sleep(5 * 60 * 1000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

編集:

さて、私は誰かのアドバイスを受けて、これにしました。しかし、今、この行のコメントを外すと.. Runtime.getRuntime().exec("cmd /c start start.bat");

このエラーが発生します..

error: unreported exception IOException; must be caught or declared to be thrown

これは私の現在のコードです:

import java.net.*;
import java.io.*;
public class Test extends Thread {
    public static void main(String args[]) {
        try {
            while (true) {
                SocketAddress sockaddr = new InetSocketAddress("cloudnine1999.no-ip.org", 43594);
                Socket socket = new Socket();
                boolean online = true;
                try {
                    socket.connect(sockaddr, 10000);
                }
                catch (IOException IOException) {
                    online = false;
        }
                if(!online){
            System.out.println("OFFLINE: Restarting Server..");
            //Runtime.getRuntime().exec("cmd /c start start.bat");
        }
                if(online){
                    System.out.println("ONLINE");
                }
                Thread.sleep(1 * 10000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
4

1 に答える 1

11

コメントで既に述べたように、Javadoc isReachable によると、選択したポートを制御できるように実装されていません。実際には、システム権限によって許可されている場合は、マシンに対して ping を実行するだけです (ICMP 要求)。

手動で (つまり、ソケットを使用して) 行うことは確かに機能し、実際にはそれほど複雑ではなく、長くもなりません。

SocketAddress sockaddr = new InetSocketAddress("cloudnine1999.no-ip.org", 43594);
// Create your socket
Socket socket = new Socket();
boolean online = true;
// Connect with 10 s timeout
try {
    socket.connect(sockaddr, 10000);
} catch (SocketTimeoutException stex) {
    // treating timeout errors separately from other io exceptions
    // may make sense
    online=false;
} catch (IOException iOException) {
    online = false;    
} finally {
    // As the close() operation can also throw an IOException
    // it must caught here
    try {
        socket.close();
    } catch (IOException ex) {
        // feel free to do something moderately useful here, eg log the event
    }

}
// Now, in your initial version all kinds of exceptions were swallowed by
// that "catch (Exception e)".  You also need to handle the IOException
// exec() could throw:
if(!online){
    System.out.println("OFFLINE: Restarting Server..");
    try {
        Runtime.getRuntime().exec("cmd /c start start.bat");
    } catch (IOException ex) {
         System.out.println("Restarting Server FAILED due to an exception " + ex.getMessage());
    }
}        

編集:IOExceptionサーバーが機能していないことも意味する処理を忘れた、追加

EDIT2: close() がスローできる IOException の処理を​​追加しました

EDIT3: および exec() の例外処理

于 2013-02-16T01:54:45.717 に答える