C GCC program for RPC



GCC Program for Remote Procedure Call (RPC) in C Programming

/**************************************************************************
TITLE: RPC - REMOTE PROCEDURE CALL
**************************************************************************/

//SERVER FILENAME: server.c 

#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"

square_out *squareproc_1_svc(square_in *inp,struct svc_req *rqstp)
{
     static square_out out;
     out.res1= inp->arg1 * inp->arg1;
     return(&out);
}

// CLIENT FILENAME: client.c
#include"errno.h"
#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"

int main(int argc,char **argv)
{
     CLIENT *cl;
 square_in in;
 square_out *outp;
 f(argc!=3)
     {
           printf("\n\n error:insufficient arguments!!!");
           exit(-1);
     }

 cl=clnt_create(argv[1],SQUARE_PROG,SQUARE_VERS,"tcp");
 in.arg1=atol(argv[2]);

     if(cl==NULL)
     {
           printf("\nerror:%s",strerror(errno));
           exit(-1);
     }

     if((outp=squareproc_1(&in,cl))==NULL)
     {
           printf("\nerror :%s",clnt_sperror(cl,argv[1]));
           exit(-1);
     }

     printf("\n\n result is : %ld",outp->res1);
     exit(0);
}

// .h FILENAME: square.h

struct square_in
{
/*input arg*/
long arg1;
};

struct square_out
{
/*op result*/
long res1;
};

program SQUARE_PROG
{
version SQUARE_VERS
{
square_out SQUAREPROC(square_in)=1; /*proc no=1*/
}=1; /*version no*/
}=0x31230000;/*prog no*/

Output & Execution :

[root@localhost~]#rpcgen -C square.x
[root@localhost~]#cc -c client.c -o client.o
[root@localhost~]#cc -c square_clnt.c -o square_clnt.o
[root@localhost~]#cc -c square_xdr.c -o square.xdr.o
[root@localhost~]#cc -o client client.o square_clnt.o square_xdr.o
[root@localhost~]#cc -c client.c server.c square_xdr.c
[root@localhost~]#cc -c server.c -o server.o
[root@localhost~]#cc -c square_svc.c -o square_svc.o
[root@localhost~]#cc -o server server.o square_svc.o square_xdr.o
[root@localhost~]#./server &
[1] 2264
[root@localhost~]#./client localhost 4
result is: 16