标签:des style blog http color os io 文件
select 可以感知文件表述符集合中的变化,如果办fd0(即标准输入)放入select的read fd set,发现只有按回车的时候select才会返回。查了下要把终端的缓冲大小设为1,这样就能实现击键后马上反应了。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <termio.h> #include <sys/time.h> #include <sys/types.h> #define BUF_SIZE 256 void setup_terminal(struct termios* old_args); void reset_terminal(struct termios* old_args); int main() { struct termios old_args; setup_terminal(&old_args); fd_set rfds; int sres = 0; char buffer[BUF_SIZE]; int nread = 0; for (;;) { /* reset read file descriptor set */ FD_ZERO(&rfds); /* add standard input file descriptor to the set */ FD_SET(0, &rfds); /* do blocking select */ printf("\nselect begin\n"); sres = select(0 + 1, &rfds, NULL, NULL, NULL); printf("\nselect return\n"); if (sres < 0) { perror("select error"); break; } else if (sres == 0) { /* should not be here for blocked select without timeout*/ printf("select timeout\n"); } else { if (FD_ISSET(0, &rfds)) { nread = read(0, buffer, sizeof(buffer)-1); if (nread <= 0) { perror("read error"); break; } buffer[nread] = ‘\0‘; printf("read string(%d): %s\n", nread, buffer); } } } reset_terminal(&old_args); return 0; } void setup_terminal(struct termios* old_args) { tcgetattr(0, old_args); struct termios new_args = *old_args; new_args.c_lflag &= ~ICANON; new_args.c_cc[VMIN] = 1; tcsetattr(0, TCSANOW, &new_args); } void reset_terminal(struct termios* old_args) { tcsetattr(0, TCSANOW, old_args); }
记得很早之前也一致想实现这个功能,终于如愿,虽然和socket编程没什么太大关系。
参考:
http://tieba.baidu.com/p/685575698
Linux 套接字编程 - select,布布扣,bubuko.com
标签:des style blog http color os io 文件
原文地址:http://www.cnblogs.com/lailailai/p/3917723.html