0

I have a strange problem with RMI. Firstly, i started the server, then i started the client. In the first time, i can connect from the client to the server. Then i stop the client. And i attempt to re-connect the client. I can get the server stub, however when i call a method from the stub, i got a bunch of exception of "Connection Refused" . I have no firewall turn on.

My code for setting up the Server

public Server(String name, int port) {
        try {
            LocateRegistry.createRegistry(port);
            ServerModel server = new ServerModel();
            String serverStr = "rmi://localhost:" + port + "/" + name;
            Naming.rebind(serverStr, server);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
}

And the code for setting up the Client

public Client(String serverName, int port) throws RemoteException {
        controller = new ClientController();
        view = new MainView();
        ClientRegistry.setData(ClientConstant.FRONT_CONTROLLER, controller);
        try {
            String serverStr = "rmi://localhost:" + port + "/" + serverName;
            ServerInterface serverStub = (ServerInterface) Naming.lookup(serverStr);
            ClientRegistry.setData(ClientConstant.STUB, serverStub);
            view.setFrontController(controller);
            view.display();
        } catch (Exception ex) {
            // Show error if server is not available
            JOptionPane.showMessageDialog(null, "Server is not available", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
}

And the Exception

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.ConnectException: Connection refused to host: 192.168.0.104

2 Answers 2

1

When you call LocateRegistry.createRegistry, you instantiate a new RMI registry on a specific port. Naming.rebind, on the other hand, tries to register your object on the "default" registry, on the default port. It therefore fails, since no registry exists on the default port.

Try this instead:

Registry registry = LocateRegistry.createRegistry(port);
registry.rebind(serverStr, server);
1
  • I have made change to my code using your suggestions, however, the problem is still happened. I can normally connect to the server in the first time. However, when i quit a client and try to reconnect to the server, i got "Connection Refused" exception.
    – Tan Nguyen
    Dec 31, 2010 at 11:07
0

Your server is exiting. Store the result of createRegistry into a static variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.