1
votes

I have a Groovy server and Flex client. When i try to connect from Flex to Groovy application, i reveive <policy-file-request/> message. Then i send some policy XML response and try to send something to the server. But instead of receiving something on the Groovy side i get absolutely nothing. And after a few seconds i get a SecurityException on the Flex application side.

Groovy server code:

import java.util.Scanner

class server
{
    public static handleConnection(client)
    {
        PrintWriter socketwriter = new PrintWriter(client.getOutputStream())

        Scanner socketreader = new Scanner(client.getInputStream())

        while (socketreader.hasNext())
        {
            String s = socketreader.nextLine()

            println s

            if (s =~ /<policy-file-request\s*\/>/)
            {

                socketwriter.print("<?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=\"*\" to-ports=\"2900-2910\" /></cross-domain-policy>\n\0\n")

                socketwriter.flush()
                socketwriter.close()
                client.close()

                println "Policy File sent."

                return
            }
        }

        socketwriter.print("Connection: Done.\n")
        socketwriter.flush()

        socketwriter.close()
        socketreader.close()
    }

    static main(args)
    {
        ServerSocket serversocket

        try
        {
            serversocket = new ServerSocket(2900)

            println "Server is up and running"

            while (true)
            {
                Socket client = serversocket.accept()

                Thread.start {
                    server.handleConnection(client)
                }
            }
        } catch (e)
        {
            e.printStackTrace()
        } finally
        {
            serversocket.close()
        }
    }
}

Flex client code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import flash.net.Socket;
            import flash.events.*;
            import mx.controls.Alert;

            private var socket : Socket;

            private function connect() : void 
            {
                socket = new Socket();

                socket.addEventListener(Event.CONNECT, onConnect);
                socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
                socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ServerSecurityError);

                socket.connect("localhost", 2900);

                Alert.show("yay!");
            }

            private function ServerSecurityError(evt : Event) : void
            {
                Alert.show("security error")
            }

            private function onConnect(evt : Event) : void
            {
                Alert.show("Connected to server!");
            }

            private function onSocketData(evt : ProgressEvent) : void
            {
                var data : String = socket.readUTFBytes(socket.bytesAvailable);
                Alert.show("Read: " + data);
            }

            private function send() : void 
            {
               var msg : String = "Hello, World!";
               socket.writeUTFBytes(msg);
               socket.flush();
               Alert.show("Sent: " + msg);
            }
        ]]>
    </mx:Script>

    <mx:Button label="connect" click="connect()" x="10" y="10" />
    <mx:Button label="send something" click="send()" x="10" y="40" />
</mx:Application>

So, the question is: How should i connect to get a stable communication between server and client?

1
I think you should give us your full error message. My guess is that this is a cross domain issue. Is the SWF being served off the groovy server? - JeffryHouser
@www.Flextras.com thank you for your response. I get the error message in the ServerSecurityError method. Do you have any ideas on how i can get the error message there? - shybovycha
All relevant info should be in the event that is sent into that method. - JeffryHouser

1 Answers

2
votes

A SecurityError occurs for the following reasons (from the docs):

  • Local untrusted SWF files cannot communicate with the Internet. You can work around this limitation by reclassifying the file as local-with-networking or as trusted.
  • You cannot specify a socket port higher than 65535.
  • In the HTML page that contains the SWF content, the allowNetworking parameter of the object and embed tags is set to "none".

We're running a similar setup (using a pure AS3 frontend), and before I make the call to connect(), I call:

Security.loadPolicyFile( "xmlsocket://" + this.m_host + ":" + this.m_policyFilePort );

where m_host can be something like "localhost" and m_policyFilePort is whatever port you're serving it on. You can find more on it here, or at the Socket files