views:

292

answers:

1

Hi, I'm trying to evaluate whether Flex can access binary sockets. Seems that there's a class calles Socket (flex.net package). The requirement is that Flex will connect to a server serving binary data. It will then subscribe to data and receive the feed which it will interpret and display as a chart. I've never worked with Flex, my experience lies with Java, so everything is new to me. So I'm trying to quickly set something simple up. The Java server expects the following:

DataInputStream in = .....
byte cmd = in.readByte();
int size = in.readByte();
byte[] buf = new byte[size];
in.readFully(buf);
... do some stuff and send binary data in something like
out.writeByte(1);
out.writeInt(10000);
... etc...

Flex, needs to connect to a localhost:6666 do the handshake and read data. I got something like this:

        try {
            var socket:Socket = new Socket();

            socket.connect('192.168.110.1', 9999);
            Alert.show('Connected.');
            socket.writeByte(108); // 'l'
            socket.writeByte(115); // 's'
            socket.writeByte(4);
            socket.writeMultiByte('HHHH', 'ISO-8859-1');
            socket.flush();
        } catch (err:Error) {
            Alert.show(err.message + ": " + err.toString());
        }

The first thing is that Flex does a <policy-file-request/>. I've modified the server to respond with:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>
   <site-control permitted-cross-domain-policies="master-only"/>
   <allow-access-from domain="192.168.110.1" to-ports="*" />
</cross-domain-policy>    

After that - EOFException happens on the server and that's it.

So the question is, am I approaching whole streaming data issue wrong when it comes to Flex? Am I sending the policy file wrong? Unfortunately, I can't seem to find a good solid example of how to do it. It seems to me that Flex can do binary Client-Server application, but I personally lack some basic knowledge when doing it.

I'm using Flex 3.5 in IntelliJ IDEA IDE.

Any help is appreciated.

Thank you!

A: 

I can confirm that Flex can access binary sockets. There is an annoyance, though, that you've seen. It will first attempt to connect to port 843 on the host to get a policy file and see if the connection is allowed. However, if that connection times out (and it only allows 3 seconds to get an answer), it will connect to the server port and ask for a policy file (by sending <policy-file-request/>). It reads the result and, if the connection deemed acceptable, it connects to the server again to undergo the "real" protocol.

Unless the policy file server on 843 is guaranteed to respond quickly there's always a possibility that the Flex application will make the policy file request on the server port. This is a real deployment problem for existing services, but not so bad if you're setting up the server yourself in which case you just need to make sure that a policy file request can be reasonably distinguished from any initial startup of your protocol.

The policy file I've been sending back looks something like this:

<?xml-version="1.0"?>
<cross-domain-policy>
  <allow-access-from domain="*" to-ports="1492"/>
</cross-domain-policy>

Yours looks pretty close save the for the site-control tag that I don't believe is relevant.

What's extremely helpful is to enable logging of policy file requests to that you can get some idea of what Flex is up to. Here's how I do it on my Vista machine; paths will be different under XP but I hope that at least this description will give you web search terms to work with.

First, install the debug version of the Flash player.

Enable logging by putting:

ErrorReportingEnable=1
TraceOutputFileEnable=1
PolicyFileLog=1
#PolicyFileLogAppend=1  # optional but possibly useful

In C:/Users/George/mm.cfg

Now you should see two files appear in C:\Users\George\AppData\Roaming\Macromedia\Flash Player\Logs":

flashlog.txt
policyfiles.txt

policyfiles.txt is the one you want to look at to see what policy files were retrieved and how Flex decided to respond to them. flashlog.txt is pretty useful, too, for capturing trace() output but you might get that from your IDE anyways.

One more thing. Adding event listeners to the socket is useful to help distinguish I/O type errors versus security policy violations. Here are the events I listen for:

socket.addEventListener(Event.CONNECT, ServerConnect);
socket.addEventListener(Event.CLOSE, ServerClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, ServerIOError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ServerSecurityError);
George Phillips
You can specify the port when loading the policy file using Security.loadPolicyFile method. I wrote an article here http://cornelcreanga.com/2008/11/bringing-data-into-flex-applications-introduction/
Cornel Creanga
Thank you! That worked. It connects fine. Now I gotta figure out how to read data in async socket in Flex. Is there a free real-time messaging protocol for Flex out there? I see they provide a commercial solution, but can I do it for free somehow?
Daniil