我是靠谱客的博主 大意猎豹,最近开发中收集的这篇文章主要介绍简约而不简单epoll之EPOLLOUT一、序二、EPOLLIN|EPOLLOUT同时注册到epoll事件模型三、建议,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

随笔记录

select模型,poll模型,epoll模型对比

 selectpollepoll
支持最大连接数1024无上限无上限
IO效率每次调用都要进行遍历,时间复杂度O(n)每次调用都要进行遍历,时间复杂度O(n)使用事件通知方式,每当fd就绪,系统注册的回调函数就会被调用,将就绪fd放到rdllist里面,这样epoll_wait返回的时候我们就拿到了就绪的fd,时间复杂度O(1)
fd拷贝每次select都拷贝,fd数组每次poll都拷贝fd数组调用epoll_ctl时拷贝进内核并由内核保存,之后每次epoll_wait不拷贝

    本篇主要分析epoll边缘触发,通过模拟各种场景,来介绍EPOLLOUT,不涉及epoll底层源码实现。

一、序

epoll_wait返回的条件

1、等待时间到期

2、发生信号事件,例如ctrl+c

3、The associated file is available for read(2) operations,如果注册了EPOLLIN, socket接收缓冲区,有新的数据到来

4、The associated file is available for write(2) operations,如果注册了EPOLLOUT, socket发送缓冲区可写时

前三种场景比较容易理解,但是第4种场景却需要深入研究一下,通过man epoll_ctl手册,可以得到官方对于EPOLLOUT解释,就是上面英文,那么在写代码的时候我们应该如何考虑呢,EPOLLOUT呢?。

二、EPOLLIN|EPOLLOUT同时注册到epoll事件模型

结论1:将EPOLLET|EPOLLIN|EPOLLOUT 注册到epoll中, 然后调用epoll_wait会立刻返回,并且只有EPOLLOUT事件,这个属于第一次EPOLLOUT事件,原因是缓冲区可写。

模拟场景1: client 调用connect后,直接睡眠10s,不发送也不接受任何数据,10s后直接退出。观察server端会有EPOLLOUT打印

代码1:

服务端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>


#define SERVPORT 9527
#define MAXBUF 8192
#define MAXFDS 5000
#define EVENTSIZE 100

char sndMsg[MAXBUF] = {0};

int setnonblocking(int fd)
{
    int opts;
    if( (opts = fcntl(fd, F_GETFL, 0)) == -1) {
        perror("fcntl");
        return -1;
    }

    opts = opts | O_NONBLOCK;
    if( (opts = fcntl(fd, F_SETFL, opts)) == -1) {
        perror("fcntl");
        return -1;
    }

    return 0;
}

int main(void)
{
    char buf[MAXBUF];
    int len, n;

    struct sockaddr_in servaddr;
    int sockfd, listenfd, epollfd, nfds;

    struct epoll_event ev;
    struct epoll_event events[EVENTSIZE];

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERVPORT);

    if( (epollfd = epoll_create(MAXFDS)) == -1) {
        perror("epoll");
        exit(1);
    }

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if(setnonblocking(listenfd) == -1){
        perror("setnonblocking");
        exit(1);
    }

    if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if(listen(listenfd, 10) == -1) {
        perror("listen");
        exit(1);
    }

    // listen fd只注册EPOLLIN事件, EPOLLOUT不需要注册
    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = listenfd;
    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1) {
        perror("epoll_ctl");
        exit(1);
    }

    for( ; ; ) {
        if( (nfds = epoll_wait(epollfd, events, EVENTSIZE, -1)) == -1) {
            perror("epoll_wait");
            exit(1);
        }

        for(n = 0; n < nfds; n++) {
            if(events[n].data.fd == listenfd) {
                while( (sockfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) > 0) {
                    if(setnonblocking(sockfd) == -1) {
                        perror("setnonblocking");
                        exit(1);
                    }
                    //新的fd: 采用边缘触发且注册IN、OUT事件
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
                    ev.data.fd = sockfd;
                    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1) {
                        perror("epoll_ctl");
                        exit(1);
                    } else {
                        printf("new socketfd = %d register epoll successn", sockfd);
                    }
                }
                continue;
            }
            printf("Events = 0x%xn", events[n].events);
            if (events[n].events & (EPOLLIN | EPOLLOUT) == (EPOLLIN | EPOLLOUT)) {
                printf(">> EPOLLIN And EPOLLOUT event, socketfd = %dn", events[n].data.fd);
            }
            else if (events[n].events & EPOLLIN) {
                printf(">> Only EPOLLIN event, socketfd = %dn", events[n].data.fd);
            }
            else if (events[n].events & EPOLLOUT) {
                printf(">> Only EPOLLOUT, socketfd = %dn", events[n].data.fd);
            }
        }
    }
}

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVPORT 9527
#define MAXBUF 1024

