Oakin
Oakin

Reputation: 143

RMI ConnectException : Connection refused, really strange behaviour

I know that this question has been asked several times but I've yet to find an answer that works for me. Basically I've implemented a client/server solution with RMI. My only problem is that connection to the server is sometimes extremely slow.

When the server is started and a client connects, for the first time, it will take seconds before it gets connected (sometimes it does not even connect but get the ConnectException instead) and after that I will get the Connection refused when trying to communicate with the server. What makes it even more mind boggling is that it works perfectly fine when I disconnect the first failing client and connect again. The client then connects in under a second and all the communication works flawlessy.

I've tried manually starting the rmiregistry, I've double checked my PATH. I wasn't able to connect to the port with telnet but it works when I play on that port with my Othello game (which is not using RMI). It might be worth to say that I'm doing this on the same network and computer so I'm using localhost.

TL;DR: When I first connect to the server it takes forever, but I'm able to connect. When I then try to communicate with methods it crashes and I receive "java.rmi.ConnectException: Connection refused to host: 192.xxx.xxx.x; nested exception is: java.net.ConnectException: Connection timed out: connect". If I then close that client and try to connect with another client it takes around one second to connect and then everything works flawlessy. Why is that?

Code for the client:

public class NetworkClient {
private Registry registry;
private IArenaServer server;

public NetworkClient(String ipAddress, int port) throws RemoteException {

    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }

    try {

        registry = LocateRegistry.getRegistry(ipAddress, port);
        server = (IArenaServer)(registry.lookup("arenaServer"));
        System.out.println("Client successfully connected to server at " + ipAddress + ":" + "port");

    } catch (NotBoundException ex) {
        Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (AccessException ex) {
        Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Code for the server:

public class ArenaServer extends UnicastRemoteObject implements IArenaServer {

private int port = 9029;
private String ipAddress;
private Registry registry;

public ArenaServer() throws RemoteException {

    if(System.getSecurityManager() == null){
        System.setSecurityManager(new RMISecurityManager());
    }

//  try {
        //ipAddress = (InetAddress.getLocalHost()).toString();
        registry = LocateRegistry.createRegistry(port);
        registry.rebind("arenaServer", this);

        System.out.println("Server successfully started...");
        //System.out.println("Server's IP address is: " + ipAddress);

//  } catch (UnknownHostException ex) {
//      ErrorHandler.getInstance().reportError("Cannot get IP address", "");
//  }
}

Generic example of what I get when trying to communicate with the server after connecting the first time:

java.rmi.ConnectException: Connection refused to host: 192.xxx.xxx.x; nested exception is: 
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.getInstalledGames(Unknown Source)
....

Upvotes: 1

Views: 1560

Answers (1)

user207421
user207421

Reputation: 310840

This sounds like a case for java.rmi.server.hostname. See item A.1 in the RMI FAQ, reachable via the RMI Home Page.

Upvotes: 1

Related Questions