Question

I am trying to learn RMI coding and when I run server side of RMI I get connection refused. This is my server main method

public static void main(String[] args)throws Exception {
Implementation impl=new Implementation();
Naming.rebind("//localhost:2020/RMI", impl);
System.out.println("Implementation has been bind to the name RMI and is ready for use");
    }

I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code. The exception I am getting is this

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; 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.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Unknown Source)
    at epl362Project.Server.main(Server.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(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)
    ... 7 more

What am I doing wrong? both client and server sides will run on my laptop as this is just an exercise. Am I missing something or is there something wrong with my code? Please besides giving me the answer explain why it is so since I do not just want to make it work, I am trying to learn.

**EDIT I found out why my code was not working. I was missing a line of code that said

LocateRegistry.createRegistry(2020);

which I found from other questions here on stackoverflow. But there was no explanation nor did I find one online as to why this code was needed for RMI to work properly. Does anyone have an explanation?

Was it helpful?

Solution

The target of Naming.rebind() is the RMI Registry. It has to be running. It wasn't running. So you couldn't connect to it. So you got a ConnectException.

I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code.

This is both meaningless and irrelevant. Interfaces do not 'run the code'. Methods in classes run the code, and in this case the implementation class runs the code. However your problem is not in 'run[ning] the code', it is in connecting to the Registry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top