在RPCGen中将字符指针从客户端传递到服务器

时间:2015-03-03 01:15:28

标签: c pointers char rpc

我正在尝试将rpc客户端的字符指针发送到rpcgen中的服务器,以下是服务器和客户端程序

RPC gen的RPC程序

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

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

RPC服务器

    #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客户端

#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);
}

这是服务器端的输出

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

我遇到的问题是,只有我从服务器传递给客户端的字符指针的第一个字符才会被打印出来。我尝试过使用字符指针的可能组合,但找不到原因。那么有人可以帮我找出原因吗?

1 个答案:

答案 0 :(得分:1)

通过查看文档,RPCGen需要一些帮助来区分单个字符参数和实际字符数组,因为所有参数都作为指针传递。要让它知道你想要一个字符数组,即字符串,你需要在结构声明中使用string关键字,如下所示:

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

answer也解释了这一点,而blog post与您想要完成的内容有类似的例子。