epoll连接关闭问题..............

Kevin_qing Kevin_qing 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
事件处理部分


for(i=0;i<nfds;++i){
if(events[i].data.fd==fdlisten){
clilen=sizeof(clientaddr);
int connfd = accept(fdlisten,(sockaddr *)&clientaddr, &clilen);
if(connfd<0){
perror("connfd<0");
exit(1);
}
setnonblocking(connfd);

char *str = inet_ntoa(clientaddr.sin_addr);
printf("connection from %s\n",str);

ev.data.fd=connfd;
ev.events=EPOLLIN|EPOLLET|EPOLLERR|EPOLLHUP;
epoll_ctl(fdep,EPOLL_CTL_ADD,connfd,&ev);
}else if(events[i].events&EPOLLIN){
char tmp[1024];
int n;
if((n=recv(events[i].data.fd,tmp,1023,0))<=0){
printf("recv[%d] %s\n",n,strerror(errno));
if(errno==ECONNRESET){
printf("fd %d connection reset \n",events[i].data.fd);
int r=epoll_ctl(fdep,EPOLL_CTL_DEL,events[i].data.fd,NULL);
assert(r==0);
close(events[i].data.fd);
}
}else{
tmp[n]=0;
printf("recv[%d]:%s\n",n,tmp);
}
}
if(events[i].events&EPOLLERR){
assert(false);
}
if(events[i].events&EPOLLHUP){
assert(false);
}




使用telnet 测试连接

#./testepoll
epoll fd 3
socket fd 4
connection from 127.0.0.1
recv[6]:asdf

recv[0] Success


在telnet关闭连接时,recv返回是0
errno是Success.
并且后来就没有其他事件产生了,如何判断一个连接已经关闭????
...全文
评论 2
收藏 已收藏
分享
返回顶部
Kevin_qing
Linux/Unix社区 23,224 成员 74,535 内容