Ich möchte überprüfen, ob Server-Anwendung verfügbar ist. Nachdem der Server gestartet wurde, möchte ich die Überprüfung stoppen, bis der Server den Status ändert. Wie geht das mit meinem Code:Prüfen, ob der Server von Java-Code online ist
private static final String SERVER_ADDRESS = "192.144.10.10";
private static final int TCP_SERVER_PORT = 8890;
private static boolean connected = false;
static Socket s;
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(task, 01, 5001); }
static TimerTask task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if (connected == false)
{
System.out.println(hostAvailabilityCheck());
}
}
};
public static boolean hostAvailabilityCheck()
{
boolean available = true;
try {
if (connected == false)
{ (s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT)).close();
}
}
catch (UnknownHostException e)
{ // unknown host
available = false;
s = null;
}
catch (IOException e) { // io exception, service probably not running
available = false;
s = null;
}
catch (NullPointerException e) {
available = false;
s=null;
}
return available;
}
Gibt es einen besseren Weg, um das zu lösen?
das statische Feld 'connected' ziemlich nutzlos zu sein scheint, weil man es nie –
Nun ändern, wird dies überprüfen, ob das Host/Port-Paar vorhanden ist, aber es wird nicht überprüfen alles funktional ... Es könnte sein, dass die Serving-Software hinter ist Fubar und gibt immer 500 für HTTP, zum Beispiel – fge
Dies könnte Ihnen helfen http://stackoverflow.com/questions/9552743/proper-way-to-test-if -server-is-up-in-java Ihre URL (URL_TO_APPLICATION) könnte SERVER_ADDRESS sein + TCP_SERVER_PORT Siehe auch http://www.codeanch.com/t/205709/sockets/java/check-server –