Remote Server Client Programs - rsanchez-wsu/jfiles GitHub Wiki

Remote Server Program

Remote Server Program

As with CORBA, the client needs a way to find the remote object. We will use Sun bootstrap registry service. This is a global naming service, so name clashes are a problem. This is not recommended for registering small objects (like our calculator). The bootstrap registry service should be used to locate other locator objects.

  • Need “import java.rmi.*”

  • Need “import java.rmi.server.*”

  • Need “import javax.naming.*”
    Instantiate an implementation object.
    CalculatorImpl calc = new CalculatorImpl();

  • Create a naming context.
    Context namingContext = new InitialContext();

  • Register the object with the naming service.
    namingContext.bind("rmi:calculator", calc);

  • Do this all in a try-catch block.

    package calc;
    import java.rmi.;
    import java.rmi.server.
    ;
    import javax.naming.*;
    public class CalcServer
    {
    //Method main() goes here.
    }

Example

public static void main(String args[])  
{  
    try  
    {  
        CalculatorImpl calc = new CalculatorImpl();  
        Context namingContext = new InitialContext();  
        namingContext.bind("rmi:calculator", calc);  
        System.out.println("Remote CalcServer ready...");  
    }  
    catch (Exception e)  
        { System.out.println("Exception while registering: " + e); }  
    } //End main.  

Running the Server

  • Start the RMI registry.
    UNIX: rmiregistry &
    Windows: start rmiregistry
  • Start the server
    UNIX: java CalcServer &
    Windows: start java CalcServer
  • Using a batch file:
    start rmiregistry
    pause
    start java calc.CalcServer
    pause

Control-C to exit

Remote Client Program

  • Need “import java.rmi.*”

  • Need “import java.rmi.server.*”

  • Need “import javax.naming.*”

  • Create a naming context.
    Context namingContext = new InitialContext();

  • Look up the object with the naming service.
    namingContext.lookup("rmi://localhost/calculator”);

  • Do this all in a try-catch block.

  • Look up the object with the naming service.

  • Extend url for a remote system.
    namingContext.lookup(
    "rmi://unixapps1.wright.edu:2024/calculator”);

  • Wrap all methods with
    “try -- catch(RemoteException e)”

  • This includes any methods using the remote object.
    e.g. the GUI, even though in a separate class.

  • Requires import java.rmi.*;

    package calc;
    import java.rmi.;
    import java.rmi.server.
    ;
    import java.rmi.*;
    public class RunCalc2Gui
    {
    //Method main() here.
    }//RunCalc2Gui

Example

public static void main(String args[])  
{  
    Calculator c = null;  
    String hostname = null;  
    String path = null;  
    if (args.length < 1)  
    {  
      System.out.println(  
      "Usage: java CalculatorClientR <hostname> (<port>)");  
      System.exit(1);  
    }  

**optional**  

    hostname = args[0];  
    path = "rmi://"+hostname+"/";  
    if (args.length > 1)  
        path = path+":"+args[1];  
    path = path+"calculator";  
    System.out.println("Looking up "+path);  
    try { c = (Calculator)namingContext.lookup(path); }  
    catch (Exception e)  
    {  
      System.out.println("Lookup error.");  
      System.exit(1);  
    }  
    //Same as before:  
    Calc2Gui ui = new Calc2Gui(c);  
}//main  

This information was taken from Dr. Hartrum's slides on RMI.

⚠️ **GitHub.com Fallback** ⚠️