-1
java.rmi.ConnectException: Connection refused to host: 10.0.0.100; nested exception is: 
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at guiproject.AddClient.main(AddClient.java:12)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 6 more

import java.rmi.*;

public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws Exception;
}

import java.rmi.*;
import java.rmi.server.*;    

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{    

public AddServerImpl() throws RemoteException{
}
    public double add(double d1, double d2) throws RemoteException{
        return d1 + d2;
    }

public static void main(String args[]){

}
}

public class AddServer {        
public static void main(String args[]){
    try{
        AddServerImpl addServerImpl = new AddServerImpl();
        Naming.rebind("AddServer", addServerImpl);
    }
    catch(Exception e){
        e.printStackTrace();
    }
 }
}

//the client side
public class AddClient {

public static void main(String args[]){
    try{            
        Scanner in = new Scanner(System.in);           
        String addServerURl = "rmi://" + "/AddServer";
        AddServerIntf addServerintf = (AddServerIntf)Naming.lookup(addServerURl);
        System.out.println("The first Numer is :");
        double d1 = in.nextDouble();
        System.out.println("The second number is:");
        double d2 = in.nextDouble();

        System.out.println("The sum is:" +addServerintf.add(d1,d2));            
    }
    catch(Exception e){
        e.printStackTrace();
    }
 }
}

Each time i connect to my client side i get this exception. I read some stuffs about the Connection exception but none of the solution seems to work. // The Code also is pasted above as requested , i hope this works...

Rhyno_xD
  • 13
  • 1
  • 7
  • Which java version are you using? Use the command `java -version` – trylimits Nov 18 '14 at 20:07
  • @trylimits java version "1.8.0_25" Java(TM) SE Runtime Environment (build 1.8.0_25-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode) thats what i got – Rhyno_xD Nov 18 '14 at 20:17
  • Please clean up this non-compiling mess. Imports in the middle of classes? Empty `main()` methods? And what do you mean by 'connect to my client side'? RMI clients connect to RMI servers, not the other way around. – user207421 Nov 19 '14 at 07:57
  • @EJP those classes are all on different files. – Rhyno_xD Nov 19 '14 at 08:37
  • @Rhyno_xD A class that starts with a `class` declaration and *then* contains some `import` statements will not compile. I've already said that. Please fix it. – user207421 Nov 19 '14 at 08:54
  • @EJP i see that, its not like that in my code, i must have made the mistake when i was add the imports here. – Rhyno_xD Nov 19 '14 at 10:55
  • So, for the third time, please fix it. – user207421 Nov 19 '14 at 20:43

2 Answers2

1

As you have not posted any code, I assume that the reason for the exception is not caused by the implementation itself.

This exception usually occurs if the remote host does not respond. Possible reasons could be:

  • You have not started rmiregistry.
  • Newer versions of Java (afaik since 7u21) require the following command: rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false
  • The firewall is blocking the connection
trylimits
  • 2,575
  • 1
  • 22
  • 32
  • i started the rmiregistry but still the same. – Rhyno_xD Nov 18 '14 at 20:35
  • Did you start `rmiregistry` on the server side? Did you start it before starting the server application? Could you please post some code in your original question? – trylimits Nov 18 '14 at 21:09
0
String addServerURl = "rmi://" + "/AddServer";

You're trying to connect to the wrong Registry. Unless server and client are on the same host, in which case you hardly need RMI at all, this line should be:

String addServerURl = "rmi://" + serverHost + "/AddServer";

where serverHost is the IP address or hostname where the RMI server and Registry are running.

user207421
  • 305,947
  • 44
  • 307
  • 483