簡體   English   中英

使用SUN-RPC從客戶端發送結構並保存到鏈接列表中的服務器

[英]Send struct from client and save to server in linked list with SUN-RPC

我想使用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”,但它只會打印當前調用中提供給服務器功能的字符串值(而不是字符串的字符串值)。列表中的第一項)。

有人知道哪里出問題了嗎?

我只是在這里猜測,但這可能是RPC實現重復使用了所使用的字符串緩沖區,因此in->author始終指向同一緩沖區嗎?

您可以通過為每個請求打印in->author的地址來輕松找到in->author

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM