WAP To demonstrate the use of RPC(Remote Procedural Call) - Distributed System

In computer science, a remote procedure call (RPC) is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction. That is, the programmer writes essentially the same code whether the subroutine is local to the executing program, or remote. When the software in question uses object-oriented principles, RPC is called remote invocation or remote method invocation.
Read about RPC --> http://en.wikipedia.org/wiki/Remote_procedure_call (wikipedia)

• File Name: - calc.x
struct data
{
long n;
char buffer[1024];
};
struct readargs
{
int opt1;
int opt2;
};
struct output
{
int result;
};
program CALC_PROG
{
version CALC_VER
{
output ADD(readargs) = 1;
output SUB(readargs) = 2;
output MUL(readargs) = 3;
output DIV(readargs) = 4;
} = 1;
} = 0x25670011;

• File Name: - calc_client.c  
#include "calc.h"
#include "calc_xdr.c"
#include "calc_clnt.c"
void calc_prog_1(char *host)
{
CLIENT *clnt;
output *result_1;
readargs add_1_arg;
output *result_2;
readargs sub_1_arg;
output *result_3;
readargs mul_1_arg;
output *result_4;
readargs div_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, CALC_PROG, CALC_VER, "udp");
if (clnt == NULL)
{
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("ADDITION:\n");
printf("Enter the operand one:");
scanf("%d",&add_1_arg.opt1);
printf("Enter the operand two:");
scanf("%d",&add_1_arg.opt2);
result_1 = add_1(&add_1_arg, clnt);
printf("Addition is %d\n",*result_1);
if (result_1 == (output *) NULL)
clnt_perror (clnt, "call failed");
printf("SUBSTRACTION:\n");
printf("Enter the operand one:");
scanf("%d",&sub_1_arg.opt1);
printf("Enter the operand two:");
scanf("%d",&sub_1_arg.opt2);
result_2 = sub_1(&sub_1_arg, clnt);
printf("Subtraction is %d\n",*result_2);
if (result_2 == (output *) NULL)
clnt_perror (clnt, "call failed");
printf("MULTIPLICATION:\n");
printf("Enter the operand one:");
scanf("%d",&mul_1_arg.opt1);
printf("Enter the operand two:");
scanf("%d",&mul_1_arg.opt2);
result_3 = mul_1(&mul_1_arg, clnt);
printf("Multiplication is %d\n",*result_3);
if (result_3 == (output *) NULL)
clnt_perror (clnt, "call failed");
printf("DIVISION:\n");
printf("Enter the operand one:");
scanf("%d",&div_1_arg.opt1);
printf("Enter the operand two:");
scanf("%d",&div_1_arg.opt2);
result_4 = div_1(&div_1_arg, clnt);
printf("Division is %d\n",*result_4);
if (result_4 == (output *) NULL)
clnt_perror (clnt, "call failed");
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int main (int argc, char *argv[])
{
char *host;
if (argc < 2)
{
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
calc_prog_1 (host);
exit (0);
}



• File Name: - calc_server.c

#include "calc.h"
#include "calc_xdr.c"
#include "calc_svc.c"
output *add_1_svc(readargs *argp, struct svc_req *rqstp)
{
static output result;
result.result = argp->opt1 + argp->opt2;
return &result;
}
output *sub_1_svc(readargs *argp, struct svc_req *rqstp)
{
static output result;
result.result = argp->opt1 - argp->opt2;
return &result;
}
output *mul_1_svc(readargs *argp, struct svc_req *rqstp)
{
static output result;
result.result = argp->opt1 * argp->opt2;
return &result;
}
output *div_1_svc(readargs *argp, struct svc_req *rqstp)
{
static output result;
result.result = argp->opt1 / argp->opt2;
return &result;


Output:-
ADDITION:
Enter the operand one:10
Enter the operand two:5
Addition is 15
SUBSTRACTION:
Enter the operand one:10
Enter the operand two:5
Subtraction is 5
MULTIPLICATION:
Enter the operand one:10
Enter the operand two:5
Multiplication is 50
DIVISION:\n");
Enter the operand one:10
Enter the operand two:5
Division is 2

Post a Comment

1 Comments

  1. Thanks a lot !
    very helpful source code .

    ReplyDelete