首页 > 后端开发 > php教程 > stream_select怎么样判断socket连接和断开?

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

WBOY
发布: 2016-06-06 20:09:54
原创
1442 人浏览过

这是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>
登录后复制
登录后复制

程序是从遍历连接的客户端是不是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>
登录后复制
登录后复制

程序是从遍历连接的客户端是不是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>
登录后复制
相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板