Mina使用IoBuffer读取字节的问题

ogiso_pest 2012-04-06 04:00:25
问题描述:
使用telnet localhost 3005
输入123456
在console里显示:
服务端与客户端创建连接...
服务端与客户端连接打开...
b:49
b:50
b:51
b:52
b:53
b:54
rrrr
nnnnn
Msg:
服务端接收到的数据为:
Encode:Fri Apr 06 15:57:03 CST 2012
服务端发送信息成功...

为什么Msg为空呢?


这个是服务器端:
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class Demo1Server {

private static int PORT = 3005;

public static void main(String[] args) {
IoAcceptor acceptor = null;
try {
// 创建一个非阻塞的server端的Socket
acceptor = new NioSocketAcceptor();
// 设置过滤器(使用Mina提供的文本换行符编解码器)
// acceptor.getFilterChain().addLast(
// "codec",
// new ProtocolCodecFilter(new TextLineCodecFactory(Charset
// .forName("UTF-8"),
// LineDelimiter.WINDOWS.getValue(),
// LineDelimiter.WINDOWS.getValue())));
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new MyTextLineCodecFactory()));

// 设置读取数据的缓冲区大小
acceptor.getSessionConfig().setReadBufferSize(2048);
// 读写通道10秒内无操作进入空闲状态
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 绑定逻辑处理器
acceptor.setHandler(new Demo1ServerHandler());
// 绑定端口
acceptor.bind(new InetSocketAddress(PORT));

} catch (Exception e) {
e.printStackTrace();
}
}
}

这个是filter类,过滤掉回车换行符号
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;


import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class MyTextLineCodecDecoder implements ProtocolDecoder {

private Charset charset = Charset.forName("UTF-8");

IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);

public static byte [] ioBufferToByte(Object message)
{
if (!(message instanceof IoBuffer))
{
return null;
}
IoBuffer ioBuffer = (IoBuffer)message;
byte[] b = new byte[ioBuffer.limit()];
ioBuffer.get(b);
return b;
}

@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception {
// TODO Auto-generated method stub

while (in.hasRemaining()) {
byte b = in.get();
switch (b){
case '\r':
System.out.println("rrrr");
break;
case '\n':
System.out.println("nnnnn");
buf.flip();
String msg = buf.getString(charset.newDecoder());
System.out.println("Msg:"+msg);
out.write(msg);
break;
default:
System.out.println("b:"+b);
buf.put(b);
}

}


}

@Override
public void dispose(IoSession arg0) throws Exception {
// TODO Auto-generated method stub

}

@Override
public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
throws Exception {
// TODO Auto-generated method stub

}
}



Handler类

import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class Demo1ServerHandler extends IoHandlerAdapter {

@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("服务端与客户端创建连接...");
}

@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("服务端与客户端连接打开...");
}

@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = message.toString();
System.out.println("服务端接收到的数据为:" + msg);
if ("bye".equals(msg)) { // 服务端断开连接的条件
session.close(true);
}
Date date = new Date();
session.write(date);
}

@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("服务端发送信息成功...");
}

@Override
public void sessionClosed(IoSession session) throws Exception {

}

@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("服务端进入空闲状态...");
}

@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
System.out.println("服务端发送异常...");
}
}
...全文
10198 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
最帅的亮 2014-05-20
  • 打赏
  • 举报
回复
引用 4 楼 awt_620 的回复:
请问楼主这么强悍的自问自答,MyTextLineCodecFactory 在哪里?
在页面里面啊 笨蛋 自己好好find 一下
andychen314 2014-04-21
  • 打赏
  • 举报
回复
怎么解决的~急需啊
芮芮 2014-02-19
  • 打赏
  • 举报
回复
把解决方案贴出来吧
程序类猿 2013-10-12
  • 打赏
  • 举报
回复
请问楼主怎么解决的
u010269523 2013-07-10
  • 打赏
  • 举报
回复
楼主看到请私信 最近我也在做类似的功能 不怎么懂 能帮办下吗 谢谢啦
awt_620 2012-10-29
  • 打赏
  • 举报
回复
请问楼主这么强悍的自问自答,MyTextLineCodecFactory 在哪里?
lovemybanana 2012-07-30
  • 打赏
  • 举报
回复
怎么解决的呢?
ogiso_pest 2012-05-07
  • 打赏
  • 举报
回复
其实已经解决了~
ogiso_pest 2012-04-08
  • 打赏
  • 举报
回复
顶一下啊

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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