H
Hobbit HK
I'm using AtivePerl 5.8 on WinXP and trying to do a script which
listens and writes to the same socket (with fork of course...). Now,
everything works great until I'm trying to write to the socket... The
program just stucks, I think because I'm trying to read and write
simultaneously... If I comment the read part everything goes smooth
(except I can't get msgs ), here's the code:
use IO::Socket;
$|=1;
($host, $port) = @ARGV;
$port=23 if !$port;
$sock=new IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port,
Proto=>'tcp') or die "Can't connect to $host on port $port! $!\n";
$sock->autoflush(1);
print "[Connected to $host on port $port]\n";
die "Can't fork! $!\n" unless defined($kidpid=fork());
if ($kidpid) {
while (sysread($sock, $byte, 1)==1) {
print "$byte";
}
kill 9, $kidpid;
}
else {
while (defined($msg=<STDIN>)) {
print "$msg\n";
}
}
listens and writes to the same socket (with fork of course...). Now,
everything works great until I'm trying to write to the socket... The
program just stucks, I think because I'm trying to read and write
simultaneously... If I comment the read part everything goes smooth
(except I can't get msgs ), here's the code:
use IO::Socket;
$|=1;
($host, $port) = @ARGV;
$port=23 if !$port;
$sock=new IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port,
Proto=>'tcp') or die "Can't connect to $host on port $port! $!\n";
$sock->autoflush(1);
print "[Connected to $host on port $port]\n";
die "Can't fork! $!\n" unless defined($kidpid=fork());
if ($kidpid) {
while (sysread($sock, $byte, 1)==1) {
print "$byte";
}
kill 9, $kidpid;
}
else {
while (defined($msg=<STDIN>)) {
print "$msg\n";
}
}