【发布时间】: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 或浏览器中执行此操作吗?