NIO实现简易聊天室功能

1.Server服务器端

NIOServer

package pers.shaojl;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;

public class NIOServer {

	private static final Logger logger = Logger.getLogger("NIOServer");

	public void start() throws IOException {

		/**
		 * 1.创建Selector
		 */
		Selector selector = Selector.open();

		/**
		 * 2.通过ServerSocketChannel创建channel通道
		 */
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

		/**
		 * 3.为channel通道绑定监听端口
		 */
		serverSocketChannel.bind(new InetSocketAddress(8000));

		/**
		 * 4.设置channel为非阻断模式
		 */
		serverSocketChannel.configureBlocking(false);

		/**
		 * 5.将channel注册到selector上,监听连接事件
		 */
		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		logger.info("服务器启动成功!");

		/**
		 * 6.循环等待新接入的连接
		 */
		while (true) {
			/**
			 * TODO 获取可用的channel数量
			 */
			int readyChannels = selector.select();

			if (readyChannels == 0)
				continue;

			/**
			 * 获取可用channel的集合
			 */
			Set<SelectionKey> selectionKeys = selector.selectedKeys();

			Iterator<SelectionKey> iterator = selectionKeys.iterator();

			while (iterator.hasNext()) {
				/**
				 * selectionKey实例
				 */
				SelectionKey selectionKey = (SelectionKey) iterator.next();

				/**
				 * 移除Set中的当前selectionKey
				 */
				iterator.remove();

				/**
				 * 7.根据就绪状态,调用对应方法处理业务逻辑
				 */
				/**
				 * 如果是 接入事件
				 */
				if (selectionKey.isAcceptable()) {
					this.acceptHandler(serverSocketChannel, selector);
				}

				/**
				 * 如果是 可读事件
				 */
				if (selectionKey.isReadable()) {
					this.readHandler(selectionKey, selector);
				}
			}
		}
	}

	/**
	 * 接入事件处理器
	 * 
	 * @throws IOException
	 */
	private void acceptHandler(ServerSocketChannel serverSocketChannel, Selector selector) throws IOException {
		/**
		 * 创建socketChannel
		 */
		SocketChannel socketChannel = serverSocketChannel.accept();

		/**
		 * 将socketChannel设置为非阻塞工作模式
		 */
		socketChannel.configureBlocking(false);

		/**
		 * 将channel注册到selector上,监听可读事件
		 */
		socketChannel.register(selector, SelectionKey.OP_READ);

		/**
		 * 回复客户端提示信息
		 */
		socketChannel.write(Charset.forName("UTF-8").encode("你与聊天室里其他人都不是朋友关系,请注意隐私安全"));

	}

	/**
	 * 可读事件处理器
	 * 
	 * @throws IOException
	 */
	private void readHandler(SelectionKey selectionKey, Selector selector) throws IOException {
		/**
		 * 要从selectionKey中获取到已经就绪的channel
		 */
		SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

		/**
		 * 创建buffer
		 */
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

		/**
		 * 循环读取客户端请求信息
		 */
		String request = "";
		while (socketChannel.read(byteBuffer) > 0) {
			/**
			 * 切换buffer为读模式
			 */
			byteBuffer.flip();

			/**
			 * 读取buffer中的内容
			 */
			request += Charset.forName("UTF-8").decode(byteBuffer);
		}

		/**
		 * 将channel再次注册到selector上,监听可读事件
		 */
		socketChannel.register(selector, SelectionKey.OP_READ);

		/**
		 * 将客户端发送的请求信息广播给其他客户端
		 */
		if (request.length() > 0) {
			// 广播给其他客户端
			this.broadCast(selector, socketChannel, request);
		}
	}

	private void broadCast(Selector selector, SocketChannel sourceChannel, String request) {
		/**
		 * 获取到所有已接入的客户端channel
		 */
		Set<SelectionKey> selectionKeys = selector.keys();

		/**
		 * 循环向所有的channel广播信息
		 */
		selectionKeys.forEach(selectionKey -> {
			Channel targetChannel = selectionKey.channel();
			if (targetChannel instanceof SocketChannel && targetChannel != sourceChannel) {
				try {
					((SocketChannel) targetChannel).write(Charset.forName("UTF-8").encode(request));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void main(String[] args) throws IOException {
		NIOServer server = new NIOServer();
		server.start();
	}

}

2.Client客户端

2.1.NIOClient

package pers.shaojl;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.logging.Logger;

public class NIOClient {

	private static final Logger logger = Logger.getLogger("NIOClient");

	public void start(String nickName) throws IOException {
		/**
		 * 连接服务器
		 */
		SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8000));
		logger.info("客户端启动成功!");

		/**
		 * 接收服务器响应 新开线程,专门负责接收服务器端的响应数据
		 */
		Selector selector = Selector.open();
		socketChannel.configureBlocking(false);
		socketChannel.register(selector, SelectionKey.OP_READ);
		new Thread(new NIOClientHandler(selector)).start();

		/**
		 * 向服务器发送数据
		 */
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNextLine()) {
			String request = scanner.nextLine();
			if (null != request && request.length() > 0) {
				socketChannel.write(Charset.forName("UTF-8").encode(nickName + " : " + request));
			}
		}

	}

}

2.2.NIOClientHandler

package pers.shaojl;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;

public class NIOClientHandler implements Runnable {

	private static final Logger logger = Logger.getLogger("NIOClientHandler");

	private Selector selector;

	public NIOClientHandler(Selector selector) {
		super();
		this.selector = selector;
	}

	public void run() {
		try {
			while (true) {
				int readyChannels = selector.select();
				if (readyChannels == 0)
					continue;
				Set<SelectionKey> selectionKeys = selector.selectedKeys();
				Iterator<SelectionKey> iterator = selectionKeys.iterator();
				while (iterator.hasNext()) {
					SelectionKey selectionKey = (SelectionKey) iterator.next();
					iterator.remove();
					if (selectionKey.isReadable()) {
						this.readHandler(selectionKey, selector);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 可读事件处理器
	 * 
	 * @throws IOException
	 */
	private void readHandler(SelectionKey selectionKey, Selector selector) throws IOException {
		/**
		 * 要从selectionKey中获取到已经就绪的channel
		 */
		SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

		/**
		 * 创建buffer
		 */
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

		/**
		 * 循环读取服务器端响应信息
		 */
		String response = "";
		while (socketChannel.read(byteBuffer) > 0) {
			/**
			 * 切换buffer为读模式
			 */
			byteBuffer.flip();

			/**
			 * 读取buffer中的内容
			 */
			response += Charset.forName("UTF-8").decode(byteBuffer);
		}

		/**
		 * 将channel再次注册到selector上,监听可读事件
		 */
		socketChannel.register(selector, SelectionKey.OP_READ);

		/**
		 * 将服务器端响应信息打印到本地
		 */
		if (response.length() > 0) {
			// 广播给其他客户端
			logger.info(response);
		}
	}

}

3.创建多个客户端用户

package pers.shaojl;

import java.io.IOException;

public class AClient {

	public static void main(String[] args) throws IOException {
		new NIOClient().start("AClient");
	}

}

package pers.shaojl;

import java.io.IOException;

public class BClient {

	public static void main(String[] args) throws IOException {
		new NIOClient().start("BClient");
	}

}

package pers.shaojl;

import java.io.IOException;

public class CClient {

	public static void main(String[] args) throws IOException {
		new NIOClient().start("CClient");
	}

}

4.运行

分别启动NIOServer,AClient,BClient,CClient

  

 


版权声明:本文为m0_37354578原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。