Distributed System and Cloud Computing Lab Manual
ISBN 9788119221868

Highlights

Notes

  

Chapter 2: Remote Procedure Call

Description:

Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-server based applications. A remote procedure call is an inter-process communication technique that is used for client-server based applications. It is also known as a subroutine call or a function call 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.

RPC Working:

The sequence of events in a remote procedure call is given as follows:

  • 1> The client stub is called by the client.
  • 2> The client stub makes a system call to send the message to the server and puts the parameters in the message.
  • 3> The message is sent from the client to the server by the client’s operating system.
  • 4> The message is passed to the server stub by the server operating system.
  • 5> The parameters are removed from the message by the server stub.
  • 6> Then, the server procedure is called by the server stub.

Program 2.1: Implement a Date time server containing date() and time() remote, through socket programming.

Source Code:

Filename: DateServer.java:

import java.net.*; import java.io.*; import java.util.*; import java.text.*; public class DateServer

{

  • DateServer() throws Exception
  • {
    • ServerSocket ss=new ServerSocket(4444); Socket s=ss.accept();
    • DataOutputStream dos=new DataOutputStream(s.getOutputStream()); dos.writeUTF(date());
    • dos.writeUTF(time()); dos.flush();
  • }
  • public String date()
  • {
    • return new SimpleDateFormat(“dd/mm/yyyy”).format(new Date()).toString();
  • }
  • public String time()
  • {
    • return new SimpleDateFormat(“hh:mm:ss”).format(new Date()).toString();
  • }
  • public static void main(String args[]) throws Exception
  • {
    • DateServer d=new DateServer();
  • }

}

Filename: DateClient.java

import java.net.*; import java.io.*; public class DateClient

{

  • public static void main(String args[]) throws Exception
  • {
    • Socket s=new Socket(“localhost”,4444);
    • DataInputStream dis=new DataInputStream(s.getInputStream()); String dt=dis.readUTF();
    • String tm=dis.readUTF(); System.out.println(“Date:” +dt); System.out.println(“Time:” +tm);
  • }

}

OUTPUT:

DateServer.java

DateClient.java

Program 2.2: Implement a Server to do the following: (Use RPC)

    i) Get two numbers from the client.

    ii) Server processing the summation of the above two numbers

    iii) Server sends the processed data to the client and client checks whether the sum is greater than 100 or not

    iv) Client displays appropriate message

Source Code:

Filename: ServerSum.java:

import java.net.*; import java.io.*; import java.util.*; public class ServerSum

{

  • ServerSum() throws Exception
  • {
    • ServerSocket ss=new ServerSocket(4444); Socket s=ss.accept();
    • DataInputStream dis=new DataInputStream(s.getInputStream());
    • DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str=dis.readUTF();
    • StringTokenizer st = new StringTokenizer(str, “ ”); String method = st.nextToken();
    • int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); dos.write(getSum(a,b));
    • System.out.println(“Sum of” +a+ “and” +b+ “is” +getSum(a,b)); dos.flush();
  • }
  • public int getSum(int a,int b)
  • {
    • return a+b;
  • }
  • public static void main(String args[]) throws Exception
  • {
    • ServerSum d=new ServerSum();
  • }

}

Filename: ClientSum.java

import java.net.*; import java.io.*; public class ClientSum

{

  • public static void main(String args[]) throws Exception
  • {
    • Socket s=new Socket(“localhost”,4444);
    • DataInputStream dis=new DataInputStream(s.getInputStream());
    • DataOutputStream dos=new DataOutputStream(s.getOutputStream());
    • dos.writeUTF(“sum 3 5”);
    • int sum=dis.read();
    • if(sum>100)
      • System.out.println(“Sum is greater than 100”);
    • else
  • System.out.println(“SUm is not greater than 100”);
  • }

}

OUTPUT:

ServerSum.java

ClientSum.java

Exercise 1: Write a program to implement a Server calculator containing ADD(), MUL(), SUB(), DIV(). Implement using RPC.