选中PHP套接字服务器中的断开客户端 #/usr/local/bin/php-q

选中PHP套接字服务器中的断开客户端 #/usr/local/bin/php-q,php,sockets,server,Php,Sockets,Server,如何检查断开连接的客户端 #!/usr/local/bin/php -q <? set_time_limit (0); $address = '192.168.0.201'; $port = 1077; $max_clients = 10; $clients = Array(); $sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($sock, $address, $port) or die('fai

如何检查断开连接的客户端

#!/usr/local/bin/php -q 
<?

set_time_limit (0); 

$address = '192.168.0.201';
$port = 1077;

$max_clients = 10; 

$clients = Array(); 

$sock = socket_create(AF_INET, SOCK_STREAM, 0); 

socket_bind($sock, $address, $port) or die('fail.'); 

socket_listen($sock); 

while (true) { 
    $read[0] = $sock; 
    for ($i = 0; $i < $max_clients; $i++) 
    { 
        if ($client[$i]['sock']  != null) 
            $read[$i + 1] = $client[$i]['sock'] ; 
    } 
    $write=NULL;
    $exceptions=NULL;
    $ready = socket_select($read,$write,$exceptions,null); 

    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++) 
        {
            if ($client[$i]['sock'] == null) { 
                $client[$i]['sock'] = socket_accept($sock); 
                break; 
            } 
            elseif ($i == $max_clients - 1) 
                print ("many clients");
        }
        if (--$ready <= 0) 
            continue; 
    }

    for ($i = 0; $i < $max_clients; $i++)
    { 
        if (in_array($client[$i]['sock'] , $read)) 
        {
            $input = socket_read($client[$i]['sock'] , 1024); 
            if ($input == null) { 
                unset($client[$i]); 
            } 
            $n = trim($input); 
            if ($input == 'exit') { 
                socket_close($client[$i]['sock']); 
            } elseif ($input) {

                $host = 'localhost';
                $uname = 'root';
                $pwd = 'taek0526';
                $db = 'InputTest';

                $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
                mysql_select_db($db,$con) or die("db selection failed");
                mysql_query("set names utf8");

                $data = explode(" ", $input);

                mysql_query("INSERT INTO `test`(`data1`, `data2`) VALUES ('".$data[0]."', '".$data[1]."')");

                mysql_close($con);

            } 
        } else { 
        } 
    } 
}
socket_close($sock); 
?> 
您已使用此行进行了此检查:

for ($i = 0; $i < $max_clients; $i++) {
    if( Check disconnect ){
        disconnect work
    }
}
您只是忘记了也要插入

            if ($input == null) { 
以前

                socket_close($client[$i]['sock']);
或者你可能想合并

                unset($client[$i]); 
然后,如何删除有关断开客户端连接的数据

#!/usr/local/bin/php -q 
<?

set_time_limit (0); 

$address = '192.168.0.201';
$port = 1077;

$max_clients = 10; 

$clients = Array(); 

$sock = socket_create(AF_INET, SOCK_STREAM, 0); 

socket_bind($sock, $address, $port) or die('fail.'); 

socket_listen($sock); 

while (true) { 
    $read[0] = $sock; 
    for ($i = 0; $i < $max_clients; $i++) 
    { 
        if ($client[$i]['sock']  != null) 
            $read[$i + 1] = $client[$i]['sock'] ; 
    } 
    $write=NULL;
    $exceptions=NULL;
    $ready = socket_select($read,$write,$exceptions,null); 

    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++) 
        {
            if ($client[$i]['sock'] == null) { 
                $client[$i]['sock'] = socket_accept($sock); 
                break; 
            } 
            elseif ($i == $max_clients - 1) 
                print ("many clients");
        }
        if (--$ready <= 0) 
            continue; 
    }

    for ($i = 0; $i < $max_clients; $i++)
    { 
        if (in_array($client[$i]['sock'] , $read)) 
        {
            $input = socket_read($client[$i]['sock'] , 1024); 
            if ($input == null) { 
                unset($client[$i]); 
            } 
            $n = trim($input); 
            if ($input == 'exit') { 
                socket_close($client[$i]['sock']); 
            } elseif ($input) {

                $host = 'localhost';
                $uname = 'root';
                $pwd = 'taek0526';
                $db = 'InputTest';

                $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
                mysql_select_db($db,$con) or die("db selection failed");
                mysql_query("set names utf8");

                $data = explode(" ", $input);

                mysql_query("INSERT INTO `test`(`data1`, `data2`) VALUES ('".$data[0]."', '".$data[1]."')");

                mysql_close($con);

            } 
        } else { 
        } 
    } 
}
socket_close($sock); 
?> 
除了上述
unset($client[$i])
之外,您还必须更换

            if ($input == null or trim($input) == 'exit')
            {
                socket_close($client[$i]['sock']);
                unset($client[$i]);

从上一个循环周期中清除数组元素

    $read = Array($sock);