zoukankan      html  css  js  c++  java
  • 观察select超时的表现

     1 #include <sys/time.h>
     2 #include <stdio.h>
     3 #include <sys/types.h>
     4 #include <sys/stat.h>
     5 #include <fcntl.h>
     6 #include <assert.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     9 #include <unistd.h>
    10 #include <sys/socket.h>
    11 #include <netinet/in.h>
    12 #include <arpa/inet.h>
    13 #include <errno.h>
    14 #include <sys/select.h>
    15 
    16 int main()
    17 {
    18     int keyboard;
    19     int ret,i;
    20     char c;
    21     fd_set readfd;
    22     struct timeval timeout;
    23     keyboard = open("/dev/tty",O_RDONLY|O_NONBLOCK);
    24     assert(keyboard>0);
    25     while(1){
    26         timeout.tv_sec = 5;
    27         timeout.tv_usec = 0;
    28         FD_ZERO(&readfd);
    29         FD_SET(keyboard,&readfd);
    30         ret = select(keyboard+1,&readfd,NULL,NULL,&timeout);
    31         if(ret == -1){
    32             perror("select error!
    ");
    33         }
    34         else if(ret){
    35             if(FD_ISSET(keyboard,&readfd)){
    36                 i = read(keyboard,&c,1);
    37                 if('
    '==c)
    38                     continue;
    39                 printf("The input is %c
    ",c);
    40                 if('q'==c)
    41                     break;
    42             }
    43         }
    44         else if(ret == 0){
    45             printf("time out !
    ");
    46         }
    47     }
    48     return 0;
    49 }

     

  • 相关阅读:
    win10安装Redis
    imemode属性解释
    DataTable导出到Excel
    Oracle中wm_concat函数报错解决方法
    GridView中生成复合表头
    WinXP下无法打开ACCESS解决办法
    提醒功能
    网页常用Javascript
    ASP.NET记录错误日志
    GridView中Js的应用
  • 原文地址:https://www.cnblogs.com/--lr/p/11295821.html
Copyright © 2011-2022 走看看