PHP code
= $MAX_USERS)
{
$reject = "Server full. Try again later.\n";
}
//將當前客戶端連接放如socket_select選擇
$connections[$i] = $newconn;
//輸入的連接資源緩存容器
$writefds[$i] = $newconn;
//連接不正常
if ($reject)
{
$close[$i] = true;
}
else
{
echo "Welcome to the PHP Chat Server!\n";
}
//初始化當前連接讀取內容的緩存容器
$input[$i] = "";
continue;
}
//客戶端連接
$i = (int)$rfd;
//讀取
$tmp = @socket_read($rfd, 14, PHP_NORMAL_READ);
if (!$tmp)
{
//讀取不到內容
print "connection closed on socket $i\n";
close($i);
continue;
}
$input[$i] .= $tmp;
$tmp = substr($input[$i], -1);
/*if ($tmp != "\r" && $tmp != "\n")
{
// no end of line, more data coming
continue;
}*/
$line = trim($input[$i]);
$input[$i] = "";
echo 'Client >>'.$line."\r\n";
socket_getpeername($connections[$i],&$remoteIP,&$remotePort);
echo $remoteIP."\r\n";
echo $remotePort."\r\n";
//$data=str_split($buffer);
//print_r($data);
$str="\xFA\x01\x01\xFF\xAA\xAA\x00\x01\x00\x00\x00\x00\x00\x01";
socket_send($connections[$i],$str,strlen($str),0);
}
foreach ($writefds as $wfd)
{
$i = (int)$wfd;
$w = socket_write($wfd, "hello");
}
}
}
function close($i)
{
global $connections, $input, $close;
socket_shutdown($connections[$i]);
socket_close($connections[$i]);
unset($connections[$i]);
unset($input[$i]);
unset($close[$i]);
}
?>