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

Kevin_qing 2006-03-11 03:19:11
事件处理部分


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.
并且后来就没有其他事件产生了,如何判断一个连接已经关闭????
...全文
327 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kevin_qing 2006-03-11
  • 打赏
  • 举报
回复
- -!
啊奥~~~~~嘿嘿
fierygnu 2006-03-11
  • 打赏
  • 举报
回复
recv返回0就是连接关闭:)

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