Java RMI Calculator program using java - Code

Latest

programs

Thursday, October 6, 2011

Java RMI Calculator program using java


//Communication Interface Program
import java.rmi.*;
public interface CommuInt extends Remote
{
   public String printTime()throws RemoteException, Exception;
}

// Communication Implementation Program
import java.io.*;
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
public class CommuImpl extends UnicastRemoteObject implements CommuInt
{
     public CommuImpl() throws RemoteException
            { super(); }
     public String printTime() throws RemoteException, Exception
            {
            Date d=new Date();   
            String str="Server Time : "+d.getHours()+" : "+d.getMinutes() + " : " +d.getSeconds();
        return str;
}          }

// Communication Server Program
import java.rmi.*;
public class CommuServer
{   public CommuServer()
{     try {
                        CommuInt c = new CommuImpl();
 Naming.rebind("rmi://127.0.0.1/CommuService", c);
                        }
            catch (Exception e) {System.out.println("Trouble: " + e);}
            }
 public static void main(String args[])
            {  new CommuServer();}
}

// Communication Client Program
import java.io.*;
import java.rmi.*;
import java.net.*;
public class CommuClient
{    public static void main(String[] args) throws Exception
{          String serverName;
            if (args.length == 0)
                                    serverName = "127.0.0.1";
                        else
                                    serverName = args[0];
                        String rmiName = "rmi://" + serverName + "/CommuService";
                        System.out.println ("Connecting to: " + rmiName);
                        try {
                                     CommuInt c = (CommuInt)Naming.lookup(rmiName);
                                    String s="No Data";
                                    System.out.println(c.printTime());
            }
        catch (Exception e)
{  System.out.println("Exception Raised: "+e);     }
} }








//Authentication Interface program
import java.rmi.*;
public interface AuthInt  extends Remote
{ public String check(String Uname, String pwd) throws RemoteException, Exception; }

//Authentication Implementation program
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class AuthImpl extends UnicastRemoteObject implements AuthInt
{   public AuthImpl() throws RemoteException
{  super();  }
     public String check(String uname, String pwd) throws RemoteException, Exception
{          String str="";
                        int a=uname.compareTo("demo");
                        int b=pwd.compareTo("pwd");
                        if ( a == 0 && b == 0 )
                        {          System.out.println ("Valied User");
                                    FileInputStream fis = new FileInputStream("c:\\abc.txt");
int c;
                                    str="";
                                    while ((c=fis.read()) != -1)
                                    {  str = str + ((char)c);  }
return str;
                        }else
                        {          System.out.println ("In Valied User");
                                    str="Invalied User";
                                    return str;
}          }          }

//Authentication Server program
import java.rmi.*;
public class AuthServer {
   public AuthServer() {
try {       AuthInt a = new AuthImpl();
   Naming.rebind("rmi://127.0.0.1/AuthService", a);
                  }
            catch (Exception e) {   System.out.println("Trouble: " + e); }
   }
   public static void main(String args[]) {     new AuthServer();   }
}

//Authentication Client Program.
import java.io.*;
import java.rmi.*;
import java.net.*;
public class AuthClient
{ public static void main(String[] args) throws Exception{
            String serverName, uname, pwd;       
            if (args.length == 0)
                        serverName = "127.0.0.1";
            else
                        serverName = args[0];
            String rmiName = "rmi://" + serverName + "/AuthService";
            System.out.println ("Connecting to: " + rmiName);
        try {         AuthInt a = (AuthInt)Naming.lookup(rmiName);
                        DataInputStream dis=new DataInputStream(System.in);
                        System.out.print( "\n\tEnter User Name:" );
                        uname=dis.readLine();           
                        System.out.print( "\n\tEnter Password:" );     
                        pwd=dis.readLine();
            String s="No Data";
                         System.out.println(a.check(uname, pwd));
            }
        catch (Exception e) { System.out.println("Exception Raised: "+e);  }
} }
//Calculator Interface Program.
import java.rmi.*;
public interface CalcInt extends Remote
{    public long arith(int flag, long a, long b) throws RemoteException;  }

