Hi
I’ve edited Raymond Fain’s socket server to have a multidimensional array of online users.
It works fine, but when i close client windows i face to a warning about socket_select in line 72.
Anybody knows where is the problem?
Thankyou
Here is the code and files have been attached.
<?php
/*
Raymond Fain
Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
For any questions or concerns, email me at ray@obi-graphics.com
or simply visit the site, www.php.net, to see if you can find an answer.
*/
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 2727;
function send_Message($allclient, $socket, $buf ,$ipaddressdds) {
socket_write($socket ,"You said : \0");
foreach($allclient as $client) {
socket_write($client['socket'], " $ipaddressdds - $socket : $buf");
}
}
//---- Start Socket creation for PHP 5 Socket Server -------------------------------------
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed, reason: " . socket_strerror($master) . "
";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "
";
}
if (($ret = socket_listen($master, 5)) < 0) {
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "
";
}
$read_sockets = array(array('socket'=>$master));
$allclients = Array();
echo count($allclients);
while (true) {
for($k=0;$k<count($read_sockets); $k++){
$changed_sockets[$k] = $read_sockets[$k]['socket'];
}
socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "
";
continue;
} else {
//=================== add new socket ==================
array_push($read_sockets, Array('socket'=>$client));
$allclients = $read_sockets;
array_shift($allclients);
echo count($allclients);
//-----------------------------------------------------
}
} else {
$bytes = socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0) {
//=============== remove disconnected ================
foreach( $read_sockets as $i => $v){
if($v['socket']==$socket){
unset($read_sockets[$i]);
break;
}
}
socket_close($socket);
$allclients = $read_sockets;
array_shift($allclients);
echo count($allclients);
//----------------------------------------------------
}else{
//=================== read message ===================
socket_getpeername($socket, $connectip);
$allclients = $read_sockets;
array_shift($allclients);
echo count($allclients);
//-----------------------------------------------------
send_Message($allclients, $socket, $buffer , $connectip);
}}}}
?>