Unix / C问题在这里.
我有多个套接字,我试图轮询周期性数据.我不希望select无限期地等待,所以我有一个超时,我正在循环运行.我发现一旦套接字准备好读取,它就可以随时读取.因为,当没有任何数据要从任何套接字读取时,我无法选择进入休眠状态.
for (i = 0; i < n_connections; i++) { FD_SET( sockfd[i],&master ); if (sockfd[i] > fdmax) fdmax = sockfd[i]; } for(;;) { int nready = 0; timeout.tv_sec = 1; timeout.tv_usec = 0; read_fds = master; if ( (nready = select(fdmax+1,&read_fds,NULL,NULL)) == -1 ) { fprintf( stderr,"Select Error\n" ); return FAILURE; } printf( "Number of ready descriptors: %d\n",nready ); for (i = 0; i <= fdmax; i++) { if (FD_ISSET(i,&read_fds)) { if (( nbytes = recv(i,buf,sizeof(buf),0)) <= 0 ) { if (nbytes == 0) { //connection closed printf("Socket %d hung up\n",i ); } else { fprintf( stderr,"Recv Error %d\n",nbytes); } } else { printf( "Data Received on %d: %s\n",i,buf ); } } } // end file descriptor loop
似乎在我第一次读取之后,1秒超时不再适用,并且套接字始终“准备好读取”,即使有0个字节可用.如何在数据进入之前选择进入休眠状态(一秒钟,或者将最终参数切换为NULL,无限期地等待数据进入套接字?)
输出:
Number of Ready Descriptors: 2 Data Received on 4: GreetingsChap Data Received on 5: HiMatengsChap Loop... Number of Ready Descriptors: 2 Socket 4 hung up Socket 5 hung up Loop... Number of Ready Descriptors: 2 Socket 4 hung up Socket 5 hung up Loop...
谢谢,
注意:为清晰起见,代码已更新
根据@yvesBraumes建议更新 – 仍然无法正常工作.