Sunday, January 12, 2014

RMI Java program 3

// RMI interface declaration Hello

    import java.rmi.Remote;
    import java.rmi.RemoteException;

    public interface Hello extends Remote {
void  sayHello(StringBuffer s) throws RemoteException;
    }


----------------------------------------------------------

//RMI Simple Server

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

    public HelloImpl() throws RemoteException {
super();
    }

    public void sayHello(StringBuffer s) {
       s = s.append("fine");

    }


    public static void main(String args[]) {
try {
   HelloImpl obj = new HelloImpl();

   // Bind this object instance to the name "HelloServer"
   Naming.rebind("HelloServer", obj);

   System.out.println("HelloServer bound in registry");
} catch (Exception e) {
   System.out.println("HelloImpl err: " + e.getMessage());
   e.printStackTrace();
}
   }
}

-------------------------------------------------------------------


//RMI Simple client


import java.rmi.Naming;
import java.rmi.RemoteException;

public class HelloClient  {

    String message = "blank";
    Hello obj = null;

    public void display() {
        try {
            obj = (Hello)Naming.lookup("HelloServer");
   StringBuffer s = new StringBuffer("hello");
            obj.sayHello(s);
   System.out.println(s);
 
        } catch (Exception e) {
            System.out.println("HelloApplet exception: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String arg[]) {
        HelloClient  c = new HelloClient();
        c.display();
    }
}


No comments: