Search code examples
cpointerscharrpc

Passing character pointers from client to server in RPCGen


I am trying to send character pointers from rpc client to server in rpcgen for which below are the server and client programs

RPC program for RPC gen

struct clientinput {
    char * optype;
    char * word;
};

program DICTIONARY_PROG {
    version DICTIONARY_VERS {
        int USEDICTIONARY(clientinput) = 1;
    } = 1;
} = 0x23451113;

RPC Server

    #include "dictionary.h"

    int *
    usedictionary_1_svc(clientinput *argp, struct svc_req *rqstp)
    {
        static int  result;
        char *optype = (char*)malloc (10);
        char *word = (char*)malloc (10);

        char *a = argp->optype;
        char *b = argp->word;

        printf("Optype is %s\n", a);
        printf("Word is %s\n", b);

        printf("The optype is %s\n", strcpy(optype, argp->optype));
        printf("The word is %s\n", strcpy(word, argp->word));
        /*
         * insert server code here
         */

        return &result;
    }

RPC Client

#include "dictionary.h"


void
dictionary_prog_1(char *host, char *optype, char *word)
{
    CLIENT *clnt;
    int  *result_1;
    clientinput  usedictionary_1_arg;

    //strcpy(usedictionary_1_arg.optype, optype);
    //strcpy(usedictionary_1_arg.word, word);

    usedictionary_1_arg.optype = optype;
    usedictionary_1_arg.word = word;

    printf("Optype input is %s\n",usedictionary_1_arg.optype);
    printf("Word input is %s \n",usedictionary_1_arg.word);

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

    result_1 = usedictionary_1(&usedictionary_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, *optype, *word;

    if (argc < 2) {
        printf ("usage: %s server_host\n", argv[0]);
        exit (1);
    }
    host = argv[1];
    optype = argv[2];
    word = argv[3];

    dictionary_prog_1 (host,optype,word);
exit (0);
}

Here is the output on the server side

Optyep is a
Word is e
The optype is a
The word is e

The problem i encounter here is that only first character of the character pointers which I have passed from server to client gets printed. I have tried possible combinations of using character pointers and could not find the reason. So can someone please help me figure out the reason for this?


Solution

  • From looking at the documentation, RPCGen needs some help to distinguish between single character argument and actual array of characters since all parameters are passed as pointers. To let it know that you want an array of character, aka string, you need to use the string keyword in your struct declaration like so:

    struct clientinput {
        string optype<>;
        string word<>;
    };
    

    This answer explains it as well and this blog post has a similar example to what you want to accomplish.