mina使用心得

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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值