2013-02-28 112 views
1

我試圖將文本文件從客戶端傳輸到使用SunRPC實現的服務器。我能夠傳輸數據,但只能傳輸前四個字符。起初我只能得到一個字符,因爲我定義的變量的數據類型是字符指針。使用SUN RPC將文件從客戶端傳輸到服務器

add.x文件

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

我可以想辦法解決它,

我們可以使用字符串數據類型,這是在有那麼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; 
相關問題