RMI & connection refused
I'm suffering from the the common "java.rmi.ConnectException: Connection
refused to host" problem.
I've searched the web and found this problem described many times.
However, the solutions, range from changing /etc/hosts to doing really
weird stuff -- but nothing applicable. ;)
All the online RMI tutorials and examples work fine when you're using the
same host, but as soon as the client moves to a remote location, all the
examples fail.
This is how it is with my code as well.
Here is an example with some simple code:
::::::::::::::
RMIClient.java
::::::::::::::
import java.rmi.registry.*;
public class RMIClient {
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
if (System.getSecurityManager() == null)
System.setSecurityManager(new SecurityManager());
try {
Registry r = LocateRegistry.getRegistry(host, port);
ServerIface s = (ServerIface) r.lookup("RMIServerName");
System.out.println(s.hello());
} catch (Exception e) {
e.printStackTrace();
}
}
}
::::::::::::::
RMIServer.java
::::::::::::::
import java.rmi.RemoteException;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
public class RMIServer extends UnicastRemoteObject implements ServerIface {
protected RMIServer() throws RemoteException { super(); }
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
if (System.getSecurityManager() == null)
System.setSecurityManager(new SecurityManager());
try {
Registry r = LocateRegistry.getRegistry(host, port);
r.bind("RMIServerName", new RMIServer());
} catch (Exception e) {
e.printStackTrace();
}
}
public String hello() throws RemoteException {
return "Hello World";
}
}
::::::::::::::
ServerIface.java
::::::::::::::
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerIface extends Remote {
public abstract String hello() throws RemoteException;
}
I also have a wide open security policy file:
::::::::::::::
policy
::::::::::::::
grant {
permission java.security.AllPermission;
};
Here's my commands:
rmiregistry &
java -Djava.security.policy=policy RMIServer localhost 1099 &
java -Djava.security.policy=policy RMIClient localhost 1099
The problem is with the next command from a remote host:
java -Djava.security.policy=policy RMIClient adam 1099
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:574)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:94)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:179)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy0.hello(Unknown Source)
at RMIClient.main(RMIClient.java:12)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
....
I tried switching server and clients, (and had to change ports) but
without any success.
I think the registry is OK, I make a simple ListRMI program using
Naming.list() to view the registry entries.
Can anyone help -- not just for me -- but for all the rest looking for a
definitive answer to what's going on and how to fix it?
Thanks!