Friday, March 6, 2009

Remote Procedure Call

RPC (Remote Procedure Call), it is higher level programming to socket programming.

.x files are just like Interface in java. 

conversion.x  
struct conv{  
float x;
};
program conversion{ 
version conversion1_vers{
int conversion1(conv)=1;
}=1;
version conversion2_vers{
int converions2(conv)=1;
}=2;
}= 0x23451111; 

In Conversion.x, conv is a structures used to send a float value.
To generate client and server code use rpcgen
rpcgen -a -C conversion.x

along with client and server, it will also generate makefile.conversion and someother files.



conversion_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 "conversion.h"
#include
#include /* getenv, exit */


void
conversion_1(float a,char *host)
{
CLIENT *clnt;
int  *result_1;
conv  conversion1_1_arg;

#ifndef DEBUG
clnt = clnt_create(host, conversion, conversion1_vers, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
conversion1_1_arg.x=a;
result_1 = conversion1_1(&conversion1_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror(clnt, "call failed");
}
        else
        {
        printf("%.2f Kilometers is equal to %d Miles\n",a,*result_1);
        }
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}


void
conversion_2(float a,char *host)
{
CLIENT *clnt;
int  *result_1;
conv  converions2_2_arg;

#ifndef DEBUG
clnt = clnt_create(host, conversion, conversion2_vers, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
converions2_2_arg.x=a;
result_1 = converions2_2(&converions2_2_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror(clnt, "call failed");
}
        else
        {
        printf("%.2f Kilometers is equal to %d Feet\n",a,*result_1);
        }
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}


main(int argc, char *argv[])
{
char *host;
float a;
if (argc <>
printf("usage:  %s server_host\n", argv[0]);
exit(1);
}
host = argv[1];
//According to your logic you need to write your code, here i put "a" as a parameter in both conversion_1 and conversion_2 inorder to send to server.
        printf("Enter kilometers\n");
        scanf("%f",&a);
conversion_1(a,host);
conversion_2(a,host);
}

conversion_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 "conversion.h"
#include
#include /* getenv, exit */
#include
#include
int *
//After generating this code, we need to write our definition to this methods.
conversion1_1_svc(conv *argp, struct svc_req *rqstp)
{
static int  result;

/*
* insert server code here
*/
        result=(int)floorf((argp->x/1.61)+0.5);
        printf("1st resutl is %d\n",result);
return (&result);
}

int *
converions2_2_svc(conv *argp, struct svc_req *rqstp)
{
static int  result;

/*
* insert server code here
*/
result=(int)floorf(((argp->x) * 3280.839895)+0.5);
        printf("2nd result is %d\n",result);
return (&result);
}

Change the make.conversion to makefile.
mv makefile.conversion makefile

in makefile you need to change the following.
CC=gcc  // default is cc
CFLAGS += -g -DRPC_SVC_FG
RPCGENFLAGS = -C

To compile
$ make

Open two command prompts, one for server and other for client.
$./conversion_server

$./conversion_client


No comments: