Chinaunix

标题: sendfile issue [打印本页]

作者: cyaln    时间: 2009-04-24 14:52
标题: sendfile issue
大家先看看code吧~!

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sendfile.h>

int main()
{
        int s = open("tux", O_RDONLY);
        int d = open("output", O_WRONLY | O_CREAT, 0600);
        off_t off = 0;
        ssize_t size = sendfile(d, s, &off, 10000);
                if(size <= 0)
                {
                        perror("sendfile");
                }
        printf("size = %d, off = %d\n", size, off);
}


编辑运行之后出错.
sendfile: Invalid argument
size = -1, off = 0

tux文件确实存在,而且是450M的文件.open应该没有错的.请大家帮忙看看!谢谢
作者: cyaln    时间: 2009-04-24 15:42
咋就没有人理我么~????
作者: xinglp    时间: 2009-04-24 16:38
sendfile 从磁盘文件 到网络
作者: xinglp    时间: 2009-04-24 16:39
也可以使用splice完成你的要求, 需要Linux内核版本 2.6.28 以上
作者: cyaln    时间: 2009-04-24 16:51
那具体说说怎么实现呢~~

sendfile()是必须的
作者: zhoubug    时间: 2009-04-24 16:55
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations  (i.e.,
   it cannot be a socket); and out_fd must refer to a socket.

In  Linux  2.4  and  earlier,  out_fd  could refer to a regular file, and sendfile() changed the current
       offset of that file.
作者: cyaln    时间: 2009-04-24 17:10
原帖由 zhoubug 于 2009-4-24 16:55 发表
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations  (i.e.,
   it cannot be  ...



这个我知道啊~~那在2.6的kernel里面,怎么样让out_fd为一个socket呢?
作者: xinglp    时间: 2009-04-25 06:49
你就是复制文件吧, 直接读取写入新文件, 还可以用splice() 需要一个中间管道, 也和sendfile()一样是零拷贝的,
sendfile()内部也是用splice()实现的.
内核版本 2.6.28以上
作者: langue    时间: 2009-04-25 07:59
请问你想做什么?

你不能在 2.6 的内核环境下进行从常规文件到常规文件的 sendfile()。
out_fd 可以是 socket,这个需要你使用 BSD sockets 接口。
作者: xinglp    时间: 2009-04-25 11:11
标题: 回复 #9 langue 的帖子
很明显是复制文件




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2