user6181090
user6181090

Reputation:

RMI with different PCs

I am learning RMI and I made a basic program that uses codebase.

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Server implements sInterface,s2int  {

public void go()
{
    System.out.println("GO");
}
public void doIt()
{

}

public static void main(String[] args)
{
    if(System.getSecurityManager()==null)
    {
        System.setSecurityManager(new SecurityManager());
    }
    try
    {
    System.setProperty("java.rmi.server.hostname","helios");
        String s = "SERVER";
        Registry r = LocateRegistry.getRegistry();

        sInterface stub = (sInterface) UnicastRemoteObject.exportObject(new Server(),0);
        r.rebind(s,stub);

    }catch(Exception x){x.printStackTrace();}
}
}

Client:

public class Client {

public static void main(String[] args)
{
    if(System.getSecurityManager()==null)
    {
        System.setSecurityManager(new SecurityManager());
    }

    try{

        String name = "SERVER";
        Registry r = LocateRegistry.getRegistry(args[0]);
        sInterface inf = (sInterface)r.lookup(name);
        inf.go();

    }catch(Exception x)
    {
        x.printStackTrace();
    }
}
}

Client does not have 's2int' interface and that is downloaded from the codebase. The commands used to start the server and the client modules are as follows:

java -Djava.rmi.server.codebase=http://helios/~owner/rmi.jar
-Djava.security.policy=server.policy Server         


java -Djava.security.policy=client.policy    
-Djava.rmi.server.codebase=http://helios/~owner/     
-Djava.rmi.server.hostname=helios Client localhost

Now,this works when both the server and client are on the same PC, but when I tried running it on a different PC on the same network, I got a

java.rmi.ConnectException: Connection refused to host: localhost; nested     exception is:
    java.net.ConnectException: Connection refused: 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.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at Client.main(Client.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at          
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource) at    
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
    ... 6 more

I am not very knowledgeable when it comes to networking. Can anyone explain why it's not working?

Upvotes: 0

Views: 1493

Answers (1)

user207421
user207421

Reputation: 310869

Your client is looking up the wrong Registry. It needs to lookup the Registry at the server host, not its own localhost.

Upvotes: 1

Related Questions