Mina 使用ProtoBuffer 序列化出了点问题。有高手请进。万急....

Dongo2 2013-06-03 11:03:34
public class MinaClientHanlder extends IoHandlerAdapter {
public void sessionOpened(IoSession session) throws Exception {
System.out.println("sessionOpened");
OpRequest.Builder builder = OpRequest.newBuilder();
builder.setMsg("1111111");
OpRequest request = builder.build();
byte[]data = request.toByteArray();
ByteBuffer buffer = ByteBuffer.wrap(data);
session.write(getObject(buffer));
}
public Object getObject(ByteBuffer byteBuffer) throws IOException, ClassNotFoundException
{
IoBuffer buffer = IoBuffer.allocate(byteBuffer.capacity()).setAutoExpand(true);
for(int i=0; i<byteBuffer.capacity();i++)
{
byteBuffer.position(i);
buffer.put(byteBuffer.get());
}
buffer.position(0);
InputStream input = buffer.asInputStream();
ObjectInputStream oi = new ObjectInputStream(input);
Object obj = oi.readObject();
input.close();
oi.close();
return obj;
}


连接后会出现exceptionCaught invalid stream header: 12073131
胜是不解啊,求大神们帮帮忙。或者有这方面经验的朋友可以说一下你们是怎么处理的。谢谢
...全文
222 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dongo2 2013-06-11
  • 打赏
  • 举报
回复
引用 5 楼 pdw_jsp 的回复:
没太明白是怎么回事! 能代码帮忙写一下你说的意思吗?灰常感谢
这是使用了Protobuffer 进行序列化的对象,在发送之前需要编码,然后发送对象。你说的那个只适用于同种语言的服务器和客户端。不知道我这样理解有咩有问题
冰思雨 2013-06-07
  • 打赏
  • 举报
回复
看楼主给出的代码,真是一种折磨。 根本搞不太清楚,这些代码要干一件什么事情。 逻辑上,有些冲突的东西。 1.首先,这个程序,是一个客户端程序。 2.其次,当客户端建立连接后,会向服务端发送一个请求包过去。 问题,就在这个发送代码当中。 众所周知,一个消息的发送操作,会依次经历 消息对象的生成、赋值、编码转换、数据发送 这几个阶段。 楼主代码中,先构建了一个工厂对象,向里面设置的内容,之后,调用方法生成请求对象。 接下来是 编码转换(对象向二进制数据的转换过程)过程,在这个过程之后,就应该是发送。 但是,楼主的代码,就将二进制数据转换成了对象进行发送,这个过程,我很是费解。 一般情况下,只有接受数据的时候,才需要解码(二进制数据向对象的转换)过程的。
Dongo2 2013-06-06
  • 打赏
  • 举报
回复
没太明白是怎么回事! 能代码帮忙写一下你说的意思吗?灰常感谢
Dongo2 2013-06-05
  • 打赏
  • 举报
回复
我不是都将ObjectInputStream关闭了吗?而且每次都是新创建的啊。怎么会是每次都冲那个流读 ?
  • 打赏
  • 举报
回复
ObjectInputStream 这个是对象输入流,只能写一个,你虽然关闭了,还是没用的。如果想写入多个对象,又都读出来,看看这个链接吧http://blog.csdn.net/zhouhuozhi/article/details/4156357
l_9style 2013-06-04
  • 打赏
  • 举报
回复
写数据应该用ObjectOutputStream
  • 打赏
  • 举报
回复
InputStream input = buffer.asInputStream(); ObjectInputStream oi = new ObjectInputStream(input); Object obj = oi.readObject(); 问题应该是在这三句上,每次读都是从ObjectInputStream这个流内读,肯定会出现问题,这句应该写在服务器端。 Your "server read method" is only reading one object. If it is called multiple times, you will get an error since it is trying to open several object streams from the same input stream. This will not work, since all objects were written to the same object stream on the client side, so you have to mirror this arrangement on the server side. That is, use one object input stream and read multiple objects from that. (The error you get is because the objectOutputStream writes a header, which is expected by objectIutputStream. As you are not writing multiple streams, but simply multiple objects, then the next objectInputStream created on the socket input fails to find a second header, and throws an exception.) To fix it, create the objectInputStream when you accept the socket connection. Pass this objectInputStream to your server read method and read Object from that.

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