c++ - rpcgen-在结构中传递字符串

标签 c++ c unix rpc

我正在尝试使用 rpcgen 包将字符串作为结构的一部分通过网络传递。这是我的 IDL 代码:

struct param
{   
    char* name;
    int voterid;
};

program VOTECLIENT_PROG
{
        version VOTECLIENT_VERS
        {
        string ZEROIZE() = 1;
                string ADDVOTER(int) = 2;
        string VOTEFOR(param) = 3;
        string LISTCANDIDATES() = 4;
        int VOTECOUNT(string) = 5;
        } = 1;
} = 0x2345111;

不知何故,字符串在服务器上被截断为单个字符。例如,如果我传递 name = "abc",我会在服务器上得到 "a"。看起来这是由于 stub 内部的某些问题而发生的,但我似乎无法弄清楚错误在哪里。

将字符串作为参数传递的函数的客户端代码:

void
voteclient_prog_1(char *host, char* c, int id)
{
    CLIENT *clnt;
    char * *result_3;
    param  votefor_1_arg;

#ifndef DEBUG
    clnt = clnt_create (host, VOTECLIENT_PROG, VOTECLIENT_VERS, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif  /* DEBUG */
    votefor_1_arg.name = c;
    votefor_1_arg.voterid = id;

    result_3 = votefor_1(&votefor_1_arg, clnt);
    if (result_3 == (char **) NULL) {
        clnt_perror (clnt, "call failed");
    }
    clnt_perror (clnt, "call failed");
#ifndef DEBUG
    clnt_destroy (clnt);
#endif   /* DEBUG */
}


int
main (int argc, char *argv[])
{
    char *host;
    int id;
    char* c = new char[20];

    if (argc < 4) {
        printf ("usage: %s server_host name voterid\n", argv[0]);
        exit (1);
    }
    host = argv[1];
    c = argv[2];
    id = atoi(argv[3]);
    voteclient_prog_1 (host, c, id);
exit (0);
}

任何帮助将不胜感激。

最佳答案

来自 rpcgen Programming Guide , 6.9.特殊情况:

Strings: C has no built-in string type, but instead uses the null-terminated “char *” convention. In XDR language, strings are declared using the “string” keyword, and compiled into “char *”s in the output header file. The maximum size contained in the angle brackets specifies the maximum number of characters allowed in the strings (not counting the NULL character). The maximum size may be left off, indicating a string of arbitrary length.

Examples:

string name<32>;   --> char *name;
string longname<>; --> char *longname;

所以,你应该声明name像上面一样,e。 G。 string name<20>; .

关于c++ - rpcgen-在结构中传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22600634/

相关文章:

c - C 中的结构扩展

c - -lm 不在 makefile 中链接数学库

c++ - 操作系统 : GLUT window never appears

c - 使用一维数组的排列填充一维数组

c - 9 键键盘打印 qwerty 键的逻辑

linux - 为什么 Bash 对启动文件有如此奇怪的行为

algorithm - 为什么 Unix block 大小会随着内存大小的增加而增加?

c++ - 如何全局设置返回语句的条件断点?

c++ - 如何在不创建新行的情况下使用 cin 输入变量?

c++ - 如何从 fstream 中获取数据,然后再获取?