php - stream_set_timeout永远不会工作,无论超时多低

标签 php sockets fsockopen

在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 );
        }
    }
} 

我究竟做错了什么?

最佳答案

我发现的 key 是stream_set_blocking($fp, TRUE )

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

关于php - stream_set_timeout永远不会工作,无论超时多低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424989/

相关文章:

c - sin_addr.s_addr = INADDR_ANY;需要 htonl 吗?

c# socket 多数据包堆栈

php - 如何在 PHP 中将 fsockopen(或兼容)与 SOCKS 代理一起使用?

php - 使用fsockopen进行PHP GET和POST数据

java - 将字符写入 Java 套接字时 fsockopen 10053 错误

PHP 查询,获取最近 24 小时的行

magento - 将产品默认图片更新为图片库中的第一张图片

c++ - 同一台计算机上的 UDP 连接。 1 个服务器。多个客户端

php - 处理期望 catch block - xdebug

php - laravel 5.4 中帖子的自动删除日期