I'm trying to create a simple wrapper to make console programs accessible with sockets.

Input from the client is being received by the wrapper and successfully forwarded to the program; however the output from the program is NOT being received by the wrapper, and consequently cannot be forwarded to the client.

This would seem to suggest a problem related to open2; however the error-checking in the IPC::Open2 perldoc indicates that the program is being run successfully, as confirmed by output from a test program. Consequently, I can only surmise that select is not identifying when there is data to be read on the $output filehandle...

UPDATE: I tried writing a test program with autoflush, but it didn't seem to work. Adding an iterative print with a very short delay got the output from the program to be received by the wrapper, which was then successfully forwarded to the client. Now I've just got to figure out how to either turn off buffering, or preferably set per line buffering. This is discussed to an extent in the "Bidirectional Communication with Another Process" section of the perlipc perldoc, but I've yet to figure out how to implement it for this application...

Code for the wrapper follows:

#!/usr/bin/perl use IPC::Open2; use IO::Socket; my $server = IO::Socket::INET->new( LocalPort => 8000, Listen => 1, Reuse => 1, Proto => 'tcp' ) or die $@; my $client = $server->accept; my ($output, $input); my $pid = open2($output, $input, '/usr/bin/bc'); my $length = 80; my $readable; vec($readable, fileno($output), 1) = 1; vec($readable, fileno($client), 1) = 1; while (select($readable, undef, undef, undef)) { if (vec($readable, fileno($output), 1)) { my $data; sysread $output, $data, $length; # Read + output from program syswrite $client, $data; # Forw +ard to client print 'PROGRAM:', "\t", $data; } if (vec($readable, fileno($client), 1)) { my $data; sysread $client, $data, $length; # Read + input from client syswrite $input, $data; # Forw +ard to program print 'CLIENT:', "\t", $data; } unless ($server and $client and $input and $output) { close $_ for $input, $output, $client, $server; # Exit + if program handles closed waitpid $pid, 0; exit; } }


In reply to select open2 filehandles for reading by eibwen

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.