RMI and packages (Error message: Connection refused to host: 127.0.0.1)

Hello everybody,

I'm having trouble connecting to the rmiregistry when I place the client and server in different packages (but on the same host). The setup is as follows (Sorry for the long post, but I'm trying to provide as much info as possible)

An eclipse project with two package (server and client). The server package has a class named FileServer that implements:

publicinterface FileServerInterfaceextends Remote{

publicchar[] getFile(String filename)throws RemoteException;

}

With a main() that executes (details left out for brevity)

// Start the fileServer RMIinterface

try{

server.FileServerInterface f = (server.FileServerInterface) UnicastRemoteObject.exportObject(this, 2006);

// Bind the remote object's stub in the registry

Registry registry = LocateRegistry.getRegistry("localhost");

registry.bind("FileServer", f);

}catch(java.rmi.AlreadyBoundException e){

System.exit(1);

}catch(Exception e){

System.exit(2);

}

Now when I connect to the rmiregistry from main() using

Registry registry = LocateRegistry.getRegistry("localhost");

String[] tmp = registry.list();

for(int i = 0; i < tmp.length; i++)

System.out.println(tmp[i]);

server.FileServerInterface stub = (server.FileServerInterface) registry.lookup("FileServer");

// Read a file

char[] response = stub.getFile("/tmp/fserver/test/test1.txt");

It prints out:

FileServer

<contents of the file>

as expected. If I execute the same code in the package client in a class file named Client's main() method. I get

FileServer

java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:

java.net.ConnectException: Connection refused

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.getFile(Unknown Source)

at client.Client.main(Client.java:38)

The VM's get executed with the following args

-Djava.rmi.server.codebase="file://${workspace_loc:FileServer}/"

-Djava.security.manager

-Djava.security.policy="${workspace_loc:FileServer}/client/client.policy"

-cp ${workspace_loc:FileServer}/server:${workspace_loc:FileServer}/client:/opt/java/lib:/opt/java/jre/lib:.

(where ${workspace_loc:FileServer} evaluates to '/home/mpr/eclipse/workspace/FileServer') and the client.policy file contains:

grant codeBase"file:/home/mpr/eclipse/workspace/FileServer/-"{

permission java.security.AllPermission;

};

grant{

permission java.security.AllPermission;

};

I've tried "telneting" to localhost:1099 and the rmiregistry is running (which the output of 'netstat -l' also shows). I've tried using ordinary socket's in the client to connect to localhost:1099, and this succeeds.

I hope someone here can help me, because I'm running out of ideas (and "googling" hasn't turned anything up).

Kind regards,

Mads

PS. A bit of OS environment info should it be helpful

> java -version

java version"1.5.0_08"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)

Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)

> cat /etc/hosts.allow

#

# /etc/hosts.allow

#

sshd: 10.0.0.0/255.255.255.0 : ALLOW

ALL: localhost : ALLOW

# End of file

> cat /etc/hosts

#

# /etc/hosts:static lookup tablefor host names

#

#<ip-address> <hostname.domain.org> <hostname>

127.0.0.1 localhost.localdomain localhost

127.0.0.1 T43.localdomain T43

# End of file

[5543 byte] By [m_ravna] at [2007-10-3 4:35:54]
# 1

Finally found the error I was committing: Calling System.exit() in the main method, where I was binding to the RMIregistry. Please note, that this leaves the name bound in the registry, but the object, whose methods are to be invoked are non-existant (hence the connection refused message).

Appearently there is quite a huge difference between the semantics of return and exit (which makes a lot of sense now that I think about it).

m_ravna at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...
# 2
Hi again,Forgot to ask if anyone knowledgeable enough, would care to comment on the fact, that the act of binding doesn't add shutdown hooks, that will unbind from the registry.Kind Regards,Mads
m_ravna at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...
# 3
That is correct. It doesn't.
ejpa at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...
# 4

> Hi again,

>

> Forgot to ask if anyone knowledgeable enough, would

> care to comment on the fact, that the act of binding

> doesn't add shutdown hooks, that will unbind from the

> registry.

>

> Kind Regards,

>

> Mads

I would just add the following tip. We always called the rebind() instead of bind()...It does the exact same thing.

With rebind(), if the object is already bound, rebind() will just drop the object. On the other hand bind() would throw an AlreadyBoundException if already bound to the registry.

In maintence terms it means you could drop your RMI server out of service without having to drop the registry. If you had code changes, only those new objects would be bound, all the rest would stay the same...

Just my two cents...

c2gilla at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...
# 5

Hello ejp,

Well I sort of figured that out for myself :) Just wondering if there was any particular reason why not - I mean if calling System.exit() is going to make RMI access impossible, would it not make sense just to undo the bindings? I'm just curious about the matter.

On another note I think it would be a good idea to have a warning about System.exit() in the RMI tutorial

http://java.sun.com/j2se/1.5.0/docs/guide/rmi/hello/hello-world.html

Anyway, thanks for the reply

Mads

m_ravna at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...
# 6

> I would just add the following tip. We always called

> the rebind() instead of bind()...It does the exact

> same thing.

I've started using rebind() myself, as this is easier (especially for testing purposes). Used bind() as this was the method used in

http://java.sun.com/j2se/1.5.0/docs/guide/rmi/hello/hello-world.html

Thanks for the reply,

Mads

m_ravna at 2007-7-14 22:39:34 > top of Java-index,Core,Core APIs...