I’m new to RMI technology and facing below issue.
We have Multiple Devices of same type connected to Local System, where on each device RMI service is running on different ports.
When we are trying to connect single device to local system through RMI its working fine.
When we try to connect second device to local system, we are getting error as below –
Could you please help us to resolve below issue?
Thanks in advance.
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at com.rmi.server.RMIServer.exportAndBindObject(Unknown Source)
Demo.java
this.myRMIServer = new RMIServer(this.RMIServerPort,this.RMIClientPort, new RMISocketFactory());
this.helloWorld = new HelloWorld();
this.myRMIServer.exportObject(this.helloWorld);
this.myRMIServer.exportAndBindObject(this.rmiServiceName, this.helloWorld);
RMIServer.java
public RMIServer(int port, int rmiPort, java.rmi.server.RMISocketFactory sf)
throws RemoteException {
this.sf = sf;
this.rmiPort = rmiPort;
this.regPort = port;
synchronized (this) {
if (registry == null)
registry = LocateRegistry.createRegistry(port);
}
}
public void exportAndBindObject(String name, RemoteObject ro)
throws RemoteException, MalformedURLException {
exportObject(ro);
String url = "//127.0.0.1:" + this.regPort + "/" + name;
Naming.rebind(url, ro);
}
You are creating the Registry on
portbut binding to it onregPort, where it isn’t found.I don’t know what the purpose of
this.RMIClientPortcan possibly be. I would get rid of it. RMI servers have no business thinking about client ports.Also you’re exporting the
HelloWorldobject twice: once in Demo.java where you callexportObject(), and once inRMIServer.exportAndBindObject()where you call exportObject() again. So one of those operations must have failed, or else it didn’t export anything. So there is something wrong in yourexportObject()method.