Perl Sockets Parent/Child Problem

K

Ken Browning

(Source at end of question)

Given this source, I am able to connect to the server, but when I try
to send data to the child process, it never seems to get there...any
suggestions?

use IO::Select;
use IO::Socket;
##############################################################

my ($host, $port, $kidpid, $handle, $line);

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

$handle=IO::Socket::INET->new (Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to $port on $host: $!";

$handle->autoflush(1);

print STDERR "[Connected to $host:$port]\n";

#die "can't fork: $!" unless defined($kidpid=fork());
$kidpid = open (KID_TO_WRITE, "|-");
unless (defined $kidpid) {
die "can't fork: $!"
}

if($kidpid){ #parent process
print $handle "0=1.3\n";
printf("sent: 0=1.3\n");
while (defined ($line = <$handle>)) {
chomp($line);
printf("RECV: %s\n",$line);
printf( KID_TO_WRITE "test %s\n",$line);
print KID_TO_WRITE "[this is a test]" ;
printf( "printed to child process...: %s\n",$line);
}
kill ("TERM" => $kidpid);
}else{ #child process
printf( STDERR "Inside child process...\n");

while (defined ($line = <STDIN>)) {
print STDERR "Inside loop : $line";
print STDERR "SEND: $line";
}
printf( STDERR "Exiting child process...\n");
exit;
}
exit;
 
B

Brian McCauley

Subject: Re: Perl Sockets Parent/Child Problem

I don't think sockets are relevant. You should always try to reduce
your problem to it's mimimal form. Did you try just the parent/child
part on it's own?
Given this source, I am able to connect to the server, but when I try
to send data to the child process, it never seems to get there...any
suggestions?

I suggest not killing the child before the data gets there.
$kidpid = open (KID_TO_WRITE, "|-");
while (defined ($line = <$handle>)) {
chomp($line);
printf("RECV: %s\n",$line);
printf( KID_TO_WRITE "test %s\n",$line);
print KID_TO_WRITE "[this is a test]" ;
printf( "printed to child process...: %s\n",$line);
}
kill ("TERM" => $kidpid);

You never unbuffered KID_TO_WRITE so at this point all the data is
probably in a buffer.

You should simply close KID_TO_WRITE once you've written the data to it.
 
B

Ben Morrow

Ken Browning said:
Thank you for your input....I agree that sockets have nothing to do
with it, but my program is a socket program, thus the title...

But your problem is not a socket problem, hence the subject was wrong.
I tried close(KID_TO_WRITE) (after writing to it) and that flushed the
buffer and the child process received the data. What I need to do is
keep that open until I have received a close indication from the
server. Is there a way to flush that buffer _without_ closing
KID_TO_WRITE?

Put
select((select(KID_TO_WRITE), $|=1)[0]);
before you do the print.

Ben
 
T

Tad McClellan

Is there a way to flush that buffer _without_ closing
^^^^^
^^^^^

Are you being serious?



perldoc -q flush

How do I flush/unbuffer an output filehandle? Why must I do this?
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top