duanlu0075 2016-05-06 17:54
浏览 456

PHP - 如何测试stream_socket_client创建的流是否是开放和可写的?

My question is very similar to this question but in my case I am wanting to know if the stream returned by stream_socket_client is open and writeable.

I know that stream_socket_client will return FALSE if the stream could not be opened, but I am needing to detect later on if the stream has been closed by the server (or closed due to some network error, for example).

At the moment I'm just doing...

$fp = stream_socket_client($apns_url, $error_code, $error_string, $timeout_secs, STREAM_CLIENT_CONNECT, $ctx); //looping through / sending data if (!fp) { //re-open connection }

...but I'm not sure if my if (!fp) {...} check is sufficient?

  • 写回答

2条回答 默认 最新

  • dougu2036 2016-05-06 18:27
    关注

    Use stream_get_meta_data for checking if stream is writable.

     $meta = stream_get_meta_data($fp);
    
     if(is_writable($meta['uri'])) { 
     // stream is writable
     }
    

    Use feof for checking if stream is open.

    • Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout);
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog