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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器