Problems with running RPC code

E

ebby83

Hi I am trying to run this basic Timestamp service using RPC . Here are
the file for the same .

I run it the foll way:

gdoshi@hobbes:!:rpcgen date.x
gdoshi@hobbes:!:lsdate.h date_clnt.c dateproc.cdate.x
date_svc.c rdate.c
gdoshi@hobbes:!:cc -o rdate rdate.c date_clnt.c
gdoshi@hobbes:!:cc -o dateproc dateproc.c date_svc.c
gdoshi@hobbes:!:./dateproc&
[1] 1611
gdoshi@hobbes:!: rdate hobbes.seas.gwu.edu
rdate: connect: Connection refused
[1]+ Done ./dateproc

I always get a connection refused whenever I do this. I also tried this
with localhost. PLS HELP!!! ..


dateproc.c

#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */

/*
* Return the binary date and time
*/

long *bin_date_1()
{
static long timeval; /* must be static */

timeval = time((long *) 0);
return(&timeval);
}

/*
* Convert a binary time and return a human readable string
*/

char **str_date_1(long *bintime)
{
static char *ptr; /* must be static */
ptr = ctime(bintime); /* convert to local time */
return(&ptr);
}

rdate.c

#include <stdio.h>

#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */

main(int argc, char *argv[])
{
CLIENT *cl; /* RPC handle */
char *server;
long *lresult; /* return value from bin_date_1() */
char **sresult; /* return value from str_date_1() */

if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);
exit(1);
}

server = argv[1];

/*
* Create client handle
*/

if ((cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) ==
NULL) {
/*
* can't establish connection with server
*/
clnt_pcreateerror(server);
exit(2);
}

/*
* First call the remote procedure "bin_date".
*/

if ( (lresult = bin_date_1(NULL, cl)) == NULL) {
clnt_perror(cl, server);
exit(3);
}
printf("time on host %s = %ld\n",server, *lresult);

/*
* Now call the remote procedure str_date
*/

if ( (sresult = str_date_1(lresult, cl)) == NULL) {
clnt_perror(cl, server);
exit(4);
}
printf("time on host %s = %s", server, *sresult);

clnt_destroy(cl); /* done with the handle */
exit(0);
}


date.h

#ifndef _DATE_H_RPCGEN
#define _DATE_H_RPCGEN

#include <rpc/rpc.h>

#define DATE_PROG 0x31234567
#define DATE_VERS 1
#define BIN_DATE 1
extern long * bin_date_1();
#define STR_DATE 2
extern char ** str_date_1();
extern int date_prog_1_freeresult();

#endif /* !_DATE_H_RPCGEN */
 
F

Flash Gordon

Hi I am trying to run this basic Timestamp service using RPC . Here are
the file for the same .

We know nothing about RPC here since it is not part of the C standard.
You will probably get more help somewhere that RPC is topical, possibly
a role playing games group or a networking group, depending on what RPC
stands for. I'll comment on the issues covered by standard C.

dateproc.c

#include <rpc/rpc.h> /* standard RPC include file */

We don't know the contents of this header. Although I don't see anything
in this file that requires it.
#include "date.h" /* this file is generated by rpcgen */
/*
* Return the binary date and time
*/

long *bin_date_1()

If it takes no parameters, make it explicit.
long *bin_date_1(void)
{
static long timeval; /* must be static */

timeval = time((long *) 0);

You need to 'include time.h for the time function, and it takes a
time_t* as a parameter and returns a time_t, not long* and long. Also,
given a prototype in scope, you would not need to cast the 0. Generally
casts are bad, although there are some exceptions.
return(&timeval);
}

/*
* Convert a binary time and return a human readable string
*/

char **str_date_1(long *bintime)
{
static char *ptr; /* must be static */
ptr = ctime(bintime); /* convert to local time */

Again, ctime takes time_t not long.
return(&ptr);
}

rdate.c

#include <stdio.h>

#include <rpc/rpc.h> /* standard RPC include file */

We still don't know the contents of this header.
#include "date.h" /* this file is generated by rpcgen */

main(int argc, char *argv[])

main returns an int, it is best to be explicit and required in C99.
int main(int argc, char *argv[])
CLIENT *cl; /* RPC handle */
char *server;
long *lresult; /* return value from bin_date_1() */
char **sresult; /* return value from str_date_1() */

if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);

argv[0] could be NULL, you should check before using it.

1 is a non-standard return value, only 0, EXIT_SUCCESS and EXIT_FAILURE
have defined semantics.
}

server = argv[1];

/*
* Create client handle
*/

if ((cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) ==
NULL) {

We have no idea what this function does since it is not included.
However, from what you say earlier it probably has something to do with
Role Playing Characters.
/*
* can't establish connection with server
*/
clnt_pcreateerror(server);
exit(2);
}

/*
* First call the remote procedure "bin_date".
*/

if ( (lresult = bin_date_1(NULL, cl)) == NULL) {

The function you have written with this name does not expect any
parameters. *BANG*.
clnt_perror(cl, server);
exit(3);
}
printf("time on host %s = %ld\n",server, *lresult);

/*
* Now call the remote procedure str_date
*/

if ( (sresult = str_date_1(lresult, cl)) == NULL) {

The function you have written with this name expects only one parameter.
*BANG*.
clnt_perror(cl, server);
exit(4);
}
printf("time on host %s = %s", server, *sresult);

clnt_destroy(cl); /* done with the handle */
exit(0);
}


date.h

#ifndef _DATE_H_RPCGEN
#define _DATE_H_RPCGEN

#include <rpc/rpc.h>

#define DATE_PROG 0x31234567
#define DATE_VERS 1
#define BIN_DATE 1
extern long * bin_date_1();
#define STR_DATE 2
extern char ** str_date_1();
extern int date_prog_1_freeresult();

You say this file is automatically generated, well, it is a very
unhelpful file to be generated since it does not specify the parameters.
I suggest you ask on a group that deals with RPC and the tools you are
using and, amongst other things, find out how to get the header file to
specify the parameters functions take so the compiler can tell you when
you get it wrong.
 
K

Kenny McCormack

We know nothing about RPC here since it is not part of the C standard.

I.e., after clipping 162 lines of useless (certainly, to the OP, at any
rate) trivia:

Not portable. Can't discuss it here. Blah, blah, blah.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top