我想使用 RPC 编写一个服务器/客户端程序,它将一个结构从客户端(包含一些字符串)传输到服务器。该结构必须使用链表保存在服务器上。此刻,我有以下代码:
.x 文件:
struct paper_node
{
long id;
string author<>;
struct paper_node *next;
};
struct add_in
{
string author<>;
};
typedef struct paper_node *list_node;
服务器
add_out *add_proc_1_svc(add_in *in, struct svc_req *rqstp)
{
static add_out out;
static long id = 1;
static paper_node *list = NULL;
//paper_node *p, *q;
paper_node *pointer, *new_paper;
new_paper = (paper_node *) malloc(sizeof(paper_node));
new_paper->id = id;
new_paper->author = in->author;
new_paper->next = NULL;
if (list == NULL)
{
list = new_paper;
}
else
{
for (pointer = list; pointer->next != NULL; pointer = pointer->next);
pointer->next = new_paper;
}
printf("%ld - %s\n", list->id, (char *)list->author);
out = id;
id += 1;
return(&out);
}
客户
void handle_new_paper(char **argv, CLIENT *cl)
{
add_in in;
add_out *out;
buffer = read_new_paper(argv);
in.author = argv[3];
out = add_proc_1(&in, cl);
if (out == NULL) { printf("Error: %s\n", clnt_sperror(cl, argv[1])); }
else
{
printf("%ld\n", *out);
}
free(buffer);
}
服务器似乎没有正确地将字符串添加到列表中。打印列表 ID(列表的头部)时,它每次都会打印“1”,但它只打印在当前调用时提供给服务器函数的字符串值(而不是列表中的第一项)。
有谁知道这哪里出错了?