epoll的两种触发方式:
代码实现如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define EPOLL_SIZE 1024
int main(void) {
//epoll_create()
//epoll_ctl()
//epoll_wait()
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0) return -1;
struct sockaddr_in serv_addr;
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(6016);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0) {
perror("bind fail");
return -2;
}
if(listen(sockfd, 5) < 0) {
perror("listen error");
return -3;
}
int epfd = epoll_create(1);
struct epoll_event ev, events[EPOLL_SIZE]={0};
ev.events = EPOLLIN|EPOLLLT;
ev.data.fd = sockfd; //传参数 在epoll_wait返回
epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&ev);
while(1) {
int nready = epoll_wait(epfd,events,EPOLL_SIZE,-1);
if(nready == -1)continue;
int i = 0;
for(i = 0 ; i < nready; i++ ) {
if(events[i].data.fd == sockfd) {
struct sockaddr_in clientaddr;
socklen_t client_len = sizeof(clientaddr);
int clientfd = accept(sockfd,(struct sockaddr*)&clientaddr,&client_len);
if(clientfd <0) continue;
ev.events = EPOLLIN|EPOLLET; //边缘触发
ev.data.fd = clientfd;
epoll_ctl(epfd,EPOLL_CTL_ADD,clientfd,&ev)
printf("sockfd:%d,max_fd:%d,clientfd:%dn",sockfd,maxfd,clientfd);
}else {
int clientfd = events[i].data.fd;
char buffer[1024]={0};
int ret = recv(clientfd,buffer,1024,0);
if(ret < 0) {
if(errno == EAGAIN || errno == EWOULDBLOCK) {//多线程和多进程时--tcb块的recvbuf里面的数据在其他线程已经读过了
printf("read all data");
continue;
}else{
close(clientfd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, clientfd, &ev);
}
}else if(ret == 0){ //客户端断开连接
printf(" disconnect %dn", i);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, clientfd, &ev);
break;
}else {
printf("Recv: %s, %d Bytesn", buffer, ret);
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)