<?xml version= "1.0" encoding= "utf-8" ?> <mx:Application xmlns:mx= "http://www.adobe.com/2006/mxml" layout= "vertical" verticalAlign= "middle" fontSize= "12" creationComplete= "onInit()" > ???? <mx:ViewStack id= "viewstack1" width= "454" height= "338" selectedIndex= "0" > ???????? <mx:Canvas label = "View 1" width= "100%" height= "100%" > ???????????? <mx:TitleWindow width= "100%" height= "100%" layout= "absolute" title= "连接设置" > ???????????????? <mx:Button x= "181" y= "186" label = "连??? 接" click= "onConnect()" id= "btnCon" /> ???????????????? <mx:TextInput x= "181" y= "31" id= "txtHost" text= "127.0.0.1" width= "184" /> ???????????????? <mx:TextInput x= "181" y= "94" id= "txtPort" width= "184" text= "6000" /> ???????????????? <mx:Label x= "74" y= "31" text= "链接地址:" /> ???????????????? <mx:Label x= "74" y= "94" text= "连接端口:" /> ???????????????? <mx:Button x= "297" y= "186" label = "清??? 空" click= "onEmpty()" /> ???????????? </mx:TitleWindow> ???????? </mx:Canvas> ???????? <mx:Canvas label = "View 1" width= "100%" height= "100%" > ???????????? <mx:TitleWindow width= "100%" height= "100%" layout= "absolute" title= "消息窗口" > ???????????????? <mx:TextArea x= "10" y= "10" width= "414" height= "176" id= "txtRec" /> ???????????????? <mx:Button x= "160" y= "262" label = "发??? 送" fontSize= "12" click= "onSendData()" /> ???????????????? <mx:Button x= "260" y= "262" label = "断??? 开" fontSize= "12" click= "onClose()" /> ???????????????? <mx:TextInput x= "10" y= "194" width= "414" id= "txtSend" height= "45" /> ???????????????? <mx:Text x= "201" y= "202" text= "正在断开,请稍后..." visible= "false" id= "msg" /> ???????????????? <mx:Button x= "355" y= "262" label = "关??? 闭" click= "onCloseWindow()" /> ???????????? </mx:TitleWindow> ???????? </mx:Canvas> ???? </mx:ViewStack> ????? ? ???? <mx:Script> ???????? <![CDATA[ ???????????? import mx.controls.Alert; ???????????? import flash.net.Socket; ???????????? import flash.utils.ByteArray; ???????????? import flash.events.SecurityErrorEvent; ???????????? import flash.events.IOErrorEvent; ???????????? import flash.events.ProgressEvent; ????????????? ? ???????????? ???????????? private var socket:Socket= new Socket(); ???????????? ???????????? private function onInit(): void ???????????? { ???????????????? socket.addEventListener(Event.CONNECT,connect); ???????????????? socket.addEventListener(Event.CLOSE,onClosed); ???????????????? socket.addEventListener(IOErrorEvent.IO_ERROR,onIoError); ???????????????? socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSeurity); ???????????????? socket.addEventListener(ProgressEvent.SOCKET_DATA,onReceiveData); ???????????? } ????????????? ? ???????????? private function connect(e:Event): void ???????????? { ???????????????? this .viewstack1.selectedIndex= 1 ; ???????????? } ????????????? ? ???????????? private function onClosed(e:Event): void ???????????? { ????????????????? ? ???????????? } ????????????? ? ???????????? ???????????? private function onConnect(): void ???????????? { ???????????????? var host: String = this .txtHost.text; ???????????????? var port: Number = Number ( this .txtPort.text); ???????????????? if (!(host&&port)) ???????????????? { ???????????????????? Alert.show( "服务器地址和端口不能为空!" , "提示" ); ???????????????????? return ; ???????????????? } ???????????????? socket.connect(host,port); ???????????? } ???????????? ???????????? private function onClose(): void ???????????? { ???????????????? socket.close(); ???????????????? this .viewstack1.selectedIndex= 0 ; ???????????? } ????????????? ? ???????????? ???????????? private function onIoError(evt:IOErrorEvent): void ???????????? { ???????????????? Alert.show(evt.text, "IO错误!" ); ???????????? } ???????????? ???????????? private function onSeurity(evt:SecurityErrorEvent): void ???????????? { ???????????????? Alert.show(evt.text, "安全策略错误" );?? ???????????? } ????????????? ? ???????????? ???????????? private function onSendData(): void ???????????? { ???????????????? var buff:ByteArray= new ByteArray(); ???????????????? buff.writeUTF( this .txtSend.text); ???????????????? socket.writeBytes(buff, 0 ,buff.length); ???????????????? socket.flush(); ???????????? } ???????????? ???????????? private function onReceiveData(evt:ProgressEvent): void ???????????? { ???????????????? while (socket.bytesAvailable) ???????????????? { ???????????????????? this .txtRec.text+=socket.readMultiByte(socket.bytesAvailable, "utf8" )+ "n" ; ???????????????? } ???????????? } ????????????? ? ???????????? ???????????? ???????????? private function onEmpty(): void ???????????? { ???????????????? this .txtHost.text= "" ; ???????????????? this .txtPort.text= "" ; ???????????????? this .txtHost.setFocus(); ???????????? } ???????????? ???????????? private function onCloseWindow(): void ???????????? { ???????????????? var request:URLRequest = new URLRequest( "javascript:window.close()" ); ???????????????? navigateToURL(request, "_self" ); ???????????? } ????????????? ? ???????? ]]> ???? </mx:Script> </mx:Application> |