前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sendfile函數的使用[通俗易懂]

sendfile函數的使用[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-05 10:17:14
4800
发布2022-09-05 10:17:14
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript
复制
    #include <sys/sendfile.h>
    ssize_t sendfile(int out_fd,int in_fd,off_t offset,size_t count);

实例:

代码语言:javascript
复制
#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;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135647.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档