Apache MINA客户端服务器简单实例

其实客户端和服务器是相对的,互发消息,在底层当然是互为服务器客户端.只是这里为了封装区分开了.


首先

需要导入的包:

log4j.jar

mina-core-2.0.4.jar 

slf4j-api-1.6.3.jar 

slf4j-log4j12-1.6.3.jar


服务端代码:


/**
 */
public class MinaTimeServer {
                // 服务器监听端口
	private static final int PORT = 8888;
	/**
	 * 
	 */
	public MinaTimeServer() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 服务器端的主要对象
		IoAcceptor acceptor = new NioSocketAcceptor();
		
        // 设置Filter链 
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        // 协议解析,采用mina现成的UTF-8字符串处理方式
		acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		
        // 设置消息处理类(创建、关闭Session,可读可写等等,继承自接口IoHandler)
		acceptor.setHandler(new TimeServerHandler() );
        // 设置接收缓存区大小
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		try {
            // 服务器开始监听
			acceptor.bind( new InetSocketAddress(PORT) );
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

服务端业务处理类:这个业务处理类就是mina给我们分开的,把收发消息和业务处理分开了.你发送消息,监听端口是一个地方.然后业务处理是另一个地方,这样你就可以更加灵活的操作业务逻辑,不用关心底层的数据发送.

package aniyo.mina.server;

import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * 继承自IoHandlerAdapter,IoHandlerAdapter继承接口 IoHandler
        类IoHandlerAdapter实现了IoHandler的所有方法,只要重载关心的几个方法就可以了
 */
public class TimeServerHandler extends IoHandlerAdapter {

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		cause.printStackTrace();
	}

    /*
     * 这个方法是目前这个类里最主要的,
     * 当接收到消息,只要不是quit,就把服务器当前的时间返回给客户端
     * 如果是quit,则关闭客户端连接*/
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		String str = message.toString();
		if (str.trim().equalsIgnoreCase("quit")) {
			session.close();
			return;
		}
		Date date = new Date();
		System.out.println("hello"+str+session.getRemoteAddress()+date.toString());
		
		session.write("i am recived");
		System.out.println("Message written...");

	}
	
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionClosed(session);
		System.out.println("客户端与服务端断开连接.....");
	}

}

客户端:客户端主要是拿到发送对象,然后向服务端端口发送数据

package aniyo.mina.server;
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;   
  
/**  
 * mina客户端  
 */  
public class MinaClient {   
  
    public static void main(String []args)throws Exception{   
           
        //Create TCP/IP connection   
        NioSocketConnector connector = new NioSocketConnector();   
           
        //创建接受数据的过滤器   
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();   
           
        //设定这个过滤器将一行一行(/r/n)的读取数据   
        chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory()));   
           
        //客户端的消息处理器:一个SamplMinaServerHander对象   
        connector.setHandler(new MinaClientHandler());   
           
        //set connect timeout   
        connector.setConnectTimeout(30);   
           
        //连接到服务器:   
        ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",8888));   
           
        //Wait for the connection attempt to be finished.   
        cf.awaitUninterruptibly();   
           
        cf.getSession().getCloseFuture().awaitUninterruptibly();   
           
        connector.dispose();   
    }  
}
客户端处理类,这个和服务端处理类是一样的,为了给用户一个统一的业务处理接口.

package aniyo.mina.server;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * 客户端业务处理逻辑

 */
public class MinaClientHandler extends IoHandlerAdapter {
	// 当客户端连接进入时
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		System.out.println("incomming 客户端: " + session.getRemoteAddress());
		session.write("i am coming");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		System.out.println("客户端发送信息异常....");
	}

	// 当客户端发送消息到达时
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {

		System.out.println("服务器返回的数据:" + message.toString());
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		System.out.println("客户端与服务端断开连接.....");
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		System.out
				.println("one Client Connection" + session.getRemoteAddress());
		session.write("我来了······");
	}

}

mina的性能很高,解决一般的数据库交互绝对没问题,和其它技术结合,实现基于消息传递非常好,其中openFire就是实现的mina框架,来解决客户端服务器通信的.建议大家了解一下.

你可能感兴趣的:(【Java基础】)