I'm developing a server-client program using rmi. I have 2 Rmi's Server: Primary and BacI'm trying to access RmiServer running on another machine. In my first attempts to connect the client to rmi on 2 different machines, it started to work fine, but after modifying some things in the methods of the classes, I stopped being able to connect the client to the server. I'll show you an excerpt of server and client code.
Server Side
public class RmiServer extends UnicastRemoteObject implements ConnectionRMI
//MAIN Function
try{
if(args.length!=4){
System.out.println("Number of arguments invalid! Rmi Server will close.");
return;
}
address = InetAddress.getByName(args[0]);
portUdp = Integer.parseInt(args[1]);
otherPort = Integer.parseInt(args[2]);
otherAddress = InetAddress.getByName(args[3]);
Registry registry = LocateRegistry.getRegistry(otherAddress.getHostAddress(), 1099);
registry.lookup("RmiServer1");
System.out.println("[INFO]: Backup Server RMI is ready! ");
primary = false;
} catch (NotBoundException | RemoteException ex) {
primary = true;
try {
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("RmiServer1", registry);
} catch (RemoteException ex1) {
System.out.println("The registry in port 1099 is already created. The program will close\n");
return;
}
System.out.println("[INFO]: Primary Server RMI is ready! ");
} catch (UnknownHostException ex) {
Logger.getLogger(RmiServer.class.getName()).log(Level.SEVERE, null, ex);
}
Interface ConnectionRMI
public interface ConnectionRMI extends Remote{
}
Client Side
try{
address1 = InetAddress.getByName(args[2]);
address2 = InetAddress.getByName(args[3]);
registry = LocateRegistry.getRegistry(address1.getHostAddress(), 1099);
atualRmi = (ConnectionRMI) registry.lookup("RmiServer1");
server1 = true;
}
The error is in this line:
atualRmi = (ConnectionRMI) registry.lookup("RmiServer1");
Error:
Exception in thread "main" java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub cannot be cast to sdeleicoes.ConnectionRMI
registry.bind("RmiServer1", registry);
The problem is here. You are binding the Registry into itself. This makes no sense. You should be binding one of your remote objects.