update page now

Voting

: max(two, zero)?
(Example: nine)

The Note You're Voting On

burninleo at gmx dot net
10 years ago
Another note alread states that blocking-reads may be an issue, if the counterpart responds very slowly - or not at all. The stream timeout may not work as expected in such a situation.

However, php.net provides very little information on how to use non-blocking reading operations. Here's a code sample:

<?php
        stream_set_timeout($c, $timeout);
        $data = '';
        while (is_resource($c) && !feof($c)) {
            // Use non-blocking reading for first loop
            if (($data === '') and ($timeout > 0)) {
                stream_set_blocking($c, false);
                $endtimeOut = time() + $timeout;
                $str = '';
                while ((time() < $endtimeOut) and (strlen($str) < 515) and !feof($c)) {
                    sleep(1);  // Note: This may require tuning
                    $str.= fgets($c, 515);
                }
                // Handling first-read timeout
                if (time() >= $endtimeOut) {
                    trigger_error('Timeout', E_USER_WARNING);
                    break;
                }
                stream_set_blocking($c, true);
            } else {
                $str = fgets($c, 515);
            }
            $data.= $str;

            // Handling of "traditional" timeout
            $info = stream_get_meta_data($c);
            if ($info['timed_out']) {
                    trigger_error('Timeout', E_USER_WARNING);
                    break;
            }
        }
?>

<< Back to user notes page

To Top