/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package sockeserver; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import flex.messaging.io.*; import flex.messaging.io.amf.*; import java.util.HashMap; import java.util.Map; import java.util.zip.DeflaterOutputStream; /** * * @author mumuyu99 */ public class socketServer implements Runnable { public static void main(String[] args) { start(); } //****************************************************************// private static boolean isRunning = false; //服务器运行标志 ServerSocket ss = null; //socket连接 int userIndexNumber; //用户索引号 int userCount; //房间人数 //Iterator it=null; //迭代器 //*************************消息协议*********************************// private byte type; //消息类型 private byte mainCmd; //消息主命令 private byte subCmd; //消息子命令 private byte[] data; //消息数据 private SerializationContext context = new SerializationContext(); public static void start() { socketServer server = new socketServer(); // server.heardHeart(); new Thread(server).start(); System.out.println("服务器打开"); } public void run() { try { isRunning = true; try { ss = new ServerSocket(9999); } catch (IOException ex) { System.out.println(ex.toString()); } Socket socket; while (isRunning) { socket = ss.accept(); handleCmd(socket); } } catch (IOException ex) { System.out.println(ex.toString()); } } private void handleCmd(Socket inComing) { try { DataInputStream dis = new DataInputStream(inComing.getInputStream()); DataOutputStream dos = new DataOutputStream(inComing.getOutputStream()); System.out.println("准备好接收数据!"); byte[] lang = new byte[dis.available()]; dis.read(lang); System.out.println(new String(lang, "utf-8")); Amf3Output out = new Amf3Output(context); DeflaterOutputStream stream = new DeflaterOutputStream(dos); out.setOutputStream(stream); Map map = new HashMap(); String[] arr = "1,2,3,4,5".split(","); map.put("datas", arr); map.put("userName", "mumuyu99"); map.put("fullName", "最新消息"); out.writeObject(map); stream.finish(); dos.writeUTF("你好啊"); dos.flush(); } catch (IOException ex) { System.out.println(ex.toString()); } } } 上面是服务器代码!socketServer.java 下面是flex <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" > <fx:Script> <!--[CDATA[ import flash.net.Socket; import mx.controls.Alert; private static var socket:Socket; private var byteaa:ByteArray; private function connectSocket():void{ socket=new Socket("localhost",9999) var byteArray:ByteArray=new ByteArray(); var s:String="hello word!" byteArray.writeUTFBytes(s); socket.writeBytes(byteArray,0,byteArray.length); socket.writeMultiByte(s,"UTF-8"); socket.flush(); byteaa=new ByteArray(); socket.addEventListener(ProgressEvent.SOCKET_DATA,loaderHandler); socket.addEventListener(IOErrorEvent.IO_ERROR,errorIO); socket.addEventListener(Event.CONNECT,connectStart); } private function connectStart(event:Event):void{ Alert.show("连接成功!"); } private function errorIO(event:IOErrorEvent):void{ Alert.show("连接不到数据源!"); } private function loaderHandler(event:ProgressEvent):void { // var intervalId:uint = setTimeout(laoderInit,100); laoderInit(); } private function laoderInit():void{ var start:Number=getTimer(); while(socket.bytesAvailable>0){ socket.readBytes(byteaa); } byteaa.uncompress(); var obj:Object = byteaa.readObject(); Alert.show(obj.fullName); var end:Number=getTimer(); Alert.show("耗时:" + (end - start) + "毫秒"); } protected function button1_clickHandler(event:MouseEvent):void { connectSocket(); } ]]--> </fx:Script> <s:Button x="190" y="370" label="连接" enabled="true" click="button1_clickHandler(event)"/> </s:Application>