【问题标题】:Send struct from client and save to server in linked list with SUN-RPC从客户端发送结构并使用 SUN-RPC 以链表的形式保存到服务器
【发布时间】:2013-10-10 09:55:42
【问题描述】:

我想使用 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”,但它只打印在当前调用时提供给服务器函数的字符串值(而不是列表中的第一项)。

有人知道这是哪里出错了吗?

【问题讨论】:

  • 我认为您的 paper_node 中还需要一个布尔 hasNext 字段。

标签: c linked-list client sunrpc


【解决方案1】:

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

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

【讨论】:

  • 确实是重用的东西。我现在首先为所有字符串分配内存,然后使用 strcpy() 将传入的数据传输到新节点。这工作正常。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 2013-04-07
  • 2013-01-12
  • 1970-01-01
相关资源
最近更新 更多