当前位置:首页 > flash 使用socket访问服务器,需要策略文件,C#服务器端的输出方法

flash 使用socket访问服务器,需要策略文件,C#服务器端的输出方法

点击次数:2628  更新日期:2011-01-03
\n

flash文件在单独运行时,如通过socket来与服务器端通信,就需要先访问服务器的834端口,来获取安全策略文件后,才可以继续访问。
\n在c#服务器端,就可以一直绑定834端口,在收到消息时,就返回策略文件内容的字符串。下面是实现代码

\n
public partial class Form1 : Form\n    {\n        public Form1()\n        {\n            InitializeComponent();\n        }\n\n        TcpListener tcpListener843;\n        Server843 server843;\n        Thread listenThread843 = null;\n        AppendText at;\n        private delegate void AppendText(string txt);\n        private void button1_Click(object sender, EventArgs e)\n        {\n            at = new AppendText(atxt);\n            server843 = new Server843();\n            tcpListener843 = new TcpListener(server843.serverEP);\n            Listen();\n\n        }\n        ///\n        /// 监听\n        ///\n        public void Listen()\n        {\n            try\n            {\n\n                tcpListener843.Start(100);\n            }\n            catch\n            {}\n            MessageBox.Show("开始监听...");\n            listenThread843 = new Thread(new ThreadStart(Listen_Process843));\n            listenThread843.Start();\n        }\n        private void atxt(string text)\n        {\n            textBox1.AppendText(text + "\\r\\n");\n        }\n        private void Appendtxt(string text)\n        {\n            textBox1.Invoke(at, new object[] { text });\n        }\n        private void Listen_Process843()\n        {\n            TcpClient clientTCP=null;\n            while (true)\n            {\n                try\n                {\n                    clientTCP = tcpListener843.AcceptTcpClient();\n                    Appendtxt("请求安全策略文件");\n                }\n                catch\n                {\n                }\n                NetworkStream ns = clientTCP.GetStream();\n                StreamWriter sw = new StreamWriter(ns);\n                sw.WriteLine("<?xml version=\\"1.0\\"?><!DOCTYPE cross-domain-policy SYSTEM \\"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\\"><cross-domain-policy><allow-access-from domain=\\"*\\" to-ports=\\"1025-9999\\" /> </cross-domain-policy>\\0");\n                sw.Close();\n                ns.Flush();\n                ns.Close();\n            }\n        }\n    }\n    public class Server843\n    {\n        public Server843()\n        {\n            IPHostEntry ipEntry;\n            try\n            {\n                string strHostName = Dns.GetHostName(); //得到本机的主机名\n                ipEntry = Dns.GetHostByName(strHostName);//取得本机IP\n                serverEP = new IPEndPoint(ipEntry.AddressList[0], 843);\n            }\n            catch\n            {\n            }\n        }\n        public IPEndPoint serverEP { get; set; }\n    }
\n