gpt4 book ai didi

sockets - 如何检测 PHP 套接字监听器上的客户端断开连接?

转载 作者:可可西里 更新时间:2023-11-01 13:20:15 25 4
gpt4 key购买 nike

我一直在测试 PHP 套接字监听,遇到了上述问题。我的测试监听器在其他方面工作正常,但如果客户端在不通知服务器的情况下断开连接,脚本将进入无限循环,直到新客户端连接。问题似乎在线 $ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL); 因为它应该在这里停止等待连接,而是它跳过等待并直接运行整个循环。

如有任何指点,我们将不胜感激。

代码:

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

$debug = true;
function e($str) {
global $debug;
if($debug) { echo($str . "\n"); }
}

e("Starting...");
error_reporting(1);
ini_set('display_errors', '1');

e("Creating master socket...");
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$max_clients = 10;

e("Setting socket options...");
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
e("Binding socket...");
socket_bind($socket, "192.168.1.38", 20000);
e("Listening...");
socket_listen($socket, $max_clients);

$clients = array('0' => array('socket' => $socket));

while(TRUE) {
e("Beginning of WHILE");
$read[0] = $socket;

for($i=1; $i<count($clients)+1; ++$i) {
if($clients[$i] != NULL) {
$read[$i+1] = $clients[$i]['socket'];
}
}

e("Selecting socket...");
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
e("socket_select returned " . $ready);
e("If...");
var_dump($socket);
var_dump($read);
if(in_array($socket, $read)) {
e("If OK");
for($i=1; $i < $max_clients+1; ++$i) {
if(!isset($clients[$i])) {
e("Accepting connection...");
$clients[$i]['socket'] = socket_accept($socket);

socket_getpeername($clients[$i]['socket'],$ip);
e("Peer: " . $ip);
$clients[$i]['ipaddr'] = $ip;

socket_write($clients[$i]['socket'], 'Welcome to my Custom Socket Server'."\r\n");
socket_write($clients[$i]['socket'], 'There are '.(count($clients) - 1).' client(s) connected to this server.'."\r\n");

echo 'New client connected: ' . $clients[$i]['ipaddr'] .' ';
break;
} elseif($i == $max_clients - 1) {
echo 'Too many Clients connected!'."\r\n";
}

if(--$ready <= 0) {
continue;
}
}
}

e("For...");
for($i=1; $i<$max_clients+1; ++$i) {
e("In...");
if(in_array($clients[$i]['socket'], $read)) {
e("Reading data...");
$data = @socket_read($clients[$i]['socket'], 1024, PHP_NORMAL_READ);

if($data === FALSE) {
unset($clients[$i]);
echo 'Client disconnected!',"\r\n";
continue;
}

$data = trim($data);

if(!empty($data)) {
if($data == 'exit') {
socket_write($clients[$i]['socket'], 'Thanks for trying my Custom Socket Server, goodbye.'."\n");
echo 'Client ',$i,' is exiting.',"\n";
socket_close($clients[$i]['socket']);
unset($clients[$i]);
continue;
}

for($j=1; $j<$max_clients+1; ++$j) {
if(isset($clients[$j]['socket'])) {
if(($clients[$j]['socket'] != $clients[$i]['socket']) && ($clients[$j]['socket'] != $socket)) {
echo($clients[$i]['ipaddr'] . ' is sending a message!'."\r\n");
socket_write($clients[$j]['socket'], '[' . $clients[$i]['ipaddr'] . '] says: ' . $data . "\r\n");
}
}
}
break;
}
}
}
if($loops == 0) {
$firstloop = time();
$loops++;
} else {
$loops++;
if((time() - $firstloop) >= 5 && $loops > 25) {
/*for($j=1; $j<$max_clients+1; ++$j) {
if(isset($clients[$j]['socket'])) {
if($clients[$j]['socket'] != $socket) {
echo('Server is looping, sending keepalive...'."\r\n");
if(!socket_write($clients[$j]['socket'], '-KEEPALIVE-' . "\r\n")) {
echo 'Client ',$j,' not found, killing...',"\n";
socket_close($clients[$j]['socket']);
unset($clients[$j]);
die("debug");
}
}
}
}*/
die("Looping started.\n");
}
}
}
?>

最佳答案

找出问题所在,我在“客户端断开连接” block 中丢失了 socket_close()。所以正确的 block 是:

if($data === FALSE) {
socket_close($clients[$i]['socket']);
unset($clients[$i]);
echo 'Client disconnected!',"\r\n";
continue;
}

关于sockets - 如何检测 PHP 套接字监听器上的客户端断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com