如何检测,当流客户端不再PHP可用(例如网线拉出)

问题描述 投票:3回答:2

有什么办法(除了检查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);
}

和不同的计算机上的插座的客户,我可以连接到服务器,发送和接收数据,并断开干净,一切都按预期。但是,如果我拉在客户端计算机上的网络连接,没有什么是在服务器端检测 - 服务器不断听取客户端套接字,并写入到它没有任何错误表现自己。

php sockets tcp stream
2个回答
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 Socket编程,做了正确的路™” http://christophh.net/2012/07/24/php-socket-programming/


0
投票

据我了解,调用feof($stream)会告诉你,如果遥控插座断开,但我不能绝对肯定这一点。我使用ping /乒乓自己,同时继续研究解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.