Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Problem handling 2 simultaneous socket streams

by zentara (Archbishop)
on Sep 29, 2011 at 18:20 UTC ( #928641=note: print w/ replies, xml ) Need Help??


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

How would I go about adding in reading STDIN to allow a user to type quit to end this program?

See Re: Ask for STDIN but don't pause for it and the last example. This is untested in your code, but something like this will work. Just add \*STDIN to your Select object and test for it.

# Add listen sockets to select my $sockets = new IO::Select(); $sockets->add(\*STDIN ); $sockets->add($stream1_lsn); $sockets->add($client1_lsn); $sockets->add($client2_lsn); ..... #Go into infinite loop, handling connections - how do I end this? while(my @ready = $sockets->can_read) { #got data foreach my $socket (@ready) { # find which socket sent data if ($socket == \*STDIN) { #read stdin and detect a q; exit; }

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


Comment on Re^3: Problem handling 2 simultaneous socket streams
Download Code
Re^4: Problem handling 2 simultaneous socket streams
by PhillyR (Acolyte) on Sep 29, 2011 at 19:04 UTC
    Adding \*STDIN to the select causes the while loop not to execute...
      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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-09-16 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (48 votes), past polls