• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception: Connection refused to host

 
pkinuk Buler
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pkinuk Buler
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Roel De Nijs
Sheriff
Posts: 9091
73
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic