tags:

views:

379

answers:

1

Hi all, I am implementing a simulated b/s stock data system. I am using flex and c# for client and server sides. I found flash has a security policy and I handled the policy-file-request in my server code. But seems it doesn't work, because the code jumped out at "socket.Receive(b)" after connection. I've tried sending message on client in the connection handler, in that case the server can receive correct message. But the auto-generated "policy-file-request" can never be received, and the client can get no data sending from server. Here I put my code snippet.

my ActionScript code:

public class StockClient extends Sprite {
    private var hostName:String = "192.168.84.103";
    private var port:uint = 55555;
    private var socket:XMLSocket;

    public function StockClient() {
        socket = new XMLSocket();
        configureListeners(socket);
        socket.connect(hostName, port);
    }

    public function send(data:Object) : void{
        socket.send(data);
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.CLOSE, closeHandler);
        dispatcher.addEventListener(Event.CONNECT, connectHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
    }

    private function closeHandler(event:Event):void {
        trace("closeHandler: " + event);
    }

    private function connectHandler(event:Event):void {
        trace("connectHandler: " + event);
        //following testing message can be received, but client can't invoke data handler
//send("<policy-file-request/>");
    }

    private function dataHandler(event:ProgressEvent):void {
        //never fired
        trace("dataHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }
}

my C# code:

    const int PORT_NUMBER = 55555;
    const String BEGIN_REQUEST = "begin";
    const String END_REQUEST = "end";
    const String POLICY_REQUEST = "<policy-file-request/>\u0000";
    const String POLICY_FILE = "<?xml version=\"1.0\"?>\n" +
        "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"&gt;\n" +
        "<cross-domain-policy> \n" +
        " <allow-access-from domain=\"*\" to-ports=\"55555\"/> \n" +
        "</cross-domain-policy>\u0000";           
    ................

    private void startListening()
    {
        provider = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        provider.Bind(new IPEndPoint(IPAddress.Parse("192.168.84.103"), PORT_NUMBER));
        provider.Listen(10);
        isListened = true;

        while (isListened)
        {
            Socket socket = provider.Accept();
            Console.WriteLine("connect!");
            byte[] b = new byte[1024];
            int receiveLength = 0;
            try
            {
                // code jump out at this statement
                receiveLength = socket.Receive(b);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
           String request = System.Text.Encoding.UTF8.GetString(b, 0, receiveLength);

            Console.WriteLine("request:"+request);

            if (request == POLICY_REQUEST)
            {
                socket.Send(Encoding.UTF8.GetBytes(POLICY_FILE));
                Console.WriteLine("response:" + POLICY_FILE);
            }
            else if (request == END_REQUEST)
            {
                Dispose(socket);
            }
            else
            {
                StartSocket(socket); break;
            }
        }
    }

Sorry for the long code, please someone help with it, thanks a million

A: 

Hi Chris, that is because the socket policy file isn't requested on the port you are trying to join, but on the static port 843.

You should listen to port 843 to serve the policy requests. Also, I had some problems when immediately closing the socket after having sent the policy file. It seems that the socket should be left open for a few seconds after the policy file had been sent, otherwise Flash might simply drop the answer.

Note that this way, you can serve policy file requests from another application, not necessary from your main server.

The whole stuff is described in this documentation.

Tyn
Hi Tyn, thanks for your reply, I listened to 843 port and it gets no request. I just rewrite my server code, using TcpClient and Stream to handle request, now my client can trigger ProgressEvent.SOCKET_DATA event, and I can handle the data in my AJAX code. I don't know why there's no policy-file-request, although I run the web client and my server on 2 PC. It's not the solution but works for me.
Chris Lee
Try hosting the server from a different system. I don't think the flash player sends the policy file request unless it's going to a separate domain from behind a web server.
Chris Lacasse
Thanks Lacasse, I know what's wrong. I simply test the page by opening it in my local file system. When I deploy it to a web container, the policy file request is sent, I handled it as Tyn said and it works. Cheers, Thank you.
Chris Lee