BinaryShrub
BinaryShrub

Reputation: 346

Java RMI only working localhost

I am working on a thin RMI client, but when I try to connect to it from any machine but localhost it gets a refused connection stack trace. I verified that my firewall is off. What else do I need to do. Is there security I need to configure for RMI?

Server.java

package com.ibm.icm.autoconfig.server.rmi;

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

public final class Server extends Commands implements ServerHook {
    private Registry registry;

    public static void main(String[] args) throws RemoteException {
        Server server = new Server();
        server.Start();
    }

    private void Start() throws RemoteException {

        ServerHook serverHook = (ServerHook) UnicastRemoteObject.exportObject(this, 0);

        // Add serverHook to the local registry -- attempt to create registry
        // instance, if it fails to create, try finding an existing one
        try {
            registry = LocateRegistry.createRegistry(1099);
        } catch (ExportException ee) {
            registry = LocateRegistry.getRegistry(1099);
        }
        registry.rebind("ServerHook", serverHook);

        System.out.println("Server Running...");

        while (true) {
        }
    }
}

ServerHook.java

package com.ibm.icm.autoconfig.server.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ServerHook extends Remote {
    public boolean write(String msg) throws RemoteException;
}

Client.java

package com.ibm.icm.autoconfig.client.rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import com.ibm.icm.autoconfig.server.rmi.ServerHook;

public class Client {
    private ServerHook serverHook;

    public static void main(String[] args) throws RemoteException, NotBoundException {
        Client client = new Client();
        client.connectTo("localhost");
        client.writeServer("Hello Server!");
    }

    private void connectTo(String serverHost) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry();
        serverHook = (ServerHook) registry.lookup("ServerHook");
    }

    private void writeServer(String msg) throws RemoteException {
        serverHook.write(msg);
    }
}

Stacktrace for non local RMI:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 9.65.186.135; 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 com.ibm.icm.autoconfig.client.rmi.Client.connectTo(Client.java:21)
    at com.ibm.icm.autoconfig.client.rmi.Client.main(Client.java:15)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(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 Source)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
    ... 7 more

Upvotes: 2

Views: 3094

Answers (2)

BinaryShrub
BinaryShrub

Reputation: 346

I figured out the issue here. It was similar to what Suraj Chandran had suggested.

private void connectTo(String serverHost) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry();
        serverHook = (ServerHook) registry.lookup("ServerHook");
}

The entry LocateRegistry.getRegistry() was essentially making my client point to its own registry. The correct code is:

private void connectTo(String **serverHost**) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(**serverHost**);
        serverHook = (ServerHook) registry.lookup("ServerHook");
}

Upvotes: 1

Suraj Chandran
Suraj Chandran

Reputation: 24791

This is because of the folowing code int eh client side:

client.connectTo("localhost"); --> Client tries to connect to itself. Wrong

This should be changed to:

client.connectTo(serverHost);

where serverHost is the address of the machine where your server code is running

Upvotes: 3

Related Questions