Java NIO实战之聊天室

简介:

在工作之余花了两个星期看完了《Java NIO》。整体来说这本书把NIO写的非常具体,没有过多的废话,讲的都是重点,仅仅是翻译的中文版看的确实吃力。英文水平太低也没办法,总算也坚持看完了。《Java NIO》这本书的重点在于第四章解说的“选择器”,要理解透还是要重复琢磨推敲。愚钝的我花了大概3天的时间才将NIO的选择器机制理解透并能较熟练的运用。于是便写了这个聊天室程序。

以下直接上代码。jdk1.5以上经过測试,能够支持多人同一时候在线聊天;

将下面代码拷贝到项目中便可执行。源代码下载地址:聊天室源代码

一、server端

package com.chat.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

/**
 * 聊天室:服务端
 * @author zing
 * 
 */
public class ChatServer implements Runnable {

	//选择器
	private Selector selector;
	//注冊ServerSocketChannel后的选择键
	private SelectionKey serverKey;
	//标识是否执行
	private boolean isRun;
	//当前聊天室中的username称列表
	private Vector<String> unames;
	//时间格式化器
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 构造函数
	 * @param port 服务端监控的端口号
	 */
	public ChatServer(int port) {
		isRun = true;
		unames = new Vector<String>();
		init(port);
	}

