非阻塞式网络编程
选择器:
NIO是非阻塞模式的,而非阻塞的实现的核心就是选择器。选择器(Selector) 是 SelectableChannle 对象的多路复用器, Selector 可以同时监控多个 SelectableChannel 的 IO 状况,也就是说,利用 Selector可使一个单独的线程管理多个 Channel。
在使用非阻塞式的时候需要一定要开启非阻塞模式:
DatagramChannel open = DatagramChannel.open();
open.configureBlocking(false);
然后需要用创建一个selector对象用于监听
Selector selector = Selector.open();接下来是把channel注册到selector监听器上ssChannel.register(selector, SelectionKey.OP_ACCEPT);
SelectionKey: 表示 SelectableChannel 和 Selector 之间的注册关系
每次向选择器注册通道时就会选择一个事件(选择键)。SelectionKey可以监听的事件类型的常量表示如下:
读 : SelectionKey.OP_READ
写 : SelectionKey.OP_WRITE
连接 : SelectionKey.OP_CONNECT
接收 : SelectionKey.OP_ACCEPT
selector.select();返回通道中已经准备好的事件的数量,通常在程序中判断使用:
while (selector.select() > 0) {
// 获取当前选择器中所有已就绪的监听事件的“选择键”
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
}
通过遍历it中的方法,判断里面的事件,然后做相应的操作
while (selector.select() > 0) {
// 获取当前选择器中所有已就绪的监听事件的“选择键”
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
// 判断具体是什么事件准备就绪
if (key.isAcceptable()) {
// 若是接收事件就绪,获取客户端连接
SocketChannel sChannel = ssChannel.accept();
// 将客户端通道切换非阻塞模式
sChannel.configureBlocking(false);
// 将通道注册到选择器上,客户端执行“读”就绪事件
sChannel.register(selector, SelectionKey.OP_READ)
}
最后,一定要移除选择键
it.remove();
//具体代码如下
public void server() throws IOException { //1. 获取通道 ServerSocketChannel ssChannel = ServerSocketChannel.open(); //2. 切换为非阻塞模式 ssChannel.configureBlocking(false); //3. 绑定连接 ssChannel.bind(new InetSocketAddress(9898)); //4. 获取选择器 Selector selector = Selector.open(); //5. 将通道注册到选择器上,并且执行“监听接收事件” ssChannel.register(selector, SelectionKey.OP_ACCEPT); //6. 轮询式的获取选择器上已经“准备就绪”的事件 //select()返回值表示“准备就绪”的事件的数量 while (selector.select() > 0) { //7. 获取当前选择器中所有已就绪的监听事件的“选择键” Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { //8. 获取准备“就绪”的事件 SelectionKey key = it.next(); //9. 判断具体是什么事件准备就绪 if (key.isAcceptable()) { //10. 若是接收事件就绪,获取客户端连接 SocketChannel sChannel = ssChannel.accept(); //11. 将客户端通道切换非阻塞模式 sChannel.configureBlocking(false); //12. 将通道注册到选择器上,客户端执行“读”就绪事件 sChannel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { //13. 若是读事件就绪,则进行数据读取 SocketChannel sChannel = (SocketChannel) key.channel(); //14,读取数据 ByteBuffer buf = ByteBuffer.allocate(1024); int len = -1; while ((len = sChannel.read(buf)) > 0) { buf.flip(); System.out.println(new String(buf.array(), 0, len)); buf.clear(); } } //15. 取消选择键 it.remove(); }//while }//while
客户端只需要开启非阻塞式模式即可
public void client() throws IOException { //1. 获取通道 SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898)); //2. 切换成非阻塞模式 sChannel.configureBlocking(false); //3. 分配执行大小的缓冲区 ByteBuffer buf = ByteBuffer.allocate(1024); //4. 发送数据给客户端 Scanner scan = new Scanner(System.in); while (scan.hasNext()) { String content = scan.nextLine(); if (content.equals("exit")) break; buf.put((new Date() + ":" + content).getBytes()); buf.flip(); //读取缓冲区并将数据写入通道中 sChannel.write(buf); buf.clear(); } scan.close(); //5. 关闭通道 sChannel.close(); }
udp的服务端
public void receive() throws IOException { DatagramChannel dc = DatagramChannel.open(); dc.configureBlocking(false); dc.bind(new InetSocketAddress(9898)); // 获取选择器 Selector selector = Selector.open(); // 注册通道 dc.register(selector, SelectionKey.OP_READ); while (selector.select() > 0) { Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = it.next(); if (key.isReadable()) { // 进行数据读取 ByteBuffer buf = ByteBuffer.allocate(1024); //接收数据包的数据并写进缓冲区中 dc.receive(buf); buf.flip(); System.out.println(new String(buf.array(), 0, buf.limit())); buf.clear(); // 取消选择器 it.remove(); } // if } // while } // while }