23,116
社区成员
发帖
与我相关
我的任务
分享
for ( ; ; ) {
cout << "time:" << time(NULL) <<endl;
//等待epoll事件的发生
nfds=epoll_wait(epfd,events,20,-1);
//处理所发生的所有事件
for(i=0;i<nfds;++i)
{
if(events[i].events&EPOLLIN)
{
cout << "EPOLLIN" << endl;
if ( (sockfd = events[i].data.fd) < 0)
continue;
n = read(sockfd, line, MAXLINE);
cout << "n:" << n << endl;
if ( n < 0) {
if (errno == ECONNRESET) {
close(sockfd);
events[i].data.fd = -1;
} else
std::cout<<"readline error"<<std::endl;
} else if (n == 0) {
close(sockfd);
events[i].data.fd = -1;
}
line[n] = 0;
cout << "read " << line << endl;
//设置用于写操作的文件描述符
ev.data.fd=sockfd;
//ev.events=EPOLLET|EPOLLIN;
ev.events=EPOLLIN;
epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
}