来自' int *'的转换无效到' off_t *'在C?

时间:2016-08-24 17:16:22

标签: c

我正在写一个程序,它将通过网络使用套接字将一个文件发送到使用C的另一个程序。但是,我遇到了障碍,因为我对C很新,我无法解决它。在尝试编译时,我收到此错误:

data_logger.c: In function ‘void sender()’:
data_logger.c:163:72: error: invalid conversion from ‘int*’ to ‘off_t* {aka   long int*}’ [-fpermissive]
     while (((sent_bytes = sendfile(peer_socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)){

我无法理解错误。非常感谢所有帮助。谢谢:))

文件:

void sender(){

    int server_socket;
    int peer_socket;
    socklen_t       sock_len;
    ssize_t len;
    struct sockaddr_in      server_addr;
    struct sockaddr_in      peer_addr;
    int fd;
    int sent_bytes = 0;
    char file_size[256];
    struct stat file_stat;
    int offset;
    int remain_data;

    /* Create server socket */
    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    if (server_socket == -1){
            fprintf(stderr, "Error creating socket --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }

    /* Zeroing server_addr struct */
    memset(&server_addr, 0, sizeof(server_addr));

    /* Construct server_addr struct */
    server_addr.sin_family = AF_INET;
    inet_pton(AF_INET, SERVER_ADDRESS, &(server_addr.sin_addr));
    server_addr.sin_port = htons(PORT_NUMBER);

    /* Bind */
    if ((bind(server_socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) == -1){
            fprintf(stderr, "Error on bind --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }

    /* Listening to incoming connections */
    if ((listen(server_socket, 5)) == -1){
            fprintf(stderr, "Error on listen --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }

    fd = open(FILE_TO_SEND, O_RDONLY);

    if (fd == -1){
            fprintf(stderr, "Error opening file --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }

    /* Get file stats */
    if (fstat(fd, &file_stat) < 0){
            fprintf(stderr, "Error fstat --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }

    fprintf(stdout, "File Size: \n%ld bytes\n", file_stat.st_size);

    sock_len = sizeof(struct sockaddr_in);

    /* Accepting incoming peers */
    peer_socket = accept(server_socket, (struct sockaddr *)&peer_addr, &sock_len);
    if (peer_socket == -1){
            fprintf(stderr, "Error on accept --> %s", strerror(errno));
            exit(EXIT_FAILURE);
    }
    fprintf(stdout, "Accept peer --> %s\n", inet_ntoa(peer_addr.sin_addr));

    sprintf(file_size, "%ld", file_stat.st_size);

    /* Sending file size */
    len = send(peer_socket, file_size, sizeof(file_size), 0);

    if (len < 0){
          fprintf(stderr, "Error on sending greetings --> %s", strerror(errno));
          exit(EXIT_FAILURE);
    }

    fprintf(stdout, "Server sent %d bytes for the size\n", len);

    offset = 0;
    remain_data = file_stat.st_size;

    /* Sending file data */
    while (((sent_bytes = sendfile(peer_socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)){
            fprintf(stdout, "1. Server sent %d bytes from file's data, offset is now : %d and remaining data = %d\n", sent_bytes, offset, remain_data);
            remain_data -= sent_bytes;
            fprintf(stdout, "2. Server sent %d bytes from file's data, offset is now : %d and remaining data = %d\n", sent_bytes, offset, remain_data);
    }

    close(peer_socket);
    close(server_socket);

}

1 个答案:

答案 0 :(得分:1)

您需要知道sendfile()函数采用的参数类型。 见sendfile() man page.

请注意,您调用offset的第3个参数是off_t *类型。确保传入指向off_t的指针,您的错误将被修复。

所以不要将偏移量定义为:

int offset;

尝试

off_t offset;