使用SUN RPC将文件从客户端传输到服务器

时间:2013-02-28 12:23:51

标签: objective-c c operating-system rpc sunrpc

我正在尝试将文本文件从客户端传输到使用SunRPC实现的服务器。我能够传输数据,但只传输前四个字符。最初我只能得到一个字符作为我定义的变量的数据类型是字符指针。

add.x file

struct file_details {
    unsigned int file_len; 
    int *file_val;
};

struct add_in {
    int *author;
    int *title;
    struct file_details *file;
};

struct node {
    int id;
    int *author;
    int *title;
    struct file_details *f;
    struct node *next;
};

struct info_out {
    int *author;
    int *title;
};

typedef struct node* add_out;
typedef struct node* list_out;
typedef struct node* list_in;
typedef int info_in;
typedef int fetch_in;
typedef char* fetch_out;

这是客户端代码

int * read_file(char* path){
    FILE *file;
    int *buffer;
    long file_length;

    file = fopen(path, "rb");
    if(file == NULL){
        perror("Unable to open file");
        exit(1);
    }

    file_length = f_length(path);
    printf("%ld", file_length);
    buffer=(int *)malloc(file_length*4);
    if(buffer == NULL){
       perror("Memory Allocation failed");
       exit(1);
    }

    fread(buffer, file_length, 1, file);
    fclose(file);

    return buffer;
}

int f_length(char* path) {
    FILE *fp;
    long file_len; 
    fp = fopen(path, "rb");
    fseek(fp, 0, SEEK_END);
    file_len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    return file_len;
}

int main(int argc, char** argv){
    CLIENT *cl;
    add_in in;
    add_out *out;
    list_out *l;
    list_out *temp;

    if(argc > 2) {
        cl = clnt_create(argv[1], FILE_TRANSFER, FILE_VERS, "udp");
        in.author = (int *)malloc(strlen(argv[3])*4);
        in.title = (int *)malloc(strlen(argv[4])*4);
        in.author = argv[3];
        in.title = argv[4];
        in.file = (struct file_details *)malloc(sizeof(struct file_details));
        in.file->file_val = (int *)malloc(f_length(argv[5])*4);
        in.file->file_val = read_file(argv[5]); 
        in.file->file_len = f_length(argv[5]);
        if (strcmp(argv[2],"add") == 0){
            out = add_proc_1(&in, cl);
            if(out == NULL) {
                fprintf(stderr,"Error: %s\n", clnt_sperror(cl,argv[1]));
            }
            else {
                printf("%s added", (*out)->title);
            }
        }

    }

    exit(0);

} 

我的服务器代码:

add_out * 
add_proc_1_svc(add_in *in, struct svc_req *rqstp)
{
    int file_length = in->file->file_len;
    static add_out  result;    
    char *a = (char *)malloc(file_length*4);
    char *b = (char *)malloc(file_length*4);
    a = in->author;
    node *temp, *newfile; 
    static node *start = NULL;
    newfile = (node *)malloc(sizeof(node));
    newfile->f = (file_details *)malloc(sizeof(file_details));
    newfile->author = (int *)malloc(sizeof(int));
    newfile->title = (int *)malloc(sizeof(int));
    newfile->author = in->author;
    newfile->title = in->title;
    newfile->f->file_val = in->file->file_val;
    b = in->file->file_val;
    newfile->f->file_len = file_length;
    newfile->id = rand();
    newfile->next = NULL;    
    if(start == NULL){
       start = newfile;
    }
    else {
        for(temp = start; temp->next != NULL; temp = temp->next);
        temp->next = newfile;
    }
    result = newfile;
    return &result;
}

为什么当我定义int *时,我在服务器端获得4个字符,在使用char *时获得1个字符。这是我定义的数据类型的问题。任何人都可以建议我一种替代方法吗?

1 个答案:

答案 0 :(得分:1)

我可以设法解决它,

我们可以使用SunRPC中的字符串数据类型,因此修改了我的add.x代码,

struct file_details {
    unsigned int file_len; 
    string file_val<>;
};

struct add_in {
    string author<>;
    string title<>;
    struct file_details *file;
};

struct node {
    int id;
    string author<>;
    string title<>;
    struct file_details *f;
    struct node *next;
};

struct info_out {
    string author<>;
    string title<>;
};

typedef struct node* add_out;
typedef struct node* list_out;
typedef struct node* list_in;
typedef int info_in;
typedef int fetch_in;
typedef char* fetch_out;