This discussion is archived
14 Replies Latest reply: Mar 19, 2012 5:38 PM by EJP RSS

java.rmi.ConnectException: Connection refused to host: Connection timed out

870997 Newbie
Currently Being Moderated
OK, I am getting the dreaded java.rmi.ConnectException: Connection refused to host: Connection timed out

I've been googling for solutions for a while and no success, although lots of people have been in this boat before.

java.rmi.ConnectException: Connection refused to host: 169.254.105.9; nested exception is:
     java.net.ConnectException: Connection timed out: connect

I can ping the server from the client. I can telnet to 169.254.105.9 1099 just fine. I have disabled all the firewalls I can think of - although corporate policy is we have to run McAfee and I seem to have no control over that. When I run the client and server on the same system using localhost everything works fine.

Can anyone think of any other things I could try to figure out why my RMI won't connect? Are there any more diagnostics I can try?

Cheers, Eric
  • 1. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    What port is your remote object exported on? And have you opened that port in your firewall or NAT?
  • 2. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    Everything should be at the RMI defaults - so I am assuming port 1099.

    I have taken down my firewalls, but NAT might be a problem. I am testing through VPN from home to work, so there is no telling what effect the tunneling may have. Maybe I need to test closer to home with two machines on the same LAN first. Ultimately the scenario I am testing in has to work because there is no telling how our customers may use our product.

    Is there some secret conspiracy in the world to prevent RMI from working? (just kidding)

    Cheers, Eric
  • 3. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    There is no default answer to the question I asked. Port 1099 is used by the Registry. Your own remote objects are exported with a zero port number unless you specify otherwise, and zero tells the system to allocate a dynamic port.
  • 4. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    Ahhh - so if you have firewall issues, then you need to explicitly declare a port that you can open up in the firewall?

    Are there any conventions around this? Is there a particular port most people choose as a best practice?

    Cheers, Eric
  • 5. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    If you start your Registry in the server JVM via LocateRegistry.createRegistry() you can use 1099, and it solves various other problems too, such as the Registry's CLASSPATH, stopping the Registrty, etc.
  • 6. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    Yes, my server already calls LocateRegistry.createRegistry(1099)

    I tried creating my remote object on a specific port, but then all hell breaks loose

    java.rmi.server.ExportException: Listen failed on port: 81; nested exception is:
         java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
         at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:312)
         at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:218)
         at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:393)
         at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:129)
         at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:190)
         at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:293)
         at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:235)
         at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:133)

    Cheers, Eric
  • 7. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    You tried exporting it on port 81, which was already in use, or disallowed. Try port 1099 as I suggested above.
  • 8. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    OK, I solve the problem with

    java.rmi.server.ExportException: Listen failed on port: 81; nested exception is:
    java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind

    Dam legacy code was creating multiple instances of the remote object - so I fixed it so that only on instance is created. This was not a showstopper before because each instance was getting a new anonymous port.

    Now my problem is

    rmiUrlString = rmi://150.247.94.130:1099/ColorStripDataManager
    java.rmi.ConnectException: Connection refused to host: 169.254.105.9; nested exception is:

    I cannot figure out why it is attempting a connection to 169.254.105.9 when that is not in the URL. Is this some sort of NAT issue?

    Cheers, Eric

    Edited by: Eric Kolotyluk on Mar 18, 2012 9:21 PM
  • 10. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    calling System.setProperty("java.rmi.server.hostname", "150.247.94.130");

    has no affect. I call this on both the client and server side before I make any calls to the RMI APIs. I still get

    rmiUrlString = rmi://150.247.94.130:1099/ColorStripDataManager
    java.rmi.server.hostname = 150.247.94.130
    java.rmi.ConnectException: Connection refused to host: 169.254.105.9; nested exception is:
         java.net.ConnectException: Connection timed out: connect

    It looks like I need stronger medicine than that.

    It turns out that 169.254.105.9 is used by VMWare on my RMI server system. I have no idea why something in RMI-land is latching on to that.

    A temporary fix is to disable those other addresses in Windows - but that does not solve the larger problem of why the fix described in http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/faq.html#domain does not work.

    Cheers, Eric

    Edited by: Eric Kolotyluk on Mar 19, 2012 12:13 AM
  • 11. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    calling System.setProperty("java.rmi.server.hostname", "150.247.94.130");

    has no effect. I call this on both the client and server side before I make any calls to the RMI APIs. I still get
    Set it at the server only. That's what it's for. It affects what IP address is placed into the stub when you export. So you need to call it before you construct/export any remote objects, and that probably includes the Registry.
  • 12. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    OK - now I have it working. Calling System.setProperty("java.rmi.server.hostname", "150.247.94.130"); before instantiating the remote object did the trick.

    I have truly learned more about RMI than I ever wanted to know. In particular it can be rather nasty to ingrate RMI into a legacy application as opposed to designing it in from the beginning.

    Thanks so much for the help - it really feels good to get something working.

    Cheers, Eric
  • 13. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    870997 Newbie
    Currently Being Moderated
    h1. Epilog

    Here is some code I wrote the saves a whole log of trouble. It is one of the first things I do in the main() method of my application. I cannot believe this issue and the solution I came up with are not more predominant in my Google searches. For that matter, a better implementation of RMI should do this by default.
    // This whole song and dance is necessary in order to figure out our Global Address so
    // Java Remote Method Invocation works correctly. EK
    // See http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/faq.html#domain
    
    String hostname = InetAddress.getLocalHost().getHostName();
    System.out.println("Searching for global address of " + hostname);
    
    String globalIp4address = null;
    String globalIp6address = null;
    
    InetAddress [] inetAddresses = InetAddress.getAllByName(hostname);
    for (InetAddress inetAddress : inetAddresses) {
        System.out.println("hostname is " + inetAddress.getHostName());
        System.out.println("address  is " + inetAddress.getHostAddress());
        boolean isSiteLocalAddress = inetAddress.isSiteLocalAddress();
        boolean isLinkLocalAddress = inetAddress.isLinkLocalAddress();
        boolean isLoopbackAddress = inetAddress.isLoopbackAddress();
        boolean isReachable = inetAddress.isReachable(10000);
        System.out.println("isSiteLocalAddress  = " + isSiteLocalAddress);
        System.out.println("isLinkLocalAddress  = " + isLinkLocalAddress);
        System.out.println("isLoopbackAddress   = " + isLoopbackAddress);
        System.out.println("isReachable         = " + isReachable);
        if (isReachable && !isSiteLocalAddress && !isLinkLocalAddress && !isLoopbackAddress) {
            if (inetAddress instanceof Inet4Address) {
                globalIp4address = inetAddress.getHostAddress();
            }
            if (inetAddress instanceof Inet6Address) {
                globalIp6address = inetAddress.getHostAddress();
            }
        }
    }
    
    // It is extremely important that this be set correctly or you can waste endless hours
    // troubleshooting why your RMI application does not work. EK
    System.setProperty("java.rmi.server.hostname", globalIp4address);
    System.out.println("java.rmi.server.hostname = " + System.getProperty("java.rmi.server.hostname"));
    Edited by: Eric Kolotyluk on Mar 19, 2012 9:21 AM
  • 14. Re: java.rmi.ConnectException: Connection refused to host: Connection timed out
    EJP Guru
    Currently Being Moderated
    a better implementation of RMI should do this by default.
    A better implementation of RMI wouldn't need to do it at all. TCP knows the local interface by which an incoming Registry lookup (or indeed any remote method call) was received. All that is required is to put that address into the stub for that caller. It's the address he connected to.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points