mina 框架如何发送字节数组

时间:2023-01-12 07:25:15
mina 框架用来做NIO,挺方便,但是如何才可以发送字节数组呢?接收没问题的.

小弟是这样做的
                int blen = Pkg.createLoginRespPackage(1).length;
                System.out.println("------------------------> len:" + blen );
                ByteBuffer buf = ByteBuffer.allocate( blen ) ;
                if(buf!=null){
                    System.out.println("send...");
                    //buf.clear();
                    //bb.get(Pkg.createLoginRespPackage(1));
                    buf.put( Pkg.createLoginRespPackage(1) );
                    //buf.put((byte) '.');
                    buf.flip();
                    //session.write(bb.duplicate());         //发送验证包
                    session.write(buf);         //发送验证包
                    //buf.clear();
                }

但是出现如下异常:

java.lang.IllegalStateException: Don't know how to handle message of type 'java.nio.HeapByteBuffer'.  Are you missing a protocol encoder?
at org.apache.mina.common.AbstractPollingIoProcessor.flushNow(AbstractPollingIoProcessor.java:536)
at org.apache.mina.common.AbstractPollingIoProcessor.flush(AbstractPollingIoProcessor.java:469)
at org.apache.mina.common.AbstractPollingIoProcessor.access$500(AbstractPollingIoProcessor.java:43)
at org.apache.mina.common.AbstractPollingIoProcessor$Worker.run(AbstractPollingIoProcessor.java:681)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
at java.lang.Thread.run(Thread.java:595)

19 个解决方案

#1


该回复于2015-05-26 11:34:56被管理员删除

#2


请懂MINA框架的朋友帮忙,MINA是好框架,但发不了字节数组吗?

#3


已经自己解决,需要者留言

#4


能告诉我你是怎么解决的吗?
我的邮箱4kghost@163.com

#5


哥们告诉我一下怎么发送byte数组,还有就是 你的客户端和服务器端都是用的什么过滤器,为什么mina里面不用过滤器就只能够建立连接却不能够进行数据的交互。???我的邮箱pengli.bj@163.com   谢谢了

#6


我也想知道,邮箱是yukikaze77@gmail.com
谢谢

#7


lilong_1985@126.com

好啊!  我为什么总报下面的异常啊。  请问下如何维持多个客户端的连接啊?? 急。。

java.lang.IllegalStateException: Already released buffer.  You released the buffer too many times.
at org.apache.mina.common.SimpleByteBufferAllocator$SimpleByteBuffer.release(SimpleByteBufferAllocator.java:81)
at org.apache.mina.util.ByteBufferUtil.releaseIfPossible(ByteBufferUtil.java:39)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageSent(AbstractIoFilterChain.java:581)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageSent(AbstractIoFilterChain.java:320)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1200(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageSent(AbstractIoFilterChain.java:653)
at org.apache.mina.filter.LoggingFilter.messageSent(LoggingFilter.java:97)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageSent(AbstractIoFilterChain.java:320)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1200(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageSent(AbstractIoFilterChain.java:653)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:222)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
at java.lang.Thread.run(Thread.java:619)

#8


IoBuffer b = IoBuffer.allocate(100);
b.putShort((short) 18);// len
String str ="Hello";
ByteArrayOutputStream outputPacket = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(outputPacket);
dos.writeUTF(str);
dos.flush();
b.put(outputPacket.toByteArray());
b.flip();
session.write(b);

#9


你好,我非常的需要你这个解决办法,希望你能帮助我,QQ 240835726  MSN jianmao334@live.cn  邮箱jianmao334@yahoo.com.cn

#10


我也遇到这个问题了,如果解决的呀,希望你能帮助我。

piaohailin@gmail.com   msn:phlsbg@msn.com

#11


我也遇到这个问题了。。谢谢 邮箱:361806691@qq.com

#12


没有设置编码的原因,你加上这句试试:
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

#13


我也遇到这个问题了。。这都过了好几年了,如何解决的,谢谢 邮箱:361806691@qq.com

#14


我也遇到了棘手啊,还能帮助我吗?邮箱13622058276@139.com 谢谢

#15


这么多人想要,你为什么不直接贴出来呢?

#16


发一份给我吧?遇到这样的问题honeyfinsh@hotmail.com

#17


发一份给我吧?遇到这样的问题wind_fzsh@163.com

#18


我也遇到这个问题的,有的能发一份给我的吗?xgurry@126.com

#19


cj111234@163.com  求一份

#1


该回复于2015-05-26 11:34:56被管理员删除

#2


请懂MINA框架的朋友帮忙,MINA是好框架,但发不了字节数组吗?

#3


已经自己解决,需要者留言

#4


能告诉我你是怎么解决的吗?
我的邮箱4kghost@163.com

#5


哥们告诉我一下怎么发送byte数组,还有就是 你的客户端和服务器端都是用的什么过滤器,为什么mina里面不用过滤器就只能够建立连接却不能够进行数据的交互。???我的邮箱pengli.bj@163.com   谢谢了

#6


我也想知道,邮箱是yukikaze77@gmail.com
谢谢

#7


lilong_1985@126.com

好啊!  我为什么总报下面的异常啊。  请问下如何维持多个客户端的连接啊?? 急。。

java.lang.IllegalStateException: Already released buffer.  You released the buffer too many times.
at org.apache.mina.common.SimpleByteBufferAllocator$SimpleByteBuffer.release(SimpleByteBufferAllocator.java:81)
at org.apache.mina.util.ByteBufferUtil.releaseIfPossible(ByteBufferUtil.java:39)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageSent(AbstractIoFilterChain.java:581)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageSent(AbstractIoFilterChain.java:320)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1200(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageSent(AbstractIoFilterChain.java:653)
at org.apache.mina.filter.LoggingFilter.messageSent(LoggingFilter.java:97)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageSent(AbstractIoFilterChain.java:320)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1200(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageSent(AbstractIoFilterChain.java:653)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:222)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
at java.lang.Thread.run(Thread.java:619)

#8


IoBuffer b = IoBuffer.allocate(100);
b.putShort((short) 18);// len
String str ="Hello";
ByteArrayOutputStream outputPacket = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(outputPacket);
dos.writeUTF(str);
dos.flush();
b.put(outputPacket.toByteArray());
b.flip();
session.write(b);

#9


你好,我非常的需要你这个解决办法,希望你能帮助我,QQ 240835726  MSN jianmao334@live.cn  邮箱jianmao334@yahoo.com.cn

#10


我也遇到这个问题了,如果解决的呀,希望你能帮助我。

piaohailin@gmail.com   msn:phlsbg@msn.com

#11


我也遇到这个问题了。。谢谢 邮箱:361806691@qq.com

#12


没有设置编码的原因,你加上这句试试:
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

#13


我也遇到这个问题了。。这都过了好几年了,如何解决的,谢谢 邮箱:361806691@qq.com

#14


我也遇到了棘手啊,还能帮助我吗?邮箱13622058276@139.com 谢谢

#15


这么多人想要,你为什么不直接贴出来呢?

#16


发一份给我吧?遇到这样的问题honeyfinsh@hotmail.com

#17


发一份给我吧?遇到这样的问题wind_fzsh@163.com

#18


我也遇到这个问题的,有的能发一份给我的吗?xgurry@126.com

#19


cj111234@163.com  求一份

#20