首页 > 代码库 > 高并发MINA框架,网络编程(SOCKET)实现,简单的网络聊天DEMO

高并发MINA框架,网络编程(SOCKET)实现,简单的网络聊天DEMO

服务端

和http://blog.csdn.net/simonchi/article/details/40613923   文章类似

Apache MINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可扩展性的网络应用程序。它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步API

对JAVA的SOCKET做了一层包装

Apache MINA 通常可被称之为:

NIO 框架库;

一篇好的关于MINA详解的文章推荐:  http://www.iteye.com/topic/1109234#%E6%B3%A8%E4%BA%8C

客户端/服务器框架库;

或者一个网络socket库。

package com.tree.demo.mina;

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

	private static int BUF_SIZE = 1024;
	
	private static int PORT = 8212;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IoAcceptor acceptor = new NioSocketAcceptor();
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		acceptor.setHandler(new MinaBizHandler());
		acceptor.getSessionConfig().setReadBufferSize(BUF_SIZE);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		try {
			acceptor.bind(new InetSocketAddress(PORT));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

服务端处理器,重写了IoHandlerAdapter类中的方法,该类负责处理网络交互的数据以及session的关闭建立,连接的处理等等

package com.tree.demo.mina;

import java.util.Date;
import java.util.Scanner;

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

public class MinaBizHandler extends IoHandlerAdapter {

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

	@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);
		session.close(true);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("IDLE : " + session.getIdleCount(status) + ">now : " + new Date());
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// TODO Auto-generated method stub
		String str = message.toString();
		System.out.println("Message received: " + str);
		if (str.trim().equalsIgnoreCase("quit")) {
			session.close(true);
			return;
		}
		Scanner sc = new Scanner(System.in);
		String cnt = sc.nextLine();
		StringBuffer say = new StringBuffer();
		while (!cnt.equals("go")) {
			say.append("\n" + cnt);
			cnt = sc.nextLine();
		}
		// Date now = new Date();
		session.write(say);
		System.out.println("Message written !");

	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		super.messageSent(session, message);
		System.out.println("server sent : " + message.toString());
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

以上就实现了服务端的程序,从handler的程序处理来看,当每次收到客户端发来的消息,就会开始等待服务端输入信息,知道输入go才会将服务端消息发送给客户端,而客户端则在输入quit后,就断开与服务端的连接了

客户端消息


服务端消息



通过该例子,各位就可以在此基础上发挥,写个GUI的聊天工具就很轻松了,核心的东西已经有了,剩下的就是一些其它处理了。






















高并发MINA框架,网络编程(SOCKET)实现,简单的网络聊天DEMO