sendfile函數的使用

sendfile函數linux內核新加的函數,可以使得網絡傳輸文件時用户层无需分配缓冲区给将要传输的文件,从而能够节约内存,并直接调用系统调用

    #include <sys/sendfile.h>
    ssize_t sendfile(int out_fd,int in_fd,off_t offset,size_t count);

实例:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>

#define LISTENQ 100


int main(int argc,char** argv) {
    if (argc != 3) {
        printf("please add <port> <sendfile-name>\n");
        return -1;
    }
    int sockfd;
    if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
        printf("socket error: %s\n",strerror(errno));
        return -1;
    }
    struct sockaddr_in server;
    bzero(&server,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(atoi(argv[1]));
    if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0) {
        printf("bind error: %s\n",strerror(errno));
        return -1;
    }
    if (listen(sockfd,LISTENQ) < 0) {
        printf("listen error: %s\n",strerror(errno));
        return -1;
    }
    int connfd;
    for (; ;) {
        if ((connfd = accept(sockfd,NULL,NULL)) < 0) {
            if (errno == EINTR) {
                continue;
            }
            printf("accept error: %s\n",strerror(errno));
            return -1;
        }
        struct stat file_stat;
        bool work = true;
        if (stat(argv[2],&file_stat) < 0) {
            printf("stat error: %s\n",strerror(errno));
            work = false;
        }
        if (S_ISREG(file_stat.st_mode)) {
            int fd;
            if ((fd = open(argv[2],O_RDONLY)) < 0) {
                printf("open error: %s\n",strerror(errno));
                return -1;
            }
            if (sendfile(connfd,fd,0,file_stat.st_size) != file_stat.st_size) {
                work = false;
                printf("sendfile error: %s\n",strerror(errno));
                return -1;
            }
            work = false;
            close(connfd);
            close(fd);
            continue;
        }
        if (!work) {
            char buf[100] = "Invalid request\n";
            if (write(connfd,buf,strlen(buf)) != strlen(buf)) {
                close(connfd);
                printf("write error: %s\n",strerror(errno));
                continue;
            }
            close(connfd);
        }   
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值