我如何检测流客户端在PHP中不再可用(例如网线拔出)



是否有任何方法(除了检查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);
}

和另一台计算机上的套接字客户端,我可以连接到服务器,发送和接收数据,并干净地断开连接,一切都如预期的那样运行。然而,如果我在客户端计算机上拉网络连接,服务器端什么也检测不到-服务器继续侦听客户端套接字,并且还向其写入,而没有任何错误显示。

据我所知,调用feof($stream)将告诉您远程套接字是否断开连接,但我对此并不完全确定。在继续研究解决方案的同时,我自己也在使用乒乓球。

您需要设置套接字超时,在这种情况下,如果客户端没有及时响应,您将获得错误。

查看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套接字编程,正确的方法™http://christophh.net/2012/07/24/php-socket-programming/

相关内容

  • 没有找到相关文章

最新更新