Advertisement
Shailrshah

RMI template

Apr 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. //Server.java
  2. import java.rmi.Remote;
  3. import java.rmi.Naming;
  4. import java.rmi.server.UnicastRemoteObject;
  5. interface Interf extends Remote{
  6.     public String sayHello(String name) throws Exception;
  7. }
  8. class Implementation extends UnicastRemoteObject implements Interf{
  9.     Implementation() throws Exception{
  10.         System.out.println("An instance of the Server is running.");
  11.     }
  12.     public String sayHello(String name) throws Exception{
  13.         return " Hello "+name+"!";
  14.     }
  15. }
  16. public class Server{
  17.     public static void main(String args[]){
  18.         try{
  19.             Naming.rebind("AnythingYouWant", new Implementation());
  20.         } catch(Exception e){e.printStackTrace();}
  21.     }
  22. }
  23.  
  24. //Client.java
  25. import java.rmi.Naming;
  26. import java.util.Scanner;
  27. public class Client{
  28.     public static void main(String[] args){
  29.         try{
  30.             Scanner sc = new Scanner(System.in);
  31.             Interf interf = (Interf)Naming.lookup("rmi://localhost/AnythingYouWant");
  32.             System.out.println(interf.sayHello(sc.next()));
  33.         } catch(Exception e){e.printStackTrace();}
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement