Search code examples
rmi

rmi - ConnectException


This is my first time that I am using RMI, basically I manage to run the following RMI example locally on my PC but not via two separate Linux machines.

The server interface:

public interface PowerService extends Remote{
public BigInteger square ( int number )
    throws RemoteException;

public BigInteger power  ( int num1, int num2) 
    throws RemoteException;
}

The server:

public class PowerServiceServer extends UnicastRemoteObject implements
    PowerService {

public PowerServiceServer() throws RemoteException {
    super();
}

public BigInteger square(int number) throws RemoteException {
    imp .....
    return (bi);
}

public BigInteger power(int num1, int num2) throws RemoteException {
    imp .....
    return bi;
}

public static void main(String[] args) throws Exception {

    PowerServiceServer svr = new PowerServiceServer();
    // ... and bind it with the RMI Registry
    Naming.bind("PowerService", svr);
    System.out.println("Service bound....");
}
}

The client:

public class PowerServiceClient {
public static void main(String args[]) throws Exception {
    // Call registry for PowerService
    PowerService service = (PowerService) Naming.lookup("rmi://" + args[0]
            + "/PowerService");
    DataInputStream din = new DataInputStream(System.in);
    for (;;) {
        System.out.println("1 - Calculate square");
        System.out.println("2 - Calculate power");
        System.out.println("3 - Exit");
        System.out.println();
        System.out.print("Choice : ");

        String line = din.readLine();
        Integer choice = new Integer(line);

        int value = choice.intValue();

        switch (value) {
        case 1:
            // Call remote method
            ....................
            break;
        case 2:
            // Call remote method
            ....................
            break;
        case 3:
            System.exit(0);
        default:
            System.out.println("Invalid option");
            break;
        }
    }
}

and the client interfaces is the same like the server

This is what I did in order to run the rmi example:

1) On the server side I created the stub

2) Run rmiregisrty

3) Run the server

4) I copy the stub from the server side to the client side in to the same package

5) Run the client

After running the client I got the following error message:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:110) at compute.PowerServiceServer_Stub.square(Unknown Source)

is possible that due to some firewall I can’t connect or perhaps I am doing something wrong ??

Thanks


Solution

  • This is the problem addressed by java.rmi.server.hostname. See item A.1 in the RMI FAQ. You need to either fix the /etc/hosts misconfiguration that causes it, or set the system property java.rmi.server.hostname in the server JVM to the correct IP address of the server, before exporting any remote objects.