简单的mina应用
一个简单的mina服务端客户端,只包含必要的元素,理解用mina搭建服务器的步骤。
简单的mina服务端
- 新建IoAccepter接收器 ;
- 设置过滤器;
- 设置缓冲区大小;
- 设置空闲时间;
- 设置业务处理类;
- 绑定端口;
服务端代码
代码片
.
import java.io.IOException;
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 MinaServer {
private static int port = 8888;//端口
public static void main(String[] args) {
IoAcceptor accept = null;//接收器
try {
accept = new NioSocketAcceptor();//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);//设置空闲时间
accept.setHandler(new MyHandler());//设置业务处理类
accept.bind(new InetSocketAddress(port));//绑定端口
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.mina;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class MyHandler implements IoHandler {
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("sessionCreated--》创建");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("sessionOpened--》打开");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("sessionClosed--》关闭");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println("sessionIdle--》空闲");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println("exceptionCaught--》出现异常");//强退时会出现异常
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
System.out.println("messageReceived--》接收消息");
String msg = (String) message;
System.out.println("接收到消息:"+msg);
if(msg.equals("exit")) {
session.close();
}
session.write(new Date());
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("messageSent--》发送消息");
}
@Override
public void inputClosed(IoSession session) throws Exception {
System.out.println("inputClosed");
}
}
简单的mina客户端
- 新建IoConnector连接器 ;
- 设置业务处理类;
- 建立连接
- 连接阻塞,监听连接成功
- 获得IoSession;
- 发送消息
- 监听关闭阻塞;
客户端代码
package com.mina;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
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;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class MinaClient {
private static String host ="127.0.0.1";
private static int port =8888;
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(MinaClient.class.getClassLoader().getResourceAsStream("gameLog4j.properties"));
} catch (Exception e) {
e.printStackTrace();
}
PropertyConfigurator.configure(properties);
IoConnector conn= new NioSocketConnector();
conn.setConnectTimeout(3000);
conn.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("utf-8"), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())));
conn.setHandler(new MyClientHandler());
ConnectFuture connectFuture = conn.connect(new InetSocketAddress(host, port));
connectFuture.awaitUninterruptibly();
IoSession session = connectFuture.getSession();
session.write("hello");
session.getCloseFuture().awaitUninterruptibly();
session.closeNow();
}
}
package com.mina;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class MyClientHandler implements IoHandler {
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("sessionCreated");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("sessionOpened");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("sessionClosed");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
System.out.println("sessionIdle");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
System.out.println("exceptionCaught");
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
System.out.println("messageReceived");
System.out.println("message:"+message);
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("messageSent");
}
@Override
public void inputClosed(IoSession session) throws Exception {
System.out.println("inputClosed");
}
}