免费注册 查看新帖 |

Chinaunix

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

为什么我的bash不能接受stdin输入?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-15 16:10 |只看该作者 |倒序浏览
我的程序如下,帮忙看看:

serv.c
=====
  1. #include <sys/socket.h>;
  2. #include <netinst.h>;
  3. #include <stdlib.h>;

  4. int main()
  5. {
  6.         char *name[2];
  7.         int fd,fd2,fromlen;
  8.         struct sockaddr_in serv;

  9.         fd=socket(AF_INET,SOCK_STREAM,0);
  10.         serv.sin_addr.s_addr=0;
  11.         serv.sin_port=htons("1234");
  12.         serv.sin_family=AF_INET;
  13.         bind(fd,(struct sockaddr *)&serv,16);
  14.         listen(fd,1);
  15.         fromlen=16; /*(sizeof(struct sockaddr)*/
  16.         fd2=accept(fd,(struct sockaddr *)&serv,&fromlen);
  17.         /* "connect" fd2 to stdin,stdout,stderr */
  18.         dup2(fd2,0);
  19.         dup2(fd2,1);
  20.         dup2(fd2,2);
  21.         name[0]="/bin/sh";
  22.         name[1]=NULL;
  23.         execve(name[0],name,NULL);
  24. }
复制代码


我在tty1运行这个程序,然后在tty2执行:
$ telnet localhost 1234
Trying 127.0.0.1...
Connected to lubo.
Escape character is '^]'.
ls
: command not found
/bin/ls
: No such file or directory

这是怎么回事?我另外测试了一个server程序,却可以读取stdin,如下:

test.c
=====
  1. #include <stdlio.h>;

  2. main()
  3. {
  4.         char buf[] = "12345678";

  5.         gets(buf);
  6.         puts(buf);
  7. }
复制代码

然后修改serv.c的19行:name[0]="test";
编以后运行,在tty2上执行:
$ telnet localhost 1234
Trying 127.0.0.1...
Connected to lubo.
Escape character is '^]'.
asdf
asdf
Connection closed by foreign host.

请高手指点,谢谢。

论坛徽章:
0
2 [报告]
发表于 2004-10-15 18:55 |只看该作者

为什么我的bash不能接受stdin输入??

怎么没人回答我的问题呢?自己顶一下。

论坛徽章:
0
3 [报告]
发表于 2004-10-18 12:28 |只看该作者

为什么我的bash不能接受stdin输入??

name[0]="/bin/bash";
name[1]="-i";
name[2]=NULL;

论坛徽章:
0
4 [报告]
发表于 2004-10-18 13:04 |只看该作者

为什么我的bash不能接受stdin输入??

我的REDHAT机器上没有netinst.h这个,不能试,但是我认为应该是



  1. name[0]="/bin/sh";
  2. name[1]="-c";
  3. name[2]=NULL;
复制代码


如果是bash也是-c[/code]

论坛徽章:
0
5 [报告]
发表于 2004-10-18 13:26 |只看该作者

为什么我的bash不能接受stdin输入??

还是版主的正解,应该加个"-i"参数,表示是"interactive shell"的意思。多谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP