Where's my STDOUT?

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

***
 
X

xhoster

Fatted Shmatted said:
while(<$handle>)

This reads a line into $_, which is then ignored.
{
if(sysread($handle,$rxd,1,0))

Since you already read the data with <>, there is probably nothing to
sysread at that point.

You could use something like:

while(sysread($handle,$rxd,1,0)) {
print "RX'ed ".$rxd."\n";
}

But that doesn't distinguish between eof and errors on the handle.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top