Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

streaming output to a socket

by Scarborough (Hermit)
on Oct 13, 2004 at 14:57 UTC ( [id://398882]=perlquestion: print w/replies, xml ) Need Help??

Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

I have a server program which works reasonable well it sits on a remote box with lots of scripts and waits for contact from a client requesting a script top run. this works fine and the following code deals with the comms.

while (my $conn = $sock->accept()){ print $conn `my_script_insrtruction`; }

This is fine, but some of the jobs take a few hours to run. During the course of the programs they print to STDOUT charting their progress. The output is finally sent to the socket and back to the client at the end of the program.

What I need is a way to stream the program output live to the the socket and the client. The client has the following to accept the input.

while ($buff ne 'end connection'){ $client->recv($buff,1000); print $buff; }

Is this possible?

Replies are listed 'Best First'.
Re: streaming output to a socket
by pizza_milkshake (Monk) on Oct 13, 2004 at 15:04 UTC
    open a pipe and read from it...
    $ perl -e'open F,"for f in a b c; do date; sleep 1; done |" or die $!; + while (<F>){ print } close F;' Wed Oct 13 08:01:27 PDT 2004 Wed Oct 13 08:01:28 PDT 2004 Wed Oct 13 08:01:29 PDT 2004 $
    instead of having the client print to stdout, just print it to the socket

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

      Here's the same code, reformatted into context:

      # Beware of using input from the user in $cmd. my $cmd = 'for f in a b c; do date; sleep 1; done'; while (my $conn = $sock->accept()) { open(PROG, "$cmd|" or die $!; while (<PROG>) { $conn->print($_); } close(PROG); }

      If you need to send stuff to $cmd's STDIN, check out the IPC::Open2 and IPC::Open3 modules (included with perl) as an alternative to open ...|.

        Thanks I've been trying to work this out and this is clear. Thanks again.
Re: streaming output to a socket
by TedPride (Priest) on Oct 13, 2004 at 20:12 UTC
    Can't you just set the output stream to automatically flush after every print? And I quote from perlvars...

    $|

    "If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering."

      I tried this first but it didn't work for me however the first two work just fine but I now have a problem getting the clients input to stream into a Tk gui so your answer might help me there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://398882]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-23 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found