Here's my PHP code (reads & write simple messages, then detects the "exit" message from the client to close the connection):
$host = "xxx.xxx.xx.xxx";
$port = "5555";
set_time_limit(0);
print "Starting Socket Server...\n";
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("could not create socket.");
print "Socket created.\n";
socket_bind($sock, $host, $port) or die ("could not bind socket.");
print "Socket bound.\n";
socket_listen($sock, 4) or die ("no data from socket.");
print "Socket listening.\n";
$childSocket = socket_accept($sock) or die ("did not accept socket.");
print "Socket child created.\n";
$i = 0;
print "starting the iterations.<br>";
$vals = array(0 => 0);
do
{
$incomingData = socket_read($childSocket,2048,PHP_NORMAL_READ);
$dd = trim($incomingData);
print "Got one data = X".$dd."X<br>";
if ($dd == "exit")
{
print "End of data."."<br>";
break;
}
$vals[$i] = $dd;
$i += 1;
} while ($i<20);
for ($ii=0;$ii<$i;$ii+=1)
{
print $ii.".\t".$vals[$ii]." ";
$response = "A".$ii.": Yes, I'm starved! <br />";
print "SENDING: ".$response;
socket_write($childSocket, $response, strlen($response));
}
socket_close($childSocket);
socket_close($sock);
?>
and here is my AS3 code
import flash.net.*;
import flash.system.Security;
import flash.system.SecurityPanel;
var host:String = "xxx.xxx.xx.xxx";
var port:uint = 5555;
Security.allowDomain("*");
// Security.allowInsecureDomain("*");
Security.loadPolicyFile("http://xxx.xxx.xx.xxx/crossdomain.xml");
var socket:Socket = new Socket(host, port);
var boxStr:String = "";
var gotPolicy:Boolean = false;
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
function whichSandbox():String
{
var str:String = "";
switch(Security.sandboxType)
{
case Security.REMOTE:
str = "This SWF is from an Internet URL. "
+ "It cannot access local files";
break;
case Security.LOCAL_WITH_FILE:
str = "This SWF is local, bu tnot trusted by "
+ "the user. It does not have access to remote files.";
break;
case Security.LOCAL_WITH_NETWORK:
str = "This SWF can communicate with remote "
+ "files, but can with local files.";
break;
case Security.LOCAL_TRUSTED:
str = "This SWF has been trusted by the user "
+ "It can read both local and remote files.";
break;
}
return str;
}
boxStr += "Sandbox type: "+whichSandbox();
trace(boxStr);
myBox_txt.text = boxStr;
// ----------------------------------------------------------------- //
function writeLine(str:String):void
{
str += "\n";
try
{
socket.writeUTFBytes(str);
trace("Sending: "+str);
boxStr += "Sending: "+str;
myBox_txt.text = boxStr;
}
catch(e:IOError)
{
trace(e);
boxStr += e;
myBox_txt.text = boxStr;
}
}
function closeHandler(event:Event):void
{
trace("Connection to [" + host + "] closed");
boxStr += "Connection to [" + host + "] closed";
myBox_txt.text += boxStr;
}
function ioErrorHandler(event:IOErrorEvent):void
{
trace(event.text);
boxStr += event.text;
myBox_txt.text = boxStr;
}
function securityErrorHandler(event:SecurityErrorEvent):void
{
trace(event.toString());
boxStr += event.toString();
myBox_txt.text = boxStr;
}
function connectHandler(event:Event):void
{
trace("Connected to [" + host + "]");
boxStr += "Connected to [" + host + "]";
myBox_txt.text = boxStr;
}
function socketDataHandler(event:ProgressEvent):void
{
var str:String = socket.readUTFBytes(socket.bytesAvailable);
trace("Socket Server Response: " + str);
boxStr += "Socket Server Response: " + str;
var i:int = 0;
while (i<10)
{
writeLine("Q"+i.toString()+": Are you hungry?");
i++;
}
writeLine("exit");
}
And here is my crossdomain.xml file:
<?xml version="1.0"> <cross-domain-policy> <allow-access-from domain="*" to-ports="*" /> </cross-domain-policy>
I would appreciate any help with this.
Thanks!

New Topic/Question
Reply


MultiQuote



|