title: Java RMI server and objective-C client
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12970505/java-rmi-server-and-objective-c-client">Java RMI server and Objective C client</a>  </p>
  </blockquote>
  
  
  
  <p>Suppose you have the following in RMI:</p>
  
  <p>This is the remote interface:</p>
  
  <pre><code>    import java.rmi.Remote;
      import java.rmi.RemoteException;
  
      public interface Hello extends Remote 
      {
           String sayHello() throws RemoteException;
      }
  </code></pre>
  
  <p>this is the server implementation code:
  (it implements the interface)</p>
  
  <pre><code>    import java.rmi.registry.Registry;
      import java.rmi.registry.LocateRegistry;
      import java.rmi.RemoteException;
      import java.rmi.server.UnicastRemoteObject;
      import java.net.*;
      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("HelloServer", stub);
          System.err.println("Server ready");
          } catch (Exception e) 
                {
                 System.err.println("Server exception: " + e.toString());
                 e.printStackTrace(); 
                }
      }
  }
  </code></pre>
  
  <p>this is the client code:</p>
  
  <pre><code>    import java.rmi.*;
      import java.rmi.server.*;//////note additional imports
      import java.io.*;
      import java.rmi.registry.LocateRegistry;
      import java.rmi.registry.Registry;
      import java.net.*;
  
      public class HelloClient2 {
      private HelloClient2() {}
      public static void main(String[] args) {
  String host = (args.length &lt; 1) ? null : args[0];
  try {/////note this addition
      RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory());     
      Registry registry = LocateRegistry.getRegistry(host);
      Hello stub = (Hello) registry.lookup("HelloServer");
      String response = stub.sayHello();
      System.out.println("response: " + response);
      }   catch (Exception e) {
      System.err.println("Client exception: " + e.toString());
      e.printStackTrace();    
      }    
    }
  }
  </code></pre>
  
  <p>Now I want to create an objective-C client connected to the rmi server. In order to be able to invoke methods on the rmi server, i created a proxy on the apache web server that embeds RMI calls in HTTP requests.</p>
  
  <p>The line in the client: RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory()); is responsible for RMI tunneling (passing RMI calls in http requests). So basically this line creates an http post request of the form </p>
  
  <pre><code>    http://localhost:80/cgi-bin/java-rmi?forward=portnum 
  </code></pre>
  
  <p>where portnum is the port of the server remote object (which is anonymous)</p>
  
  <p>what does this mean? it means there should be a web server listening on port 80 on the localhost. We have the apache tomcat web server ideally listening on 8080, but we changed its port to 80. </p>
  
  <p>That solved, we still have to handle the rest of the url. So in web apps of tomcat, we created a new directory called cgi-bin. cgi-bin has a WEB-INF directory, which contains a classes directory and a web.xml file. In the classes directory we added the compiled classes of the server and client and the servlet handler. the servlet handler receives http post requests, fetches the rmi calls and forwards them to the rmi server. </p>
  
  <p>How to make the web server know that he should call the servlet handler? in the web.xml file, we added an entry for that servlet with the name ServletHandler (name of the class) and a url mapping : /java-rmi. That way the localhost knows where to forward the request. As I said before, once the servlet handler receives the request it will deal with it and send it to the rmi server. </p>
  
  <p>now my problem is with these:</p>
  
  <pre><code>    Registry registry = LocateRegistry.getRegistry(host);
      Hello stub = (Hello) registry.lookup("HelloServer");
  </code></pre>
  
  <p>I simply don't know how to locate the registry and to look it up in objective-C to find the remote object. (stub is the local reference of the remote object) </p>
  
  <p>And I don't think it can be done using the proxy, the proxy is specifically made to handle requests directed to the rmi server. So in order to test the servlet, one needs to call a certain remote method like sayHello() which is implemented in the server. Stuff related to registry are not remote method calls.</p>
  
  <p>So how do I handle these in objective-C ? </p>