Char数组在通过ONC RPC后损坏

时间:2016-11-27 14:26:25

标签: c rpc

当调用返回char数组的ONC-RPC函数时,我在第一次调用时得到(显然)未初始化的内存。随后的电话会给我正确的结果。

此示例展示了问题:

ftp.x:

const BLOCKSIZE = 1024;

struct block {
    char arr<BLOCKSIZE>;
};

program FILE_GET_BLOCK_PROG {
    version FILE_GET_BLOCK_VERS {
        block FILE_GET_BLOCK() = 1;
    } = 1;
} = 0x42424243;

ftp_client.c:

#include "ftp.h"

#include <errno.h>
#include <string.h>

size_t
file_get_block_prog_1(char *host, char *buf)
{
    CLIENT *clnt;
    block  *result_1;

#ifndef DEBUG
    clnt = clnt_create (host, FILE_GET_BLOCK_PROG, FILE_GET_BLOCK_VERS, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif  /* DEBUG */

    result_1 = file_get_block_1(NULL, clnt);
    if (result_1 == (block *) NULL) {
        clnt_perror (clnt, "call failed");
    }
#ifndef DEBUG
    clnt_destroy (clnt);
#endif   /* DEBUG */

    memcpy(buf, result_1->arr.arr_val, result_1->arr.arr_len);
    return result_1->arr.arr_len;
}

int
main (int argc, char *argv[])
{
    char *host, *source_filename;
    char buf[BLOCKSIZE+1];
    int block_count;
    size_t bytes_read;

    if (argc < 2) {
        printf ("usage: %s server_host\n", argv[0]);
        exit (1);
    }
    host = argv[1];

    bytes_read = file_get_block_prog_1 (host, buf);
    buf[bytes_read] = '\0';
    printf("%d bytes:\n%s\n", bytes_read, buf);
    exit (0);
}

ftp_server.c:

#include "ftp.h"

#include <errno.h>
#include <string.h>
#include <sys/stat.h>

block *
file_get_block_1_svc(void *argp, struct svc_req *rqstp)
{
    static block  result;
    static int request = 1;
    char buf[BLOCKSIZE+1];
    size_t bytes_read;

    strcpy(buf, "This is just a simple test block. There is nothing relevant in here.");

    result.arr.arr_len = strlen(buf);
    result.arr.arr_val = buf;
    printf("Request #%d:\n%s\n", request++, buf);

    return &result;
}

运行服务器并调用客户端两次时,这是我的输出: 客户端:

$ ./ftp_client localhost
68 bytes:

$ ./ftp_client localhost
68 bytes:
This is just a simple test bock. There is nothing relevant in here.

服务器:

Request #1:
This is just a simple test bock. There is nothing relevant in here.
Request #2:
This is just a simple test bock. There is nothing relevant in here.

在第一次请求之前,我是否错过了我需要做的初始化?还有什么会导致这种行为?

0 个答案:

没有答案