Distributed Systems Tutorial 03
Implementation
of RPC(Remote Procedure Call)
addClient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rpc/rpc.h>
#include "add.h"
#define YES 0
#define NO 1
char srvr[25];
void Myshutdown ();
int addfunc (int num1, int
num2, int num3);
int main (int argc, char
*argv[])
{
int
num1, num2, num3, result, no;
int
loopFlag;
char
ans, dummy;
if (argc != 2)
{
fprintf(stderr, "usage: %s
host\n", argv[0]);
exit (1);
}
strcpy (srvr, argv[1]);
loopFlag = YES;
while(loopFlag == YES)
{
printf("1. Addition \n2.
Substraction");
scanf("%d",&no);
switch(no)
{
case 1:
printf("Enter 3 numbers to add ");
scanf
("%d%d%d%c", &num1, &num2, &num3, &dummy);
result =
addfunc (num1, num2, num3);
printf
("The sum of those numbers is %d\n", result);
break;
case 2: printf("Enter
3 numbers to substract ");
scanf
("%d%d%d%c", &num1, &num2, &num3, &dummy);
result =
subfunc (num1, num2, num3);
printf
("The substraction of those numbers is %d\n", result);
break;
}
printf ("Do you want to do
another calculation (y or n)? ");
scanf ("%c%c",
&ans, &dummy);
if (ans != 'y' && ans
!= 'Y')
{
loopFlag = NO;
printf
("Goodbye...\n");
if (ans == 'Z')
{
Myshutdown ();
}
continue;
}// end-of-if
}//end-of-while
return 0;
}
int addfunc (int num1, int
num2, int num3)
{
CLIENT *cl;
int *result;
struct numtype nums;
cl = clnt_create(srvr, ADDSERV,
ADDVERS, "tcp");
if (cl == NULL) {
printf("Couldn't create
client\n");
exit (1);
}
nums.int1 = num1;
nums.int2 = num2;
nums.int3 = num3;
result = addfunc_1(&nums, cl);
clnt_destroy(cl);
return (*result);
}
int subfunc (int num1, int
num2, int num3)
{
CLIENT *cl;
int *result;
struct numtype nums;
cl = clnt_create(srvr, ADDSERV,
ADDVERS, "tcp");
if (cl == NULL) {
printf("Couldn't create
client\n");
exit (1);
}
nums.int1 = num1;
nums.int2 = num2;
nums.int3 = num3;
result = subfunc_1(&nums, cl);
clnt_destroy(cl);
return (*result);
}
void Myshutdown ()
{
CLIENT *cl;
void *dummy;
cl = clnt_create(srvr, ADDSERV,
ADDVERS, "tcp");
if (cl == NULL)
{
printf("Couldn't create
client\n");
exit (1);
}
myshutdown_1(dummy, cl);
clnt_destroy(cl);
}
addServer.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include "add.h"
static int sum;
static int res;
int *addfunc_1_svc
(numtype *nums, struct svc_req *data){
int nuint1, nuint2, nuint3;
nuint1 = nums->int1;
nuint2 = nums->int2;
nuint3 = nums->int3;
printf("We got numbers %d, %d, and
%d. ",nuint1,nuint2,nuint3);
sum = nuint1 + nuint2 + nuint3;
printf("and the sum is %d\n",
sum);
sleep(1);
return (&sum);
}
int *subfunc_1_svc
(numtype *nums, struct svc_req *data){
int nuint1, nuint2, nuint3;
nuint1 = nums->int1;
nuint2 = nums->int2;
nuint3 = nums->int3;
printf("We got numbers %d, %d, and
%d. ",nuint1,nuint2,nuint3);
res = nuint1 - nuint2 - nuint3;
printf("and the result is
%d\n", res);
sleep(1);
return (&res);
}
void *myshutdown_1_svc
(void *dummy, struct svc_req *data)
{
printf ("Shutting down
server....\n");
exit (0);
}
Add.x
program ADDSERV {
version ADDVERS {
int addFunc(struct numtype) =
1;
int subFunc(struct numtype) =
2;
void myshutdown(void) = 3;
} = 1;
} = 9876;
struct numtype
{
int int1;
int int2;
int int3;
};
>>Download Word File<<