doushansu9012 2012-06-22 02:44
浏览 192
已采纳

PHP从Socket获取可用字节

So I've been writing this tool and I am using these Sockets (not these) and the difference with them I surmise that the second uses strictly stream-based sockets. The thing is that you can't use socket_get_status on the sockets I am using.

Is there any other way to get the amount of available bytes besides socket_get_status?

  • 写回答

1条回答 默认 最新

  • douzhang5199 2012-06-22 03:36
    关注

    The streams returned from fsockopen() are a managed wrapper around regular sockets; the unread bytes (which is what you're probably after) is the difference between the read() on the socket (made by the wrapper itself) and the fread() on the wrapper (made by your script).

    The wrapper reads in chunks of 8192 bytes, but the fread() or fgets() may not read all data at once, which is why there's an unread meta data field you could query; whether that's useful is another thing ;-)

    When using regular sockets, you could either:

    • use socket_select() to wait for data to become available, followed by a suitably large socket_read()
    • use socket_set_nonblock() to make the socket unblocked and attempt to read from it
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?