doushan5222 2014-12-07 19:10
浏览 91

PHP unread_bytes在PHP 5.3中工作,但在PHP 5.4中不工作

I have a problem with a connection to a telnet-server using PHP sockets. I've a finished telnet class, but on my other server did that class not work because of the stream_get_meta_data unread_bytes value. Does PHP have changed that in Version 5.4? I can't find what about this change.

The code that i use:

        $buff = '';
        while (!feof($this->socket)) {
            $buff .= fread($this->socket, 1024);
            $stream_meta_data = stream_get_meta_data($this->socket);
            if ($stream_meta_data['unread_bytes'] <= 0)
                break;
        }

Can anyone help me or say me, what can i change?

  • 写回答

1条回答 默认 最新

  • doujia1939 2017-04-07 12:14
    关注

    You didn't clearly state what your code snippet is supposed to do:

    1. read bytes until the socket connection is shut down, or
    2. read bytes that are available at the moment, on a live connection.

    But your comment feof() dont work correctly suggests that you're after 2., since a feof() check would be sufficient for 1.; cf. the comment from Wez to the "Not a bug" unread_bytes always 0:

    unread_bytes is the number of bytes remaining in PHPs buffering layer after the last read.
    If you have consumed all data from the buffer on a prior read, unread_bytes will remain at zero until you next read a chunk of data from the network.
    So, unread_bytes should not be used to determine if more data is pending; you should use either:

    feof() to detect end of file. Don't forget that you can use non-blocking mode here. PHP 4.3 has a new function called stream_select() which behaves like socket_select() from the sockets extension, but works on all files returned by fopen() and fsockopen(). You can use it to test which files are ready for reading/writing and also specify a timeout.

    So, if you want 2., you can use stream_select() or socket_select().

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题