mina框架入门(简单客户端和服务端实例)

学习资料来自:守望者
Mina 是apche开发的一个开源的网络通信框架,基于JavaNIO来实现

需要用到的jar包如下:可自行到官网下载mina Apache官网

mina框架入门(简单客户端和服务端实例)_第1张图片

案例:入门服务端程序

写一个服务端:
MinaServer.java:

package com.v523.mina;

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 {
     
	static int PORT = 7080;
	static IoAcceptor accept = null;
	public static void main(String[] args) {
     
		try {
     
			accept = new 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));
			System.out.println("Server ->"+PORT);
		} catch (Exception e) {
     
			e.printStackTrace();
		}
	}
}

Myhandler .java:

package com.v523.mina;

import java.util.Date;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class Myhandler extends IoHandlerAdapter{
     

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
     
		System.out.println("連接異常:exceptionCaught");
	}

	@Override
	public void inputClosed(IoSession session) throws Exception {
     
		
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
     
		String msg = (String)message;
		System.out.println("服務端接收到數據"+msg);
		if(msg.equals("exit")){
     
			session.close();
		}
		Date date = new Date();
		session.write(date);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
     
		System.out.println("發送數據時要調用的方法:messageSent");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
     
		System.out.println("session關閉時調用的方法:sessionClosed");
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
     
		System.out.println("session創建時調用的方法:sessionCreated");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
     
		System.out.println("空閒狀態:sessionIdle");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
     
		System.out.println("打開session:sessionOpened");
	}

}

通过telnet模拟客户端:打开命令提示窗口输入 telnet 127.0.0.1 7080
mina框架入门(简单客户端和服务端实例)_第2张图片

入门客户端程序(来代替telnet的功能)

MinaClient .java:

package com.v523.mina;

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 MinaClient {
     
	private static String host="127.0.0.1";
	private static int port=7080;
	public static void main(String[] args) {
     
		IoSession session = 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 MyClientHander());
		ConnectFuture future = connector.connect(new InetSocketAddress(host, port));
		future.awaitUninterruptibly();//等待我們的連接
		session = future.getSession();
		session.write("你好!watchmen");
		session.getCloseFuture().awaitUninterruptibly();//等待關閉連接
		connector.dispose();
	}
}

MyClientHander.java:

package com.v523.mina;

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

public class MyClientHander extends IoHandlerAdapter {
     
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
     
		System.out.println("發生異常時:exceptionCaught");
	}
	
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
     
		String msg = (String)message;
		System.out.println("客戶端收到數據"+msg);
	}
}

测试效果如下:
mina框架入门(简单客户端和服务端实例)_第3张图片

你可能感兴趣的:(Apache,mina通信框架学习笔记)