3, ఏప్రిల్ 2012, మంగళవారం

REMOTE PROCEDURE CALL IN UNIX NETWORK PROGRAMMING:-


Step 1:
[ramu@localhost ~]$ cat rpc.x
struct vars{
        int a;
        int b;
};
program ADDSUB{
        version MCVERS{
                int ADD(vars)=1;
                int SUBTRACT(vars)=2;
        }=1;
}=0x20000578;



[ramu@localhost ~]$ rpcgen -a -C rpc.x
file `rpc_server.c' already exists and may be overwritten


these files will get generated  by the above compaliation
[ramu@localhost ~]$ ls r*.*
rpc_client.c  rpc_client.o  rpc_clnt.o  rpc_server.c  rpc_server.o  rpc_svc.o  rpc.x  rpc_xdr.o  Makefile.rpc



open the makefile.rpc



CFLAGS += -g -DRPC_SVC_FG    in this by default   -g to this add –DRPC_SVC_FG
LDLIBS += -lnsl   no change in this line
RPCGENFLAGS = -C  this is empty for this we add –C



Step2:Open the server program  rpc_server.c   2 functions will come add1.svc  sub1.svc



This code is automatically get generated
[ramu@localhost ~]$ vim rpc_server.c
[ramu@localhost ~]$ cat rpc_server.c
/*  This is sample code generated by rpcgen. These are only templates and you can use them
  as a guideline for developing your own functions.*/

#include "rpc.h"

int *
add_1_svc(vars *argp, struct svc_req *rqstp)
{
        static int  result;
        /*
         * insert server code here
         */ these lines are edited by  us
        printf("addition() called\n");
        printf("parameters are %d %d \n:",argp->a,argp->b);
        result=argp->a+argp->b;
        printf("add is:%d\n",result);
        return &result;
}

int *
subtract_1_svc(vars *argp, struct svc_req *rqstp)
{
        static int  result;

        /*
         * insert server code here
         */
         printf("subtraction() called\n");
        printf("parameters are %d %d \n:",argp->a,argp->b);
        result=argp->a-argp->b;
        printf("sub is: %d\n",result);
        return &result;
}
[ramu@localhost ~]$for the client section for rpc

[ramu@localhost ~]$ cat rpc_client.c

/ * This is sample code generated by rpcgen. These are only templates and you can use them as a guideline for developing your own functions.*/

#include "rpc.h"


void
addsub_1(char *host)
{
        CLIENT *clnt;
        int  *result_1;
        vars  add_1_arg;
        int  *result_2;
        vars  subtract_1_arg;

#ifndef DEBUG
        clnt = clnt_create (host, ADDSUB, MCVERS, "udp");
        if (clnt == NULL) {
                clnt_pcreateerror (host);
                exit (1);
        }
#endif  /* DEBUG */   edited by the user
        add_1_arg.a=456;
        add_1_arg.b=345;
        result_1 = add_1(&add_1_arg, clnt);
        if (result_1 == (int *) NULL) {
                clnt_perror (clnt, "call failed");
        }
        else
                printf("addition is:%d \n",*result_1);
        subtract_1_arg.a=456;
        subtract_1_arg.b=300;
        result_2 = subtract_1(&subtract_1_arg, clnt);
        if (result_2 == (int *) NULL) {
                clnt_perror (clnt, "call failed");
        }
        else
                printf("subtraction is :%d\n",*result_2);
#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];
        addsub_1 (host);
exit (0);
}
[ramu@localhost ~]$

Compalation
$Make –f  Makefile.rpc for compaliation of all files
Running the server
$./rpc_server no port number
Client side
$./rpc_client localhost no portnumber

login as: ramu
ramu@192.168.15.5's password:
Last login: Wed Sep 15 08:49:56 2010 from 192.168.15.4
[ramu@localhost ~]$ vim pipes.c
[ramu@localhost ~]$ vim rpc.x
[ramu@localhost ~]$ vim rpc_server.c
[ramu@localhost ~]$ ./rpc_server
addition() called
parameters are 456 345
:add is:801
subtraction() called
parameters are 456 300
:sub is: 156



కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి