[ActionScript 3.0] AS3 ServerSocket示例(官方示例)
下面的示例创建一个套接字服务器。要使用该服务器,可将套接字绑定到本地端口,然后从其他应用程序连接到该端口。该服务器仅识别 UTF-8 字符串。
1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.events.MouseEvent; 6 import flash.events.ProgressEvent; 7 import flash.events.ServerSocketConnectEvent; 8 import flash.net.ServerSocket; 9 import flash.net.Socket; 10 import flash.text.TextField; 11 import flash.text.TextFieldType; 12 import flash.utils.ByteArray; 13 14 /** 15 * @author Frost.Yen 16 * @E-mail 871979853@qq.com 17 * @create 2015-8-21 下午3:49:14 18 * 19 */ 20 public class ServerSocketExample extends Sprite 21 { 22 private var serverSocket:ServerSocket = new ServerSocket(); 23 private var clientSocket:Socket; 24 25 private var localIP:TextField; 26 private var localPort:TextField; 27 private var logField:TextField; 28 private var message:TextField; 29 30 public function ServerSocketExample() 31 { 32 setupUI(); 33 34 } 35 private function onConnect( event:ServerSocketConnectEvent ):void 36 { 37 clientSocket = event.socket; 38 clientSocket.addEventListener( ProgressEvent.SOCKET_DATA, onClientSocketData ); 39 log( "Connection from " + clientSocket.remoteAddress + ":" + clientSocket.remotePort ); 40 } 41 42 private function onClientSocketData( event:ProgressEvent ):void 43 { 44 var buffer:ByteArray = new ByteArray(); 45 clientSocket.readBytes( buffer, 0, clientSocket.bytesAvailable ); 46 log( "Received: " + buffer.toString() ); 47 } 48 49 private function bind( event:Event ):void 50 { 51 if( serverSocket.bound ) 52 { 53 serverSocket.close(); 54 serverSocket = new ServerSocket(); 55 56 } 57 serverSocket.bind( parseInt( localPort.text ), localIP.text ); 58 serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect ); 59 serverSocket.listen(); 60 log( "Bound to: " + serverSocket.localAddress + ":" + serverSocket.localPort ); 61 } 62 63 private function send( event:Event ):void 64 { 65 try 66 { 67 if( clientSocket != null && clientSocket.connected ) 68 { 69 clientSocket.writeUTFBytes( message.text ); 70 clientSocket.flush(); 71 log( "Sent message to " + clientSocket.remoteAddress + ":" + clientSocket.remotePort ); 72 } 73 else log("No socket connection."); 74 } 75 catch ( error:Error ) 76 { 77 log( error.message ); 78 } 79 } 80 81 private function log( text:String ):void 82 { 83 logField.appendText( text + "\n" ); 84 logField.scrollV = logField.maxScrollV; 85 trace( text ); 86 } 87 88 private function setupUI():void 89 { 90 localIP = createTextField( 10, 10, "Local IP", "0.0.0.0"); 91 localPort = createTextField( 10, 35, "Local port", "0" ); 92 createTextButton( 170, 60, "Bind", bind ); 93 message = createTextField( 10, 85, "Message", "Lucy can't drink milk." ); 94 createTextButton( 170, 110, "Send", send ); 95 logField = createTextField( 10, 135, "Log", "", false, 200 ) 96 97 this.stage.nativeWindow.activate(); 98 } 99 100 private function createTextField( x:int, y:int, label:String, defaultValue:String = '', editable:Boolean = true, height:int = 20 ):TextField 101 { 102 var labelField:TextField = new TextField(); 103 labelField.text = label; 104 labelField.type = TextFieldType.DYNAMIC; 105 labelField.width = 100; 106 labelField.x = x; 107 labelField.y = y; 108 109 var input:TextField = new TextField(); 110 input.text = defaultValue; 111 input.type = TextFieldType.INPUT; 112 input.border = editable; 113 input.selectable = editable; 114 input.width = 280; 115 input.height = height; 116 input.x = x + labelField.width; 117 input.y = y; 118 119 this.addChild( labelField ); 120 this.addChild( input ); 121 122 return input; 123 } 124 125 private function createTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField 126 { 127 var button:TextField = new TextField(); 128 button.htmlText = "<u><b>" + label + "</b></u>"; 129 button.type = TextFieldType.DYNAMIC; 130 button.selectable = false; 131 button.width = 180; 132 button.x = x; 133 button.y = y; 134 button.addEventListener( MouseEvent.CLICK, clickHandler ); 135 136 this.addChild( button ); 137 return button; 138 } 139 } 140 }
------------------------------------------------------------------
Always put yourself in the other's shoes.If you feel that it hurts you,it probably hurts others,too.------------------------------------------------------------------