01.JAVA/Java2008. 12. 15. 15:08
반응형

[Calculator.java]

import java.rmi.*;

public interface Calculator extends Remote
{
 public int sum(int num1, int num2) throws RemoteException;
 public int sub(int num1, int num2) throws RemoteException;
 public int multy(int num1, int num2) throws RemoteException;
 public int division(int num1, int num2) throws RemoteException;
}



[CalculatorImpl.java]

import java.rmi.server.*;
import java.rmi.*;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator
{
 public CalculatorImpl() throws RemoteException{

 }

  public int sum(int num1, int num2) {
   return num1+num2;
  }

  public int sub(int num1, int num2) {
   return num1-num2;
  }
 
  public int multy(int num1, int num2) {
   return num1*num2;
  }
 
  public int division(int num1, int num2) {
   return num1/num2;
  }

}


[Server.java]

import java.rmi.*;

public class Server
 {
  public static void main(String[] args) throws Exception
   {
    CalculatorImpl c = new CalculatorImpl();
    Naming.rebind("Calculator",c);
    System.out.println("server ready...");
   }
 }


[Client]

 public static void main(String[] args) throws Exception
 {
  Calculator c = (Calculator)Naming.lookup("Calculator");
  int num1 = Integer.parseInt(args[0]);
  int num2 = Integer.parseInt(args[1]);

  int result1 = 0, result2 = 0, result3 = 0, result4 = 0;
  result1 = c.sum(num1, num2);
  result2 = c.sub(num1, num2);
  result3 = c.multy(num1, num2);
  result4 = c.division(num1, num2);

  System.out.println("sum : " + result1);
  System.out.println("sub : " + result2);
  System.out.println("multy : " + result3);
  System.out.println("division : " + result4);
 }
}



출처 : http://shizuku.tistory.com/category/☆%20IT/JAVA




Posted by 1010