Remote Procedure Calls - Neuroon Networks

Breaking

Monday, March 18, 2019

Remote Procedure Calls

What is RPC???

 

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.
A client has a request message that the RPC translates and sends to the server. This request may be a procedure or a function call to a remote server. When the server receives the request, it sends the required response back to the client. The client is blocked while the server is processing the call and only resumed execution after the server is finished.

So let's try to implement one of RPC model using RPCGEN in Ubuntu OS.

Get Start

Make sure you already installed RPCGEN. To check just type rpcinfo one of your terminal file. If it is not installed use the following command to install it.

sudo apt-get install rpcbind

Then the next step. It is easy if you create another folder for this tutorial. So create a directory and change your terminal directory to it. Let's begin coding.

In this tutorial I'm going to show how to add two numbers. So basically you need a something.x format file to compile and generate those relevant anythings.c files to run the server and client. Follow the steps in the below. Make sure to not to miss any step.

  • Open a preferred editor and place the following code in it and save it as add.x.
                 struct numbers{
             int a;
             int b;
         };

         program ADD_PROG{
             version ADD_VERS{
                 int add(numbers) = 1;
             } = 1;
         } = 0x23451111; 

  • In the terminal ( working directory should be the folder that you created the  add.x file) type,
           rpcgen -a -C add.x
  • This will create some additional files. In there, find the filename called Makefile.add. So what this file does is, create the relevant server and client side executable (bash script) files. These files are needed to run the server and client side.
  • But before that you should change the add_client.c and add_server.c file to get proper outputs. In add_server.c,
          #include "add.h"

           int * add_1_svc(numbers *argp, struct svc_req *rqstp) {
                   static int  result;

                   printf("add(%d,%d) is called\n",argp->a,argp->b);
                   result = argp->a + argp->b;

                   return &result;
           }


       Just add the highlighted phases to the code. 
  • In the add_client.c, do the same as follows.
         #include "add.h"

         void add_prog_1(char *host, int x, int y)
         {
                 CLIENT *clnt;
                 int  *result_1;
                 numbers  add_1_arg;

         #ifndef    DEBUG
                clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");
                if (clnt == NULL) {
                      clnt_pcreateerror (host);
                      exit (1);
                }
        #endif    /* DEBUG */
                add_1_arg.a = x;
                add_1_arg.b = y;

                result_1 = add_1(&add_1_arg, clnt);
                if (result_1 == (int *) NULL) {
                      clnt_perror (clnt, "call failed");
               }else{
                      printf("Result:%d\n",*result_1);
               }

      #ifndef    DEBUG
               clnt_destroy (clnt);
      #endif     /* DEBUG */
        }

      int main (int argc, char *argv[])
     {
          char *host;

          if (argc < 4) {
                 printf ("usage: %s server_host a b\n", argv[0]);
                 exit (1);
          }
          host = argv[1];
          add_prog_1 (host, atoi(argv[2]),atoi(argv[3]));
          exit (0);
     }

  • So now you are ready to run the Makefile.add file. Just type in the terminal 
       make -f Makefile.add.
  • This will generate the bash script files to start the client and server. So in one terminal type,
                  sudo ./add_server
  • and another terminal in the same directory,
                  sudo ./add_client localhost <number 1> <number 2>
  • Place number 1 and number 2 as you like. Make sure that them are integers. And observe server terminal and client terminal for outputs.
 

1 comment:

  1. Wow, what great information. I am sure the info on your blog will help others.

    Would you like to create an App Like Groupon App ? then we have the best solution for you by The App Ideas. contact us.

    ReplyDelete