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.no-ip.org:43594
– PhthaleinA 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? – Sines