mina使用心得
1、mina中设置过滤器有先后顺序,如果不按照顺序,mina的服务器段可能会接收一部分数据的现象。
mina设置服务器顺序 eg:
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();//创建一个过滤器链对象
TextLineDecoder txld = new TextLineDecoder(Charset.forName("UTF-8"),new LineDelimiter("\0"));
//创建一个文本行解码器,编码为UTF-8,且行定符合是"\0"
txld.setMaxLineLength(Integer.MAX_VALUE);
TextLineEncoder txed = new TextLineEncoder(Charset.forName("UTF-8"),new LineDelimiter("\0"));
txed.setMaxLineLength(Integer.MAX_VALUE);
TextLineCodecFactory codecFactory = new TextLineCodecFactory();
codecFactory.setDecoderMaxLineLength(1024 * 1024);// 设定后服务器可以接收大数据
chain.addLast("codec", new ProtocolCodecFilter(txed,txld));//过滤器链添加编码解码器
chain.addLast( "Executor", new ExecutorFilter(Executors.newCachedThreadPool()) );
chain.addLast("logging", new LoggingFilter());//这个用于打印日志 可以不写
2、mina设置tcp端口重用(不设置可以重用端口,当你要重启服务器的时候,如果服务器上还有tcp的链接,重启服务器会不成功!)
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress(true);//设置可以端口重用
3、mina获取客户端的ip地址
String clientIP = ((InetSocketAddress)session.getRemoteAddress()).getAddress().getHostAddress();
1、mina中设置过滤器有先后顺序,如果不按照顺序,mina的服务器段可能会接收一部分数据的现象。
mina设置服务器顺序 eg:
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();//创建一个过滤器链对象
TextLineDecoder txld = new TextLineDecoder(Charset.forName("UTF-8"),new LineDelimiter("\0"));
//创建一个文本行解码器,编码为UTF-8,且行定符合是"\0"
txld.setMaxLineLength(Integer.MAX_VALUE);
TextLineEncoder txed = new TextLineEncoder(Charset.forName("UTF-8"),new LineDelimiter("\0"));
txed.setMaxLineLength(Integer.MAX_VALUE);
TextLineCodecFactory codecFactory = new TextLineCodecFactory();
codecFactory.setDecoderMaxLineLength(1024 * 1024);// 设定后服务器可以接收大数据
chain.addLast("codec", new ProtocolCodecFilter(txed,txld));//过滤器链添加编码解码器
chain.addLast( "Executor", new ExecutorFilter(Executors.newCachedThreadPool()) );
chain.addLast("logging", new LoggingFilter());//这个用于打印日志 可以不写
2、mina设置tcp端口重用(不设置可以重用端口,当你要重启服务器的时候,如果服务器上还有tcp的链接,重启服务器会不成功!)
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress(true);//设置可以端口重用
3、mina获取客户端的ip地址
String clientIP = ((InetSocketAddress)session.getRemoteAddress()).getAddress().getHostAddress();