//Calculatro Implementation program
import java.rmi.*;
import java.rmi.server.*;
public class CalcImpl extends UnicastRemoteObject  implements CalcInt
{  public CalcImpl() throws RemoteException
    {  super(); }
    public long arith(int flag, long a, long b) throws RemoteException
    {      if (flag==1)       
            {          System.out.println ("Adding " + a + " and " + b);
                        return a + b;
            }
            else if(flag==2)
             {        System.out.println ("Subtracting " + b + " from " + a);
                         return a - b;
            }
            else if(flag==3)
            {          System.out.println ("Multiplying " + a + " by " + b);
                        return a * b;
            }
       else if(flag==4)
            {         System.out.println ("Dividing " + a + " by " + b);
                        return a / b;
            }
            return 0;
} }

// Calculator Server Program
import java.rmi.*;
public class CalcSer
{   public CalcSer()
     {     try {
                        CalcInt c = new CalcImpl();
Naming.rebind("rmi://127.0.0.1/CalculatorService", c);
                   }
catch (Exception e) {System.out.println("Exception Raised: " + e);}
   }
   public static void main(String args[])
   { new CalcSer();}
}

// Calculator Client Program.
import java.rmi.*;
import java.rmi.registry.*;
import java.awt.*;
import java.awt.event.*;
public class CalcCli extends Frame implements ActionListener
{
            Button B1=new Button("Sum");
            Button B2=new Button("Subtract");
            Button B3=new Button("Multiply");
            Button B4=new Button("Divide");
            Label l1=new Label("Number 1");
            Label l2=new Label("Number 2");
            Label l3=new Label("Result");
            TextField t1=new TextField(20);
            TextField t2=new TextField(20);
            TextField t3=new TextField(20);
            int val;
           


public CalcCli()
            {
                        super("Calculator");
                        val=0;
                        setLayout(null);
                        l1.setBounds(20,50,55,25);                 add(l1);
                        l2.setBounds(20,100,55,25);               add(l2);
                        l3.setBounds(20,150,55,25);               add(l3);
                        t1.setBounds(150,50,100,25);                        add(t1);
                        t2.setBounds(150,100,100,25);          add(t2);
                        t3.setBounds(150,150,100,25);          add(t3);
                        B1.setBounds(20,200,80,25);              add(B1);
                        B2.setBounds(100,200,80,25);                        add(B2);
                        B3.setBounds(180,200,80,25);                        add(B3);
                        B4.setBounds(260,200,80,25);                        add(B4);
                        B1.addActionListener(this);
                        B2.addActionListener(this);
                        B3.addActionListener(this);
                        B4.addActionListener(this);
                        addWindowListener(
                                    new WindowAdapter()
                                                {          public void windowClosing(WindowEvent e)
                                                            { System.exit(0);}
                                                });
            }
            public void actionPerformed(ActionEvent AE)
            {                      if(AE.getSource()==B1)
                                    {          val=1;  }
                                    else if(AE.getSource()==B2)
                                    {          val=2;  }
                                    else if(AE.getSource()==B3)
                                    {          val=3;  }
                                    else if(AE.getSource()==B4)
                                    {          val=4;  }
                                    fun();
            }
            public void fun()
            {          int i=Integer.parseInt(t1.getText());
                        int j=Integer.parseInt(t2.getText());
                        long result;
                        try{
                                    String rmiName = "rmi://127.0.0.1/CalculatorService";
                                    CalcInt MI=(CalcInt)Naming.lookup(rmiName);
                                    result=MI.arith(val,i,j);
                                    t3.setText(""+result);
                           }
                        catch(Exception ex)
                        {System.out.println("Exception:"+ex);           }
            }
            public static void main(String args[])
            {          CalcCli MC=new CalcCli();
                        MC.setVisible(true);
                        MC.setSize(600,500);
            }
}

No comments:

Post a Comment