In a nonforking server I need to send STDOUT to any number of attached clients. I'm currently trying to bind STDOUT to the socket via $fd = filno $client; open STDOUT, ">&=$fd"; and this doesn't seem to be working when I start cylcing through the attached clients.

I've included by code so you can see exactly what I am trying to do. Perhaps (in fact even likely) I am making some basic mistake. Any input would be appreciated.

The log data indicates that I am, in fact, cycling through the clients as expected. I'm just lost at this point as to how to proceed.

#!/usr/bin/perl -w # nonforker - server who multiplexes without forking use POSIX; use IO::Socket; use IO::Select; use Socket; use Fcntl; use Tie::RefHash; use Carp; BEGIN { $coral_dir = "/usr/local/Coral"; } # This will be edited durin +g build. use lib "$coral_dir/lib"; #coral global libdir# use lib "../../lib"; #coral local libdir# use CRL; use IO::Scalar; $port = 9001; # change this at will # Listen to port. $server = IO::Socket::INET->new(LocalPort => $port, Listen => 10 ) or die "Can't make server socket: $@\n"; %ready = (); tie %ready, 'Tie::RefHash'; nonblock($server); $select = IO::Select->new($server); print "Server started\n"; open LOG, ">./errlog" or die "cannot open errlog $!\n"; print "Starting CoralReef\n"; #Coral::quick_start(0, 0, Coral::API_CELL); Coral::quick_start(); print "Quick start finished\n"; # Main loop: check reads/accepts, check writes, check ready to process while (1) { my $client; my $rv; my $data; # check for new information on the connections we have # anything to read or accept? foreach $client ($select->can_read(0)) { if ($client == $server) { # accept a new connection $client = $server->accept(); $select->add($client); nonblock($client); recv($client, $duration, 10, 0); $ready{$client} = time + $duration; $out_name = time; print LOG "MAIN: $client\n"; &swap_stdout($client); $writer = Coral::write_open("-", undef, "none"); &revert_stdout; if (!defined($writer)) { Coral::puts("Error, could not open output file!"); exit(1); } else { Coral::puts("Writing to file: $out_name"); } Coral::write_init_all($writer); $endtime = time + $duration; Coral::puts("$duration, $endtime, $out_name\n"); } } $iface = Coral::read_cell_all(\$binfo, \$cell); foreach $client (keys %ready) { handle($client); } } # handle($socket) deals with all pending requests for $client sub handle { # requests are in $ready{$client} # send output to $outbuffer{$client} my $client = shift; my $request; print LOG "HANDLE: $client\n"; &swap_stdout($client); if (Coral::write_cell($writer, $iface, $cell) < 0) { Coral::puts("Could not write cell! exiting!"); exit(2); } &revert_stdout; if ($ready{$client} <= time) { &swap_stdout($client); Coral::write_close($writers{$writer}[2]); &revert_stdout; delete $ready{$client}; $select->remove($client); $client->close; next; } } # nonblock($socket) puts socket into nonblocking mode sub nonblock { my $socket = shift; my $flags; $flags = fcntl($socket, F_GETFL, 0) or die "Can't get flags for socket: $!\n"; fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or die "Can't make socket nonblocking: $!\n"; } #bind STDOUT to the client socket and unbuffer sub swap_stdout { my $client = shift; my $fd = fileno($client); print LOG "SWAP: $client == $fd\n"; open(STDSAVE, ">&STDOUT"); open(STDOUT, ">&=$fd"); select(STDOUT); $|=1; } # revert to the default STDOUT sub revert_stdout { close STDOUT; open(STDOUT, ">&STDSAVE"); }

In reply to Sending STDOUT to Multiple Sockets by rapier1

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.