flex与C# Socket通信
原文地址: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>\0"; buffer = System.Text.Encoding.UTF8.GetBytes(policy.ToCharArray()); ClientSocket.Send(buffer, buffer.Length, 0); } else { break; } } ClientSocket.Close(); receivethread.Abort(); } } }
as socket程序更简单如下view plaincopy to clipboardprint?package Socket { import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.Socket; import flash.utils.ByteArray; public class SPSocket { /** * Socket 套接字 */ public var socket:Socket=new Socket(); /** * 域名或者主机Ip */ public var host:String="localhost"; /** * 通讯端口 */ public var port:int=1111; /** * 接收数据的类型 */ public var RecieveDataType:String=""; /** * 发送数据的类型 */ public var SendDataType:String=""; /** * 通讯状态 */ private var stateMap:Object; /** * 当前通讯状态 */ private var currentState:int; /** * 字符类型 */ public static const CHARS:String="chars"; /** * 字节类型 */ public static const BYTES:String="bytes"; public function SPSocket() { socket.addEventListener(Event.CONNECT,onConnect); socket.addEventListener(Event.CLOSE,onClose); socket.addEventListener(IOErrorEvent.IO_ERROR,onIoError); socket.addEventListener(ProgressEvent.SOCKET_DATA,onSocketData); } public function connect():void{ if(!socket.connected){ socket.connect(host,port); } } public function close():void{ socket.close(); } /** * 处理Socket接收数据 */ private function onSocketData(e:ProgressEvent):void{ if(RecieveDataType==BYTES){ var bytes:ByteArray; socket.readBytes(bytes); /*trace("收到的数据:"+socket.bytesAvailable+"b/s"); while(socket.bytesAvailable){ var data:int=socket.readByte(); trace(data); }*/ }else if(RecieveDataType==CHARS){ var recieveData:String=socket.readUTFBytes(socket.bytesAvailable); } } /** * 写入数据 */ public function WriteDate():void{ if(socket.connected){ } } private function onConnect(e:Event):void{ trace("正在连接服务器..."); } private function onClose(e:Event):void{ trace("正在与服务器断开连接..."); } private function onIoError(e:Event):void{ trace("正在与服务器断开连接..."); } } } 演示结果: