aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Exception: Connection refused to host Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Exception: Connection refused to host" Watch "Exception: Connection refused to host" New topic
Author

Exception: Connection refused to host

pkinuk Buler
Ranch Hand

Joined: May 22, 2009
Posts: 63
Hi folks,

I did some researchs in Sun Java RMI. I copied the simple RMI example Hello World from: http://java.sun.com/j2se/1.5.0/docs/guide/rmi/hello/hello-world.html

However, it showed the following exceptions when I run the Server class in Eclipse:

Server exception: java.rmi.ConnectException: Connection refused to host: 192.168.99.125; nested exception is:
java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: 192.168.99.125; 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.bind(Unknown Source)
at example.hello.Server.main(Server.java:38)
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)
... 6 more



Would you please help me solve this issue please?

Thank you in advance.
pkinuk Buler
Ranch Hand

Joined: May 22, 2009
Posts: 63
Here are the 3 classes I used from the Sum RMI example:

The remote interface



The Server class:




The client class:




I also noticed a paragraph from the example:

Note: The call to LocateRegistry.getRegistry simply returns an appropriate stub for a registry. The call does not check to see if a registry is actually running. If no registry is running on TCP port 1099 of the local host when the bind method is invoked, the server will fail with a RemoteException.

How do I know if the registry is running?

Thank you in advance.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2416
    
    7

Hello Buler,

To get the registry working, you need to change your server class to create the registry


I used rebind rather than bind but I don't think it matter much. Also the "Hello" is suppose to be the server RMI path which looks like "rmi://<ip address>:<port> <name>". So yours should really looks like "rmi://192.168.x.x:1099 Hello".

Just to give you a pointer about RMI, if you bind with IP address on the server then the client must get the registry using IP address. If use hostname then both sides use hostname. Also for IP address binding with "127.0.0.1" vs "192.168.x.x" is a big difference. 127.0.0.1 only allow both server and client to run on same machine. 192.168.x.x allows you to run server and client on different machines - like unix server and windows client.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
pkinuk Buler
Ranch Hand

Joined: May 22, 2009
Posts: 63
K. Tsang wrote:Hello Buler,
To get the registry working, you need to change your server class to create the registry


I used rebind rather than bind but I don't think it matter much. Also the "Hello" is suppose to be the server RMI path which looks like "rmi://<ip address>:<port> <name>". So yours should really looks like "rmi://192.168.x.x:1099 Hello".

Just to give you a pointer about RMI, if you bind with IP address on the server then the client must get the registry using IP address. If use hostname then both sides use hostname. Also for IP address binding with "127.0.0.1" vs "192.168.x.x" is a big difference. 127.0.0.1 only allow both server and client to run on same machine. 192.168.x.x allows you to run server and client on different machines - like unix server and windows client.


Hello K.Tsang,

Thank you for your reply. I've solved the problem after running your code. I continued to search the Sun's RMI tutorial , I found another problem about the use of RMI.

Do I have to create and install a Security Manager when the server and client class are called? Do I need to know very well about the knowledge of IP address for this exam? e.g. "127.0.0.1" vs "192.168.x.x"

I've modified above code so that a Security Manager can be installed in both of classes' main method. The server class run fine. Unfortunately, I came up with the following exception when I run the client class:

Client exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
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.lookup(Unknown Source)
at example.hello.Client.main(Client.java:55)


Is there any method to solve it?

Server class:



Client class:


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2416
    
    7

Hi Buler,

for SCJD you can't use a security manager if you doing RMI or else you will auto fail!!!

Why would you want a security manager? My code works well and what potential problems you see using RMI? If you have doubts with RMI, do it with Sockets.

Of course deciding RMI vs Sockets is a major decision for SCJD. You should document your decisions in the choices.txt file.

For the IP thing, no you don't need to be a Cisco guy.
Bably Das
Greenhorn

Joined: Sep 17, 2009
Posts: 28

Hi all,
Searching for basic RMI tutorial led me to the same program followed by Buler. My program works fine when I call start rmiregistry.
But as start rmiregistry is not allowed in the SCJD exam , I created the registry in the Server and do a lookUp from the Client but still there is ConnectionException: Connection Refused to host:192.168.0.3........exception

My complete code is :
Hello



Server



Client


I can guess that there is problem with registry.lookup("rmi://192.168.0.3:1099 Hello") and have tried different ways of writing the URL but nothing seems to work and I am always getting the Connection Refused to host exception
Any suggestion will be greatly appreaciated.
Thanks,
Bably


SCJP 1.5, SCJD (URLyBird 1.3.2)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5273
    
  13

Hi Bably,

I would try something liketo get a reference to the stub. And as host you should pass "192.168.0.3" or "localhost".

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception: Connection refused to host