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

标签 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

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

最佳答案

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

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

这个answer也解释了这一点,这个 blog post有一个与您想要完成的类似的示例。

关于c - 在 RPCGen 中将字符指针从客户端传递到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822436/

相关文章:

C++/SDL 'void*' 不是点对对象类型

c++ - 如何通过 vector::pointer push_back vector 中的数据?

c - 在 C linux 的字符数组中存储一个位

java - 我如何将 char 作为 java 中的命令行参数

c - 为什么我会因为使用 char 指针数组而出现段错误?

c - C 中的表达式求值

c++ - 为什么用 new 创建的 C++ 数组与 C 样式数组的行为不同?

c++ - 将字符串转换为 char*

在 C 中更改注册表值

无法使用 MinGW 编译 C 代码