Monthly Archives: February 2015

PRACTICAL 4: Write a program to find average of given numbers using rpcgen command.

third.x

struct average

{

int a[10];

int n;

};

program third

{

version ver

{

int ave(average)=1;

}=1;

}=0x36767676;

 

third_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 “third.h”

#include “third_clnt.c”

 

void

third_1(char *host)

{

CLIENT *clnt;

int *result_1,i;

average ave_1_arg;

 

printf(“Enter Value:\n”);

scanf(“%d”,&ave_1_arg.n);

 

for(i=0;i<ave_1_arg.n;i++)

{

printf(“Enter Value%d:\n”,i+1);

scanf(“%d”,&ave_1_arg.a[i]);

}

 

#ifndef DEBUG

clnt = clnt_create (host, third, ver, “udp”);

if (clnt == NULL) {

clnt_pcreateerror (host);

exit (1);

}

#endif /* DEBUG */

 

result_1 = ave_1(&ave_1_arg, clnt);

printf(“Average is %d\n”,*result_1);

 

if (result_1 == (int *) 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];

third_1 (host);

exit (0);

}

 

third_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 “third.h”

#include “third_svc.c”

int *

ave_1_svc(average *argp, struct svc_req *rqstp)

{

static int result;

int sum=0,i;

 

for (i=0;i<((*argp).n);i++)

{

sum=sum+(*argp).a[i];

}

 

result=sum/(*argp).n;

printf(“Average is %d\n”,result);

return &result;

}

 

 

third_xdr.c

 

/* Please do not edit this file. It was generated using rpcgen. */

#include “third.h”

bool_t

xdr_average (XDR *xdrs, average *objp)

{

register int32_t *buf;

int i;

if (xdrs->x_op == XDR_ENCODE) {

buf = XDR_INLINE (xdrs, (1 + 10 )* BYTES_PER_XDR_UNIT);

if (buf == NULL) {

if (!xdr_vector (xdrs, (char *)objp->a, 10,

sizeof (int), (xdrproc_t) xdr_int))

return FALSE;

if (!xdr_int (xdrs, &objp->n))

return FALSE;

} else {

{

register int *genp;

for (i = 0, genp = objp->a;

i < 10; ++i) {

IXDR_PUT_LONG(buf, *genp++);

}

}

IXDR_PUT_LONG(buf, objp->n);

}

return TRUE;

} else if (xdrs->x_op == XDR_DECODE) {

buf = XDR_INLINE (xdrs, (1 + 10 )* BYTES_PER_XDR_UNIT);

if (buf == NULL) {

if (!xdr_vector (xdrs, (char *)objp->a, 10,

sizeof (int), (xdrproc_t) xdr_int))

return FALSE;

if (!xdr_int (xdrs, &objp->n))

return FALSE;

} else {

{

register int *genp;

for (i = 0, genp = objp->a; i < 10; ++i)

{ *genp++ = IXDR_GET_LONG(buf);}

}

objp->n = IXDR_GET_LONG(buf);

}

return TRUE;

}

if (!xdr_vector (xdrs, (char *)objp->a, 10,

sizeof (int), (xdrproc_t) xdr_int))

return FALSE;

if (!xdr_int (xdrs, &objp->n))

return FALSE;

return TRUE;

}

Practical 3: Write a program to add two given numbers at server side using rpcgen command.

second.x

struct add

{

int a;

int b;

};

program second

{

version ver

{

int sum(add)=1;

}=1;

}=0x33000000;

second_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 “second.h”

#include “second_clnt.c”  // *** first modification ***********

void

second_1(char *host)

{

CLIENT *clnt;

int *result_1;

add sum_1_arg;

printf(“Enter Two Numbers\n”);   // *** second modification ***********

scanf(“%d%d”, &sum_1_arg.a, &sum_1_arg.b);

#ifndef DEBUG

clnt = clnt_create (host, second, ver, “udp”);

if (clnt == NULL) {

clnt_pcreateerror (host);

exit (1);

}

#endif /* DEBUG */

result_1 = sum_1(&sum_1_arg, clnt);

if (result_1 == (int *) 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];

second_1 (host);

exit (0);

}

second_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 “second.h”

#include “second_svc.c”

int * sum_1_svc(add *argp, struct svc_req *rqstp)

{

static int result;

result=(*argp).a+(*argp).b;

printf(“Addition=%d\n”,result);

return &result;

}