int main(void)
{
    char buf[MAXBUF];
    struct sockaddr_in servaddr;
    int fd;
    int n, len;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(SERVPORT);

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        perror("connect");
        exit(1);
    }
    printf("Connection ok, Send data after sleep 10s.n");
    sleep(10);
    printf(">> bye byen");
    close(fd);// 会出发server端 EPOLLIN和EPOLLOUT
    return 0;
}

输出结果:

结论2:在注册的时候,将EPOLLET|EPOLLIN|EPOLLOUT注册到epoll中,然后客户端client发送数据,server就会触发EPOLLIN|EPOLLOUT, 原因还是缓冲区是可以写的。

模拟场景2:建立连接后clien立即发送数据,每次间隔5秒,server端不调用recv函数也不调用send函数。

输出结果:执行了两次,两次基本相同,只不过是第二次多出现一个Only EPOLLOUT。

代码:

服务端-与结论1中代码相同,此处不在罗列

客户端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVPORT 9527
#define MAXBUF 1024

int main(void)
{
    char buf[MAXBUF];
    struct sockaddr_in servaddr;
    int fd;
    int n, len;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(SERVPORT);

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    {
        perror("connect");
        exit(1);
    }
    printf(">> Send "helloworld-1"n");
    send(fd, "helloworld-1", sizeof("helloworld-1") - 1, 0);
    sleep(5);
    printf(">> Send "helloworld-2"n");
    send(fd, "helloworld-2", sizeof("helloworld-2") - 1, 0);
    printf("Will colse after sleep 8s.n");
    sleep(8);
    close(fd);
    return 0;
}

结论3:在注册的时候,将EPOLLET|EPOLLIN|EPOLLOUT注册到epoll中, client不发送也不接收数据,sever一直
发送数据,直到将socket发送缓冲区打满, 此时client发送数据到server端, 这时server只会触发EPOLLIN事件
没有EPOLLOUT,原因是发送缓冲区是满的,不可写,所以不会触发EPOLLOUT。

模拟场景:设置server端socket 发送缓冲区是4096(4k), 设置client端接收rcvbuf缓冲区3072(3k),客户端不接收数据即不调用recv函数,服务端一直发送数据,最终会把服务端发送缓冲区打满,我们这里改变缓冲区大小,只是为了更快打满缓冲区。

输出数据:

代码:

服务端代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>


#define SERVPORT 9527
#define MAXBUF 1024*1024  //1MB
#define MAXFDS 5000
#define EVENTSIZE 100

