.as3与.net进行socket通信
.net代码:
public class SocketTest
{
private Socket _server;
public void Start()
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.11"), 9999);
this._server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._server.Bind(iep);
this._server.Listen(5);
while (true)
{
SocketInfo si=new SocketInfo();
Socket client = this._server.Accept();
si.ClientSocket=client;
client.BeginReceive(si.Content, 0, 1024, SocketFlags.None, new AsyncCallback(ReceiveCallBack), si);
}
}
private void ReceiveCallBack(IAsyncResult ir)
{
SocketInfo si = ir.AsyncState as SocketInfo;
try
{
Socket client = si.ClientSocket;
int receiveLen = client.EndReceive(ir);
if (receiveLen > 0)
{
byte[] temp = new byte[receiveLen];
Array.Copy(si.Content, 0, temp, 0, temp.Length);
Console.WriteLine(string.Format("收到从{0}发来的消息:{1}", client.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(temp)));
client.Send(temp);
}
client.BeginReceive(si.Content, 0, 1024, SocketFlags.None, new AsyncCallback(ReceiveCallBack), si);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public class SocketInfo
{
private byte[] _Content=new byte[1024];
public byte[] Content
{
get { return this._Content; }
set { this._Content = value; }
}
public Socket ClientSocket { get; set; }
}
as代码:
封装的socket连接类:
package netTest
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
public class CSocket extends EventDispatcher
{
private var _ipAddress:String="192.168.0.11";
private var _port:uint=9999;
private var _socket:Socket;
private var _receiveData:String;
public function CSocket()
{
this._socket=new Socket();
this._socket.addEventListener(Event.CONNECT,SocketConnectHandler);
this._socket.addEventListener(IOErrorEvent.IO_ERROR,SocketIoErrorHandler);
this._socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,SocketSecErrorHandler);
this._socket.addEventListener(ProgressEvent.SOCKET_DATA,ReceiveHandler);
this._socket.addEventListener(Event.CLOSE,SocketCloseHandler);
}
public function Connect():void
{
if(!this._socket.connected)
{
this._socket.connect(this._ipAddress,this._port);
}
}
public function Send(txt:String):void
{
if(!this._socket.connected)
{
this.Connect();
}
this._socket.writeUTFBytes(txt);
this._socket.flush();
}
public function Close():void
{
this._socket.close();
}
public function Dispose():void
{
this._socket.removeEventListener(Event.CONNECT,SocketConnectHandler);
this._socket.removeEventListener(IOErrorEvent.IO_ERROR,SocketIoErrorHandler);
this._socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,SocketSecErrorHandler);
this._socket.removeEventListener(ProgressEvent.SOCKET_DATA,ReceiveHandler);
this._socket.removeEventListener(Event.CLOSE,SocketCloseHandler);
if(this._socket.connected)
{
this._socket.close();
}
this._socket=null;
}
public function get ReceiveString():String
{
return this._receiveData;
}
private function ReceiveHandler(e:ProgressEvent):void
{
//trace(this._socket.bytesAvailable);
this._receiveData=this._socket.readUTFBytes(this._socket.bytesAvailable);
//trace(this._receiveData);
this.dispatchEvent(e);
}
private function SocketConnectHandler(e:Event):void
{
this.dispatchEvent(e);
}
private function SocketCloseHandler(e:Event):void
{
this.dispatchEvent(e);
}
private function SocketIoErrorHandler(e:IOErrorEvent):void
{
this.dispatchEvent(e);
}
private function SocketSecErrorHandler(e:SecurityErrorEvent):void
{
this.dispatchEvent(e);
}
}
}
flex 实现代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="init()">
<mx:Text x="10" y="19" text="发送内容:"/>
<mx:TextInput x="73" y="15" width="292" id="tbSend"/>
<mx:Text x="10" y="68" text="执行结果:"/>
<mx:TextArea x="73" y="67" width="292" height="166" id="tbResult"/>
<mx:Button x="73" y="257" label="连 接" id="btnConn" width="68" click="Connect()"/>
<mx:Button x="167" y="257" label="发 送" id="btnSend" width="68" click="Send()"/>
<mx:Button x="261" y="257" label="关 闭" id="btnDispose" width="68" click="Dispose()"/>
<mx:Script>
<![CDATA[
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.ProgressEvent;
private var _cs:CSocket;
internal function init():void
{
this._cs=new CSocket();
this._cs.addEventListener(Event.CONNECT,SocketConnectHandler);
this._cs.addEventListener(IOErrorEvent.IO_ERROR,SocketIoErrorHandler);
this._cs.addEventListener(SecurityErrorEvent.SECURITY_ERROR,SocketSecErrorHandler);
this._cs.addEventListener(ProgressEvent.SOCKET_DATA,ReceiveHandler);
this._cs.addEventListener(Event.CLOSE,SocketCloseHandler);
}
private function Dispose():void
{
this._cs.removeEventListener(Event.CONNECT,SocketConnectHandler);
this._cs.removeEventListener(IOErrorEvent.IO_ERROR,SocketIoErrorHandler);
this._cs.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,SocketSecErrorHandler);
this._cs.removeEventListener(ProgressEvent.SOCKET_DATA,ReceiveHandler);
this._cs.removeEventListener(Event.CLOSE,SocketCloseHandler);
this._cs.Dispose();
this._cs=null;
}
private function Send():void
{
this._cs.Send(this.tbSend.text);
}
private function Connect():void
{
this._cs.Connect();
}
private function ReceiveHandler(e:ProgressEvent):void
{
this.tbResult.text=this._cs.ReceiveString;
}
private function SocketConnectHandler(e:Event):void
{
this.tbResult.text="Socket连接成功!";
}
private function SocketCloseHandler(e:Event):void
{
this.tbResult.text="Socket关闭!";
}
private function SocketIoErrorHandler(e:IOErrorEvent):void
{
this.tbResult.text=e.toString();
}
private function SocketSecErrorHandler(e:SecurityErrorEvent):void
{
this.tbResult.text=e.toString();
}
]]>
</mx:Script>
</mx:Canvas>
练手做的,不是很全面。嘿嘿。欢迎拍砖。