在尝试 select() 时,我得到了一个奇怪的结果。
fd_set tempset,set; //set of descriptors
FD_ZERO(&set); //initialize descriptor set to NULL
FD_SET(serversock,&set); //add server's socket to descriptor set 'set'
timeout.tv_sec=2;
timeout.tv_usec=0;
while(1){
tempset=set;
timeout.tv_sec=2;
timeout.tv_usec=0;
printf("%s","Waiting on select\n");
int ret=select(FD_SETSIZE,&tempset,NULL,NULL,&timeout);
if(ret==0) printf("timeout ");
else if(ret==-1){
printf("Error occured\n");
exit(0);
}
else if(ret>0){ //socket ready for reading
for(i=0;i<FD_SETSIZE;i++){
if(FD_ISSET(i,&tempset)){ //check if it's serversock
if(i==serversock){
//accept connection and read/write
printf("Client connected\n");
}//i==serversock close
}
}//for ends
}
当我删除行 printf("%s","Waiting on select\n"); 选择无限期地等待。但是,当我重新插入线路时,一切都按预期工作(通过客户端连接测试)
我错过了什么吗?
提前致谢!