int setnonblocking(int fd)
{
    int opts;
    if( (opts = fcntl(fd, F_GETFL, 0)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    opts = opts | O_NONBLOCK;
    if( (opts = fcntl(fd, F_SETFL, opts)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    return 0;
}

int main(void)
{
    int len = 0, n = 0, once = 0;
    int  hasSend;
    char buf[MAXBUF] = {0};
    char sndMsg[MAXBUF] = {1};

    struct sockaddr_in servaddr;
    int sockfd, listenfd, epollfd, nfds;

    struct epoll_event ev;
    struct epoll_event events[EVENTSIZE];

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERVPORT);

    if( (epollfd = epoll_create(MAXFDS)) == -1) {
        perror("epoll");
        exit(1);
    }

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if(setnonblocking(listenfd) == -1) {
        perror("setnonblocking");
        exit(1);
    }

    if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if(listen(listenfd, 10) == -1) {
        perror("listen");
        exit(1);
    }


    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = listenfd;
    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1) {
        perror("epoll_ctl");
        exit(1);
    }

    for( ; ; ) {
        if( (nfds = epoll_wait(epollfd, events, EVENTSIZE, -1)) == -1) {
            perror("epoll_wait");
            exit(1);
        }

        for(n = 0; n < nfds; n++) {
            if(events[n].data.fd == listenfd) {
                while( (sockfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) > 0) {
                    if(setnonblocking(sockfd) == -1) {
                        perror("setnonblocking");
                        exit(1);
                    }
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
                    ev.data.fd = sockfd;
                    int nSendBuf = 4096; //上层应用设置成4096 但是底层内核实际是4096*2 = 8192
                    socklen_t opt_len = sizeof(int);
                    int ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &nSendBuf, sizeof(int));
                    if (ret == -1) {
                        perror("setsockopt");
                    }
                    getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &nSendBuf, &opt_len);
                    printf("SndBuf-new  = %dn", nSendBuf);
                    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1)
                    {
                        perror("epoll_ctl");
                        exit(1);
                    } else {
                        printf("new socketfd = %d register epoll successn", sockfd);
                    }
                }
                continue;
            }

            printf("Events = 0x%xn", events[n].events);
            if (events[n].events & EPOLLIN && events[n].events & EPOLLOUT) {
                printf(">> EPOLLIN And EPOLLOUT event, socketfd = %dn", events[n].data.fd);
                len = recv(events[n].data.fd, buf, MAXBUF, 0);
                len = send(events[n].data.fd, sndMsg + hasSend, MAXBUF-hasSend, 0);
                if (len > 0) {// 说明发送成功
                    hasSend += len;
                    once = 0;
                    printf("EPOLLIN | EPOLLOUT ==》 Send ok, length = %d.n", len);
                } else if (len == -1) {
                    if (errno == EAGAIN) {//说明发送缓冲区已经满
                        printf("EPOLLIN | EPOLLOUT ==》 Socket SndBuf is fulled. n");
                    }
                } else {//出现错误
                    perror("EPOLLIN | EPOLLOUT ==》 Send failed.n");
                    exit(-1);
                }
            }
            else if (events[n].events & EPOLLIN) {
                printf(">> Only EPOLLIN event, socketfd = %dn", events[n].data.fd);
            }
            else if (events[n].events & EPOLLOUT) {
                printf(">> Only EPOLLOUT, socketfd = %dn", events[n].data.fd);

                while (1) {
                    len = send(events[n].data.fd, sndMsg + hasSend, MAXBUF-hasSend, 0);
                    if (len > 0) {// 说明发送成功
                        hasSend += len;
                        once = 0;
                        printf("Send ok, length = %d.n", len);
                    } else if (len == -1) {
                        if (errno == EAGAIN) {//说明发送缓冲区已经满
                            printf(" EPOLLOUT ==》 Socket SndBuf is fulled. n");
                            break;
                        }
                    } else {//出现错误
                        perror(" EPOLLOUT ==》 Send failed.n");
                        exit(-1);
                    }
                }
            }
        }
    }
}

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVPORT 9527
#define MAXBUF 1024

int main(void)
{
    char buf[MAXBUF];
    struct sockaddr_in servaddr;
    int fd;
    int n, len;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(SERVPORT);

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    int nSendBuf = 0;
    socklen_t opt_len = sizeof(int);
    nSendBuf = 3072;
    int ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, sizeof(int));
    if (ret == -1) {
        perror("setsockopt");
    }
    getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, &opt_len);
    printf("Client recvbuf len = %dn", nSendBuf);
    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    {
        perror("connect");
        exit(1);
    }
    printf("Connection ok, sleep 30sn");
    sleep(30); //睡眠60秒保证  服务端缓冲区被打满
    printf("Send msg==>""hello server!! do you sndbuf is fulled?n");
    send(fd, "hello server!! do you sndbuf is fulled?", strlen("hello server!! do you sndbuf is fulled?"), 0);
    sleep(10);
    send(fd, "hello server!! do you sndbuf is fulled?", strlen("hello server!! do you sndbuf is fulled?"), 0);
    sleep(10);
    close(fd);

    return 0;
}

结论4:在注册的时候,将EPOLLET|EPOLLIN|EPOLLOUT注册到epoll中, client不发送也不接收数据,sever一直发送数据,直到将socket 发送缓冲区打满, 此时client 开始连续接收n次数据(不发送数据), 此时server又会产生EPOLLOUT,因为发送缓冲区变成可写(由满->不满),由于服务端要发送1MB数据,客户端每次只读取256字节,所有服务端发送缓冲区状态变化是:满->不满->满(不可写->可写->不可写)。

输出数据:

代码:

服务端,与结论3中服务端代码一样

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>

#define SERVPORT 9527
#define MAXBUF 1024

