Home > Backend Development > PHP Tutorial > stream_select怎么样判断socket连接和断开?

stream_select怎么样判断socket连接和断开?

WBOY
Release: 2016-06-06 20:09:54
Original
1440 people have browsed it

这是php网站上关于stream_select 的例子
http://php.net/manual/en/function.stream-socket-server.php

<code><?php $master = array();
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$master[] = $socket;
$read = $master;
while (1) {
        $read = $master;
        $_w=$_e=NULL;
        $mod_fd = stream_select($read, $_w , $_e, 5);

        for ($i = 0; $i < $mod_fd; ++$i) {
            if ($read[$i] === $socket) {
                $conn = stream_socket_accept($socket);
                fwrite($conn, "Hello! The time is ".date("n/j/Y g:i a")."\n");
                $master[] = $conn;
            } else {
                $sock_data = fread($read[$i], 1024);
                var_dump($sock_data);
                if (strlen($sock_data) === 0) { // connection closed
                    $key_to_del = array_search($read[$i], $master, TRUE);
                    fclose($read[$i]);
                    unset($master[$key_to_del]);
                } else if ($sock_data === FALSE) {
                    echo "Something bad happened";
                    $key_to_del = array_search($read[$i], $master, TRUE);
                    unset($master[$key_to_del]);
                } else {
                    echo "The client has sent :"; var_dump($sock_data);
                    fwrite($read[$i], "You have sent :[".$sock_data."]\n");
                    fclose($read[$i]);
                     unset($master[array_search($read[$i], $master)]);
                }
            }
        }
    }
</code></code>
Copy after login
Copy after login

程序是从遍历连接的客户端是不是serversocket来判断连接, 遍历每个客户端都fread来读,根据读的结果是FALSE还是0判断是不是断开连接了,但是上面的程序一运行,cpu就上升,然后机器就卡住了,是不是这种判断连接有断开的方式有问题?

回复内容:

这是php网站上关于stream_select 的例子
http://php.net/manual/en/function.stream-socket-server.php

<code><?php $master = array();
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$master[] = $socket;
$read = $master;
while (1) {
        $read = $master;
        $_w=$_e=NULL;
        $mod_fd = stream_select($read, $_w , $_e, 5);

        for ($i = 0; $i < $mod_fd; ++$i) {
            if ($read[$i] === $socket) {
                $conn = stream_socket_accept($socket);
                fwrite($conn, "Hello! The time is ".date("n/j/Y g:i a")."\n");
                $master[] = $conn;
            } else {
                $sock_data = fread($read[$i], 1024);
                var_dump($sock_data);
                if (strlen($sock_data) === 0) { // connection closed
                    $key_to_del = array_search($read[$i], $master, TRUE);
                    fclose($read[$i]);
                    unset($master[$key_to_del]);
                } else if ($sock_data === FALSE) {
                    echo "Something bad happened";
                    $key_to_del = array_search($read[$i], $master, TRUE);
                    unset($master[$key_to_del]);
                } else {
                    echo "The client has sent :"; var_dump($sock_data);
                    fwrite($read[$i], "You have sent :[".$sock_data."]\n");
                    fclose($read[$i]);
                     unset($master[array_search($read[$i], $master)]);
                }
            }
        }
    }
</code></code>
Copy after login
Copy after login

程序是从遍历连接的客户端是不是serversocket来判断连接, 遍历每个客户端都fread来读,根据读的结果是FALSE还是0判断是不是断开连接了,但是上面的程序一运行,cpu就上升,然后机器就卡住了,是不是这种判断连接有断开的方式有问题?

运行这个例子,报了一堆错误,你没报错?
我修改了一下

<code>$master = array();
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$master[] = $socket;
$read = $master;
while (1) {
    $read = $master;
    $_w = $_e = null;
    $mod_fd = stream_select($read, $_w, $_e, 0);

    foreach ($read as $rstream) {
        $conn = stream_socket_accept($socket);
        if ($conn) {
            fwrite($conn, "Hello! The time is " . date("n/j/Y g:i a") . "\n");
            $sock_data = fread($conn, 1024);
//            var_dump($sock_data);
            if ($sock_data === 0) {

            } elseif ($sock_data === false) {
                echo "Something bad happened" . PHP_EOL;
            } else {
                fwrite($conn, "You have sent :[" . $sock_data . "]\n");
            }
            fclose($conn);
        }
    }
}</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template