title: Java Rmi UnknownHostException
body: <p>I am trying to write a simple RMI file but for some reason it is giving me an <em>unknown host name</em> exception. I have tried simply everything before actually asking a question. Here is the code for the project.</p>
  
  <p>RMI interface class:</p>
  
  <pre><code>import java.rmi.Remote;
  import java.rmi.RemoteException;
  
  public interface Hello extends Remote {
      String sayHello() throws RemoteException;
  }
  </code></pre>
  
  <p>Server Class:</p>
  
  <pre><code>import java.rmi.registry.Registry;
  import java.rmi.registry.LocateRegistry;
  import java.rmi.RemoteException;
  import java.rmi.server.UnicastRemoteObject;
  
  public class Server implements Hello {
  
      public Server() {}
  
      public String sayHello() {
          return "Hello, world!";
      }
  
      public static void main(String args[]) {
  
          try {
              Server obj = new Server();
              Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
  
              // Bind the remote object's stub in the registry
              Registry registry = LocateRegistry.getRegistry();
              registry.bind("Hello", stub);
  
              System.err.println("Server ready");
          } catch (Exception e) {
              System.err.println("Server exception: " + e.toString());
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Client class:</p>
  
  <pre><code>import java.rmi.registry.LocateRegistry;
  import java.rmi.registry.Registry;
  
  public class Client {
  
      private Client() {}
  
      public static void main(String[] args) {
  
          String host = (args.length &lt; 1) ? null : args[0];
          try {
              Registry registry = LocateRegistry.getRegistry(host);
              Hello stub = (Hello) registry.lookup("Hello");
              String response = stub.sayHello();
              System.out.println("response: " + response);
          } catch (Exception e) {
              System.err.println("Client exception: " + e.toString());
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>When I run this over localhost this runs completely fine but for the life of me I cannot get this to run over the network across two machines. I've tried:</p>
  
  <ul>
  <li>Connecting the machines over Wifi.</li>
  <li>Connecting over ethernet so they have addresses like <code>192.168.1.1</code> and <code>192.168.1.2</code> respectively. Both machines can ping each other fine. If I <code>nma -PN</code> the server from the client I can see that port <code>1099</code> is open and the <code>rmiregistry</code> is running. But not matter what combination of hostnames I try it still doesn't connect the client to the server.</li>
  </ul>
  
  <p>Combinations I've tried for input as host name on client side:</p>
  
  <pre><code>rmi://192.168.1.1
  rmi://192.168.1.1:1099
  </code></pre>
  
  <p>And maybe tens more with all possible slashes etc. still no clue why this isn't working (even though it works perfectly over localhost). Any ideas would be welcome.</p>
  
  <p>For completeness here is the actualy error I get:</p>
  
  <pre><code> java -classpath classes/ Client
  Client exception: java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
      java.net.UnknownHostException: rmi://192.168.1.1
  java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
      java.net.UnknownHostException: rmi://192.168.1.1
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:598)
      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.lookup(Unknown Source)
      at Client.main(Client.java:14)
  Caused by: java.net.UnknownHostException: rmi://192.168.1.1
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:529)
      at java.net.Socket.connect(Socket.java:478)
      at java.net.Socket.&lt;init&gt;(Socket.java:375)
      at java.net.Socket.&lt;init&gt;(Socket.java:189)
      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)
      ... 5 more
  </code></pre>