【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
服务器端主程序及IoHandler业务程序:
public class MinaServer {
static int PORT=7080;
//Mina三个主要的部分之一是IoService,其中服务器端实现IoService的是IoAcceptor,客户端实现IoService的是IoConnector
//IoAcceptor和IoConnector都分别有实现TCP/IP,UDP/IP及虚拟机管道通讯的子接口
static IoAcceptor accept = null;
// AbstractIoService 是IoService接口的适配层,是一个abstact class
// AbstractIoAcceptor 也是一个适配器,是一个 abstactr class ,只不过是继承自AbstarctIoService
// IoHandler是mina的三个主要部分之一,这主要定义了session相关的接口(create,open,status,idle),异常(exceptionCaught)及数据发送和接收接口(messageReceive,messageSend)
// IoHandlerAdapter是一个适配器,也是一个abstract class ,实现了IoHandler的接口
public static void main(String[] args)
{
try
{
accept = new NioSocketAcceptor();
//设置过滤器
accept.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
//设置缓冲区
accept.getSessionConfig().setReadBufferSize(1024);
accept.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10); //10s
accept.setHandler(new MyHandler());//业务
accept.bind(new InetSocketAddress(PORT));
System.out.println("server->"+PORT);
//Mina主要在个部分分别是:
//1:IoService:执行实际的IO操作,管理IO会话
//2:IoFilter Chain 将数据进行过滤或者转化为期望的数据结构,反之变然
//3:IoHandler 实际的业务操作
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class MyHandler extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println("exceptionCaught:" + "连接出现异常");
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String)message;
System.out.println("服务端接收到数据:" + msg);
Date date = new Date();
session.write(date); // 用于写入数据并发送
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("messageSent:" + "发送数据");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("sessionClosed:" + "session关闭");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("sessionCreated:" + "创建Session");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("sessionIdle:" + "处于多长时间是空闲状态");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("sessionOpened:" + "打开Session用于读写数据");
}
}
然后idea中编译程序,发现提示如下错误信息:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/joe/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/joe/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.1/log4j-slf4j-impl-2.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
可以先不管它,直接进行实验,具体解决多slf冲突的办法参考:http://www.slf4j.org/codes.html#multiple_bindings:
用手机给服务器电脑发现消息,手机上能够收到服务器返回的数据,并且服务器上出现如下界面:
server->7080
sessionCreated:创建Session
sessionOpened:打开Session用于读写数据
sessionIdle:处于多长时间是空闲状态
15:16:20.436 [NioProcessor-2] DEBUG org.apache.mina.filter.codec.ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 1
sessionIdle:处于多长时间是空闲状态
15:16:37.633 [NioProcessor-2] DEBUG org.apache.mina.filter.codec.ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 1
来源:oschina
链接:https://my.oschina.net/u/2963604/blog/3154854