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

标签 objective-c c operating-system rpc sunrpc

我正在尝试将文本文件从客户端传输到使用 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 个字符。是我定义的数据类型的问题吗?谁能建议我另一种方法?

最佳答案

我可以设法解决它,

我们可以使用 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;

关于objective-c - 使用 SUN RPC 将文件从客户端传输到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135497/

相关文章:

objective-c - TableView 无/不更新

c - 从 C 中字符串的每个单词中删除子字符串(如果包含它)

ios - (WeeLoader) 'UITapGestureRecognizer' 未在此范围内声明

ios - 如果 View Controller 不是立即被转移到的那个 View Controller,则无法使用 prepareForSegue 方法将数据传递给另一个 View Controller

c - C中的双向链表排序函数

c - 将选择哪个功能?

architecture - 了解虚拟地址和虚拟地址空间

operating-system - 地址绑定(bind)生成相同地址

java - 是什么让线程的执行顺序不可预测?

objective-c - 应用激活时的 iOS 网络操作