<?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"
?
"100%"
?
"100%"
>
????????????
<mx:TitleWindow width=
"absolute"
?
title=
"连接设置"
>
????????????????
<mx:Button x=
"181"
?
y=
"186"
?
"连??? 接"
?
click=
"onConnect()"
?
id=
"btnCon"
/>
<mx:TextInput x=
"31"
?
"txtHost"
?
text=
"127.0.0.1"
?
"184"
/>
"94"
?
"txtPort"
?
"184"
?
"6000"
/>
<mx:Label x=
"74"
?
"链接地址:"
/>
"连接端口:"
/>
"297"?
"清??? 空"
?
"onEmpty()"
/>
</mx:TitleWindow>
</mx:Canvas>
>
"消息窗口"
>
<mx:TextArea x=
"10"
?
"414"
?
"176"
?
"txtRec"
/>
"160"?
"262"
?
"发??? 送"
?
"onSendData()"
?
/>
"260"
?
"断??? 开"
?
"onClose()"
/>
"194"?
"txtSend"
?
"45"
/>
<mx:Text x=
"201"
?
"202"
?
"正在断开,请稍后..."
?
visible=
"false"
?
"msg"
/>
"355"?
"关??? 闭"
?
"onCloseWindow()"
/>
</mx:TitleWindow>
</mx:Canvas>
</mx:ViewStack>
?????
<mx:Script>
<![CDATA[
????????????
import
?
mx.controls.Alert;
flash.net.Socket;
flash.utils.ByteArray;
flash.events.SecurityErrorEvent;
flash.events.IOErrorEvent;
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);
}
????????????
?
connect(e:Event):
void
{
????????????????
this
.viewstack1.selectedIndex=
1
;
}
?????????????
onClosed(e:Event):
void
{
????????????????
?
}
????????????
?
//连接服务器
onConnect():
void
{
????????????????
host:
String
=
.txtHost.text;
port:
Number
?
=
Number
(
.txtPort.text);
if
(!(host&&port))
{
????????????????????
Alert.show(
"服务器地址和端口不能为空!"
,
"提示"
);
????????????????????
return
;
}
socket.connect(host,port);
}
//关闭服务器
onClose():
void
{
socket.close();
0
;
}
?????????????
//IO错误
onIoError(evt:IOErrorEvent):
void
{
Alert.show(evt.text,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"IO错误!"
);
}
//安全策略错误
onSeurity(evt:SecurityErrorEvent):
void
{
"安全策略错误"
);?
}
????????????
?
//发送
onSendData():
void
{
buff:ByteArray=
ByteArray();
buff.writeUTF(
.txtSend.text);
socket.writeBytes(buff,
socket.flush();
}
//接收
onReceiveData(evt:ProgressEvent):
void
{
while
(socket.bytesAvailable)
{
.txtRec.text+=socket.readMultiByte(socket.bytesAvailable,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"utf8"
)+
"n"
;
}
}
?????????????
/**ui操作**/
//清空
onEmpty():
void
{
.txtHost.text=
""
;
.txtPort.text=
;
.txtHost.setFocus();
}
//关闭浏览器
onCloseWindow():
void
{
request:URLRequest =
URLRequest(
"javascript:window.close()"
);
navigateToURL(request,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"_self"
);
}
????????????
?
]]>
</mx:Script>
</mx:Application>