F
Fatted Shmatted
I'm writing a bidirectional client ( simplified version below),
messages to a server and replies from a server, however the client
doesn't need to wait for replies before sending further messages.
I can't get the child process to write to STDOUT (see code below).
STDOUT is:
[Connected to 127.0.0.1:2999]
TX'ed
done
If I change the while loop in the child loop to be:
while(1) instead of while(<$handle>)
I do get output on STDOUT but this stops me from receiving a SIG{CHLD}
(naturally child loop never exits).
STDOUT is:
[Connected to 127.0.0.1:2999]
TX'ed
RX'ed g
RX'ed o
RX'ed o
RX'ed d
RX'ed b
RX'ed y
RX'ed e
RX'ed
I've also tried a select on STDOUT in the child loop :
if(sysread($handle,$rxd,1,0))
{
my $old_fh = select(STDOUT);
$| = 1;
print "RX'ed ".$rxd."\n";
select($old_fh);
}
but no change...
Any idea's?
--
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
use IO::Socket;
my $host = "127.0.0.1";
my $port = "2999";
my $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host,
PeerPort => $port) or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
my $child_pid = "";
die "can't fork: $!" unless defined($child_pid = fork());
if ($child_pid)
{
$SIG{CHLD} = sub{ print "done\n"; exit(0) };
print $handle "hello";
print "TX'ed\n";
$handle->shutdown(1);
sleep;
}
else
{
my $rxd = "";
while(<$handle>)
{
if(sysread($handle,$rxd,1,0))
{
print "RX'ed ".$rxd."\n";
}
}
}
***
server for this test could be:
$ netcat -l -p 2999 < out.put > in.put
Where out.put contains the word goodbye
***
messages to a server and replies from a server, however the client
doesn't need to wait for replies before sending further messages.
I can't get the child process to write to STDOUT (see code below).
STDOUT is:
[Connected to 127.0.0.1:2999]
TX'ed
done
If I change the while loop in the child loop to be:
while(1) instead of while(<$handle>)
I do get output on STDOUT but this stops me from receiving a SIG{CHLD}
(naturally child loop never exits).
STDOUT is:
[Connected to 127.0.0.1:2999]
TX'ed
RX'ed g
RX'ed o
RX'ed o
RX'ed d
RX'ed b
RX'ed y
RX'ed e
RX'ed
I've also tried a select on STDOUT in the child loop :
if(sysread($handle,$rxd,1,0))
{
my $old_fh = select(STDOUT);
$| = 1;
print "RX'ed ".$rxd."\n";
select($old_fh);
}
but no change...
Any idea's?
--
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
use IO::Socket;
my $host = "127.0.0.1";
my $port = "2999";
my $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host,
PeerPort => $port) or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
my $child_pid = "";
die "can't fork: $!" unless defined($child_pid = fork());
if ($child_pid)
{
$SIG{CHLD} = sub{ print "done\n"; exit(0) };
print $handle "hello";
print "TX'ed\n";
$handle->shutdown(1);
sleep;
}
else
{
my $rxd = "";
while(<$handle>)
{
if(sysread($handle,$rxd,1,0))
{
print "RX'ed ".$rxd."\n";
}
}
}
***
server for this test could be:
$ netcat -l -p 2999 < out.put > in.put
Where out.put contains the word goodbye
***