注意:flash/flex客户服端与服务器端的编码要统一,都用二进制形式传输 flash文档类: package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.utils.*; public class SocketExample extends Sprite { private var hostName:String = "127.0.0.1"; private var port:uint = 6666; private var socket:Socket; public function SocketExample() { socket = new Socket(); configureListeners(socket); socket.connect(hostName, port); } private function configureListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.CLOSE, closeHandler); dispatcher.addEventListener(Event.CONNECT, connectHandler); dispatcher.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); } private function closeHandler(event:Event):void { //trace("closeHandler: " + event); } private function connectHandler(event:Event):void { trace("connectHandler: " + event); socket.writeMultiByte("I'm from flash.","ascii"); socket.flush(); } private function dataHandler(event:ProgressEvent):void { //trace("dataHandler: " + event); //trace(socket.readBytes(); var rec:String = socket.readUTFBytes(socket.bytesAvailable); trace(rec); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } } }