REMOTE PROCEDURE CALL

 

 REMOTE PROCEDURE CALL

remote procedure call

Implementation of Remote Procedure Calls

  • RPC for simple calculator

  • RPC for Math Functions

  • RPC to find the factorial

  • RPC to find the Fibonacci Series

Introduction to Remote Procedure Calls:

Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-server based applications. It is based on extending the conventional local procedure calling so that the called procedure need not exist in the same address space as the calling procedure. The two processes may be on the same system, or they may be on different systems with a network connecting them.

What happens when we make a Ramote Procedure Calls ?

remote procedure call

1. The calling environment is suspended, procedure parameters are transferred across the network to the environment where the procedure is to execute, and the procedure is executed there.

2. When the procedure finishes and produces its results, its results are transferred back to the calling environment, where execution resumes as if returning from a regular procedure call.


How Remote Procedure Call works ?

remote procedure call

Remote Procedure Call For Calculator :

SOURCE CODE :

Client.java

import java.io.*;
import java.net.*;
class calc_client{
    calc_client()
    {
        try
        {
            InetAddress objIA = InetAddress.getLocalHost();
            DatagramSocket objDS = new DatagramSocket();
            DatagramSocket objDS1 = new DatagramSocket(1300);
            System.out.println("\nRPC Client\n");
            System.out.println("Enter method name and parameter like add 3 4\n");
            while (true)
            {
                BufferedReader objBr = new BufferedReader(new
                InputStreamReader(System.in));
                String strInput = objBr.readLine();
                byte b[] = strInput.getBytes();
                DatagramPacket objDP = new DatagramPacket(b, b.length, objIA,1200);
                objDS.send(objDP);
                objDP = new DatagramPacket(b, b.length);
                objDS1.receive(objDP);
                String s = new String(objDP.getData(), 0, objDP.getLength());
                System.out.println("\nResult = " + s + "\n");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        new calc_client();
    }
}




Server.java

import java.util.*;
import java.net.*;

class calc_server
{
    DatagramSocket objDS;
    DatagramPacket objDP;
    String strInput, strMethodName, strResult;
    int intVal1, intVal2;
    calc_server()
    {
        try
        {
            objDS=new DatagramSocket(1200);
            byte b[]=new byte[4096];
            while(true)
            {
                objDP=new DatagramPacket(b, b.length);
                objDS.receive(objDP);
                strInput=new String(objDP.getData(), 0, objDP.getLength());
                if(strInput.equalsIgnoreCase("q"))
                {
                    System.exit(1);
                }
                else
                {
                    StringTokenizer st = new StringTokenizer(strInput," ");
                    int i=0;
                    while(st.hasMoreTokens())
                    {
                    String token=st.nextToken();
                    strMethodName=token;
                    intVal1 = Integer.parseInt(st.nextToken());
                    intVal2 = Integer.parseInt(st.nextToken());
                    }
                }
                System.out.println(strInput);
                InetAddress ia = InetAddress.getLocalHost();
                if(strMethodName.equalsIgnoreCase("add"))
                {
                    strResult= "" + add(intVal1,intVal2); 
                }
                else if(strMethodName.equalsIgnoreCase("sub")
                {
                    strResult= "" + sub(intVal1,intVal2);
                }
                else if(strMethodName.equalsIgnoreCase("mul"))
                {
                    strResult= "" + mul(intVal1,intVal2);
                }
                else if(strMethodName.equalsIgnoreCase("div"))
                {
                    strResult= "" + div(intVal1,intVal2);
                }
                byte b1[]=strResult.getBytes();
                DatagramSocket objDS1 = new DatagramSocket();
                DatagramPacket objDP1 = new DatagramPacket(b1, b1.length,
                InetAddress.getLocalHost(), 1300);
                System.out.println("Result : " +strResult+ "\n");
                objDS1.send(objDP1);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public int add(int val1, int val2)
    {
        return val1+val2;
    }

    public int sub(int val3, int val4)
    {
        return val3-val4;
    }

    public int mul(int val3, int val4)
    {
        return val3*val4;
    }

    public int div(int val3, int val4)
    {
        return val3/val4;
    }

    public static void main(String[] args)
    {
        new calc_server();
    }
}


OUTPUT


Remote Procedure Call For Math Functions :

SOURCE CODE :

Client.java

import java.io.*;
import java.net.*;

class math_client
{
    math_client()
    {
        try
        {
            InetAddress objIA = InetAddress.getLocalHost();
            DatagramSocket objDS = new DatagramSocket();
            DatagramSocket objDS1 = new DatagramSocket(1300);
            System.out.println("\nRPC Client\n");
            System.out.println("1. Square of the number - square\n");
            System.out.println("2. Square root of the number - squareroot\n");
            System.out.println("3. Cube of the number - cube\n");
            System.out.println("4. Cube root of the number - cuberoot\n");
            System.out.println("Enter method name and the number: \n");
            while (true)
            {
                BufferedReader objBR = new BufferedReader(new
                InputStreamReader(System.in));
                String strInput = objBR.readLine();
                byte b[] = strInput.getBytes();
                DatagramPacket objDP = new DatagramPacket(b, b.length, objIA, 1200);
                objDS.send(objDP);
                objDP = new DatagramPacket(b, b.length);
                objDS1.receive(objDP);
                String strOutput = new String(objDP.getData(), 0, objDP.getLength());
                System.out.println("\nResult = " + strOutput + "\n");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        new math_client(); 
    } 
}

Server.java

import java.util.*;
import java.net.*;
import java.io.*;

class math_server
{
    DatagramSocket objDS;
    DatagramPacket objDP;
    String strInput, strMethodName, strResult;
    int intVal;

    math_server()
    {
        try
        {
            objDS=new DatagramSocket(1200);
            byte b[]=new byte[4096];
            while(true)
            {
                objDP=new DatagramPacket(b, b.length);
                objDS.receive(objDP);
                strInput=new String(objDP.getData(), 0, objDP.getLength());
                if(strInput.equalsIgnoreCase("q"))
                {
                    System.exit(1);
                }
                else
                {
                    StringTokenizer st = new StringTokenizer(strInput," ");
                    int i=0;
                    while(st.hasMoreTokens())
                    {
                    String token=st.nextToken();
                    strMethodName=token;
                    intVal = Integer.parseInt(st.nextToken());
                    }
                }
                System.out.println(strInput);
                InetAddress ia = InetAddress.getLocalHost();
                if(strMethodName.equalsIgnoreCase("square"))
                {
                    strResult= "" + square(intVal);
                }
                else if(strMethodName.equalsIgnoreCase("squareroot"))
                {
                    strResult= "" + squareroot(intVal); 
                    System.out.println(strResult);
                }
                else if(strMethodName.equalsIgnoreCase("cube"))
                {
                    strResult= "" + cube(intVal);
                }
                else if(strMethodName.equalsIgnoreCase("cuberoot"))
                {
                    strResult= "" + cuberoot(intVal);
                }
                byte b1[]=strResult.getBytes();
                DatagramSocket objDS1 = new DatagramSocket();
                DatagramPacket objDP1 = new DatagramPacket(b1, b1.length,
                InetAddress.getLocalHost(), 1300);
                System.out.println("Result : " +strResult+ "\n");
                objDS1.send(objDP1);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public double square(int a) throws Exception
    {
        double ans;
        ans = a*a;
        return ans;
    }
    public double squareroot(int a) throws Exception
    {
        double ans;
        ans = Math.sqrt(a);
        return ans;
    }
    public double cube(int a) throws Exception
    {
        double ans;
        ans = a*a*a;
        return ans;
    }
    public double cuberoot(int a) throws Exception
    {
        double ans;
        ans = Math.cbrt(a);
        return ans;
    }
    public static void main(String[] args) 
    {
        new math_server();
    }
}


OUTPUT



Comments