在 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 );
}
}
}
我究竟做错了什么?