Aim: Design a distributed application using RPC for remote computation where client submits an integer value to the server and server calculates factorial and returns the result to the client program.

Step for running programs in terminal: 
  1. sudo apt-get install rpcbind
  2. rpcgen arith.x
  3. cc -c arith_client.c
  4. cc -c  arith_clnt.c
  5. cc -c  arith_xdr.c
  6. cc -o arith_client arith_client.o arith_clnt.o arith_xdr.c
  7. cc -c  arith_server.c
  8. cc -c  arith_svc.c
  9. cc -o arith_server arith_server.o arith_svc.o arith_xdr.c
  10. ./arith_server
  11. Open new terminal ./arith_client localhost
Code:

1. In terminal gedit arith.x

struct intpair {
int a;
};

program FACT_PROG {  
version FACT_VERS {
int FACT(intpair) = 1; 
} = 1;                        
} = 0x23451111;             


2. In terminal gedit arith_client.c

#include "arith.h"


void fact_prog_1(char *host,int a)
{
 CLIENT *clnt;
 int *result_1;
 intpair fact_1_arg;
 clnt = clnt_create (host, FACT_PROG, FACT_VERS, "udp");
 if (clnt == NULL) 
 {
  clnt_pcreateerror (host);
  exit (1);
 }

 fact_1_arg.a=a;
 result_1 = fact_1(&fact_1_arg, clnt);
 if (result_1 == (int *) NULL) 
 {
  clnt_perror (clnt, "call failed");
 }
 else
 {
  printf("Factorial=%d",*result_1);
 }

 clnt_destroy (clnt);

}
int main (int argc, char *argv[])
{
 char *host;
 int a,ch;
 if (argc < 2) 
 {
  printf ("usage: %s server_host\n", argv[0]);
  exit (1);
 }
 host = argv[1];
 do
 {
  system("clear");
  printf("\nEnter a no:: ");
  scanf("%d",&a);
  fact_prog_1 (host,a);
  printf("\nTry again : (1/0) :: ");
  scanf("%d",&ch);
 } while(ch==1);
 exit (0);
}           


3. In terminal gedit arith_server.c

#include "arith.h"
int * fact_1_svc(intpair *argp, struct svc_req *rqstp)
{
 static int result,n,fact;
 int i;
 n=argp->a;
 // factorial logic
 fact = 1;
 printf("\n Received : n= %d \n",n);
 for (i=n;i>0;i--)
 {
  fact=fact * i;
 }
 result=fact;
 return &result;
}