int setnonblocking(int fd)
{
    int opts;
    if( (opts = fcntl(fd, F_GETFL, 0)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    opts = opts | O_NONBLOCK;
    if( (opts = fcntl(fd, F_SETFL, opts)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    return 0;
}

int main(void)
{
    char buf[MAXBUF];
    struct sockaddr_in servaddr;
    int fd;
    int n, len;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(SERVPORT);

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    int nSendBuf = 0;
    socklen_t opt_len = sizeof(int);
    nSendBuf = 3072;
    int ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, sizeof(int));
    if (ret == -1) {
        perror("setsockopt");
    }

    getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, &opt_len);
    printf("Client recvbuf len = %dn", nSendBuf);

    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    {
        perror("connect");
        exit(1);
    }
    setnonblocking(fd); //设置成非阻塞
    printf("Connection ok, sleep 10sn");
    sleep(10); //睡眠10秒保证  服务端缓冲区被打满

    while(1) {
        len = recv(fd, buf, 256, 0);// 由于服务端发送的数据是1MB所以需要多次调用
        if (len > 0) {
            n += len;
        } else if (len == -1 && errno == EAGAIN) {
            printf("EAGAIN, Has recv n = %dn", n);
            sleep(3);
        } else {
            printf("Recv failed.n");
            exit(-1);
        }
    }
    close(fd);
    return 0;
}

结论5:在注册的时候,将EPOLLET|EPOLLIN|EPOLLOUT注册到epoll中, client不发送也不接收数据,sever一直发送数据,直到将socket 发送缓冲区打满,打满之后server再也不会发送数据。 此时client 开始连续接收n次数据(不发送数据), 此时server又会产生EPOLLOUT,因为发送缓冲区变成可写(由满->不满)。

输出数据:

代码:

服务端代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>


#define SERVPORT 9527
#define MAXBUF 1024*1024  //1MB
#define MAXFDS 5000
#define EVENTSIZE 100

int setnonblocking(int fd)
{
    int opts;
    if( (opts = fcntl(fd, F_GETFL, 0)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    opts = opts | O_NONBLOCK;
    if( (opts = fcntl(fd, F_SETFL, opts)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    return 0;
}

int main(void)
{
    int len = 0, n = 0, once = 0;
    int  hasSend;
    char buf[MAXBUF] = {0};
    char sndMsg[MAXBUF] = {1};

    struct sockaddr_in servaddr;
    int sockfd, listenfd, epollfd, nfds;

    struct epoll_event ev;
    struct epoll_event events[EVENTSIZE];

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERVPORT);

    if( (epollfd = epoll_create(MAXFDS)) == -1) {
        perror("epoll");
        exit(1);
    }

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if(setnonblocking(listenfd) == -1) {
        perror("setnonblocking");
        exit(1);
    }

    if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if(listen(listenfd, 10) == -1) {
        perror("listen");
        exit(1);
    }


    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = listenfd;
    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) == -1) {
        perror("epoll_ctl");
        exit(1);
    }

    for( ; ; ) {
        if( (nfds = epoll_wait(epollfd, events, EVENTSIZE, -1)) == -1) {
            perror("epoll_wait");
            exit(1);
        }

        for(n = 0; n < nfds; n++) {
            if(events[n].data.fd == listenfd) {
                while( (sockfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) > 0) {
                    if(setnonblocking(sockfd) == -1) {
                        perror("setnonblocking");
                        exit(1);
                    }
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
                    ev.data.fd = sockfd;
                    int nSendBuf = 4096; //上层应用设置成4096 但是底层内核实际是4096*2 = 8192
                    socklen_t opt_len = sizeof(int);
                    int ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &nSendBuf, sizeof(int));
                    if (ret == -1) {
                        perror("setsockopt");
                    }
                    getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &nSendBuf, &opt_len);
                    printf("SndBuf-new  = %dn", nSendBuf);
                    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1)
                    {
                        perror("epoll_ctl");
                        exit(1);
                    } else {
                        printf("new socketfd = %d register epoll successn", sockfd);
                    }
                }
                continue;
            }

            printf("Events = 0x%xn", events[n].events);
            if (events[n].events & EPOLLIN && events[n].events & EPOLLOUT) {
                printf(">> EPOLLIN And EPOLLOUT event, socketfd = %dn", events[n].data.fd);
                len = recv(events[n].data.fd, buf, MAXBUF, 0);
                len = send(events[n].data.fd, sndMsg + hasSend, MAXBUF-hasSend, 0);
                if (len > 0) {// 说明发送成功
                    hasSend += len;
                    once = 0;
                    printf("EPOLLIN | EPOLLOUT ==》 Send ok, length = %d.n", len);
                } else if (len == -1) {
                    if (errno == EAGAIN) {//说明发送缓冲区已经满
                        printf("EPOLLIN | EPOLLOUT ==》 Socket SndBuf is fulled. n");
                    }
                } else {//出现错误
                    perror("EPOLLIN | EPOLLOUT ==》 Send failed.n");
                    exit(-1);
                }
            }
            else if (events[n].events & EPOLLIN) {
                printf(">> Only EPOLLIN event, socketfd = %dn", events[n].data.fd);
            }
            else if (events[n].events & EPOLLOUT) {
                printf(">> Only EPOLLOUT, socketfd = %dn", events[n].data.fd);

                while (!once) {
                    len = send(events[n].data.fd, sndMsg + hasSend, MAXBUF-hasSend, 0);
                    if (len > 0) {// 说明发送成功
                        hasSend += len;
                        printf("Send ok, length = %d.n", len);
                    } else if (len == -1) {
                        if (errno == EAGAIN) {//说明发送缓冲区已经满
                            printf(" EPOLLOUT ==》 Socket SndBuf is fulled. n");
                            once = 1;// 以后再也不会发送数据了,保证缓冲区不会再满
                            break;
                        }
                    } else {//出现错误
                        perror(" EPOLLOUT ==》 Send failed.n");
                        exit(-1);
                    }
                }
            }
        }
    }
}

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>

