原文地址:http://blog.csdn.net/LX10752p/archive/2011/04/27/6366526.aspx
Socket 通信没什么好说,一个服务端,多个客户端,很容易搭建环境,网上代码一大片,这里主要的问题是as进行socket通信,必须跨A域B域,这时问题出现了,flash player会阻止你跨域访问,这时,flash player会自动检查安全策略。
在Adobe Flash Player升级到9.0.124后,由于安全策略的更改,使得在socket或xmlsocket的应用里,原先如用http方式加载安全策略的手段不能继续使用了,类似此类应用必须使用xmlsocket:
flashplayer的安全策略检测过程如下:
1,首先检测目标服务器的843端口是否提供安全策略
2,如果843端口没有检测到策略,则检测actionscript是否使用了
Security.loadPolicyFile(xmlsocket:ip:端口)//使用其他特定端口提供安全策略
3,检测目标服务器目标端口是否提供安全策略
C#利用socket提供安全策略,我们可以通过多种方式实现,如Window应用窗台,控制台应用程序,或者打包成服务等等
下面以控制台为例子
view plaincopy to clipboardprint?using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections; namespace serverSocket { class Program { private Thread Beginthread, receivethread; private Socket serverSocket, ClientSocket; //主程序 static void Main(string[] args) { Program program = new Program(); IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 843);// 可以尝试 Security.loadPolicyFile(xmlsocket:ip:端口),使用其他的端口 program.serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); program.serverSocket.Bind(remotePoint); program.serverSocket.Listen(10); Console.WriteLine("Listening..."); program.Beginthread = new Thread(new ThreadStart(program.BeginListen)); program.Beginthread.Start(); } //循环监听 private void BeginListen() { while (true) { ClientSocket = serverSocket.Accept(); receivethread = new Thread(new ThreadStart(receive)); receivethread.Start(); } } //等待客户端请求 <policy-file-request/> private void receive() { IPEndPoint clienTep = (IPEndPoint)ClientSocket.RemoteEndPoint; int bytes = 0; while (true) { Byte[] buffer = new Byte[24]; bytes = ClientSocket.Receive(buffer); if (bytes > 0) { string clientCommand = Encoding.UTF8.GetString(buffer, 0, bytes); String policy = "<cross-domain-policy><allow-access-from domain="*" to-ports="1111" /></cross-domain-policy>