Thread: RPC problem

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Cool [SOLVED] RPC problem

    I am trying to program a C program using Sun RPC for a school assignment, but i'm stuck.

    What is has to do is to have the client add some papers to the server (for simplicity this is just in main memory) when adding a paper it displays the title correctly at the server side. But when I try to get the details of a paper the pointer to the title is different and it displays garbage.
    I dont understand why.

    What am i missing?

    note:
    I have not implemented working with the files correclty t the moment. Just trying to get the title and author to work..

    EDIT:
    solved it
    I had to use strcpy(var1 ,var2) instead of just using var1 = var2.


    <removed code>
    Last edited by klompie; 10-13-2011 at 06:57 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Just so you know - editing your posts to remove all the detail is considered rude.

    The forum works because people can read and learn from previous posts. If everyone just deleted everything as soon as they got the answer, it just wouldn't work anymore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Very rude

    Out of interest, this was his code


    paperserver.c
    Code:
    #include "paperproc.h"
    
    #define ERR_PAPER_NOT_FOUND 2
    #define PAPER_FOUND_OK 4
    
    
    struct paper_store {
    paper_id id;
    paper_author author;
    paper_title title;
    paper_data paper;
    };
    typedef struct paper_store paper_store;
    
    
    struct paper_store_list {
    struct paper_store *paper;
    struct paper_store_list *next;
    };
    typedef struct paper_store_list paper_store_list;
    
    
    paper_store_list* paperStore;
    
    
    paper_id *add_1_svc(paperadd_param *param, struct svc_req *sr) {
    printf("Recv new paper\n");
    paper_store_list* newPaperStore = malloc(sizeof (paper_store_list));
    paper_store* newPaper = malloc(sizeof (paper_store));
    
    
    newPaperStore->paper = newPaper;
    
    
    int newID;
    if (paperStore == NULL) {
    newID = 1;
    } else {
    newID = paperStore->paper->id + 1;
    }
    
    
    newPaperStore->paper->id = newID;
    newPaperStore->paper->author = param->author;
    newPaperStore->paper->title = param->title;
    newPaperStore->paper->paper = param->paper;
    newPaperStore->next = paperStore;
    paperStore = newPaperStore;
    printf("Done! Saved `%s` with ID `%d`\n", paperStore->paper->title, paperStore->paper->id);
    printf("Memloc title: %x\n", *paperStore->paper->title); // different
    printf("Memloc paper: %x\n", *paperStore->paper); // the same
    
    
    return &paperStore->paper->id;
    }
    
    
    struct paper_list *list_1_svc(void *vd, struct svc_req *sr) {
    printf("Serving List\n");
    
    
    paper_list* returnList = malloc(sizeof (paper_list));
    paper_store_list* current;
    
    
    for (current = paperStore; current != NULL; current = current->next) {
    paper_detail* tmp_detail = malloc(sizeof (paper_detail));
    paper_list* tmp_list_node = malloc(sizeof (paper_list()));
    
    
    tmp_detail->id = current->paper->id;
    tmp_detail->title = current->paper->title;
    tmp_detail->author = current->paper->author;
    tmp_list_node->paperDetail = tmp_detail;
    
    printf("%d, %s\n", tmp_detail->id, tmp_detail->title);
    
    
    tmp_list_node->next = returnList;
    returnList = tmp_list_node;
    }
    return returnList;
    }
    
    
    struct paper_detail_return *detail_1_svc(paper_id *id, struct svc_req *sr) {
    printf("Memloc title: %x\n", *paperStore->paper->title); // different
    printf("Memloc paper: %x\n", *paperStore->paper); // the same
    paper_detail_return* out = malloc(sizeof (paper_detail_return));
    printf("Recv request for details about paper `%d`\n", *id);
    paper_store_list* current;
    
    
    for (current = paperStore; current != NULL; current = current->next) {
    if (current->paper->id == *id) {
    out->statuscode = PAPER_FOUND_OK;
    out->paper->title = current->paper->title;
    out->paper->author = current->paper->author;
    free(current);
    return (out);
    }
    }
    
    
    printf("Paper not found `%d`\n", *id);
    out->statuscode = ERR_PAPER_NOT_FOUND;
    return (out);
    }
    
    
    paper_return *fetch_1_svc(paper_id *id, struct svc_req *sr) {
    paper_return* out = malloc(sizeof (paper_data));
    printf("Recv request for paper `%d`\n", *id);
    paper_store_list* current;
    
    
    printf(">%s<\n", paperStore->paper->paper);
    
    
    for (current = paperStore; current != NULL; current = current->next) {
    printf(">%s<\n", current->paper->title);
    if (current->paper->id == *id) {
    out->statuscode = PAPER_FOUND_OK;
    out->paper = current->paper->paper;
    
    
    return (out);
    }
    }
    
    
    printf("Paper not found `%d`\n", *id);
    out->statuscode = ERR_PAPER_NOT_FOUND;
    return (out);
    }
    paperclient.c
    Code:
    #include "paperproc.h"
    
    #define ERR_PAPER_NOT_FOUND 2
    #define PAPER_FOUND_OK 4
    
    
    CLIENT *cl;
    
    
    void printUsage(char **argv) {
    printf("Usage:\n"
    ""
    ""
    "%s add <machine> <author> <title> <data>\n"
    "%s list <machine>\n"
    "%s detail <machine> <paper_id>\n"
    "%s fetch <machine> <paper_id>\n"
    , argv[0], argv[0], argv[0], argv[0]);
    }
    
    
    void checkArguments(int argc, char **argv, int n) {
    if (argc < n) {
    printf("Error: Missing argument(s)\n\n");
    printUsage(argv);
    exit(EXIT_FAILURE);
    }
    }
    
    
    paper_data readFile(char *filename) {
    FILE *ifp;
    char *mode = "r";
    char *buffer;
    unsigned long fileSize;
    
    
    ifp = fopen(filename, mode);
    
    
    if (ifp == NULL) {
    fprintf(stderr, "Can't open input file %s!\n", filename);
    exit(EXIT_FAILURE);
    }
    //Get file length
    fseek(ifp, 0, SEEK_END);
    fileSize = ftell(ifp);
    fseek(ifp, 0, SEEK_SET);
    
    
    //Allocate memory
    buffer = (char *) malloc(fileSize + 1);
    if (!buffer) {
    fprintf(stderr, "Memory error!");
    fclose(ifp);
    exit(EXIT_FAILURE);
    }
    
    
    //Read file contents into buffer
    fread(buffer, fileSize, 1, ifp);
    fclose(ifp);
    
    
    return buffer;
    }
    
    
    void add(int argc, char **argv) {
    checkArguments(argc, argv, 6);
    paperadd_param *param = malloc(sizeof (paperadd_param));
    param->author = argv[3];
    param->title = argv[4];
    param->paper = readFile(argv[5]);
    paper_id* id = add_1(param, cl);
    printf("Paper added with id `%d`\n", *id);
    }
    
    
    void detail(int argc, char **argv) {
    checkArguments(argc, argv, 4);
    
    
    paper_id id = atoi(argv[3]);
    paper_detail_return* awnser = detail_1(&id, cl);
    
    
    if (awnser == NULL) {
    printf("Error: %s\n", clnt_sperror(cl, argv[1]));
    exit(EXIT_FAILURE);
    }
    
    
    switch (awnser->statuscode) {
    case ERR_PAPER_NOT_FOUND:
    printf("Error: The paper with id `%d` was not found.\n", id);
    exit(EXIT_FAILURE);
    break;
    case PAPER_FOUND_OK:
    printf("Details about paper `%d`:\n"
    "Title: %s\n"
    "Author: %s\n", id, awnser->paper->author, awnser->paper->title);
    break;
    }
    }
    
    
    void list() {
    paper_list* awnser = list_1(NULL, cl);
    
    
    while(awnser != NULL){
    printf("%d: %s, %s\n", awnser->paperDetail->id, awnser->paperDetail->author, awnser->paperDetail->title);
    awnser = awnser->next;
    }
    }
    
    
    void fetch(int argc, char **argv) {
    checkArguments(argc, argv, 4);
    printf("Fetching document...\n");
    
    
    paper_id id = atoi(argv[3]);
    paper_return* awnser = fetch_1(&id, cl);
    
    
    if (awnser == NULL) {
    printf("Error: %s\n", clnt_sperror(cl, argv[1]));
    exit(EXIT_FAILURE);
    }
    
    
    switch (awnser->statuscode) {
    case ERR_PAPER_NOT_FOUND:
    printf("Error: The paper with id `%d` was not found.\n", id);
    exit(EXIT_FAILURE);
    break;
    case PAPER_FOUND_OK:
    printf(">>>%s<<<\n", awnser->paper);
    break;
    }
    }
    
    
    int main(int argc, char **argv) {
    if (argc <= 2) {
    printUsage(argv);
    return 1;
    }
    cl = clnt_create(argv[2], PAPERPROC_PROG, PAPERPROC_VERS, "tcp");
    if (cl == NULL) {
    printf("Error: Unable to create RPC client connection to `%s`\n", argv[2]);
    exit(EXIT_FAILURE);
    
    
    }
    
    
    if (!strcmp(argv[1], "add")) {
    add(argc, argv);
    } else if (!strcmp(argv[1], "list")) {
    list();
    } else if (!strcmp(argv[1], "detail")) {
    detail(argc, argv);
    } else if (!strcmp(argv[1], "fetch")) {
    fetch(argc, argv);
    } else {
    printf("Error: Command `%s` is invalid", argv[1]);
    exit(EXIT_FAILURE);
    }
    clnt_destroy(cl);
    return 0;
    }
    paperproc.x
    Code:
    typedef string paper_data < 10240 >;typedef string paper_title < 256 >;
    typedef string paper_author < 256 >;
    typedef int paper_id;
    
    
    /*typedef opaque paper_data[1024];*/
    
    
    struct paper_detail {
    paper_id id;
    paper_title title;
    paper_author author;
    };
    
    
    struct paper_list {
    paper_detail *paperDetail;
    struct paper_list *next;
    };
    
    
    struct paperadd_param {
    paper_author author;
    paper_title title;
    paper_data paper;
    };
    
    
    struct paper_return {
    int statuscode;
    paper_data paper;
    };
    -
    struct paper_detail_return {
    int statuscode;
    paper_detail *paper;
    };
    Code:
    program PAPERPROC_PROG
    {
    version PAPERPROC_VERS
    {
    paper_id ADD(paperadd_param) = 1;
    paper_list LIST() = 2;
    paper_detail_return DETAIL(paper_id) = 3;
    paper_return FETCH(paper_id) = 4;
    }
    = 1;
    }
    = 0x31423456;
    Wouldn't want the world missing out on it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM