Getting Started With Remote Procedure Call

Distributed applications are not very compatible with IPC protocols as they work for only certain applications. Instead of IPC, Distributed applications use explicit message passing where they use “Send” and “Receive” primitives for communication, but as these primitives have to be handled by programmers this model couldn’t achieve transparency.

Therefore there is a need for a generalized protocol which can handle such transparency with ease- for that “Remote Procedure Call” has been modelled. RPC is a middleware that comes in between your application and an Operating System.


Remote Procedure Call Operation

In RPC, the caller and sender process are executed on different machines, they can communicate with the help of the transport and network layers of an OSI model.

Initially the caller process sends a request message with procedure parameters to the server process. Then, the caller process gets into the wait state until some reply not come from server process. A process on the server side will extract the call message with procedure parameters, it will then compute the result and sends a reply message back to the caller process.

To achieve transparency, they had used some part of the local procedure call, using the concept of stubs – that hides the actual implementation from the programmers. It converts the parameters that are passed between sender and receiver. The goal of stub is to allow any local machine to connect or can remotely call the server machine (as both client and server are connected to stub procedures).


Basic program of RPC

Server Machine 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.Sockets;  
  6. using System.Net;  
  7.   
  8. namespace server_test  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.   
  15.             try  
  16.             {  
  17.                 IPAddress ipaddress = IPAddress.Parse("192.168.119.1");  
  18.                 TcpListener mylist = new TcpListener(ipaddress, 8000);  
  19.                 mylist.Start();  
  20.                 Console.WriteLine("Server is Running on Port: 8000");  
  21.                 Console.WriteLine("Local endpoint:" + mylist.LocalEndpoint);  
  22.                 Console.WriteLine("Waiting for Connections...");  
  23.                 Socket s = mylist.AcceptSocket();  
  24.                 Console.WriteLine("Connection Accepted From:" + s.RemoteEndPoint);  
  25.                 byte[] b = new byte[100];  
  26.                 int k = s.Receive(b);  
  27.                 Console.WriteLine("Recieved..");  
  28.                 for (int i = 0; i < k; i++)  
  29.                 {  
  30.                     Console.Write(Convert.ToChar(b[i]));  
  31.                 }  
  32.                 ASCIIEncoding asencd = new ASCIIEncoding();  
  33.                 s.Send(asencd.GetBytes("Automatic Message:" + "String Received byte server !"));  
  34.                 Console.WriteLine("\nAutomatic Message is Sent");  
  35.                 s.Close();  
  36.                 mylist.Stop();  
  37.                 Console.ReadLine();  
  38.             }  
  39.             catch (Exception ex)  
  40.             {  
  41.                 Console.WriteLine("Error.." + ex.StackTrace);  
  42.             }  
  43.            
  44.         }  
  45.     }  
  46. }   



Client Machine
 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.Sockets;  
  6. using System.IO;  
  7.   
  8. namespace Client_test  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             try  
  15.             {  
  16.                 TcpClient Tcpclient = new TcpClient();  
  17.                 Console.WriteLine("Connecting..");  
  18.                 Tcpclient.Connect("192.168.119.1", 8000);  
  19.                 Console.WriteLine("Connected");  
  20.                 Console.WriteLine("Ente the String you want to send ");  
  21.                 string str = Console.ReadLine();  
  22.                 Stream stm = Tcpclient.GetStream();  
  23.                 ASCIIEncoding ascnd = new ASCIIEncoding();  
  24.                 byte[] ba = ascnd.GetBytes(str);  
  25.                 Console.WriteLine("Sending..");  
  26.                 stm.Write(ba, 0, ba.Length);  
  27.                 byte[] bb = new byte[100];  
  28.                 int k = stm.Read(bb, 0, 100);  
  29.                 for (int i = 0; i < k; i++)  
  30.                 {  
  31.                     Console.Write(Convert.ToChar(bb[i]));  
  32.                 }  
  33.   
  34.                 Tcpclient.Close();  
  35.                 Console.ReadLine();  
  36.             }  
  37.             catch (Exception ex)  
  38.             {  
  39.                 Console.WriteLine("Error" + ex.StackTrace);  
  40.             }  
  41.         }  
  42.     }  
  43. }   



OUTPUT

Up Next
    Ebook Download
    View all
    Learn
    View all