select监听socket请求超时处理
本帖最后由 xcy19890309 于 2013-12-17 17:10:13 编辑 ......省去socket初始化
fd_set wfds;
struct timeval tv;
FD_ZERO(&wfds);
FD_SET(sock, &wfds);
tv.tv_sec = 200;
tv.tv_usec = 0;
ret = select(sock+1, NULL, &wfds, NULL, &tv);
if(ret < 0){
printf("select failed in %s and %d\n",__FUNCTION__,__LINE__);
close(fd);
return 0;
}else if(ret == 0){
if(timeOutVal == 5){
close(fd);
return 0;
}
timeOutVal++;
}else if(FD_ISSET(sock, &wfds)){
NSLog("crash!");
}
当我测试网络超时时,为什么没执行到ret==0那个分支,而是执行了最后一个分支,导致程序崩溃!求解!
[解决办法]
对方进程终止连接(发FIN),select fd读/写会返回0,这时读写socket会触发SIGPIPE,进程终止
which may be zero if the timeout expires before anything interesting happens.