title: Java RMI client and remote server Connection Refused to localhost
body: <p>I am not so experienced in Java RMI. I am trying to implement simple RMI communication, but faced with the problem.    </p>
  
  <p>Here my RMI client</p>
  
  <pre><code>public class MainClientApplication {
  
      public static final String FILE_NAME_RMI_POLICY = "rmi.policy";
      public static final String RMI_DOMAIN = "mydomain.net";
      public static final String RMI_ENDPOINT = "/MD5Decrypter";
      public static final String PROTOCOL = "rmi://";
  
      public static void main(String[] args) {
          System.out.println(System.getProperty("user.dir") + File.separator + FILE_NAME_RMI_POLICY);
          System.setProperty("java.security.policy", System.getProperty("user.dir") + File.separator + FILE_NAME_RMI_POLICY);
          String remoteHost = String.format("%s%s%s", PROTOCOL, RMI_DOMAIN, RMI_ENDPOINT);
          System.setSecurityManager(new SecurityManager());
          try {
              ComputeEngine computeEngine = (ComputeEngine) Naming.lookup(remoteHost);
              SwingUtilities.invokeLater(() -&gt; new MainWindow("MD5 Decryption", computeEngine));
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>Communication inerface</p>
  
  <pre><code>public interface ComputeEngine extends Remote {
      Object executeTask(Task t) throws RemoteException;
  }
  </code></pre>
  
  <p>My server </p>
  
  <pre><code>public class MainServer {
      public static final int PORT = 1099;
      public static final String RMI_ENDPOINT = "MD5Decrypter";
      public static final String FILE_NAME_RMI_POLICY = "rmi.policy";
      public static final String RMI_DOMAIN = "mydomain.net";
      public static final String PROTOCOL = "rmi://";
  
     public MainServer() {
      try {
          System.setProperty("java.security.policy", System.getProperty("user.dir") + File.separator + FILE_NAME_RMI_POLICY);
          //System.setProperty("java.rmi.server.hostname", RMI_IP);
          System.setProperty("java.rmi.server.hostname", RMI_DOMAIN);
          System.setSecurityManager(new SecurityManager());
          ComputeEngine computeEngine = new ComputeEngineExecutor();
          //  Naming.rebind(String.format("%s%s:%d/%s", PROTOCOL, RMI_DOMAIN, PORT, RMI_ENDPOINT), computeEngine);
          Naming.rebind(RMI_ENDPOINT, computeEngine);
          System.out.println("Successfully started RMI Server");
      } catch (RemoteException e) {
          e.printStackTrace();
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }
  }
  
      public static void main(String[] args) {
          MainServer mainServer = new MainServer();
      }
  
  }
  </code></pre>
  
  <p>And <code>rmi.policy</code> </p>
  
  <pre><code>grant {
      permission java.net.SocketPermission "*:1024-65535", "connect, accept";
      permission java.io.FilePermission "-", "read";
      permission java.net.SocketPermission "*:80", "connect";
  };
  </code></pre>
  
  <p>So I have started rmiregistry on my server <code>rmiregistry 1099</code>. And everything works well until calling remote object method. </p>
  
  <p>Here are my steps </p>
  
  <p>Lets consider that my domain is <code>mydomain.net</code> , I host it on my server at home with static IP.</p>
  
  <ol>
  <li>Run <code>rmiregistry</code> on the server </li>
  <li>Run server application on the server </li>
  <li>Run client application on any PC</li>
  <li>Click button to invoke remote method </li>
  </ol>
  
  <p>So it seems that everything connects and binds successfully, because if force enter invalid domain, endpoint or refused if rmiregistry is not started - <code>java.rmi.ConnectException</code> is thrown. </p>
  
  <p>But when I try to invoke any method on remote object, BTW which has been already retrieved successfully it throws a <code>java.rmi.ConnectException</code> </p>
  
  <pre><code>java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
      java.net.ConnectException: Connection refused
      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.invoke(UnicastRef.java:130)
      at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
      at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
      at com.sun.proxy.$Proxy0.executeTask(Unknown Source)
      at ui.MainWindow.lambda$initGui$0(MainWindow.java:49)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
  </code></pre>
  
  <p>As I can understand the problem is that it (server) forces me to connect to my localhost instead of remote.</p>
  
  <p>P.S on localhost everything works perfectly (both client and server on the one PC) </p>
  
  <p>Please help to solve this problem.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have forgotten to tell that I have tried to set property of <code>java.rmi.server.hostname</code>, in this case it takes a lot of time to start the server and ends with exception.</p>