Socket connection PHP<>Flash

Hello,

i build a working Socket connection between PHP and Flash.
Starting the Server an connect with Flash, works fine.

I also can send and recieve data from Flash to PHP to Mysql
and back. But if i close the Flash movie and start it again,
Flash try to connect…the policy message pops out and
then the Server connection get lost.

I think there is an problem with the socket_close and if a client
stops the connection, and reconnect again, the server tries to
close him self.

I hope someone can help me.

Sorry for my english :wink:



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

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = 'xxxx';
$port = xxxx;

function send_Message($allclient,$buf){
    foreach($allclient as $client){
    socket_write($client, $buf.chr(0));
}
}

function send_Single($socket, $buf) {
    socket_write($socket, $buf.chr(0));}

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, 25)) < 0){
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "
";}

$read_sockets = array($master);

$policy_file ='<'.'?xml version="1.0" encoding="UTF-8"?'.'>'.'<cross-domain-policy xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd">'.'<allow-access-from domain="*" to-ports="*" secure="false" />'.'<site-control permitted-cross-domain-policies="master-only" />'.'</cross-domain-policy>';


//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------

while (true){
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket){
if ($socket == $master){if (($client = socket_accept($master)) < 0){continue;}else{
array_push($read_sockets, $client);
socket_write($client, $policy_file.chr(0));}}else{
$bytes = socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0){
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);socket_close($socket);
}else{
 $allclients = $read_sockets;array_shift($allclients);

if(substr(trim($buffer),0, 7) != "u_name="){
send_Single($socket, "do=login_now");
}        

if(substr(trim($buffer),0, 7) == "u_name=" && substr(trim($buffer),8, 9) != ""){
$array_temp1 = split("&", trim($buffer));
$username_temp1 = split("=", $array_temp1[0]);
 $username_temp2 = $username_temp1[1];
$password_temp1 = split("=", $array_temp1[1]);
$password_temp2 = $password_temp1[1];
$tempBuf = $username_temp2;
$passwordBuf = $password_temp2;
$client_list[$socket]['username'] = $tempBuf;                                                                    $client_list[$socket]['password'] = $passwordBuf;   

                             //HERE BEGINS MYSQL

}

}}}}
?>