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 that 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

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 the server process. A process on the server side will extract the call message with procedure parameters, it will then compute the result and send a reply message back to the caller process.

Server process

To achieve transparency, they used some part of the local procedure call, using the concept of stubs – which 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 remotely call the server machine (as both client and server are connected to stub procedures).

Client program

The basic program of RPC
 

Server Machine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace server_test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                IPAddress ipaddress = IPAddress.Parse("192.168.119.1");
                TcpListener mylist = new TcpListener(ipaddress, 8000);
                mylist.Start();
                Console.WriteLine("Server is Running on Port: 8000");
                Console.WriteLine("Local endpoint:" + mylist.LocalEndpoint);
                Console.WriteLine("Waiting for Connections...");
                Socket s = mylist.AcceptSocket();
                Console.WriteLine("Connection Accepted From:" + s.RemoteEndPoint);
                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved..");
                for (int i = 0; i < k; i++)
                {
                    Console.Write(Convert.ToChar(b[i]));
                }
                ASCIIEncoding asencd = new ASCIIEncoding();
                s.Send(asencd.GetBytes("Automatic Message:" + "String Received byte server !"));
                Console.WriteLine("\nAutomatic Message is Sent");
                s.Close();
                mylist.Stop();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error.." + ex.StackTrace);
            }
        }
    }
}

User

Client Machine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace Client_test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient Tcpclient = new TcpClient();
                Console.WriteLine("Connecting..");
                Tcpclient.Connect("192.168.119.1", 8000);
                Console.WriteLine("Connected");
                Console.WriteLine("Enter the String you want to send ");
                string str = Console.ReadLine();
                Stream stm = Tcpclient.GetStream();
                ASCIIEncoding ascnd = new ASCIIEncoding();
                byte[] ba = ascnd.GetBytes(str);
                Console.WriteLine("Sending..");
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                for (int i = 0; i < k; i++)
                {
                    Console.Write(Convert.ToChar(bb[i]));
                }

                Tcpclient.Close();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error" + ex.StackTrace);
            }
        }
    }
}

Connecting

Output

Output


Similar Articles