LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 883|回复: 1

[gcc编程]请进。

[复制链接]
自由狼-台风 该用户已被删除
发表于 2003-2-26 14:50:20 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2003-2-26 17:51:27 | 显示全部楼层
我觉得通过管道应该有办法实现。
但并不确定,尤其不确定是否是更好的办法。
我给个例子你看一下,就是那个防idle的BBS程序。
因为我现在没有时间写程序来测试,所以你先试试,
等我忙完手头上的东西,就来写个程序看看。


  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <stdio.h>
  9. struct termios tsave;
  10. void
  11. scan_mode (void)
  12. {
  13.         struct termios tbuf;

  14.         if (!isatty (0))
  15.                 exit (1);
  16.         if (tcgetattr (0, &tbuf) == -1)
  17.                 exit (1);
  18.         tsave = tbuf;
  19.         tbuf.c_lflag &= ~(ECHO | ICANON | ISIG);
  20.         tbuf.c_cc[VMIN] = tbuf.c_cc[VTIME] = 0;
  21.         if (tcsetattr (0, TCSANOW, &tbuf) == -1)
  22.                 exit (1);
  23. }

  24. void
  25. restore_mode (int i)
  26. {
  27.         tcsetattr (0, TCSANOW, &tsave);
  28.         exit (1);
  29. }
  30. main (int argc, char **argv)
  31. {
  32.         int     fdin[2], fdout[2];
  33.         int     fdw, fdr, i;
  34.         int     j;
  35.         fd_set  rdfds;
  36.         struct timeval timeout;
  37.         char    bufer[1024], ch = 12;

  38.         if (pipe (fdin) == -1)
  39.                 exit (1);
  40.         if (pipe (fdout) == -1)
  41.                 exit (1);
  42.         switch (fork ())
  43.         {
  44.         case -1:
  45.                 exit (1);
  46.         case 0:
  47. //printf("son\n");
  48.                 close (0);
  49.                 dup (fdin[0]);
  50.                 close (fdin[0]);
  51.                 close (fdin[1]);
  52.                 close (1);
  53.                 dup (fdout[1]);
  54.                 close (fdout[0]);
  55.                 close (fdout[1]);
  56.                 execl ("/usr/bin/telnet", "telnet", "202.120.225.9", 0);
  57. //这里你可以把202.112.58.200换成别的bbs地址.
  58.                 printf ("failed %s", argv[1]);
  59.                 exit (1);
  60.         default:
  61. //printf("parent");
  62.                 close (fdin[0]);
  63.                 fdw = fdin[1];
  64.                 close (fdout[1]);
  65.                 fdr = fdout[0];
  66.         }
  67.         scan_mode ();
  68.         (void) signal (SIGPIPE, restore_mode);
  69.         fcntl (0, F_SETFL, O_NONBLOCK);
  70.         fcntl (fdr, F_SETFL, O_NONBLOCK);
  71.         timeout.tv_sec = 600;
  72.         timeout.tv_usec = 0;
  73.         while (1)
  74.         {
  75.                 FD_ZERO (&rdfds);
  76.                 FD_SET (0, &rdfds);
  77.                 FD_SET (fdr, &rdfds);
  78.                 if (select (fdr + 1, &rdfds, NULL, NULL, &timeout))
  79.                 {
  80.                         i = read (0, bufer, 1024);
  81.                         if (i > 0)
  82.                         {
  83.                                 timeout.tv_sec = 600;
  84.                                 timeout.tv_usec = 0;
  85.                                 write (fdw, bufer, i);
  86.                         }
  87.                         i = read (fdr, bufer, 1024);
  88.                         if (i > 0)
  89.                         {
  90.                                 write (1, bufer, i);
  91.                         }
  92.                 }
  93.                 else
  94.                 {
  95.                         timeout.tv_sec = 600;
  96.                         timeout.tv_usec = 0;
  97.                         write (fdw, &ch, 1);
  98.                 }
  99.         }
  100. }

  101. //end

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表