dr200166 2014-05-28 15:32
浏览 27
已采纳

Php stream_select如何工作?

public function __construct() {
    if (!$this->Listen()) {
        throw new Exception;
    }
     while (1) {
        $read = $this->master;
        $mod_fd = stream_select($read, $w = null, $e = null, 5, 00);
        if ($mod_fd === FALSE) {
            throw new Exception;
        }
        foreach ($read as $feed => $stream) {
            if ($stream === $this->master['main']) {
                $conn = stream_socket_accept($this->master['main']);
                $this->master[count($this->master)] = $conn;
            } else {
                $this->ReadStream($feed, $stream);
            }
        }
    }
}

private function Listen() {
    $socket = stream_socket_server("tcp://0.0.0.0:6001", $errno, $errstr);
    if ($errstr != '') {
        return false;
    }
    $this->master['main'] = $socket;
    return true;
}

My understanding is that stream_select will fill $read with the streams that have data available for reading.

My intention was to read each stream in $read one after the other but something really screwey is going on inside ReadStream where sometimes the data appears to be coming from a different stream than I expected.

Perhaps $read is being modified by stream_select before I got a chance to loop through them? But I thought that everyting in the process will happen asynchronously so now I'm totally confused

  • 写回答

1条回答 默认 最新

  • dongxikuo5171 2014-05-28 15:48
    关注

    You are creating a socket, therefore you need to use socket_select, not stream_select. The later is for file streams.

    You need to call socket_read to get any data from it.

    The PHP manual has some create examples in the comment section.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能