Search code examples
javarmijava-7javac

Yet another RMI Issue


Before you said that, yes, I know, there are a lot of threads which cover the same issue here on Stackoverflow, but any of those solved my problem. My problem is using the RMI interface (that is mandatory for my purposes) in two distinct computers, where one provides the RMI object (Server) and one asks for the Stub and obtains a Proxy. My code is the exact copy of the one provided by the Java 7 Reference Manual by Oracle and edited by Oracle Press:

IRmi.java

import java.rmi.*;

public interface IRmi extends Remote {
    double add() throws RemoteException;
}

RmiImpl.java

import java.rmi.*;
import java.rmi.server.*;

public class RmiImpl extends UnicastRemoteObject implements IRmi {
    public RmiImpl() throws RemoteException {}

    public double add() { double d = 5.0; return d; }
}

Server.java

import java.net.*;
import java.rmi.*;

public class Server {

    public static void main(String s[]) {
        try {
            RmiImpl ri = new RmiImpl();
            Naming.rebind("Server",ri);
        } catch (Exception e) {
            System.err.println("Err");
        }
    }

}

Client.java

import java.rmi.*;

public class Client {

    public static void main(String s[]) {
        try {
            IRmi itf = (IRmi)Naming.lookup("rmi://192.168.0.8/Server");
            System.out.println(itf.add());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

After that, I compile with:

javac IRmi.java
javac RmiImpl.java
rmic RmiImpl
javac Client.java
javac Server.java

After that passage, I copy all the classes on both the client and the server, and then I run rmiregistry on the same folder where the classes were transfered. Assuming that in my local lan (192.168.0.0/255) there are two machines, where the client is 192.168.0.3 and the server 192.168.0.8 I run on those machine respectively java client and java Server, where the Client returns me the following error:

java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
    java.net.ConnectException: Connessione rifiutata
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129)
    at RmiImpl_Stub.add(Unknown Source)
    at Client.main(Client.java:8)
Caused by: java.net.ConnectException: Connessione rifiutata
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 5 more

For instance, In another test I've also tried to implement the server with this following code:

    try {
        RemImpl obj = new RemImpl(this.serv);
        if (this.ob_list.size()>0) {
            for (Observer ob: ob_list) {
                obj.addObserver(ob);
            }
        }
        this.myrec = (Rem) UnicastRemoteObject.exportObject(obj, 9999);
        Registry registry = LocateRegistry.createRegistry(9999);
        registry.rebind(this.serv, this.myrec);
        //this.has_error = false;
        System.out.println("Binded as "+this.serv);
    } catch (RemoteException e) {
        System.err.println("Remote exception catched: " + e.getMessage());
        //this.has_error = true;
        this.myrec = null;
    }

and the client with the other following code:

    try {
        Registry registry = LocateRegistry.getRegistry(host);
        this.myrec = (Rem) registry.lookup(service);
    } catch (Exception e) {
        e.printStackTrace();
    }

and, in this case, the client returns me the following and different error:

java.rmi.NoSuchObjectException: no such object in table
ERROR
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at com.sun.proxy.$Proxy0.send(Unknown Source)
    at rmi.lowlevel.NullSenderPolicy.send(NullSenderPolicy.java:81)
    at message.policy.old.BroadcastSenderPolicy.single_send(BroadcastSenderPolicy.java:104)
    at message.policy.old.AtomicBroadcastSender.fifo_send(AtomicBroadcastSender.java:54)
    at message.policy.old.AtomicBroadcastNode.fifo_send(AtomicBroadcastNode.java:131)
    at elements.testunit.TestPairBroadcastNodes.main(TestPairBroadcastNodes.java:20)

At this point I don't know which way to turn. Thanks in advance for any other kind suggestion.


Solution

  • Actually, the problem was lately solved by adding the -Djava.rmi.server.hostname=192.168.0.x argument on both client and server. Thanks for all the advices.