mihsathe
mihsathe

Reputation: 9154

Java RMI : connection refused

I have written following code for the client of RMI. But getting

java.rmi.ConnectException: Connection refused to host: localhost; nested 
exception is:
    java.net.ConnectException: Connection refused: connect

code :

import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;

class client
{
    public static void main(String [] ars)
    {
        Iface serv;
        Registry r;
        String serveraddr = ars[0];
        String serverport = ars[1];
        String text = "Hey jude";

        System.out.println("Sending" + text);

        try{
            r = LocateRegistry.getRegistry(
            serveraddr,
            (new Integer(serverport)).intValue()
            );
            serv = (Iface) r.lookup("rmi://server");

            serv.receive(text);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }   
}

Upvotes: 3

Views: 10391

Answers (2)

user207421
user207421

Reputation: 311054

If you're getting that on bind, rebind, or lookup, the Registry isn't running. If you get it doing the remote call, see item A.1 in the RMI FAQ supplied with the Javadoc, and if you're running Linux also check that your /etc/hosts file maps 127.0.0.1 to localhost and your real ip address to your real hostname - this has been a common problem in some Linux distributions.

Upvotes: 11

eric-haibin-lin
eric-haibin-lin

Reputation: 377

I met the same problem. It's silly but just that I forgot to start the RMI registry process.

So, you also need to run RMI Registry process

rmiregistry 

Before you try to rebind(address, obj) with RMI registry.

Upvotes: 2

Related Questions