- 论坛徽章:
- 0
|
/************ 原贴经 flw 整理后内容如下 ***************/
- #include <sys/types.h>;
- #include <sys/time.h>;
- #include <fcntl.h>;
- #include <stdio.h>;
- int fds;
- int openFiles(char *);
- void readFiles(fd_set *);
- int main(int argc, char **argv)
- {
- fd_set readfds;
- int i, n, maxfd;
- char buf[255];
-
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <ttypath>;\n", argv[0]);
- exit(1);
- }
- maxfd = openFiles(argv[1]);
- printf("maxfd = %d\n", maxfd);
- for (;;) {
- FD_ZERO(&readfds);
- FD_SET(fds, &readfds);
- n = select(maxfd, &readfds, NULL, NULL, NULL);
- switch(n) {
- case -1:
- perror("select error\n");
- exit(1);
- default:
- printf("receiving.....\n");
- if (FD_ISSET(fds, &readfds)) {
- read(fds, buf, sizeof(buf));
- printf("%s\n",buf);
- }
- break;
- }
- }
-
- }
- int openFiles(char *files)
- {
- int i, maxfd;
-
- maxfd = 0;
- if ((fds = open(files, O_RDONLY) < 0)) {
- perror(files);
- exit(1);
- }
- if (!isatty(fds)) {
- fprintf(stderr, "<ttypath>; must be tty devices.\n");
- exit(1);
- }
- printf("fds = %d\n", fds);
- if (fds >; maxfd)
- maxfd = fds;
- return(maxfd+1);
- }
复制代码
我的目的要实现输入如:
progname /dev/pts/2 (/dev/pts/2是另一用户登陆进来的tty)
看到他的屏幕上所输入和所显示的所有东西
现在的问题是:
select()一直阻塞,没有反应,调试下来,open()返回总为0,所以 maxfd总为1,不知道问题出在哪里?[/code] |
|