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?

#!/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"; } }
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 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; } } }

In reply to Using pipe to communicate with child processes by perlnwb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.