Archive for June, 2007

19
Jun

Fun with Sockets and Port 80.

So the problem is you want to connect to port 80 with a socket connection. Now you would think this would be simple, but of course it is not. To connect to port 80 or for that matter any port below port 1024 you need to first connect the xmlsocket:// protocol, which requires a different type of server than an http server such as apache. Before we get real deep into this I will briefly explain the rules. Or should I say describe them as I understand them.

1. To load any type of content from a domain, you must first load a crossdomain.xml file, which tells flash which sites the site it is connecting to allows. An example would be.

cross-domain-policy
allow-access-from domain="mysite.com" ports="80"
/allow-access-from
/cross-domain-policy

so this policy file allows flash content from mysite.com to load content and it also says it allows connections from there to connect to port 80 with to-ports

2. Socket connections can load a crossdomain.xml file over standard http through a socket connection on any port over 1024, but the to-ports property will only allow ports above 1024 if the port connected to is above 1024.

3. To connect to a port below 1024 you must load the crossdomain.xml file from a port below 1024, and to do this you must use the xmlsocket protocol

Example:

Security.loadPolicyFile(xmlsocket://mysite.com/crossdomain.xml);

This line is not necessary unless you would like to specify the crossdomain.xml file location. By default the socket connection will request it from the root.

4. After the connection is made the socket will load the crossdomain.xml file and it will see that it can now access port 80 or whichever port you are trying to allow below 1024.

Conclusions:
Connecting to a port below 1024 is way too complicated. I do not understand why it is necessary to load the policy file over a different protocol. This just results in having to support yet another service, when the only reason for it is just simply to load an xml file. I understand there are security precautions there and that it is the main reason that this exists. I just feel that this could have been handled in a much more graceful or possibly better documented way.

01
Jun

Dynamic Object Instantiation

So you would think this would be pretty simple. An eval here an eval there and you would be done right? Nope. The reason this is difficult is because flash does not import a class even though you have the import statement in the actionscript unless the class is actually used. So the solution is to instantiate a dummy object, which will then force flash to import the class that is necessary to perform the eval().


import test;

var dummy:test = new test(); //flash does not import the class unless it is instantiated, so the eval will fail without this line

var func = eval("test");
trace(typeof(func)); //func is equal to a function, or the test class constructor

var obj = new func;
trace(typeof(obj)); //obj is equal to the object type of test