简体   繁体   中英

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);
        }
    }   
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM