Mina是什么?Apache的定义如下:
Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
Mina是一个网络应用架构,它能帮助我们很轻松的开发出功能强大、可控性强的网络应用。它在 TCP/IP及UDP/IP的基础上通过 Java NIO提供了一个抽象的事件驱动的异步传输消息的编程接口。目前最新版本是:Apache MINA 2.0.0-M5 release。
根据Mina的样例,我将演示一个HelloWorldServer和HelloWordClient的创建
准备工作
您需要准备如下包:
下载地址如下:
mina-2.0.0-M5.zip:http://mina.apache.org/index.html
slf4j-1.5.6.zip:www.slf4j.org
准备好之后,编写HelloWorldServerHandler
package com.ysh.mina.sampleserver;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class HelloWorldServerHandler extends IoHandlerAdapter {
//当一个客端端连结进入时
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming client : "+session.getRemoteAddress());
}
//当一个客户端关闭时
@Override
public void sessionClosed(IoSession session) {
System.out.println("one Clinet Disconnect !");
}
//当客户端发送的消息到达时:
@Override
public void messageReceived(IoSession session, Object message) throws Exception
{
//我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:
String s=(String)message;
// Write the received data back to remote peer
System.out.println("收到客户机发来的消息: "+s);
session.write("Hello Word: "+"Greeting From Server");
}
}
编写HelloWorld服务器端启动程序HelloWorldServer
package com.ysh.mina.sampleserver;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class HelloWorldServer {
public static void main(String[] args) {
try{
//创建一个非阻塞的Server端Socket,用NIO
SocketAcceptor acceptor = new NioSocketAcceptor();
//创建接收数据的过滤器
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
//设定这个过滤器将一行一行(/r/n)的读取数据
chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
//设定服务器端的消息处理器:一个SamplMinaServerHandler对象,
acceptor.setHandler(new HelloWorldServerHandler());
// 服务器端绑定的端口
int bindPort=9988;
//绑定端口,启动服务器
acceptor.bind(new InetSocketAddress(bindPort));
System.out.println("Mina Server is Listing on:= " + bindPort);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
接着编写HelloWorldClientHandler
package com.ysh.mina.sampleclient;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class HelloWorldClientHandler extends IoHandlerAdapter{
//当一个客端端连结进入时
@Override
public void sessionOpened(IoSession session) throws Exception {
session.write("Hello World: server :"+ "Client: "+session.getRemoteAddress()+" is coming!");
}
//当一个客户端关闭时
@Override
public void sessionClosed(IoSession session) {
System.out.println("Clinet"+session.getRemoteAddress()+" is disconnected!");
}
//当服务器端发送的消息到达时:
@Override
public void messageReceived(IoSession session, Object message)throws Exception {
//我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:
String s=(String)message;
System.out.println("收到服务器发来的消息: "+s);
}
}
编写我们的客户端程序和服务器端很相似HelloWorldClient
package com.ysh.mina.sampleclient;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class HelloWorldClient {
public static void main(String[] args) throws Exception {
//实际应用中,这里可以生成一个线程来监听
// Create TCP/IP connector.
NioSocketConnector connector = new NioSocketConnector();
// 创建接收数据的过滤器
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
//设定这个过滤器将一行一行(/r/n)的读取数据
chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));
//设定服务器端的消息处理器:一个SamplMinaServerHandler对象,
connector.setHandler(new HelloWorldClientHandler());
connector.setConnectTimeoutCheckInterval(3000000);
//连结到服务器:
try
{
ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 9988));
cf.awaitUninterruptibly();
cf.getSession().getCloseFuture().awaitUninterruptibly();
}
catch(Exception e)
{
connector.dispose();
System.out.println("未能连接到服务器");
}
}
}
大功告成:可以运行了。。。。