从客户端发送struct并使用SUN-RPC保存到链接列表中的服务器

时间:2013-10-10 09:55:42

标签: c linked-list client sunrpc

我想使用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);
}

服务器似乎没有正确地将字符串添加到列表中。当打印list-id(列表的头部)时,它每次都打印'1',但它只打印在当前调用时给予服务器函数的字符串值(而不是字符串值)列表中的第一项。)

有人知道这出错了吗?

1 个答案:

答案 0 :(得分:0)

我只是在这里猜测,但可能是RPC实现重用使用的字符串缓冲区,所以in->author总是指向同一个缓冲区?

您可以通过为每个请求打印in->author的地址轻松找到它。