A simple XDR program

#include <stdlib.h>
#include <rpc/rpc.h>

#define bool_t int
#ifndef TRUE
#define TRUE   1
#endif
#ifndef FALSE
#define FALSE  0
#endif

typedef struct student {
    char* name;
    int age;
}STU;

int
main(void)
{
    char *buf;
    XDR x;
    char* buf1;
    char* buf2;
    STU *s_snd = calloc(1, sizeof(struct student));
    s_snd->name = calloc(20, sizeof(char));
    strcpy(s_snd->name, "platform");
    s_snd->age = 25;

    STU *s_rcv = calloc(1, sizeof(struct student));
    s_rcv->name = calloc(20, sizeof(char));

    /* start to encode */
    buf = malloc(100) ;
    xdrmem_create(&x,buf,100,XDR_ENCODE);
    /* encode data */
    if (!xdr_stu(&x, &s_snd)) {
        free(s_snd->name);
        free(s_snd);
        free(s_rcv->name);
        free(s_rcv);
        xdr_destroy(&x);
        return FALSE;
    }

    xdr_destroy(&x);
    /* start to decode */
    xdrmem_create(&x,buf,100,XDR_DECODE);
    /* decode data */
    if (!xdr_stu(&x, &s_rcv)) {
        free(s_snd->name);
        free(s_snd);
        free(s_rcv->name);
        free(s_rcv);
        xdr_destroy(&x);
        return FALSE;
    }

    xdr_destroy(&x);
    printf("s_rcv>name: %s, s_rcv->age: %d/n",s_rcv->name, s_rcv->age);
    return 0;
}

bool_t
xdr_stu(XDR *xdrs, struct student *stu)
{
    if(!xdr_string(xdrs, &(stu->name), 20)
            || !xdr_int(xdrs, &(stu->age))) {
        return FALSE;
    }  
}
 

你可能感兴趣的:(A simple XDR program)