然后打印 No data within 3 seconds, 有时候几行简单的实例比文字说明更直观了
程序代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
int main(void) {
int keyboard;
int ret, i;
char c;
fd_set readfd;
struct timeval timeout;
keyboard = open( "/dev/tty", O_RDONLY | O_NONBLOCK);
assert(keyboard > 0);
while(1) {
timeout.tv_sec = 3;
timeout.tv_usec = 0;
FD_ZERO(&readfd);
FD_SET (keyboard, &readfd);
ret = select ( keyboard + 1, &readfd, NULL, NULL, &timeout);
if(0 == ret )
printf("No data within 3 seconds\n");
if( FD_ISSET ( keyboard, &readfd)) {
i = read(keyboard, &c, 1);
if( '\n' == c )
continue;
printf("wzj typed '%c'\n", c);
if('q' == c )
break;
}
}
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
int main(void) {
int keyboard;
int ret, i;
char c;
fd_set readfd;
struct timeval timeout;
keyboard = open( "/dev/tty", O_RDONLY | O_NONBLOCK);
assert(keyboard > 0);
while(1) {
timeout.tv_sec = 3;
timeout.tv_usec = 0;
FD_ZERO(&readfd);
FD_SET (keyboard, &readfd);
ret = select ( keyboard + 1, &readfd, NULL, NULL, &timeout);
if(0 == ret )
printf("No data within 3 seconds\n");
if( FD_ISSET ( keyboard, &readfd)) {
i = read(keyboard, &c, 1);
if( '\n' == c )
continue;
printf("wzj typed '%c'\n", c);
if('q' == c )
break;
}
}
}