Jump to content

tough stdin question


lanstein

Recommended Posts

Hi,

I'm having a tough time with my chat socket program, which works perfectly except for hanging on fgets(stdin).  Is there anyone who can fix the following example (which attempts to bypass fgets())?  I've had no luck with (!feof(stdin)) or even ftell-if($tell<ftell(stdin)) { echo "you entered input"; $tell=ftell(stdin); }
It seems like there's nothing in stdin until fgets() (when it hangs waiting for user input).  php 4.3.11 under slack 10.0.  Thanks!

#!/usr/bin/php                                               
<?php                                                         
        $stdin=fopen('php://stdin', 'r');                     
        $counter=0;                                           
        while (true) {                                       
                echo $counter."\n";                               
                sleep (2);                                   
                if (fgets($stdin)) { echo "received input"; }                                             
                $counter++;                                   
                }                                             
?>                       
Link to comment
Share on other sites

Hi,

Sorry I didn't hear anything, a little disappointed, but I've found the answer, there is a stream_select function, so the solution is something like

$is_there_input=stream_select($read=array(STDIN), $write=NULL,$except=NULL, 0);
if ($is_there_input>0) { $msg=fgets(STDIN);
                                  echo $msg;
                                }

hope this helps someone!
David
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.