Monday, October 12, 2009

server

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


public interface inter extends Remote
{
double operate(double a, double b, int choice) throws RemoteException;
}

















import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.rmi.server.UnicastRemoteObject;
import java.net.*;


public class server extends UnicastRemoteObject implements inter
{
public static Registry registry;


public double operate(double a, double b, int choice)
{

System.out.println("The first number : " + a);
System.out.println("The first number : " + b);

if(choice == 1)
{ System.out.println((a + b)); return (a + b); }
else if(choice == 2)
{ System.out.println((a - b)); return (a - b); }
else if(choice == 3)
{ System.out.println((a * b)); return (a * b); }
else
{ System.out.println((a / b)); return (a / b); }
}

server() throws RemoteException
{}

static public void main(String[] par)
{
try
{
server s = new server();
registry = LocateRegistry.createRegistry(3333);
registry.rebind("serverop", s);
}

catch(RemoteException e)
{
System.out.println(e);
System.exit(1);
}

}

}

client

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

import java.util.Scanner;


public class client
{
public static Scanner in = new Scanner(System.in);
public static inter server;
public static Registry registry;

public static double a,
b;
public static String choice;
public static String cont;



static public void main(String[] par)
{

try
{
registry = LocateRegistry.getRegistry("localhost", 3333);
//registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue());

server = (inter)(registry.lookup("serverop"));


do
{
do
{
System.out.println("\n\tPlease select operation.");
System.out.println("\t 1. Addition.");
System.out.println("\t 2. Subtraction.");
System.out.println("\t 3. Multiplication.");
System.out.println("\t 4. Division.");

System.out.print("\tYour choice : "); choice = in.nextLine();

if(!((choice.equals("1"))||(choice.equals("2"))||(choice.equals("3"))||(choice.equals("4"))))
{ System.out.println("You have entered an invalid choice."); }

}while(!((choice.equals("1"))||(choice.equals("2"))||(choice.equals("3"))||(choice.equals("4"))));




System.out.print("\tYour first number : "); a = Double.parseDouble(in.nextLine());
System.out.print("\tYour second number : "); b = Double.parseDouble(in.nextLine());

System.out.println("The result : " + server.operate(a, b, Integer.parseInt(choice)));
System.out.println("Do you want to continue [Y / N] : "); cont = in.nextLine();




}while(cont.equals("Y")||cont.equals("y"));
}

catch(RemoteException e)
{
e.printStackTrace();
}
catch(NotBoundException e)
{
e.printStackTrace();
}


}
}