3 Replies Latest reply on Jul 31, 2013 10:14 AM by EJP

    Why "Connection refused to host"  in registry.rebind() ?

    pvbemmel

      My program TestBind0 (code below) tries to find/create a registry and bind an object.

      Find/create: it first tests if there is already a registry on that port; if not, then it tries to create one.

      The program tries to find/create the registry on ports 40654, 50876, 30321, 33445, 1099, in this order, until it succeeds in both creating the registry and binding the object.

      Why does TestBind0 throw for each attempt

      java.rmi.ConnectException: Connection refused to host: 192.168.1.64; 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 TestBind0.tryPort(TestBind0.java:48)
           at TestBind0.main(TestBind0.java:21)
      Caused by: java.net.ConnectException: Connection refused: connect
           at java.net.PlainSocketImpl.socketConnect(Native Method)
           at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
           at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
           at java.net.Socket.connect(Socket.java:519)
           at java.net.Socket.connect(Socket.java:469)
           at java.net.Socket.(Socket.java:366)
           at java.net.Socket.(Socket.java:180)
           at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
           at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
           at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
           ... 6 more

      (note: 192.168.1.64 is my localhost)

      in

      reg.rebind("TestBind0", obj);

      even when I have specified -Djava.security.policy==all.policy , with file all.policy in the current dir, containing

      grant {
        permission java.security.AllPermission;
      };
      

      The program is run using command

      java -cp bin -Djava.security.policy==all.policy TestBind0

      The code:

      import java.rmi.*;
      import java.rmi.registry.*;
      import java.rmi.server.*;
      import java.util.*;
      
      public class TestBind0 extends UnicastRemoteObject implements Counter {
        private static final long serialVersionUID = 1L;
        protected int count;
      
        protected TestBind0() throws RemoteException {
          super();
          count = 0;
        }
        public static void main(String[] args) {
          List ports = Arrays.asList(40654, 50876, 30321, 33445, 1099);
      
          Integer successPort = null;
          for(int port : ports) {
            boolean success = tryPort(port);
            if(success) {
              successPort = port;
              break;
            }
          }
          System.out.println("successPort:" + successPort);
          System.out.flush();
        }
        public static boolean tryPort(int port) {
          Registry reg = null;
          try {
            reg = LocateRegistry.getRegistry(port);
          }
          catch (RemoteException e) {
            try {
              reg = LocateRegistry.createRegistry(port);
            }
            catch (RemoteException e1) {
            }
          }
          if(reg==null) {
            return false;
          }
          boolean success = false;
          try {
            TestBind0 obj = new TestBind0();
            reg.rebind("TestBind0", obj);   // line 48
            success = true;
          }
          catch (AccessException e) {
            e.printStackTrace();
          }
          catch (RemoteException e) {
            e.printStackTrace();
          }
          return success;
        }
        public int getCount() throws RemoteException {
          return count++;
        }
      }
      
        • 1. Re: Why "Connection refused to host"  in registry.rebind() ?
          EJP

          > it first tests if there is already a registry on that port; if not, then it tries to create one.

           

          That's back to front. You should try to create one with LocateRegistry.createRegistry(), then if that fails use LocateRegistry.getRegistry() on the registry that you now know to exist.

           

          You are assuming that `getRegistry()` throws an exception if the registry doesn't exist. It doesn't do that. It just constructs a Registry stub. It doesn't do anything on the network at all.

          • 2. Re: Why "Connection refused to host"  in registry.rebind() ?
            pvbemmel

            Thanks for your reply. I guess that explains the behaviour.

            It's not what I expected, but I'm learning all the time.

             

            If I do createRegistry() , and that fails, does that guarantee that a registry on that port exists?

            There may be other reasons why createRegistry() fails: most likely another application already is using the port.

             

            How can I be sure a registry exists on some specified port ?

             

            I guess the answer to that can be found in

            How to know if RMI server is running or not

             

            As for your remark

              that's back to front

            some background info:

             

            The program TestBind is a simplified program, to test feasibility of following program functionality:

             

              Program A starts, and shows main window; the main window can be "closed" while program

              keeps running. From system tray the main window can be opened again.

             

              If user starts another program A, it is assumed that he forgot he has already one running;

              that second run must tell the first run to show its window,  and then the second run must

              simply exit.

             

            The second run uses RMI to tell the first run to show its window.

            Each run must check if there is already another run, and find the remote object of that other

            run, and call the remote object's method setVisible() .

            I could hardcode the registry port to be 1099 . But there may be another java application that

            I don't know about, that started a registry on 1099 ; if I'd use that registry, I would have to tell

            it where to find the classes particular to my application A; and with my limited experience

            with rmi , I don't know how to do that.

            • 3. Re: Why "Connection refused to host"  in registry.rebind() ?
              EJP

              > There may be other reasons why createRegistry() fails: most likely another application already is using the port.

               

              Correct.

               

              > How can I be sure a registry exists on some specified port ?

               

              The best way to test whether any resource is available is to try to *use* it. In this case, Registry.list(), Registry.lookup(), Registry.bind()/rebind()/unbind().

               

              I guess the answer to that can be found in

              How to know if RMI server is running or not

               

              Not really except as above. "Don't test, use".

              1 person found this helpful