Flash Socket Policy Server

We have a custom built communication server that can handle video/audio and text. Well a while back we wrote a flash client for this server. Well Adobe had just updated their flash player to include some new security enhancements one of which involved not allowing cross domain policy files to be served up via HTTP. There had to be a server listening on port 843 in order to serve the policy file. So I went to task of whipping one up real fast.

        private static TcpListener tcl;
        //Generate policy
        private static string response = "" +
                                    "\0";
        public static void Main(string[] args)
        {
            Console.WriteLine("Starting up server...");

            tcl = new TcpListener(IPAddress.Any, 843);
            tcl.Start();
            tcl.BeginAcceptSocket(new AsyncCallback(AcceptCallback), tcl);
            Console.WriteLine("Hit enter/return to close");
            Console.ReadLine();
        }
        private static void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = null;
                Socket client = null;

                listener = (TcpListener)ar.AsyncState;
                client = listener.EndAcceptSocket(ar);
                NetworkStream ns = new NetworkStream(client);
                StreamReader sr = new StreamReader(ns);
                StreamWriter sw = new StreamWriter(ns);

                sr.Read();
                //Send policy
                sw.Write(response);
                sw.Flush();
                ns.Flush();
                //Cleanup
                sw.Close();
                sr.Close();
                ns.Close();
                client.Close();
                //Do it again!
                tcl.BeginAcceptSocket(new AsyncCallback(AcceptCallback), tcl);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }

So there it is. A quick and dirty flash socket policy server written in c#. You can compile/run this in windows or linux running mono. I’ve been using a modified version of this for quite some time to handle tens of thousands of users and never had a problem with it.

5 Comments

  1. Hot! Love it!

  2. Exactly what i need…..but cant make it work. Its a console app right? when i start it up it just exits immediately…am i being an idiot?

  3. why is it not working for me, I replaced the response with the actual policy file. and ran it on the web server but still my .swf is not able to connect to it.

    • You created a console or windows service right? An easy way to test if it’s working is to telnet to it. Open a cmd window and type telnet YOURSERVER.COM 843 It should connect but not show anything, hit the enter key and you should see your policy text returned to you. If that doesn’t work you need to make sure you have the 843 port open and that the app really is running on your server.

Leave a Comment

Back to Top