benoit5699
Nouveau membre
J'utilise donc ce script php pour faire tourner un chat qui renvoie pour l'instant à chaque utilisateur le nombre de clients connectés.
Bon nouveau problème ce soir...MOn chat fonctionne mieux qu'hier sauf que je voudrait arriver à arrêter le script quand le dernier client s'est déconnecté. Pour cela j'ai rajouté les lignes 64 à 72 à mon sript d'hier.
Lorsque qu'un client se déconnecte ça marche très bien, je fais bien $nbclients-- ce qui me permet de compter mes clients sans soucis. Mais lorsque le dernier se déconnecte, impossible...en effet le "il faut s'en aller" ne s'affiche jamais et ça continue à tourner. Pourquoi ? Est ce que quelqu'un voit ? C'est comme si la déconnexion du dernier ne faisait pas un $input == null...
[cpp]
<?php
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 2010;
$max_clients = 10;
// Array that will hold client information
$client = Array();
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
$nbclients=0;
// Loop continuously
while (true) {
echo 'je commence une boucle while</br>';
// Setup clients listen socket for reading
$read[0] = $sock;
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] != null)
$read[$i + 1] = $client[$i]['sock'] ;
}
// Set up a blocking call to socket_select()
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
// if a new connection is being made add it to the client array
if (in_array($sock, $read)) {
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] == null) {
$client[$i]['sock'] = socket_accept($sock);
$nbclients++;
break;
}
elseif ($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) // for each client
{
if (in_array($client[$i]['sock'] , $read))
{
$input = socket_read($client[$i]['sock'] , 1024);
if ($input == null) {
// Zero length string meaning disconnected
unset($client[$i]);
if($nbclients==1)
{
echo 'il faut s\'en aller</br>';
socket_close($client[$i]['sock']);
socket_close($sock);
break(2);
}
else
{
$nbclients--;
}
}
$n = trim($input);
if ($input == 'exit') {
// requested disconnect
socket_close($client[$i]['sock']);
} elseif ($input) {
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",'<thenodemyData="nbclients : "'.$nbclients.' />').chr(0);
for ($j = 0; $j < $max_clients; $j++) // for each client
{
if ($client[$j]['sock'])
{
socket_write($client[$j]['sock'], $output);
}
}
}
}
}
} // end while
// Close the master sockets
socket_close($sock);
?>
[/cpp]
Merci énormément de votre attention ! Bonne nuit à toutes et à tous !
++
Benoît
Bon nouveau problème ce soir...MOn chat fonctionne mieux qu'hier sauf que je voudrait arriver à arrêter le script quand le dernier client s'est déconnecté. Pour cela j'ai rajouté les lignes 64 à 72 à mon sript d'hier.
Lorsque qu'un client se déconnecte ça marche très bien, je fais bien $nbclients-- ce qui me permet de compter mes clients sans soucis. Mais lorsque le dernier se déconnecte, impossible...en effet le "il faut s'en aller" ne s'affiche jamais et ça continue à tourner. Pourquoi ? Est ce que quelqu'un voit ? C'est comme si la déconnexion du dernier ne faisait pas un $input == null...
[cpp]
<?php
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 2010;
$max_clients = 10;
// Array that will hold client information
$client = Array();
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
$nbclients=0;
// Loop continuously
while (true) {
echo 'je commence une boucle while</br>';
// Setup clients listen socket for reading
$read[0] = $sock;
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] != null)
$read[$i + 1] = $client[$i]['sock'] ;
}
// Set up a blocking call to socket_select()
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
// if a new connection is being made add it to the client array
if (in_array($sock, $read)) {
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] == null) {
$client[$i]['sock'] = socket_accept($sock);
$nbclients++;
break;
}
elseif ($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) // for each client
{
if (in_array($client[$i]['sock'] , $read))
{
$input = socket_read($client[$i]['sock'] , 1024);
if ($input == null) {
// Zero length string meaning disconnected
unset($client[$i]);
if($nbclients==1)
{
echo 'il faut s\'en aller</br>';
socket_close($client[$i]['sock']);
socket_close($sock);
break(2);
}
else
{
$nbclients--;
}
}
$n = trim($input);
if ($input == 'exit') {
// requested disconnect
socket_close($client[$i]['sock']);
} elseif ($input) {
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",'<thenodemyData="nbclients : "'.$nbclients.' />').chr(0);
for ($j = 0; $j < $max_clients; $j++) // for each client
{
if ($client[$j]['sock'])
{
socket_write($client[$j]['sock'], $output);
}
}
}
}
}
} // end while
// Close the master sockets
socket_close($sock);
?>
[/cpp]
Merci énormément de votre attention ! Bonne nuit à toutes et à tous !
++
Benoît