www.pudn.com > linuxsocket.rar > test_select.c
// 示例函数select()的使用 #include#include #include #include #include void display_time(const char *string) { int seconds; seconds = time((time_t*)NULL); printf("%s, %d\n", string, seconds); } int main(void) { fd_set readfds; struct timeval timeout; int ret; /*监视文件描述符0是否有数据输入,即文件描述符0表示标准输入即键盘输入*/ FD_ZERO(&readfds); // 开始使用一个描述符集合前一般要将其清空 FD_SET(0, &readfds); /*设置阻塞时间为10秒*/ timeout.tv_sec = 10; timeout.tv_usec = 0; while (1) { display_time("before select"); ret = select(1, &readfds, NULL, NULL, &timeout); display_time("after select"); switch (ret) { case 0: printf("No data in ten seconds.\n"); exit(0); break; case -1: perror("select"); exit(1); break; default: getchar(); // 将数据读入,否则标准输入上将一直为读就绪 printf("Data is available now.\n"); } } return 0; }