【问题标题】:stream_set_timeout never works no matter how low the timeout无论超时多低,stream_set_timeout 都不会起作用
【发布时间】:2012-11-05 16:10:52
【问题描述】:

在 PHP 中,我打开一个流,写入它,然后从中读取。我想设置读取流的超时时间,但无论我设置多低(0 微秒、10 微秒),元数据都不会显示“timed_out”!

相关代码:

//open the socket
if ( $fp = fsockopen( gethostbyname(host), port, $errno, $errstr, $timeout ) ) {

    //Send command to the host
    if ( fwrite( $fp, $requestCommand ) ) {
        //Set timeout and blocking
        stream_set_blocking( $fp, FALSE );
        stream_set_timeout( $fp, 0, 10 ); 

        //Check for timeout
        $info = stream_get_meta_data( $fp );
        echo $info[ 'timed_out' ];

        //Read and check for timeout
        while ( !$info['timed_out'] && !feof( $fp ) ) {
            $response .= fread( $fp, 4096 );

            //Get meta data (which has timeout info)
            $info = stream_get_meta_data( $fp );
        }
    }
} 

我做错了什么?

【问题讨论】:

  • 您是在 CLI 或浏览器中执行此操作吗?

标签: php sockets fsockopen


【解决方案1】:

我找到的密钥是stream_set_blocking($fp, TRUE )

如果FALSE,那么$status['timed_out']似乎没有任何实际作用。 TRUE [PHP 默认] 有效。

【讨论】:

  • 成功了!谢谢!我在 PHP 网站上看到的示例将其设置为 FALSE,出于某种原因,我从未想过将其作为阻塞尝试。你知道这是否是 API“合同”应该工作的方式吗?
猜你喜欢
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多