9

I am using InetAddress to determine if my server is online.

If the server is offline it will restart the server.

This process loops every 5 minutes to check once again if the server is online.

It works fine but now I need to figure out how to specify that I want to use port 43594 when checking the server status instead of the default port 80.

Thanks! Here's my code:

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();
        }
    }
}

EDIT:

Okay so I took someones advice and I made it into this. But now when I uncomment this line.. Runtime.getRuntime().exec("cmd /c start start.bat");

I get this error..

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

This is my current code:

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();
        }
    }
}
Cloudnine1999
  • 93
  • 1
  • 1
  • 5
  • Have you tried modifying the host parameter to include the port? `cloudnine1999.no-ip.org:43594` – FThompson Feb 16 '13 at 01:36
  • The [Javadoc](http://docs.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29) says : `A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host. ` - are you sure the implementation you're using is actually using a testconnection on port 80? – fvu Feb 16 '13 at 01:43
  • @Vulcan Thanks but I tried that. – Cloudnine1999 Feb 16 '13 at 01:45
  • @fvu I'm actually trying to test port 43594, not 80, I thought it was testing port 80, I didn't realize that by default it tests port 7. But thats just details, I need it to change from whatever port it is checking, to port 43594. – Cloudnine1999 Feb 16 '13 at 01:46

1 Answers1

11

As I already mentioned in the comments, according to the Javadoc isReachable isn't implemented in a way that would allow you to control the selected port. Actually, if it is allowed to do so by system privileges it will just ping the machine (ICMP request).

Doing it manually (ie, using a socket) will certainly work and isn't really more complicated and/or longer:

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());
    }
}        

EDIT: forgot to handle IOException which also means the server isn't functioning, added

EDIT2: added the handling of the IOException that close() can throw

EDIT3: and exception handling for exec()

fvu
  • 31,143
  • 5
  • 57
  • 77
  • Oh also the socket.close() had to be be removed it also gave me errors. – Cloudnine1999 Feb 16 '13 at 02:38
  • No you should not remove it, I modified my code to show how to catch it locally. IMO throwing exceptions on close() operations is not the most useful feature in Java, but that's the way it is. Sorry for the confusion, the small testprogram I use to test such snippets already had a `throws IOException` which causedme to miss this issue. – fvu Feb 16 '13 at 02:41
  • And an example of how to catch and handle the exception thrown by exec added - I think the program is almost complete now, don't you think? :-) – fvu Feb 16 '13 at 02:47
  • almost... Unreachable catch block for SocketTimeoutException. It is already handled by the catch block for IOException :-) – harschware Feb 18 '15 at 18:38
  • @harschware indeed, nice catch - code has been corrected. – fvu Feb 19 '15 at 18:16