【问题标题】:Transfer a file from client to server using SUN RPC使用 SUN RPC 将文件从客户端传输到服务器
【发布时间】:2013-02-28 12:23:51
【问题描述】:

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

添加.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 个字符。是我定义的数据类型的问题吗?谁能建议我另一种方法?

【问题讨论】:

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


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      相关资源
      最近更新 更多