当前位置:我的异常网» Flex » flex和java在socket交互,该如何处理
flex和java在socket交互,该如何处理(2)
www.myexceptions.net 网友分享于:2015-08-26 浏览:75次
} catch (IOException e) {
e.printStackTrace();
}
//将ByteArrayOutputStream流中转化成字节数组
byte[] messageBytes=byteoutStream.toByteArray();//amf3数据
OutputStreamWriter osw;//使用amf3格式将写入流中的数据编码成字节
BufferedWriter bwrite;//用来封装OutputStreamWriter,以提高效率
try {
System.out.println("输出数组长度"+messageBytes.length);
serverSocekt=new ServerSocket(5556);//开启服务器进程
System.out.println("服务器已经启动。。。。。。。");
socket=serverSocekt.accept();
if(socket.isConnected())
{
System.out.println(">>>>>>>>>>客户端已连接");
}
//socket.
osw=new OutputStreamWriter(socket.getOutputStream());//将字符流转化为字节流
bwrite=new BufferedWriter(osw);//封装osw对象,提高写入的效率
socket.getOutputStream().write(messageBytes);//向流中写入二进制数据
socket.getOutputStream().flush();
socket.getOutputStream().close();
serverSocekt.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Flex 端
import vo.Amf;
import mx.collections.ArrayCollection;
var obj:Object=new Object();
private function Appinit():void{
var socket:Socket=new Socket();
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
//socket.connect("192.168.1.31",1234);
socket.connect("127.0.0.1",5556);
//socket.connect("127.0.0.1",1234);
addEventListener("recieved",recievedData);
function closeHandler(event:Event):void {
trace("连接关闭");
}
function connectHandler(event:Event):void {
trace("连接成功");
//sendSocket("1232123");
}
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler信息: " + event);
}
function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler信息: " + event);
}
function socketDataHandler(event:ProgressEvent):void {
trace("接收数据");
obj=socket.readObject();
dispatchEvent(new Event("recieved"));
}
function sendSocket(msg:String):void{
var message:ByteArray=new ByteArray();
var amf:Amf = new Amf();
amf.name = "123";
amf.id = "1";
message.writeObject(amf);
socket.writeBytes(message);
socket.flush();
}
}
文章评论