1: <?xml version="1.0" encoding="utf-8"?>
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
3: creationComplete="init()">
4: <mx:Script>
5: <![CDATA[
6: private var custSocket:Socket;
7: [Bindable] private var response:String = "";
8:
9: private function init():void {
10: custSocket = new Socket("localhost", 21567);
11: configureListeners();
12: }
13:
14: private function onClick(evt:Event):void {
15: sendRequest();
16: }
17:
18: private function configureListeners():void {
19: custSocket.addEventListener(Event.CLOSE, closeHandler);
20: custSocket.addEventListener(Event.CONNECT, connectHandler);
21: custSocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
22: custSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
23: custSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
24: }
25:
26: private function writeln(str:String):void {
27: str += "\n";
28: try {
29: custSocket.writeUTFBytes(str);
30: }
31: catch(e:IOError) {
32: trace(e);
33: }
34: }
35:
36: private function sendRequest():void {
37: trace("sendRequest");
38: writeln(inTxt.text);
39: custSocket.flush();
40: }
41:
42: private function readResponse():void {
43: var str:String = custSocket.readUTFBytes(custSocket.bytesAvailable);
44: response += str;
45: }
46:
47: private function closeHandler(event:Event):void {
48: trace("closeHandler: " + event);
49: trace(response.toString());
50: }
51:
52: private function connectHandler(event:Event):void {
53: trace("connectHandler: " + event);
54: }
55:
56: private function ioErrorHandler(event:IOErrorEvent):void {
57: trace("ioErrorHandler: " + event);
58: }
59:
60: private function securityErrorHandler(event:SecurityErrorEvent):void {
61: trace("securityErrorHandler: " + event);
62: }
63:
64: private function socketDataHandler(event:ProgressEvent):void {
65: trace("socketDataHandler: " + event);
66: readResponse();
67: }
68:
69: ]]>
70: </mx:Script>
71: <mx:TextArea text="{response}" id="outTxt"
72: height="126" width="283" fontSize="12"/>
73: <mx:HBox verticalAlign="bottom" width="282" height="40">
74: <mx:TextArea id="inTxt" width="100%" height="100%" fontSize="12"/>
75: <mx:Button label="发送" fontSize="12" click="onClick(event)"/>
76: </mx:HBox>
77:
78: </mx:Application>