NIO流行框架 Mina helloword

一.Mina框架介绍

Apche开发的一个开源的网络通信框架,基于我们javaNIO来实现


NIO流行框架 Mina helloword_第1张图片

二.练习简单的服务端步骤


 1.NIOSocket
 2.设置编码解码过滤器
 3.设置一些session属性
 4.绑定一个端口


package com.ccop;

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 Server {


	private static int port=7080;
	static IoAcceptor accpet = null;
	
	public static void main(String[] args) throws IOException{
		accpet = new NioSocketAcceptor();
		//设置编码过滤器
		accpet.getFilterChain().addLast("codec",new ProtocolCodecFilter(
						new TextLineCodecFactory(
						Charset.forName("UTF-8"),    //编码集
						LineDelimiter.WINDOWS.getValue(), //编码器
						LineDelimiter.WINDOWS.getValue())));
		accpet.getSessionConfig().setReadBufferSize(1024);//缓存区
		accpet.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);//10毫秒进入空闲状态
		accpet.setHandler(new ServerHandler()); //具体业务层
		accpet.bind(new InetSocketAddress(port));
		System.out.println("Server-->"+port);
	}
	
}

package com.ccop;

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 ServerHandler extends IoHandlerAdapter{

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionCreated(session);
		System.out.println("ServerHandler.sessionCreated()");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionOpened(session);
		System.out.println("ServerHandler.sessionOpened()");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionClosed(session);
		System.out.println("ServerHandler.sessionClosed()");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		// TODO Auto-generated method stub
		super.sessionIdle(session, status);
		System.out.println("ServerHandler.sessionIdle()");
	}

	/**
	 * 异常
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(session, cause);
		System.out.println("ServerHandler.exceptionCaught()");
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		String msg = (String) message;
		System.out.println("服务端接收到数据:"+msg);
		if(msg.equals("exit")){
			session.close();
		}
		Thread.sleep(2000);
		Date date =new Date();
		session.write(date);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		super.messageSent(session, message);
	}

	@Override
	public void inputClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.inputClosed(session);
	}

}

NIO流行框架 Mina helloword_第2张图片

package com.ccop;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
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.NioSocketConnector;

public class client {

	private static String host = "127.0.0.1";
	private static int port=7080;
	
	public static void main(String[] args){
		
		IoSession ioSession=null;
		
		IoConnector connector = new NioSocketConnector();
		connector.setConnectTimeout(3000);
		connector.getFilterChain().addLast(
				"coderc", 
				new ProtocolCodecFilter(
				new TextLineCodecFactory(Charset.forName("UTF-8"),
				LineDelimiter.WINDOWS.getValue(),
				LineDelimiter.WINDOWS.getValue())));
		connector.setHandler(new clientHandler());
		
		ConnectFuture futrue= connector.connect(new InetSocketAddress(host,port));
		futrue.awaitUninterruptibly(); //等待我的连接
		ioSession=futrue.getSession();
		ioSession.write("你好 !");
		ioSession.getCloseFuture().awaitUninterruptibly();//等待关闭连接
		
		connector.dispose();
		
	}
}

package com.ccop;

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 clientHandler extends IoHandlerAdapter{

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionCreated(session);
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionOpened(session);
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionClosed(session);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		// TODO Auto-generated method stub
		super.sessionIdle(session, status);
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(session, cause);
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		String msg = (String) message;
		System.out.println("客户端收到数据:"+msg);
		if(msg.equals("exit")){
			session.close();
		}
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		super.messageSent(session, message);
	}

	@Override
	public void inputClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.inputClosed(session);
	}

}

Server-->7080
ServerHandler.sessionCreated()
ServerHandler.sessionOpened()
10:50:30.333 [NioProcessor-2] DEBUG o.a.m.f.codec.ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 1
服务端接收到数据:你好 !
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()
ServerHandler.sessionIdle()

10:50:32.353 [NioProcessor-2] DEBUG o.a.m.f.codec.ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 1
客户端收到数据:Sun Mar 19 10:50:32 CST 2017



你可能感兴趣的:(NIO流行框架 Mina helloword)