#define SERVPORT 9527
#define MAXBUF 4096

int setnonblocking(int fd)
{
    int opts;
    if( (opts = fcntl(fd, F_GETFL, 0)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    opts = opts | O_NONBLOCK;
    if( (opts = fcntl(fd, F_SETFL, opts)) == -1)
    {
        perror("fcntl");
        return -1;
    }

    return 0;
}

int main(void)
{
    char buf[MAXBUF];
    struct sockaddr_in servaddr;
    int fd;
    int n, len;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(SERVPORT);

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    int nSendBuf = 0;
    socklen_t opt_len = sizeof(int);
    nSendBuf = 3072;
    int ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, sizeof(int));
    if (ret == -1) {
        perror("setsockopt");
    }

    getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nSendBuf, &opt_len);
    printf("Client recvbuf len = %dn", nSendBuf);

    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    {
        perror("connect");
        exit(1);
    }
    setnonblocking(fd); //设置成非阻塞
    printf("Connection ok, sleep 10sn");
    sleep(10); //睡眠10秒保证  服务端缓冲区被打满
    int i = 0;
    while(1) {
        len = recv(fd, buf, MAXBUF, 0);
        if (len > 0) {
            n += len;
            printf("第%d次,recv len = %dn", ++i, len);
            sleep(10);
        } else if (len == -1 && errno == EAGAIN) {
            printf("EAGAINn", n);
            sleep(20);
            break;//默认读取完毕
        } else {
            printf("Recv failed.n");
            exit(-1);
        }
    }
    printf("closen");
    close(fd);
    return 0;
}


三、建议

1、对于服务端listen socket不需要将EPOLLOUT注册到epoll事件模型中。因为listen socket只是负责接收数据(接收客户端建立连接请求),不会发送数据,所以不需要注册时EPOLLOUT。

2、按需注册EPOLLOUT。当我们调用send接口时,如果返回的-1且errno=EAGAIN时,再注册EPOLLOUT,后续send发送成功后,再将EPOLLOUT从epoll事件模型中移除,这就是按需注册EPOLLOUT。当然我们也可以不用移除,只不过需要判断是否真的有数据需要发送。大名鼎鼎的nginx的做法是:发送完成后会将发送的回调函数设置成一个空函数(这个函数只是定义里面什么都没有做)。nginx为什么不移除呢?因为反复添加、移除EPOLLOUT性能不友好,总是在用户层和内核层来回切换。

下一篇介绍,EPOLLRDHUP。

最后

以上就是大意猎豹为你收集整理的简约而不简单epoll之EPOLLOUT一、序二、EPOLLIN|EPOLLOUT同时注册到epoll事件模型三、建议的全部内容,希望文章能够帮你解决简约而不简单epoll之EPOLLOUT一、序二、EPOLLIN|EPOLLOUT同时注册到epoll事件模型三、建议所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部