本文是入门篇,先来写一个简单的socket server服务器程序,可以用telnet来连接,当服务器收到客户端的连接时会把服务器上的时间返回给客户。
首先引入四个包:
[size=large]mina-core-2.0.0.jar
slf4j-api-1.6.1.jar
slf4j-jdk14-1.6.1.jar
slf4j-log4j12-1.6.1.jar
接着创建一个主服务类:MinaTimeServer
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.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MinaTimeServer {
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
//This filter will log all information such as newly created
//sessions, messages received, messages sent, session closed
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
//This filter will translate binary or protocol specific data into
//message object and vice versa. We use an existing TextLine
//factory because it will handle text base message for you (
//you don't have to write the codec part)
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("UTF-8"))));
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(BUF_SIZE);
//the first parameter defines what actions to check for when
//determining if a session is idle, the second parameter defines
//the length of time in seconds that must occur before a session
//is deemed to be idle.
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}
private static final int PORT = 8181,BUF_SIZE = 2048;
}
再创建一个handler来管理事件
package mina;
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 TimeServerHandler extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
cause.printStackTrace();
}
@SuppressWarnings("deprecation")
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String str = message.toString();
System.out.println("Message received:"+str);
if(str.trim().equalsIgnoreCase("quit")){
session.close();
return;
}
Date date = new Date();
session.write(date.toString());
System.out.println("Message written.");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
super.sessionCreated(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("IDLE"+session.getIdleCount(status));
}
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionOpened(session);
}
}
运行MinaTimeServer,再用telnet来连接,命令格式如:telnet 127.0.0.1 8181
(第一节结束,第二节待续)[/size]
分享到:
相关推荐
apache mina 框架的学习文档,通过此文档可以基本了解mina框架。
Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)
这是一个关于mina框架非常全的自学文档
mina2学习笔记
真的是学习MIna很好的资源,找了好久才找到的,看了很多相关的知识书籍,这本书真的不错。
Apache的Mina(Multipurpose Infrastructure Networked Applications)是一个网络应用框架,可以帮助用户开发高性能和高扩展性的网络应用程序;它提供了一个抽象的、事件驱动的异步API,使Java NIO在各种传输协议...
这个是我学的apache mina2.0学习笔记的实例
Mina学习笔记。作者:李少华
Mina2.0学习笔记(修订版) 文档中附带大量实现的源码
Mina框架+常用JAR包 Mina框架+常用JAR包 Mina框架+常用JAR包 Mina框架+常用JAR包 Mina框架+常用JAR包 Mina框架+常用JAR包
最近使用Mina开发一个Java的NIO服务端程序,因此也特意学习了Apache的这个Mina框架。 引言 1 一. Mina入门 2 第一步.下载使用的Jar包 2 第二步.工程创建配置 2 第三步.服务端程序 3 第四步.客户端程序 6 第五步.长...
mina学习笔记,记录所有API,mina学习笔记,记录所有API
主要Mina框架的架构和使用流程做了一个详细的简介,方便用户和读者使用和下载
java mina框架
apache mina 框架 实例 自定义协议包 自定义编码器 解码器 服务端 客户端
要实现mina框架最基本的资源包,拥有这个就可以实现mina的通讯
mina框架的demo 入门,开发 学习
该文档较好的介绍了,apache mina框架的使用方法。
丰富的MINA实例介绍,java NIO 学习,mina快速入门宝典