如何检测PHP客户端何时不再可用(例如网络电缆拔出)

时间:2013-05-23 13:32:58

标签: php sockets tcp stream

是否有任何方法(检查ping响应除外)检测客户端流(我不知道流是否与套接字有任何不同)变得不可用(即不再有任何连接但没有干净)断开连接)?

使用此代码:

#!/usr/bin/env php
<?php
$socket = stream_socket_server(
    'tcp://192.168.1.1:47700',
    $errno,
    $errstr,
    STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
    stream_context_create(
        array(),
        array()
    )
);
if (!$socket) {
    echo 'Could not listen on socket'."\n";
}
else {
    $clients = array((int)$socket => $socket);
    $last = time();
    while(true) {
        $read = $clients;
        $write = null;
        $ex = null;
        stream_select(
            $read,
            $write,
            $ex,
            5
        );
        foreach ($read as $sock) {
            if ($sock === $socket) {
                echo 'Incoming on master...'."\n";
                $client = stream_socket_accept(
                    $socket,
                    5
                );
                if ($client) {
                    stream_set_timeout($client, 1);
                    $clients[(int)$client] = $client;
                }
            }
            else {
                echo 'Incoming on client '.((int)$sock)."...\n";
                $length = 1400;
                $remaining = $length;

                $buffer = '';
                $metadata['unread_bytes'] = 0;
                do {
                    if (feof($sock)) {
                        break;
                    }

                    $result = fread($sock, $length);

                    if ($result === false) {
                        break;
                    }

                    $buffer .= $result;

                    if (feof($sock)) {
                        break;
                    }

                    $continue = false;

                    if (strlen($result) == $length) {
                        $continue = true;
                    }

                    $metadata = stream_get_meta_data($sock);
                    if ($metadata && isset($metadata['unread_bytes']) && $metadata['unread_bytes']) {
                        $continue = true;
                        $length = $metadata['unread_bytes'];
                    }
                } while ($continue);

                if (strlen($buffer) === 0 || $buffer === false) {
                    echo 'Client disconnected...'."\n";
                    stream_socket_shutdown($sock, STREAM_SHUT_RDWR);
                    unset($clients[(int)$sock]);
                }
                else {
                    echo 'Received: '.$buffer."\n";
                }
                echo 'There are '.(count($clients) - 1).' clients'."\n";
            }

        }
        if ($last < (time() - 5)) {
            foreach ($clients as $id => $client) {
                if ($client !== $socket) {
                    $text = 'Yippee!';
                    $ret = fwrite($client, $text);
                    if ($ret !== strlen($text)) {
                        echo 'There seemed to be an error sending to the client'."\n";
                    }
                }
            }
        }
    }
}
if ($socket) {
    stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
}

和另一台计算机上的套接字客户端,我可以连接到服务器,发送和接收数据,并干净地断开连接,一切都按预期运行。但是,如果我在客户端计算机上拉网络连接,则在服务器端没有检测到任何内容 - 服务器继续监听客户端套接字,并且写入它时没有任何错误显示。

2 个答案:

答案 0 :(得分:0)

您需要设置套接字超时,如果客户端没有及时响应,您将收到错误。

检查PHP的stream_set_timeout函数: http://www.php.net/manual/en/function.stream-set-timeout.php

同时检查socket_set_option: http://php.net/manual/en/function.socket-set-option.php

最后,看看这篇关于如何有效地在PHP中使用套接字的好文章: “PHP套接字编程,完成了Right Way™” http://christophh.net/2012/07/24/php-socket-programming/

答案 1 :(得分:0)

据我了解,调用feof($stream)会告诉您远程套接字是否断开连接,但是我不确定。我在继续研究解决方案的同时使用ping / pong。