Search code examples
crpccode-generation

Problem with program generated by rpcgen (RPC, C)


I have a problem with an assignment. I have to make a program that receives time and date from a remote host using the RPC protocol and generated by rpcgen.

rpctime.x

program RPCTIME
{
    version RPCTIMEVERSION
    {
        long GETTIME() = 1;
    } = 1;
} = 2000001;

I created this file with rpcgen -a rpctime.x

generated files (lines added by me marked by comment //+):

rpctime_server.c

#include "rpctime.h"
#include <time.h> //+

long *
gettime_1_svc(void *argp, struct svc_req *rqstp)
{
    static long result;

    time(&result); //+`

    return &result;
}

rpctime_client.c

#include "rpctime.h"
#include "time.h" //+`

void
rpctime_1(char *host)
{
    CLIENT *clnt;
    long *result_1;
    char buf[50];
    char *gettime_1_arg;

#ifndef DEBUG
    clnt = clnt_create (host, RPCTIME, RPCTIMEVERSION, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif  /* DEBUG */

    result_1 = gettime_1((void*)&gettime_1_arg, clnt);
    if (result_1 == (long *) NULL) {
        clnt_perror (clnt, "call failed"); //here client program stops
    } else {
        printf("%s\n", ctime_r(result_1, buf)); //+`
    }
#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];
    rpctime_1 (host);
exit (0);
}

Makefile.rpctime

# This is a template Makefile generated by rpcgen

# Parameters

CLIENT = rpctime_client
SERVER = rpctime_server

SOURCES_CLNT.c = 
SOURCES_CLNT.h = 
SOURCES_SVC.c = 
SOURCES_SVC.h = 
SOURCES.x = rpctime.x

TARGETS_SVC.c = rpctime_svc.c rpctime_server.c   
TARGETS_CLNT.c = rpctime_clnt.c rpctime_client.c   
TARGETS = rpctime.h   rpctime_clnt.c rpctime_svc.c rpctime_client.c rpctime_server.c

OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o)
OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o)
# Compiler flags 

CFLAGS += -g 
LDLIBS += -ltirpc // here was generated -lnsl but it didn`t work
RPCGENFLAGS = 

# Targets 

all : $(CLIENT) $(SERVER)

$(TARGETS) : $(SOURCES.x) 
    rpcgen $(RPCGENFLAGS) $(SOURCES.x)

$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) $(TARGETS_CLNT.c) 

$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) $(TARGETS_SVC.c) 

$(CLIENT) : $(OBJECTS_CLNT) 
    $(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS) 

$(SERVER) : $(OBJECTS_SVC) 
    $(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)

 clean:
     $(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC) $(CLIENT) $(SERVER)

I compiled the program with make -f Makefile.rpctime.

I can run ./rpctime_server and ./rpctime_client 127.0.0.1, but the client always finishes with this error message:

call failed: RPC: Procedure unavailable

However, the server is still running


Solution

  • This problem occurs on Ubuntu 22.04, but earlier versions has no got similar problem. I tested on Ubuntu 21 and all worked properly.