below is a static method to check if another RMI server is on the network, I basically call a method that, if it answers true, means that the connection is turned on, if it does not respond and instead gives an exception, itβs wrong. Is there a better way to do this? And is there a better way to speed things up? If there is a connection, it returns with the value quickly, but if it is not required, then time.
public static boolean checkIfOnline(String ip,int port)
{
boolean online = false;
try {
InetAddress ipToConnect;
ipToConnect = InetAddress.getByName(ip);
Registry registry = LocateRegistry.getRegistry(ipToConnect.getHostAddress(),port);
ServerInterface rmiServer = (ServerInterface)registry.lookup("ServerImpl");
online = rmiServer.checkStudentServerOnline();
if(online)
{
System.out.println("Connected to "+ipToConnect);
}
} catch (RemoteException e) {
System.out.println(e.getMessage());
return false;
} catch (NotBoundException e) {
System.out.println(e.getMessage());
return false;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return online;
}
source
share