- 论坛徽章:
- 0
|
本帖最后由 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
- #!C:/perl/bin/perl.exe -w
- #
- use strict;
- use IO::Socket;
- my ($host, $port, $kidpid, $handle, $line);
- unless (@ARGV == 2) { die "usage: $0 host port" }
- ($host, $port) = @ARGV;
- # create a tcp connection to the specified host and port
- $handle = IO::Socket::INET->new(Proto => "tcp",
- PeerAddr => $host,
- PeerPort => $port)
- or die "can't connect to port $port on $host: $!";
- $handle->autoflush(1); # so output gets there right away
- print STDERR "[Connected to $host:$port]\n";
- # split the program into two processes, identical twins
- die "can't fork: $!" unless defined($kidpid = fork());
- # the if{} block runs only in the parent process
- if ($kidpid) {
- # copy the socket to standard output
- while (defined ($line = <$handle>)) {
- print STDOUT $line;
- }
- kill("TERM", $kidpid); # send SIGTERM to child
- }
- # the else{} block runs only in the child process
- else {
- # copy standard input to the socket
- while (defined ($line = <STDIN>)) {
- print $handle $line; # 这里塞住了
- }
- }
复制代码 |
|