Link to home
Start Free TrialLog in
Avatar of RajAcharya
RajAcharya

asked on

RPC

Dear friends,
I am into RPC (Just) and that's why these simple programs.
I am trying to pass a variable to another server and have
that server increment the value of the variable and return the variable.
The .x compiles with rpcgen and does not generate an XDR.
I know the reason for this. When I compile the client and
the server stubs with cnt.c and cnt_svc_proc.c, I do not seem
to have any compilation errors.
When I rsh the second server and try to run my client program, I get
output as follows:
================================
SERVER=rigel
Segmentation fault
================================

Please find my original code for your kind perusal. Please help.
cnt.x
=====
program COUNTPROG {
        version COUNTVERS {
        int COUNTINCR(int)=1;
        }=1;
}=0x20000001;
========================================
cnt.c
=====
#include<stdio.h>
#include<rpc/rpc.h>
#include"cnt.h"
#include<stdlib.h>
main(argc, argv)
int argc;
char *argv[];
{
CLIENT *cl;
char *server;
static int *counter1,*counter2;
        if (argc != 2) {
                fprintf(stderr,"usage: %s server\n", argv[0]);
                exit(1);
        }
server = argv[1];
printf("SERVER=%s\n",server);
/* *counter1 = atoi(*argv[2]); */
*counter1=15;
printf("counter1 = %p\n",*counter1);
cl=clnt_create(server, COUNTPROG,COUNTVERS,"udp");
        if (cl == NULL) {
                clnt_pcreateerror(server);
             exit(1);
        }
*counter2=*countincr_1(*counter1,cl);
printf("counter1 = %d\n", *counter2);
}
===================================================================
cnt_svc_proc.c
==============
#include<stdio.h>
#include<rpc/rpc.h>
#include "cnt.h"
int *countincr_1(counter1)
{
static int result;
result=counter1+1;
return(&result);
}
===================================================================
Rajesh
arc7292p@uel.ac.uk
Avatar of dhm
dhm

I don't know about the rest of your RPC code (haven't bothered to try it out), but on inspection I can see two serious errors that will cause your client program to crash.

Your two variables, counter1 and counter2, are declared to be pointers to integer.  However, you never initialize the pointers; you just try to stuff the value 15 into the location pointed to by counter1, and the result of the RPC call into the location pointed to by counter2.

If you declare two integer variables, and then make the pointers counter1 and counter2 point to them, perhaps things will work better.

E.g.:

/* ... the first part of your program ... */

static int *counter1, *counter2;
int integer1, integer2;

counter1 = &integer1;
counter2 = &integer2;

/* ... the rest of your program ... */

ASKER CERTIFIED SOLUTION
Avatar of albertp
albertp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial