【发布时间】:2014-03-24 03:34:36
【问题描述】:
我正在尝试使用 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"。看起来这是由于存根内部的一些问题而发生的,但我似乎无法弄清楚错误在哪里。
将字符串作为参数传递的函数的客户端代码:
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);
}
任何帮助将不胜感激。
【问题讨论】:
-
这可能是一个我不知道的
C++ism,但是在你已经分配c = new char[20]之后c=argv[2]似乎不对... -
实际上,我自己也不确定。我正在尝试不同的方法,看看是否能找到导致此问题的原因。