To implement Remote Procedure Call, and perform addition and subtraction of two numbers.
(Explanation will be given soon).
Program
simp.x
#define VERSION_NUMBER 1
%#define foo 127
struct operands {
int x;
int y;
};
program SIMP_PROG {
version SIMP_VERSION {
int ADD(operands) = 1;
int SUB(operands) = 2;
} = VERSION_NUMBER;
} = 555555555;
simpclient.c
/* RPC client for simple addition example */
#include "stdio.h"
#include "simp.h" /* Created for us by rpcgen - has everything we need ! */
/* Wrapper function takes care of calling the RPC procedure */
int add( CLIENT *clnt, int x, int y) {
operands ops;
int *result;
/* Gather everything into a single data structure to send to the server */
ops.x = x;
ops.y = y;
/* Call the client stub created by rpcgen */
result = add_1(&ops,clnt);
if (result==NULL) {
fprintf(stderr,"Trouble calling remote procedure\n");
exit(0);
}
return(*result);
}
/* Wrapper function takes care of calling the RPC procedure */
int sub( CLIENT *clnt, int x, int y) {
operands ops;
int *result;
/* Gather everything into a single data structure to send to the server */
ops.x = x;
ops.y = y;
/* Call the client stub created by rpcgen */
result = sub_1(&ops,clnt);
if (result==NULL) {
fprintf(stderr,"Trouble calling remote procedure\n");
exit(0);
}
return(*result);
}
int main( int argc, char *argv[]) {
CLIENT *clnt;
int x,y;
if (argc!=4) {
fprintf(stderr,"Usage: %s hostname num1 num\n",argv[0]);
exit(0);
}
/* Create a CLIENT data structure that reference the RPC
procedure SIMP_PROG, version SIMP_VERSION running on the
host specified by the 1st command line arg. */
clnt = clnt_create(argv[1], SIMP_PROG, SIMP_VERSION, "udp");
/* Make sure the create worked */
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(argv[1]);
exit(1);
}
/* get the 2 numbers that should be added */
x = atoi(argv[2]);
y = atoi(argv[3]);
printf("%d + %d = %d\n",x,y, add(clnt,x,y));
printf("%d - %d = %d\n",x,y, sub(clnt,x,y));
return(0);
}
simpservice.c
/* Definition of the remote add and subtract procedure used by
simple RPC example
rpcgen will create a template for you that contains much of the code
needed in this file is you give it the "-Ss" command line arg.
*/
#include "simp.h"
/* Here is the actual remote procedure */
/* The return value of this procedure must be a pointer to int! */
/* we declare the variable result as static so we can return a
pointer to it */
int *
add_1_svc(operands *argp, struct svc_req *rqstp)
{
static int result;
printf("Got request: adding %d, %d\n", argp->x, argp->y);
result = argp->x + argp->y;
return (&result);
}
int *
sub_1_svc(operands *argp, struct svc_req *rqstp)
{
static int result;
printf("Got request: subtracting %d, %d\n", argp->x, argp->y);
result = argp->x - argp->y;
return (&result);
}
Compilation and Output
Server
vivek@ubuntu~$ rpcgen -C -a simp.x
vivek@ubuntu~$ cc -o server simpservice.c simp_svc.c simp_xdr.c
vivek@ubuntu~$ ./server
Got request: adding 4, 5
Got request: subtracting 4, 5
Client
vivek@ubuntu~$ cc -o client simpclient.c simp_clnt.c simp_xdr.c
vivek@ubuntu~$ ./client linux 4 5
4 + 5 = 9
4 - 5 = -1
No comments:
Post a Comment