perlnwb has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to communicate with a forked process via a pipe. I have created filehanders for pipe with pipe subroutine, closed reader for child process and writer for parent process. But still I cannot get any information from pipe. It accepts cients, but there is something wrong with socket Where is my fault?
I have edited code, but it shows data in pipe only after print in connection, what is wrong with it ?#!/usr/bin/perl # ioforkserv.pl use warnings; use strict; use IO::Socket; use IPC::Shareable; my $buffer ; my $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 }; $buffer = "Buffer init"; # Turn on autoflushing $|=1; # Open pipe my ($reader, $writer); pipe($reader,$writer); my $port = 4444; my $server = IO::Socket->new( Domain => PF_INET, Proto => 'tcp', LocalPort => $port, Listen => SOMAXCONN, Reuse => 1, ); die "Bind failed: $!n" unless $server; # tell OS to clean up dead children $SIG{CHLD} = 'IGNORE'; print "Multiplex server running on port $port...\n"; print "Initial buffer value is ".$buffer."\n"; while (my $connection = $server->accept) { my $name = $connection->peerhost; my $port = $connection->peerport; if (my $pid = fork) { close $connection; print "Forked child $pid for new client $name:$port\n"; next; # on to the next connection } else { # child process - handle connection close($reader); $buffer = "Child # $$\n"; print $connection "You're connected to the server!\n"; while (<$connection>) { print $writer "Client $name:$port says: $_"; print "Client $name:$port says: $_"; print $connection "Message received OK\n"; print "Buffer = $buffer\n"; } print "Client $name:$port disconnected\n"; $connection->shutdown(SHUT_RDWR); exit; } close($writer); while (my $data = <$reader>) { print "In pipe : ".$data."\n"; } }
#!/usr/bin/perl # ioforkserv.pl use warnings; use strict; use IO::Socket; use IPC::Shareable; my $buffer ; my $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 }; $buffer = "Buffer init"; # Turn on autoflushing $|=1; # Open pipe my ($reader, $writer); pipe($reader,$writer); my $port = 4444; my $server = IO::Socket->new( Domain => PF_INET, Proto => 'tcp', LocalPort => $port, Listen => SOMAXCONN, Reuse => 1, ); die "Bind failed: $!n" unless $server; # tell OS to clean up dead children $SIG{CHLD} = 'IGNORE'; print "Multiplex server pid $$ running on port $port...\n"; print "Initial buffer value is ".$buffer."\n"; #Creating new process for taking data from pipe my $chpid = fork(); if( $chpid == 0 ){ close($writer); print "Before Reading from pipe $$\n"; while (my $data = <$reader>) { print "In pipe $$: ".$data."\n"; } print "After reading from pipe $$ \n"; exit 0; } else { print "This is parent process and child ID is $$ and ch $chpid\n"; while (my $connection = $server->accept) { my $name = $connection->peerhost; my $port = $connection->peerport; if (my $pid = fork) { close $connection; print "Forked child $pid for new client $name:$port\n"; next; # on to the next connection } else { # child process - handle connection close($reader); $buffer = "Child # $$\n"; print $connection "You're connected to the server!\n"; while (<$connection>) { print $writer "Client $name:$port says: $_ \n"; print "Client $name:$port says: $_"; print $connection "Message received OK\n"; print "Buffer = $buffer\n"; } print "Client $name:$port disconnected\n"; $connection->shutdown(SHUT_RDWR); exit; } } }
|
|---|