title: How to make a RMI server work for both local clients (within the same network) and external clients
body: <p>I'm pretty new to Java and RMI, and I decided to try to set up my own server for a chat client on my home computer (just for learning purposes, no real use intended for it) . To sum it up, I'm using a socket connection for messages and RMI for communication between the server and the client . The problem is I'm using a router and I want the client to work both for external connections and internal connections (my laptop for example- connected to the same router as the server).</p>
  
  <hr>
  
  <p>My public ip is: <strong>109.99.33.251</strong> <br>
  My local ip adress is:  <strong>196.168.1.9</strong> <br>
  The problem is that if I set the java.rmi.server.hostname  to</p>
  
  <ul>
  <li>Server local ip : 
  
  <ul>
  <li>everything works just as intended, but only when accessing the server from local machines. </li>
  <li>when accessing from an external machine, only the socket connects , the rmi lookup throws connection refused: connection timed out. (I want to mention that I'm 99% sure the port forwarding is properly set for both the RMI (1099) and the server (5000) )</li>
  </ul></li>
  <li>Server public ip: everything works, but just for external machines. 
  <ul>
  <li>when trying to connect from a local machine with the local ip of the server the socket connects fine, but the rmiregistry lookup method fails. </li>
  <li>when connecting from a local machine with the public ip of the server, neither the RMIregistry nor the socket connects. </li> </ul> </ul>
  
  <p><br> <br> My guess is that it's a problem with the router. I think it doesnt support <a href="https://en.wikipedia.org/wiki/Network_address_translation#NAT_loopback" rel="nofollow">NAT loopback</a> (Accessing the server from a LAN component with it's public ip) since it never works for me to connect to the server it's public ip from a local machine, even though the port forwarding is set up correctly (it works when connecting externally for both the RMI and socket connections). <br>Is there anything I can do to make this work?
  <BR> <BR>
  EDIT: 
  On the server side, I set <code>System.setProperty("java.rmi.server.hostname","109.99.33.251");</code> just after the server starts and then I:
  <code> LocateRegistry.createRegistry(1099); </code></li>
  </li>
  
  <br> 
  <br> 
  then i bind the object:</p>
  
  <pre><code>MyRemote stub= new Communicator();
  registry = LocateRegistry.getRegistry();
  registry.bind("comm", stub);
  </code></pre>
  
  <p>where MyRemote is the interface that extends Remote interface and 
  Communicator is: <br></p>
  
  <pre><code>public class Communicator extends UnicastRemoteObject implements MyRemote
  {
      public Communicator() throws RemoteException
      {
          super(Registry.REGISTRY_PORT);
          System.out.println("I've created the host");
      }
      //the methods in the MyRemote
  }`
  </code></pre>
  
  <p><br> <br>
  For the sockets i use: <br></p>
  
  <pre><code>ServerSocket serverSock = new ServerSocket(5000);
  while(true) 
  {
      Socket clientSocket = serverSock.accept();
      ClientHandler handler =new ClientHandler(clientSocket);
      clients.add(handler);
      Thread t = new Thread(handler);
      t.start();
      System.out.println("got a connection");
  }
  </code></pre>
  
  <p><br><br>
  For the client lookup:
  <code>service=(MyRemote) Naming.lookup("rmi://"+serverIp+"/comm");</code> <br>
  and for the socket connection <code>Socket socket=new Socket(serverIp,new Integer(serverPort));</code>
  <br> <code>serverIp</code> holds either the public ip of the server, or the local one, depending on the case i presented earlier. and <code>serverPort</code> is 5000<br> <br></p>
  
  <hr>
  
  <p>To make myself clear, I want the client to work on both local and external machines, so I am looking for a way to bind the remote objects and set up the connections to make both work, if that's possible. <br> <br>
  NOTE: I'm sorry, I'm new to networking , java and this site, so please don't mind the mistakes I make when asking questions. Thank you in advance!</p>
  
  <p>FINAL EDIT: I've contacted my ISP about this and they said the router has its latest firmware version and it simply doesn't support NAT loopback.</p>