Linux----网络编程(ET模式与LT模式的区别及代码演示)

 

epoll对文件描述符的操作有两种模式:LT模式和ET模式。

 

LT模式(普通模式):也叫水平触发。描述符上有数据就绪,如果用户没有处理完,可以反复提醒,当下一轮IO函数执行时会继续提醒用户该描述符上有数据,直到用户将数据读完为止。

 

ET模式(高效模式):也叫边沿触发。描述符上有数据就绪,如果用户把数据没有处理或没处理完,也只提醒一次,下一轮IO函数执行时不会提醒,除非有新数据到达。

 

一、LT模式

ltepoll.c

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

//ltepoll
#define MAXFD 10

void epoll_add(int epfd, int fd)
{
    struct epoll_event ev;

   //ev.events = EPOLLIN;
    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = fd;

   if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1)
    {
       perror("epoll ctl error");
   }


}

void epoll_del(int epfd, int fd)
{
    if(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) == -1)
    {
        perror("epoll ctl del error");
    }
}

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    assert(sockfd != -1);

    struct sockaddr_in saddr, caddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值