免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3250 | 回复: 12
打印 上一主题 下一主题

错在哪里?一直阻塞在select [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-19 14:19 |只看该作者 |倒序浏览
/************ 原贴经 flw 整理后内容如下 ***************/
  1. #include <sys/types.h>;
  2. #include <sys/time.h>;
  3. #include <fcntl.h>;
  4. #include <stdio.h>;

  5. int fds;

  6. int openFiles(char *);
  7. void readFiles(fd_set *);

  8. int main(int argc, char **argv)
  9. {
  10.   fd_set readfds;
  11.   int i, n, maxfd;
  12.   char buf[255];
  13.   
  14.   if (argc != 2) {
  15.         fprintf(stderr, "Usage: %s <ttypath>;\n", argv[0]);
  16.         exit(1);
  17.   }
  18.   maxfd = openFiles(argv[1]);
  19.   printf("maxfd = %d\n", maxfd);
  20.   for (;;) {
  21.         FD_ZERO(&readfds);
  22.   FD_SET(fds, &readfds);
  23.   n = select(maxfd, &readfds, NULL, NULL, NULL);
  24.   switch(n) {
  25.     case -1:
  26.         perror("select error\n");
  27.         exit(1);
  28.     default:
  29.         printf("receiving.....\n");
  30.         if (FD_ISSET(fds, &readfds)) {
  31.                 read(fds, buf, sizeof(buf));
  32.                 printf("%s\n",buf);
  33.         }
  34.         break;
  35.   }
  36.   }              
  37.         
  38. }

  39. int openFiles(char *files)
  40. {
  41.   int i, maxfd;
  42.   
  43.   maxfd = 0;
  44.   if ((fds = open(files, O_RDONLY) < 0)) {
  45.         perror(files);
  46.         exit(1);
  47.   }
  48.   if (!isatty(fds)) {
  49.         fprintf(stderr, "<ttypath>; must be tty devices.\n");
  50.         exit(1);
  51.   }
  52.   printf("fds = %d\n", fds);
  53.   if (fds >; maxfd)
  54.         maxfd = fds;
  55.   return(maxfd+1);
  56. }  
复制代码

我的目的要实现输入如:
progname /dev/pts/2 (/dev/pts/2是另一用户登陆进来的tty)
看到他的屏幕上所输入和所显示的所有东西
现在的问题是:
select()一直阻塞,没有反应,调试下来,open()返回总为0,所以 maxfd总为1,不知道问题出在哪里?[/code]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2003-02-19 14:24 |只看该作者

错在哪里?一直阻塞在select

要把终端设置成非阻塞的。

论坛徽章:
0
3 [报告]
发表于 2003-02-19 14:43 |只看该作者

错在哪里?一直阻塞在select

楼主写代码时用
[code]
功能写一下可以吗
这样大家提得都方便

论坛徽章:
0
4 [报告]
发表于 2003-02-19 14:44 |只看该作者

错在哪里?一直阻塞在select

另外你的main函数什么那么多

论坛徽章:
0
5 [报告]
发表于 2003-02-19 14:46 |只看该作者

错在哪里?一直阻塞在select

FD_ZERO(&amp;readfds);
FD_SET(fds, &amp;readfds);
n = select(maxfd, &amp;readfds, NULL, NULL, NULL);

根据你写的
maxfd应该为
readfd+1

论坛徽章:
0
6 [报告]
发表于 2003-02-19 14:48 |只看该作者

错在哪里?一直阻塞在select

另外select 的最后一个参数是NULL表示等待无限长时间

这与不使用select 功能是一样的

论坛徽章:
0
7 [报告]
发表于 2003-02-19 15:41 |只看该作者

错在哪里?一直阻塞在select

你输入

  1. [code]时应该使用英文输入
复制代码

[/code]

论坛徽章:
0
8 [报告]
发表于 2003-02-20 16:21 |只看该作者

错在哪里?一直阻塞在select

我的问题还没有解决,请大家帮忙

论坛徽章:
0
9 [报告]
发表于 2003-02-20 16:52 |只看该作者

错在哪里?一直阻塞在select

不会吧,你的是什么平台?
我在linux下可以的。

论坛徽章:
0
10 [报告]
发表于 2003-02-20 21:25 |只看该作者

错在哪里?一直阻塞在select

我在sco5.0.5和linux redhat 8.0得到的结果和楼主一样

open("/dev/ttyp22", READONLY)始终都是得到0
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP