Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Problem handling 2 simultaneous socket streams

by PhillyR (Acolyte)
on Sep 29, 2011 at 19:04 UTC ( #928649=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Problem handling 2 simultaneous socket streams
in thread Problem handling 2 simultaneous socket streams

Adding \*STDIN to the select causes the while loop not to execute...


Comment on Re^4: Problem handling 2 simultaneous socket streams
Re^5: Problem handling 2 simultaneous socket streams
by zentara (Archbishop) on Sep 30, 2011 at 10:55 UTC
    Use sysread instead of reading <>, I mentioned it in comments in the script. And be sure to add a timeout value to your select loop.
    my $input; sysread( \*STDIN, $input, 1024);

    Update: I forgot to mention you can set the nonblocking flag on STDIN

    use Fcntl; fcntl(\*STDIN, F_SETFL, O_NONBLOCK) || die "$!\n"; # Set the non-block flags

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      I tried several versions of adding \*STDIN to the select and using sysread but I finally got soo close by using a separate thread to run in a while(1) loop doing a sysread. However, now when the stream starts flowing it hangs after a while until I provide some input and then works normally. I tried setting the nonblock flag as you mention above but then I get a new error: Your vendor has not defined Fcntl macro F_SETFL. Any help with this?
        You might want to post a new node, describing your platform. First try this threaded attempt.
        #!/usr/bin/perl use warnings; use strict; use Term::ReadKey; use threads; $|++; # I commented out some possible ReadMode settings #ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $thr = threads->new(\&read_in)->detach; # loop to keep main thread alive while(1){ print "test\n"; sleep 1; } #ReadMode('normal'); # restore normal tty settings sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; #process key presses here if($char eq 'q'){exit} #if(length $char){exit} # panic button on any key :-) } } } __END__

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://928649]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-11-27 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls