Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sockets在PHP中的速度很慢吗?_Php_Sockets - Fatal编程技术网

sockets在PHP中的速度很慢吗?

sockets在PHP中的速度很慢吗?,php,sockets,Php,Sockets,我将此代码用于套接字。控制台显示页面处理时间很短,但Chrome显示页面加载时间约为1秒 $this->serv_sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($this->serv_sock, $this->serv, $this->port) or die("Could not bind to address\n"); socket_listen($this->

我将此代码用于套接字。控制台显示页面处理时间很短,但Chrome显示页面加载时间约为1秒

    $this->serv_sock = socket_create(AF_INET, SOCK_STREAM, 0); 

    socket_bind($this->serv_sock, $this->serv, $this->port) or die("Could not bind to address\n");

    socket_listen($this->serv_sock);

    while (1) {
        echo "Waiting...\n";        
        $client = socket_accept($this->serv_sock); 

        $start_mtime = microtime(true);

        echo "Accepted at ".$start_mtime.".\n";

        $input = '';

        $len = 0;

        do {
            //echo "Reading.\n";
            $inp = socket_read($client, 1024);
            $input .= $inp;

            if (strpos($input, "\n\n") === false && strpos($input, "\r\n\r\n") === false)
                continue;

            if (!$len) {
                if (!preg_match("/Content-Length: (\d+)/", $input, $matches)) {
                    break;
                }
                $len = $matches[1];
                if (!$len)
                    break;
                echo "We want $len bytes.\n";
            }

            if (strpos($input, "\n\n") !== false)
                list($headers, $content) = explode("\n\n", $input);
            else 
                list($headers, $content) = explode("\r\n\r\n", $input);

            if (strlen($content) >= $len)
                break;
        } while ($inp);

        echo "Calling callback as ".microtime(true).".\n";

        if (strpos($input, "\n\n") !== false)
            list($headers, $content) = explode("\n\n", $input);
        else 
            list($headers, $content) = explode("\r\n\r\n", $input);

        $output = $this->translate($callback, $headers, $content); // nothing slow here

        $time_end = microtime(true);
        echo "Sending output at ".$time_end." (total ".($time_end - $start_mtime).")\n\n";

        $output = "HTTP/1.0 Ok\n".
        "Content-Type: text/html; charset=utf-8\n".
        "Content-Length: ".strlen($output)."\n".
        "Connection: close\n\n".
        $output;

        socket_write($client, $output);

        socket_close($client);
    }

如果我明白你的意思。据我所知,服务器处理时间和客户端处理时间是有区别的


在服务器上处理信息所需的实际时间总是更少。服务器完成信息处理后,数据仍必须发送到浏览器并进行渲染。数据到达和浏览器渲染的时间是我怀疑的原因,是因为Chrome告诉您需要约1秒。

如果我理解您的意思。据我所知,服务器处理时间和客户端处理时间是有区别的

在服务器上处理信息所需的实际时间总是更少。服务器完成信息处理后,数据仍必须发送到浏览器并进行渲染。数据到达和浏览器渲染的时间是我怀疑的,原因是为什么Chrome告诉你需要1秒钟