You are closing your pipe handles in the wrong place. Also if you read with angle brackets, you need the proper line terminator (default \n).
#!/usr/bin/perl # http://perlmonks.org/?node_id=1173754 use strict; use warnings; my ($reader, $writer); pipe $reader, $writer or die "$! on pipe"; $writer->autoflush; if( my $pid = fork ) # parent { close $reader; for (1 .. 5) { my $line = "-- $_ --"; print "PARENT: Going to transfer line $line\n"; print $writer "$line\n"; sleep 1; } close $writer; print "PARENT: closed writer, waiting for child to exit\n"; 1 while wait > 0; print "PARENT: exiting\n"; } elsif( defined $pid ) # child { close $writer; while( 1 ) { defined( my $line = <$reader> ) or last; print "CHILD: Received line $line"; } close $reader; print "CHILD: exiting\n"; exit; } else { die "$! on fork"; }
produces:
PARENT: Going to transfer line -- 1 -- CHILD: Received line -- 1 -- PARENT: Going to transfer line -- 2 -- CHILD: Received line -- 2 -- PARENT: Going to transfer line -- 3 -- CHILD: Received line -- 3 -- PARENT: Going to transfer line -- 4 -- CHILD: Received line -- 4 -- PARENT: Going to transfer line -- 5 -- CHILD: Received line -- 5 -- PARENT: closed writer, waiting for child to exit CHILD: exiting PARENT: exiting
In reply to Re: pipe used to send continuously to a TCP Socket does not work
by tybalt89
in thread pipe used to send continuously to a TCP Socket does not work
by Bloehdian
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |