1 Replies - 4897 Views - Last Post: 10 March 2012 - 02:46 AM

#1 NewAtSockets  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-February 12

Sockets: PHP<-->AS3

Posted 17 February 2012 - 05:04 PM

I've been banging my head for the past week on how to solve a Security issue with reading data from a socket into AS3 from PHP on a server. So, I've tested the server PHP code with a client PHP code both locally (localhost) and on my website and it works beautifully. If I then use my AS3 code, it works only if I run it from the IDE. I've included a crossdomain.xml file, but no use!

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Sockets: PHP<-->AS3

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Sockets: PHP<-->AS3

Posted 10 March 2012 - 02:46 AM

The URL given to loadPolicyFile() is not correct. Also you can try sending the crossdomain file's content right from your PHP code.

A read here should help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1