Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RMI Connection Refused to Host

249 views
Skip to first unread message

JMecc

unread,
Dec 5, 2006, 7:56:17 PM12/5/06
to
I am trying to get Java RMI to work on a simple tutorial case. I have
these classes to use in a Hello World example:

(HelloClient.java):
import java.rmi.Naming;
public class HelloClient {
public static void main(String[] args) {
try {
HelloInterface hello = (HelloInterface)
Naming.lookup("//localhost/Hello");
System.out.println (hello.say());
} catch (Exception e) {
System.out.println ("HelloClient exception: " + e);
}
}
}

(HelloServer.java):
import java.rmi.Naming;
public class HelloServer {
public static void main(String[] args) {
try {
Hello h = new Hello ("Hello, world!");
Naming.rebind ("Hello", h);
System.out.println ("Hello Server is ready.");
} catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
}

(Hello.java):
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject implements
HelloInterface {
private String message;
/**
* Construct a remote object
* @param msg the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the object handle cannot be
constructed.
*/
public Hello (String msg) throws RemoteException {
message = msg;
}
/**
* Implementation of the remotely invocable method.
* @return the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException {
return message;
}
}

(HelloInterface.java):
import java.rmi.*;
public interface HelloInterface extends Remote {
/**
* Remotely invocable method.
* @return the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException;
}

I compiled all the classes & used rmic (>>rmic Hello) which only
created Hello_Stub.class, not Hello_Skel.class like is apparently
supposed to happen.

I can't get anywhere though since every time I start the HelloServer
I instantly get this error:
Trouble: java.rmi.ConnectException: Connection refused to host:
192.168.0.102; nested exception is:
java.net.ConnectException: Connection refused: connect

I have been trying other tutorials and on other computers but having no
luck. What am I doing wrong?

Thanks,
Jo

Bart Cremers

unread,
Dec 6, 2006, 5:17:38 AM12/6/06
to

On Dec 6, 1:56 am, "JMecc" <j...@telus.net> wrote:
> I am trying to get Java RMI to work on a simple tutorial case. I have
> these classes to use in a Hello World example:

<snip>

> compiled all the classes & used rmic (>>rmic Hello) which only
> created Hello_Stub.class, not Hello_Skel.class like is apparently
> supposed to happen.
>
> I can't get anywhere though since every time I start the HelloServer
> I instantly get this error:
> Trouble: java.rmi.ConnectException: Connection refused to host:
> 192.168.0.102; nested exception is:
> java.net.ConnectException: Connection refused: connect
>
> I have been trying other tutorials and on other computers but having no
> luck. What am I doing wrong?
>
> Thanks,
> Jo

To work with rmi you'll have to start a RMI registry before binding
objects. To start the registry you've got 2 options:

- run the 'rmiregistry' command from the command line (in the directory
where your compiled classes reside)
- start the registry in your program:
LocateRegistry.createRegistry(Registry.REGISTRY_PORT); (before binding
in the server)

The reason no skeletons are generated is because the 1.2 JRMP protocol
no longer needs them. You can still generate them by telling the rmic
compiler to do so, but I don't think you'll have to.

Regards,

Bart

JMecc

unread,
Dec 6, 2006, 10:24:19 PM12/6/06
to
Thanks Bart; I did forget to use rmiregistry and this got things going.
I read that for Windows it should be 'start rmiregistry' but that
didn't work, only 'rmiregistry &' did.

So that worked for a bit and is now back to not working:

C:\Java\RMI Sample Server>rmic SampleServerImpl
C:\Java\RMI Sample Server>java -Djava.security.policy=policy.all
SampleServerImpl
Remote exception: java.rmi.ServerException: RemoteException occurred in
server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException: SampleServerImpl_Stub
C:\Java\RMI Sample Server>dir
Directory of C:\Java\RMI Sample Server
12/06/2006 08:06 PM <DIR> .
12/06/2006 08:06 PM <DIR> ..
12/06/2006 02:54 PM 226 .classpath
12/06/2006 02:54 PM 393 .project
12/06/2006 02:55 PM 130 policy.all
12/06/2006 08:03 PM 288 policy.test
12/06/2006 03:55 PM 1,643 SampleClient.class
12/06/2006 03:55 PM 1,404 SampleClient.java
12/06/2006 02:56 PM 203 SampleServer.class
12/06/2006 02:56 PM 128 SampleServer.java
12/06/2006 08:06 PM 1,475 SampleServerImpl.class
12/06/2006 08:06 PM 1,228 SampleServerImpl.java
12/06/2006 08:06 PM 1,774 SampleServerImpl_Stub.class

It seems to work when I use 'rmiregistry &', then kill it (cntr-c),
then 'start rmiregistry', then 'rmiregistry &' again, or maybe it is
that I have to use 'rmiregistry &' within the directory of my classes I
want to set as remote. I am not sure what the correct procedure is and
so am playing around trying to get it working, but I'd like to get a
confirmed 'correct way'.

I was also trying to define a policy, but that is not going so well
either. The connection was made fine with

grant {
permission java.security.AllPermission;
};

but I think this allows others to obtain my classes (& then decompile
them...). I saw this in a tutorial:

grant {
permission java.io.filePermission "/tmp/*", "read",
"write";
permission java.net.SocketPermission
"somehost.somedomain.com:999","connect";
permission java.net.SocketPermission
"*:1024-65535","connect,request";
permission java.net.SocketPermission "*:80","connect";
};

but all I got were cannot parse file errors:

C:\Java\RMI Sample Server>java -Djava.security.policy=policy.hkbu
SampleServerImpl
java.security.policy: error parsing
file:/C:/Java/RMI%20Sample%20Server/policy.hkbu:
line 2: expected [;], found [?]
Exception in thread "main" java.security.AccessControlException: access
denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown
Source)
at java.security.AccessController.checkPermission(Unknown
Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(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)
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.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at SampleServerImpl.main(SampleServerImpl.java:33)

Is it ok to just leave the policy to all & use a wrapper class to pass
the calls with arguments through to the real classes only if the
password is correct or something like that?

Jo

0 new messages