mina框架接收数据包连在一块问题。

angreens 2012-08-13 11:23:31
我最近用java Mina框架写了一个服务器程序,在100个连接同时发送数据时候,服务器这边有时候收到的数据包会连在一块,有时候一个包会拆成两次发送,我很郁闷,这样给我进行协议解析造成很大麻烦。请教一下CSDN上面有用过Mina框架的大侠这是怎么回事?
...全文
582 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
changzhen1990 2012-11-16
  • 打赏
  • 举报
回复
也遇到了同样的问题…… protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { IoBuffer buffer = IoBuffer.allocate(10).setAutoExpand(true); // CharsetDecoder decoder = charset.newDecoder(); //装配数据 while (in.hasRemaining()) { byte b = in.get(); buffer.put(b); } //校验数据是否接受完成 byte[] recDatas = buffer.array(); String recDatasStr = new String(recDatas,"GBK"); if(recDatas.length > 4){ //取出前两位对比数据长度 String recDatasLen = recDatasStr.substring(0, 4); int recDatasLenInt = 0; try { recDatasLenInt = Integer.parseInt(recDatasLen); if((recDatasLenInt + 4) > recDatasStr.length()){ //说明数据没有接收完整 return true; } } catch (Exception e) { e.printStackTrace(); //数据格式转换错误 //将数据解码,往上一层发 out.write(recDatasStr); return false; } } //获取银联数据响应的有效数据 recDatasStr.substring(4, recDatasStr.length()); //将数据解码,往上一层发 out.write(recDatasStr); return false; } 让能说一下为什么我没收到数据?
  • 打赏
  • 举报
回复
遇到同样的问题了!
  • 打赏
  • 举报
回复
遇到同样的问题了!
angreens 2012-08-16
  • 打赏
  • 举报
回复
谢谢 dracularking,问题解决了,和上面说的一样,自己实现解码器,按照协议对收到的字节数据进行解析,生成自己消息对象,不完整数据暂时保存等待下次接收,完美解决了粘包,断包问题。
dracularking 2012-08-14
  • 打赏
  • 举报
回复
哦我也只是初步怀疑一下,现在找到官方对于多包并作一包发送的解释:

是OS提高发包效率的做法

Why does SocketConnector send several messages as one message?

For example, I tried using SocketConnector to send "abc" and "def", but it sent "abcdef". Is it a MINA bug?

No, this is due to your OS trying to send packets more efficiently (see http://en.wikipedia.org/wiki/Nagle_algorithm). You can enable/disable Nagle's algorithm by a call to SocketSessionConfig.setTcpNoDelay(), e.g.:

((SocketSessionConfig) connector.getSessionConfig()).setTcpNoDelay(false)

However, even if you do this you cannot expect one session.write(bytes) in MINA to correspond to one TCP packet on your network. You should probably implement your own MINA ProtocolDecoder to handle the assembly of incoming bytes into message objects. The TextLineCodec is a good start if the protocol you're implementing is based on text lines. For a more advanced example have a look at the SumUp example in the MINA distribution.

http://mina.apache.org/faq.html#FAQ-WhydoesSocketConnectorsendseveralmessagesasonemessage%253F
angreens 2012-08-13
  • 打赏
  • 举报
回复
NioSocketAcceptor tcp_server=null;
if (null == tcp_server) {
tcp_server = new NioSocketAcceptor(3);
tcp_server.getSessionConfig().setReadBufferSize(4096);
tcp_server.getSessionConfig().setReceiveBufferSize(4096);
tcp_server.getSessionConfig().setTcpNoDelay(true);
tcp_server.setReuseAddress(true);
tcp_server.getFilterChain().clear();
}

if (null == clientHandle) {
clientHandle = new ClientHandle(myForm);
tcp_server.setHandler(clientHandle);
}
上面是我的代码,设置过了啊?
dracularking 2012-08-13
  • 打赏
  • 举报
回复
看一下是否和输入输出缓冲区大小有关

acceptor.getSessionConfig().setReceiveBufferSize(1024);
acceptor.getSessionConfig().setSendBufferSize(10240);

http://www.blogjava.net/crespochen/archive/2009/04/17/266203.html

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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