免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2516 | 回复: 1

perldoc perlipc中的interactive client代码在Win上失败(fork进程分管socket和stdin) [复制链接]

论坛徽章:
0
发表于 2010-05-24 17:48 |显示全部楼层
本帖最后由 maybenot 于 2010-05-26 15:18 编辑

文档里说,fork出一个进程,父进程听socket,子进程听stdin。

我试了试,当client放在Linux上是可以的(此时server放哪都可),而client放windows上就失败。

fork没有报错,阻塞在print $handle $line;这行,server根本收不到

还试了试chomp($line); print $handle “$line\r\n"; 也不行

环境Windows XP, perl 5.10.0

望高手赐教。多谢!

代码如下,我就改了第一行,原来是/usr/bin/perl

  1. #!C:/perl/bin/perl.exe -w
  2. #
  3. use strict;
  4. use IO::Socket;
  5. my ($host, $port, $kidpid, $handle, $line);

  6. unless (@ARGV == 2) { die "usage: $0 host port" }
  7. ($host, $port) = @ARGV;

  8. # create a tcp connection to the specified host and port
  9. $handle = IO::Socket::INET->new(Proto     => "tcp",
  10.                                         PeerAddr  => $host,
  11.                                         PeerPort  => $port)
  12.            or die "can't connect to port $port on $host: $!";

  13. $handle->autoflush(1);              # so output gets there right away
  14. print STDERR "[Connected to $host:$port]\n";

  15. # split the program into two processes, identical twins
  16. die "can't fork: $!" unless defined($kidpid = fork());

  17. # the if{} block runs only in the parent process
  18. if ($kidpid) {
  19.         # copy the socket to standard output
  20.         while (defined ($line = <$handle>)) {
  21.                 print STDOUT $line;
  22.         }
  23.         kill("TERM", $kidpid);                  # send SIGTERM to child
  24. }
  25. # the else{} block runs only in the child process
  26. else {
  27.         # copy standard input to the socket
  28.         while (defined ($line = <STDIN>)) {
  29.                 print $handle $line;                   # 这里塞住了
  30.         }
  31. }

复制代码

论坛徽章:
0
发表于 2010-12-03 13:37 |显示全部楼层
我也有同样的疑惑,不知哪个高手解释一下,win32下fork的情况
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP