Search code examples
clinked-listclientsunrpc

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


I want to write a server/client program using RPC which transfers a struct from a client (containing some strings) to the server. This struct has to be saved on the server using a linked list. At this moment, I have the following code:

.x-file:

struct paper_node
{
    long id;
    string author<>;
    struct paper_node *next;
};

struct add_in
{
    string author<>;
};

typedef struct paper_node *list_node;

server

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);
}

client

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);
}

The server doesn't seem to add the strings to the list correctly. When printing the list-id (head of the list) it does print '1' every time, but it just prints the string-values that were given to the server-function at the current call (and not the string-values of the first item in the list).

Anybody knows where this goes wrong?


Solution

  • I'm just guessing here, but it might be that the RPC implementation reuses the string buffer used, so in->author always points to the same buffer?

    You can easily find that out by printing the address of in->author for every request.