	/**
	 * 初始化选择器和服务器套接字
	 * 
	 * @param port 服务端监控的端口号
	 */
	private void init(int port) {
		try {
			//获得选择器实例
			selector = Selector.open();
			//获得服务器套接字实例
			ServerSocketChannel serverChannel = ServerSocketChannel.open();
			//绑定端口号
			serverChannel.socket().bind(new InetSocketAddress(port));
			//设置为非堵塞
			serverChannel.configureBlocking(false);
			//将ServerSocketChannel注冊到选择器,指定其行为为"等待接受连接"
			serverKey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
			printInfo("server starting...");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		try {
			//轮询选择器选择键
			while (isRun) {
				//选择一组已准备进行IO操作的通道的key,等于1时表示有这种key
				int n = selector.select();
				if (n > 0) {
					//从选择器上获取已选择的key的集合并进行迭代
					Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
					while (iter.hasNext()) {
						SelectionKey key = iter.next();
						//若此key的通道是等待接受新的套接字连接
						if (key.isAcceptable()) {
							//记住一定要remove这个key,否则之后的新连接将被堵塞无法连接服务器
							iter.remove();
							//获取key相应的通道
							ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
							//接受新的连接返回和client对等的套接字通道
							SocketChannel channel = serverChannel.accept();
							if (channel == null) {
								continue;
							}
							//设置为非堵塞
							channel.configureBlocking(false);
							//将这个套接字通道注冊到选择器,指定其行为为"读"
							channel.register(selector, SelectionKey.OP_READ);
						}
						//若此key的通道的行为是"读"
						if (key.isReadable()) {
							readMsg(key);
						}
						//若次key的通道的行为是"写"
						if (key.isWritable()) {
							writeMsg(key);
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 从key相应的套接字通道上读数据
	 * @param key 选择键
	 * @throws IOException
	 */
	private void readMsg(SelectionKey key) throws IOException {
		//获取此key相应的套接字通道
		SocketChannel channel = (SocketChannel) key.channel();
		//创建一个大小为1024k的缓存区
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		StringBuffer sb = new StringBuffer();
		//将通道的数据读到缓存区
		int count = channel.read(buffer);
		if (count > 0) {
			//翻转缓存区(将缓存区由写进数据模式变成读出数据模式)
			buffer.flip();
			//将缓存区的数据转成String
			sb.append(new String(buffer.array(), 0, count));
		}
		String str = sb.toString();
		//若消息中有"open_",表示client准备进入聊天界面
		//client传过来的数据格式是"open_zing",表示名称为zing的用户请求打开聊天窗口
		//username称列表有更新,则应将username称数据写给每个已连接的client
		if (str.indexOf("open_") != -1) {//client连接服务器
			String name = str.substring(5);
			printInfo(name + " online");
			unames.add(name);
			//获取选择器已选择的key并迭代
			Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey selKey = iter.next();
				//若不是服务器套接字通道的key,则将数据设置到此key中
				//并更新此key感兴趣的动作
				if (selKey != serverKey) {
					selKey.attach(unames);
					selKey.interestOps(selKey.interestOps() | SelectionKey.OP_WRITE);
				}
			}
		} else if (str.indexOf("exit_") != -1) {// client发送退出命令
			String uname = str.substring(5);
			//删除此username称
			unames.remove(uname);
			//将"close"字符串附加到key
			key.attach("close");
			//更新此key感兴趣的动作
			key.interestOps(SelectionKey.OP_WRITE);
			//获取选择器上的已选择的key并迭代
			//将更新后的名称列表数据附加到每个套接字通道key上,并重设key感兴趣的操作
			Iterator<SelectionKey> iter = key.selector().selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey selKey = iter.next();
				if (selKey != serverKey && selKey != key) {
					selKey.attach(unames);
					selKey.interestOps(selKey.interestOps() | SelectionKey.OP_WRITE);
				}
			}
			printInfo(uname + " offline");
		} else {// 读取client聊天消息
			String uname = str.substring(0, str.indexOf("^"));
			String msg = str.substring(str.indexOf("^") + 1);
			printInfo("("+uname+")说:" + msg);
			String dateTime = sdf.format(new Date());
			String smsg = uname + " " + dateTime + "\n  " + msg + "\n";
			Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey selKey = iter.next();
				if (selKey != serverKey) {
					selKey.attach(smsg);
					selKey.interestOps(selKey.interestOps() | SelectionKey.OP_WRITE);
				}
			}
		}
	}

	/**
	 * 写数据到key相应的套接字通道
	 * @param key
	 * @throws IOException
	 */
	private void writeMsg(SelectionKey key) throws IOException {
		SocketChannel channel = (SocketChannel) key.channel();
		Object obj = key.attachment();
		//这里必要要将key的附加数据设置为空。否则会有问题
		key.attach("");
		//附加值为"close",则取消此key,并关闭相应通道
		if (obj.toString().equals("close")) {
			key.cancel();
			channel.socket().close();
			channel.close();
			return;
		}else {
			//将数据写到通道
			channel.write(ByteBuffer.wrap(obj.toString().getBytes()));
		}
		//重设此key兴趣
		key.interestOps(SelectionKey.OP_READ);
	}

	private void printInfo(String str) {
		System.out.println("[" + sdf.format(new Date()) + "] -> " + str);
	}

	public static void main(String[] args) {
		ChatServer server = new ChatServer(19999);
		new Thread(server).start();
	}
}

二、client

1、服务类。用于与服务端交互

package com.chat.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class ClientService {
	private static final String HOST = "127.0.0.1";
	private static final int PORT = 19999;
	private static SocketChannel sc;
	
	private static Object lock = new Object();
	
	private static ClientService service;
	
	public static ClientService getInstance(){
		synchronized (lock) {
			if(service == null){
				try {
					service = new ClientService();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return service;
		}
	}

	private ClientService() throws IOException {
		sc = SocketChannel.open();
		sc.configureBlocking(false);
		sc.connect(new InetSocketAddress(HOST, PORT));
	}

	public void sendMsg(String msg) {
		try {
			while (!sc.finishConnect()) {
			}
			sc.write(ByteBuffer.wrap(msg.getBytes()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String receiveMsg() {
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		buffer.clear();
		StringBuffer sb = new StringBuffer();
		int count = 0;
		String msg = null;
		try {
			while ((count = sc.read(buffer)) > 0) {
				sb.append(new String(buffer.array(), 0, count));
			}
			if (sb.length() > 0) {
				msg = sb.toString();
				if ("close".equals(sb.toString())) {
					msg = null;
					sc.close();
					sc.socket().close();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return msg;
	}

}

2、登陆窗口,用户设置名称

package com.chat.client;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * 设置名称窗口
 * 
 * @author zing
 * 
 */
public class SetNameFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private static JTextField txtName;// 文本框
	private static JButton btnOK;// okbutton
	private static JLabel label;// 标签

	public SetNameFrame() {
		this.setLayout(null);
		Toolkit kit = Toolkit.getDefaultToolkit();
		int w = kit.getScreenSize().width;
		int h = kit.getScreenSize().height;
		this.setBounds(w / 2 - 230 / 2, h / 2 - 200 / 2, 230, 200);
		this.setTitle("设置名称");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		txtName = new JTextField(4);
		this.add(txtName);
		txtName.setBounds(10, 10, 100, 25);
		btnOK = new JButton("OK");
		this.add(btnOK);
		btnOK.setBounds(120, 10, 80, 25);
		label = new JLabel("[w:" + w + ",h:" + h + "]");
		this.add(label);
		label.setBounds(10, 40, 200, 100);
		label.setText("<html>在上面的文本框中输入名字<br/>显示器宽度:" + w + "<br/>显示器高度:" + h
				+ "</html>");

		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String uname = txtName.getText();
				ClientService service = ClientService.getInstance();
				ChatFrame chatFrame = new ChatFrame(service, uname);
				chatFrame.show();
				setVisible(false);
			}
		});
	}

	public static void main(String[] args) {
		SetNameFrame setNameFrame = new SetNameFrame();
		setNameFrame.setVisible(true);
	}

}

3、聊天室窗口

 

}








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5124066.html,如需转载请自行联系原作者

相关文章
|
3月前
|
存储 Java 开发者
Java Map实战:用HashMap和TreeMap轻松解决复杂数据结构问题!
【10月更文挑战第17天】本文深入探讨了Java中HashMap和TreeMap两种Map类型的特性和应用场景。HashMap基于哈希表实现,支持高效的数据操作且允许键值为null;TreeMap基于红黑树实现,支持自然排序或自定义排序,确保元素有序。文章通过具体示例展示了两者的实战应用,帮助开发者根据实际需求选择合适的数据结构,提高开发效率。
104 2
|
24天前
|
监控 Java API
探索Java NIO:究竟在哪些领域能大显身手?揭秘原理、应用场景与官方示例代码
Java NIO(New IO)自Java SE 1.4引入,提供比传统IO更高效、灵活的操作,支持非阻塞IO和选择器特性,适用于高并发、高吞吐量场景。NIO的核心概念包括通道(Channel)、缓冲区(Buffer)和选择器(Selector),能实现多路复用和异步操作。其应用场景涵盖网络通信、文件操作、进程间通信及数据库操作等。NIO的优势在于提高并发性和性能,简化编程;但学习成本较高,且与传统IO存在不兼容性。尽管如此,NIO在构建高性能框架如Netty、Mina和Jetty中仍广泛应用。
37 3
|
1月前
|
存储 监控 Java
Java的NIO体系
通过本文的介绍,希望您能够深入理解Java NIO体系的核心组件、工作原理及其在高性能应用中的实际应用,并能够在实际开发中灵活运用这些知识,构建高效的Java应用程序。
41 5
|
28天前
|
Java
Java基础却常被忽略:全面讲解this的实战技巧!
本次分享来自于一道Java基础的面试试题,对this的各种妙用进行了深度讲解,并分析了一些关于this的常见面试陷阱,主要包括以下几方面内容: 1.什么是this 2.this的场景化使用案例 3.关于this的误区 4.总结与练习
|
1月前
|
Java 程序员
Java基础却常被忽略:全面讲解this的实战技巧!
小米,29岁程序员,分享Java中`this`关键字的用法。`this`代表当前对象引用,用于区分成员变量与局部变量、构造方法间调用、支持链式调用及作为参数传递。文章还探讨了`this`在静态方法和匿名内部类中的使用误区,并提供了练习题。
45 1
|
2月前
|
安全 Java 开发者
Java 多线程并发控制:深入理解与实战应用
《Java多线程并发控制:深入理解与实战应用》一书详细解析了Java多线程编程的核心概念、并发控制技术及其实战技巧,适合Java开发者深入学习和实践参考。
84 7
|
2月前
|
存储 安全 Java
Java多线程编程中的并发容器:深入解析与实战应用####
在本文中,我们将探讨Java多线程编程中的一个核心话题——并发容器。不同于传统单一线程环境下的数据结构,并发容器专为多线程场景设计,确保数据访问的线程安全性和高效性。我们将从基础概念出发,逐步深入到`java.util.concurrent`包下的核心并发容器实现,如`ConcurrentHashMap`、`CopyOnWriteArrayList`以及`BlockingQueue`等,通过实例代码演示其使用方法,并分析它们背后的设计原理与适用场景。无论你是Java并发编程的初学者还是希望深化理解的开发者,本文都将为你提供有价值的见解与实践指导。 --- ####
|
2月前
|
消息中间件 缓存 Java
java nio,netty,kafka 中经常提到“零拷贝”到底是什么?
零拷贝技术 Zero-Copy 是指计算机执行操作时,可以直接从源(如文件或网络套接字)将数据传输到目标缓冲区, 而不需要 CPU 先将数据从某处内存复制到另一个特定区域,从而减少上下文切换以及 CPU 的拷贝时间。
java nio,netty,kafka 中经常提到“零拷贝”到底是什么?
|
11天前
|
监控 Java
java异步判断线程池所有任务是否执行完
通过上述步骤,您可以在Java中实现异步判断线程池所有任务是否执行完毕。这种方法使用了 `CompletionService`来监控任务的完成情况,并通过一个独立线程异步检查所有任务的执行状态。这种设计不仅简洁高效,还能确保在大量任务处理时程序的稳定性和可维护性。希望本文能为您的开发工作提供实用的指导和帮助。
60 17
|
22天前
|
Java
Java—多线程实现生产消费者
本文介绍了多线程实现生产消费者模式的三个版本。Version1包含四个类:`Producer`(生产者)、`Consumer`(消费者)、`Resource`(公共资源)和`TestMain`(测试类)。通过`synchronized`和`wait/notify`机制控制线程同步,但存在多个生产者或消费者时可能出现多次生产和消费的问题。 Version2将`if`改为`while`,解决了多次生产和消费的问题,但仍可能因`notify()`随机唤醒线程而导致死锁。因此,引入了`notifyAll()`来唤醒所有等待线程,但这会带来性能问题。
Java—多线程实现生产消费者

热门文章

最新文章