flex和java在socket交互
转自http://hjy2099.iteye.com/blog/306376
首要要了解.两种语言是怎样通信的..主要在JAVA端.
JAVA工程需最少加入两个FLEX包.一个是flex-messaging-common.jar另一个是flex-messaging-core.jar.
这两个就是序列化JAVA给FLEX或着FLASH 用.以下为实例.介绍
JAVA 中的 ArrayList Array HashMap.
| | |
AS 中的 ArrayCollection Array Object
java端
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import vo.Amf;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
public class AmfServer
{
public static void main(String args[])
{
SerializationContext serializationContext=new SerializationContext();
//序列化amf3对象
Amf3Output amfout=new Amf3Output(serializationContext);
//实现了一个输出流,其中的数据被写入一个 byte 数组。
ByteArrayOutputStream byteoutStream=new ByteArrayOutputStream();
//将byteoutStream产生的数组流导入到DataOutputStream流中
DataOutputStream dataoutstream=new DataOutputStream(byteoutStream);
//创建ServerSocket和Socket对象
ServerSocket serverSocekt;
Socket socket;
// 设置流的编码格式为amf3
amfout.setOutputStream(dataoutstream);
//创建Map对象、Double对象数组
HashMap map=new HashMap();
map.put("Event", "移动");
map.put("user", "OKD");
map.put("x", "123");
map.put("y", "321");
//创建vo对象.存到ArrayList中.
Amf amf = new Amf();
amf.setId("1");
amf.setName("okd");
amf.setPassword("China Boy");
Amf amf2 = new Amf();
amf2.setId("2");
amf2.setName("okd_Ya");
amf2.setPassword("terry_boss.blog.163.com");
Amf amf3 = new Amf();
amf3.setId("3");
amf3.setName("okd_Gook");
amf3.setPassword("terry_boos@163.com");
ArrayList al = new ArrayList();
al.add(amf);
al.add(amf2);
al.add(amf3);
//创建Array对象.
Amf[] ary = new Amf[3];
ary[0] = amf;
ary[1] = amf2;
ary[2] = amf3;
try {
//amfout.writeObject(al);//实际上是将map对象写入到dataoutstream流中
//dataoutstream.flush();//清空缓存
// amfout.writeObject(map);//实际上是将map对象写入到dataoutstream流中
// dataoutstream.flush();
// amfout.writeObject(ary);//实际上是将map对象写入到dataoutstream流中
// dataoutstream.flush();//清空缓存
} 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 端
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creatingContentPane="{Appinit()}">
<mx:Script>
<![CDATA[
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();
}
}
public function recievedData(evt:Event)
{
//trace(obj as ArrayCollection);
var objCol:ArrayCollection = obj as ArrayCollection;
if(objCol != null){
if(objCol.length){
for(var i:int = 0 ;i<objCol.length;i++){
var object:Object = objCol.getItemAt(i) as Object;
trace(object.id,object.name,object.password);
}
}
trace("数据长度Col:"+objCol.length);
}else{
var objAry:Array = obj as Array;
if(objAry != null){
if(objAry.length){
for(var i:int = 0 ;i<objAry.length;i++){
trace(objAry[i].id,objAry[i].name,objAry[i].password);
}
}
trace("数据长度Ary:"+objAry.length);
}else{
var objmap:Object = obj as Object;
if(objmap != null){
switch (obj.Event) {
case "移动" :
trace("把玩家:"+obj.user+" 移动到"+obj.x+","+obj.y);
break;
case "人物移动" :
trace("系统信息");
break;
default :
break;
}
}
}
}
}
]]>
</mx:Script>
</mx:Application>
以上实例重点是客户端的SOCKET要用readObject().而readObject()是要AMF3格式的...发送时也是要用writeObject()..主要是服务端的JAVA一定要能接受和发送AMF3和解析AMF3代码.主要就是靠Adobe公司看发的那个包...
[解决办法]
学习中
[解决办法]
学习一下!