通过SunRPC发送带缓冲区的链表(c)



在过去的几天里我一直在为以下事情伤脑筋:我想通过SunRPC发送任何大小/类型的文件。在客户端,我将文件读入一个链表,每个节点包含一个大小为1024的缓冲区。如果我在通过RPC发送之前打印所有缓冲区,我将得到正确的输出(即打印整个文件)。但是,当我通过RPC发送它并在服务器端打印所有缓冲区时,最后一个节点打印错误,就好像它没有正确传输一样。

我用一个大的。txt文件(100,5kb)测试了我的代码,这里它似乎确实有效。但是,对于。pdf文件(7,9 kb)则不会。

这是我的(相关)代码:

。x文件:

struct node {
    string buf<>;
    long bytes;
    struct node* next;
};
struct paper {
    string author<>;
    string title<>;
    struct node* file;
    struct paper* next;
};
typedef long add_out;
program PAPERSERVER_PROG {
    version PAPERSERVER_VERS {
        add_out ADDPAPER(paper) = 1;
    } = 1;
} = 0x20001234;

paperclient.c:(当我在这个函数的末尾打印所有缓冲区时,输出是正确的)

#define BUF_SIZE 1024
void add_paper(char** argv, CLIENT* cl) {
    struct paper in;
    add_out *out;
    struct node* tmp;
    struct node* tmp2;
    struct node start;
    char buf[BUF_SIZE];
    size_t read;
    // Open the file
    FILE *fp = fopen(argv[5], "rb");
    if (fp == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    in.author = strdup(argv[3]);
    in.title = strdup(argv[4]);
    in.file = &start;
    in.next = NULL;
    bzero(buf, BUF_SIZE);
    read = fread(buf, 1, BUF_SIZE, fp);
    if (ferror(fp)) {
        perror("fread error");
        exit(2);
    }
    tmp = &start;
    tmp->buf = calloc(BUF_SIZE, 1);
    if (tmp->buf == NULL) {
        perror("malloc tmp->buf");
        exit(2);
    }
    tmp->bytes = read;
    tmp->buf = memcpy(tmp->buf, buf, read);
    // Cycle until end of file
    while (!feof(fp)) {
        bzero(buf, BUF_SIZE);
        tmp->next = (struct node*) calloc(sizeof(struct node), 1);
        if (tmp->next == NULL) {
            perror("malloc tmp->next");
            exit(2);
        }
        tmp = tmp->next;
        read = fread(buf, 1, BUF_SIZE, fp);
        if (ferror(fp)) {
            perror("fread error");
            exit(2);
        }
        printf("read:%zun", read);
        tmp->buf = calloc(BUF_SIZE+1, 1);
        if (tmp->buf == NULL) {
            perror("malloc tmp->buf");
            exit(2);
        }
        tmp->bytes = read;
        tmp->buf = memcpy(tmp->buf, buf, read);      
    }
    tmp->next = NULL;
    out = addpaper_1(&in, cl);
    if (out == NULL) {
        printf("Error: %sn", clnt_sperror(cl, argv[1]));
    } else {
        printf("%ldn", *out);
    }
    clnt_destroy(cl);
}

paperserver.c(这里输出错误)

add_out* addpaper_1_svc(paper* in, struct svc_req* rqstp) { 
    static add_out out;
    struct node* file;
    long written;
    file = in->file;
    char* buf;
    while (file != NULL) {
        buf = calloc(BUF_SIZE + 1, 1);
        buf = memcpy(buf, file->buf, file->bytes);
        written = fwrite(buf, 1, file->bytes, stdout);
        if (ferror(stdout)) {
            perror("fwrite error");
            exit(2);
        } 
        file = file->next;
    }
    out = 0;
    return &out;
}
编辑:

指定更多关于预期输出的信息。我希望通过服务器发送的文件的输出与调用cat <filename>时的输出完全相同。然而,输出似乎是错误的顺序。最后一个缓冲区似乎被文件开头的一些内容覆盖了。这是与一个。pdf文件一起使用的。与另一个。pdf它工作,与。txt它工作,但与。jpg它没有。当我得到输出的md5sum时,它会随着每次调用而改变。这让我认为它打印的字节实际上没有被我的程序初始化。此外,当我使用valgrind时,我得到以下错误(仅适用于不能正常工作的文件):

==2375== Invalid read of size 8
==2375==    at 0x4C2F790: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2375==    by 0x400D3E: addpaper_1_svc (paperserver.c:38)
==2375==    by 0x40128D: paperserver_prog_1 (paperserver_svc.c:76)
==2375==    by 0x4F626D0: svc_getreq_common (svc.c:534)
==2375==    by 0x4F6281D: svc_getreq_poll (svc.c:460)
==2375==    by 0x4F65BFE: svc_run (svc_run.c:96)
==2375==    by 0x401437: main (paperserver_svc.c:114)
==2375==  Address 0x527af48 is 392 bytes inside a block of size 394 alloc'd
==2375==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2375==    by 0x4F64D04: xdr_string (xdr.c:788)
==2375==    by 0x401487: xdr_node (paperserver_xdr.c:13)
==2375==    by 0x4F656B7: xdr_pointer (xdr_ref.c:84)
==2375==    by 0x40155C: xdr_paper (paperserver_xdr.c:31)
==2375==    by 0x401268: paperserver_prog_1 (paperserver_svc.c:72)
==2375==    by 0x4F626D0: svc_getreq_common (svc.c:534)
==2375==    by 0x4F6281D: svc_getreq_poll (svc.c:460)
==2375==    by 0x4F65BFE: svc_run (svc_run.c:96)
==2375==    by 0x401437: main (paperserver_svc.c:114)
==2375== 
==2375== Invalid read of size 8
==2375==    at 0x4C2F79E: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2375==    by 0x400D3E: addpaper_1_svc (paperserver.c:38)
==2375==    by 0x40128D: paperserver_prog_1 (paperserver_svc.c:76)
==2375==    by 0x4F626D0: svc_getreq_common (svc.c:534)
==2375==    by 0x4F6281D: svc_getreq_poll (svc.c:460)
==2375==    by 0x4F65BFE: svc_run (svc_run.c:96)
==2375==    by 0x401437: main (paperserver_svc.c:114)
==2375==  Address 0x527af50 is 6 bytes after a block of size 394 alloc'd
==2375==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2375==    by 0x4F64D04: xdr_string (xdr.c:788)
==2375==    by 0x401487: xdr_node (paperserver_xdr.c:13)
==2375==    by 0x4F656B7: xdr_pointer (xdr_ref.c:84)
==2375==    by 0x40155C: xdr_paper (paperserver_xdr.c:31)
==2375==    by 0x401268: paperserver_prog_1 (paperserver_svc.c:72)
==2375==    by 0x4F626D0: svc_getreq_common (svc.c:534)
==2375==    by 0x4F6281D: svc_getreq_poll (svc.c:460)
==2375==    by 0x4F65BFE: svc_run (svc_run.c:96)
==2375==    by 0x401437: main (paperserver_svc.c:114)  

您使用string buf<>来包含文件数据。字符串将以第一个零字节结束,因此如果数据中间有任何零字节(PDF文件可能会这样,但文本文件不会),则不会写入完整的数据。

(请注意,RPC无法知道node中有多少数据-您恰好将该数据放入bytes,但RPC基础设施不知道…)。

要解决这个问题,您需要将buf改为opaque:

opaque buf<>;

…并沿着

一行对C代码进行相应的更改。
buf.buf_val = calloc(...);
buf.buf_len = num_bytes_read;

相关内容

  • 没有找到相关文章

最新更新