Sto usando InetAddress per determinare se il mio server è online.JAVA Specifica porta con InetAddress
Se il server è offline, il server verrà riavviato.
Questo processo viene ripetuto ogni 5 minuti per verificare nuovamente se il server è in linea.
Funziona bene ma ora ho bisogno di capire come specificare che voglio utilizzare la porta 43594 quando si controlla lo stato del server anziché la porta predefinita 80.
Grazie! Qui è il mio codice:
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:
Va bene così ho preso qualcuno consiglio e ho fatto in questo. Ma ora quando Decommentare questa linea .. Runtime.getRuntime().exec("cmd /c start start.bat");
ottengo questo errore ..
error: unreported exception IOException; must be caught or declared to be thrown
Questo è il mio codice corrente:
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();
}
}
}
Hai provato a modificare il parametro host per includere la porta? 'cloudnine1999.no-ip.org: 43594' – Vulcan
Il [Javadoc] (http://docs.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29) dice : 'Un'implementazione tipica utilizzerà ICMP ECHO REQUEST se è possibile ottenere il privilegio, altrimenti tenterà di stabilire una connessione TCP sulla porta 7 (Echo) dell'host di destinazione. '- sei sicuro che l'implementazione che stai utilizzando stia effettivamente utilizzando un testconnection sulla porta 80? – fvu
@Vulcan Grazie ma ci ho provato. – Cloudnine1999