NIO使用指南
序言
主要是对java.nio包下的相关开发工具做一个简单的学习笔记,并不会太多深入的探讨。
一、简介
NIO即New IO,这个库是在JDK1.4中才引入的。NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多。在Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。两者没有孰优孰劣,NIO是java io的拓展,根据不同的场景,两者各有用处。
1、NIO和IO的主要区别
IO | NIO |
---|---|
面向流 | 面向缓冲 |
阻塞IO | 非阻塞IO |
无 | 选择器 |
1.1、面向流与面向缓冲
Java IO和NIO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的。 Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方。此外,它不能前后移动流中的数据。如果需要前后移动从流中读取的数据,需要先将它缓存到一个缓冲区。 Java NIO的缓冲导向方法略有不同。数据读取到一个它稍后处理的缓冲区,需要时可在缓冲区中前后移动。这就增加了处理过程中的灵活性。但是,还需要检查是否该缓冲区中包含所有您需要处理的数据。而且,需确保当更多的数据读入缓冲区时,不要覆盖缓冲区里尚未处理的数据。
1.2、阻塞与非阻塞IO
Java IO的各种流是阻塞的。这意味着,当一个线程调用read() 或 write()时,该线程被阻塞,直到有一些数据被读取,或数据完全写入。该线程在此期间不能再干任何事情了。Java NIO的非阻塞模式,使一个线程从某通道发送请求读取数据,但是它仅能得到目前可用的数据,如果目前没有数据可用时,就什么都不会获取,而不是保持线程阻塞,所以直至数据变的可以读取之前,该线程可以继续做其他的事情。 非阻塞写也是如此。一个线程请求写入一些数据到某通道,但不需要等待它完全写入,这个线程同时可以去做别的事情。 线程通常将非阻塞IO的空闲时间用于在其它通道上执行IO操作,所以一个单独的线程现在可以管理多个输入和输出通道(channel)。
io的各种流是阻塞的,就是当一个线程调用读写方法时,该线程会被阻塞,直到读写完,在这期间该线程不能干其他事,CPU转而去处理其他线程,假如一个线程监听一个端口,一天只会有几次请求进来,但是CPU却不得不为该线程不断的做上下文切换,并且大部分切换以阻塞告终。
NIO通讯是将整个任务切换成许多小任务,由一个线程负责处理所有io事件,并负责分发。它是利用事件驱动机制,而不是监听机制,事件到的时候再触发。NIO线程之间通过wait,notify等方式通讯。保证了每次上下文切换都有意义,减少无谓的进程切换。
1.3、选择器(Selectors)
Java NIO的选择器允许一个单独的线程来监视多个输入通道,你可以注册多个通道使用一个选择器,然后使用一个单独的线程来“选择”通道:这些通道里已经有可以处理的输入,或者选择已准备写入的通道。这种选择机制,使得一个单独的线程很容易来管理多个通道。
二、缓冲区与管道
Java NIO系统的核心在于:通道(Channel)和缓冲区(Buffer)。通道表示打开到IO设备(例如:文件、Socket)的连接。如果需要使用NIO系统,需要获取用于连接IO设备的通道以及用于容纳数据的缓冲区,然后操作缓冲区,对数据进行处理。
- 简而言之,Channel负责传输,Buffer负责存储。
1、缓冲区(Buffer)
- 缓冲区(Buffer):一个用于特定基本数据类型的容器。由java.nio包定义的,所有缓冲区都是Buffer抽象类的子类。
- Java NIO中的Buffer主要用于和NIO通道进行交互,数据是从通道读入到缓冲区的,然后从缓冲区中写入到通道中的。
- Buffer就像一个数组(其实也只是对数组进行了封装),可以保存多个相同类型的数据。根据数据类型的不同(boolean)除外,有以下Buffer常用子类:ByteBuffer、CharBuffer、ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer。上述Buffer类他们都是此阿勇相似的方法进行管理数据的,只是各自管理的数据类型不同而已。
1.1、缓冲区的基本属性
- 容量(capacity):表示Buffer最大数据容量,缓冲区容量不能为负,并且一旦创建不能更改。
- 限制(limit):第一个不应该读取或写入的数据的索引,即位于limit后的数据不可读写。缓冲区的限制不能为负,并且不能大于容量。
- 位置(position):下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制。
- 标记(mark)和重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中的一个特定的position,之后可以通过调用reset()方法恢复到这个position。
几个基本属性的大小关系:0 <= mark <= position <= limit <= capacity。
1.2、Buffer的常用方法
方法 | 说明 |
---|---|
Buffer clear() | 清空缓冲区并返回对缓冲区的引用 |
Buffer flip() | 将缓冲区的界限设置为当前位置,并将当前位置设置为0。切换到读数据模式。 |
int capacity() | 返回Buffer的capacity大小 |
boolean hasRemaining() | 判断缓冲区中是否还有元素 |
int limit() | 返回Buffer的界限(limit)的位置 |
Buffer limit(int n) | 设置缓冲区界限为n,并返回一个具有新limit的缓冲区对象 |
Buffer mark() | 对缓冲区进行标记 |
int position() | 返回缓冲区的当前位置position |
Buffer position(int n) | 将设置缓冲区的当前位置为n,并返回修改后的Buffer对象 |
int remaining() | 返回position和limit之间的元素个数 |
Buffer reset() | 将位置position为以前设置的mark所在的位置 |
Buffer rewind() | 将位置position设置为0,取消设置的mark |
对数据的操作:Buffer所有子类提供了两个数据操作的方法:get()和put()方法。
1.3、buffer示例使用
public class BufferTest {
@Test
public void test1() {
//分配一个指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
System.out.println("----------------allocate--------------");
System.out.println("position:" + buffer.position());//0
System.out.println("limit:" + buffer.limit());//1024
System.out.println("capacity:" + buffer.capacity());//1024
//利用put()方法存入数据到缓冲区中
String str = "abcde";
buffer.put(str.getBytes());
System.out.println("----------------put--------------");
System.out.println("position:" + buffer.position());//5
System.out.println("limit:" + buffer.limit());//1024
System.out.println("capacity:" + buffer.capacity());//1024
//切换到读数据模式
buffer.flip();
System.out.println("----------------flip--------------");
System.out.println("position:" + buffer.position());//0
System.out.println("limit:" + buffer.limit());//5
System.out.println("capacity:" + buffer.capacity());//1024
//利用get()方法读取数据
byte[] dst = new byte[buffer.limit()];
buffer.get(dst);
System.out.println("获取缓冲区中的数据:" + new String(dst, 0, dst.length));
System.out.println("----------------get--------------");
System.out.println("position:" + buffer.position());//5
System.out.println("limit:" + buffer.limit());//5
System.out.println("capacity:" + buffer.capacity());//1024
//重复读数据,将位置position设置为0
buffer.rewind();
System.out.println("----------------rewind--------------");
System.out.println("position:" + buffer.position());//0
System.out.println("limit:" + buffer.limit());//5
System.out.println("capacity:" + buffer.capacity());//1024
//清空缓冲区,但是缓冲区的数据依然存在,数据处于“被遗忘状态”。
buffer.clear();
System.out.println("----------------clear--------------");
System.out.println("position:" + buffer.position());//0
System.out.println("limit:" + buffer.limit());//1024
System.out.println("capacity:" + buffer.capacity());//1024
}
@Test
/**
* mark()与reset示例
*
*/
public void test2() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
String str = "abcde";
buffer.put(str.getBytes());
buffer.flip();
byte[] dst = new byte[buffer.limit()];
buffer.get(dst,0,2);
System.out.println(new String(dst,0,2));
System.out.println("position:"+buffer.position());//2
buffer.mark();
buffer.get(dst,2,2);
System.out.println(new String(dst,2,2));
System.out.println("position:"+buffer.position());//4
buffer.reset();
System.out.println("position:"+buffer.position());//2
//判断缓冲区中是否含有剩余数据
if(buffer.hasRemaining()){
System.out.println("获取缓冲区中可以操作的数量"+buffer.remaining());
}
}
}
1.4、直接缓冲区与非直接缓冲区
-
字节缓冲区要么是直接的,要么是非直接的。如果为直接字节缓冲区,则Java虚拟机会尽最大努力直接在此缓冲区上执行本机IO操作。也就是说,在每次调用基础操作系统的一个本机I/O操作之前(或之后),虚拟机都会尽量避免将缓冲区的内容复制到中间缓冲区中(或从中间缓冲区中复制内容)。
-
直接字节缓冲区可以通过调用此类的allocateDirect()工厂方法来创建。此方法返回的缓冲区进行分配和取消分配所需成本通常要高于非直接缓冲区。直接缓冲区的内容可以驻留在常规的垃圾回收堆之外,因此,它们对应用程序的内存需求量造成的影响可能并不明显。所以,建议将直接缓冲区主要分配给那些易受基础系统的本地IO操作相应的大型、持久的缓冲区。一般情况下,最好仅在直接缓冲区能在程序性能方面带来明显好处时分配它们。
-
直接字节缓冲区还可以通过FileChannel的map()方法将文件区域直接映射到内存中来创建。该方法返回MappedByteBuffer。Java平台的实现有助于通过JNI从本机代码创建直接字节缓冲区。如果以上这些缓冲区中的某个缓冲区实例指的是不可访问的内存区域,则试图访问该区域不会更改该缓冲区的内容,并且将会在访问期间或稍后的某个时间导致抛出不确定的异常。
字节缓冲区是直接缓冲区还是非直接缓冲区可以通过isDirect()方法来确定。提供此方法是为了能够在性能关键型代码中执行显示缓冲区管理。
关于内核地址空间与用户地址空间,《Linux内核空间与用户空间》博客做了详细的解释,感兴趣的可以去看看。
2、通道
通道(Channel):由java.nio.channels包定义的。Channel表示IO源和目标打开的链接。Channel类似于传统的“流”。只不过CHannel本身不能直接访问数据,Channel只能和Buffer进行交互。
2.1、通道的主要实体类
Java为Channel(java.nio.channels.Channel)接口提供的最主要的实现类如下:
- FileChannel:用于读取、写入、映射和操作文件的通道。
- DatagramChannel:通过UDP读写网络中的数据通道。
- SocketChannel:通过TCP读写网络中的数据。
- ServerSocketChannel:可以监听新进来的TCP连接,对每一个新进来的连接都会创建一个SocketChannel。
2.2、获取通道方式
-
①Java针对支持通道的类提供了getChannel()方法
- FileInputStream - FileOutputStream - RandomAccessFile - Socket - ServerSocket - DatagramSocket
-
②JDK1.7中的NIO2提供了针对各个通道提供了静态方法open()方法。
-
③JDK1.7中的NIO2的Files工具类的newByteChannel()方法。
2.3、文件通道(FileChannel)
FileChannel对象不能直接创建。一个FileChannel实例只能通过在一个打开的file对象(RandomAccessFile、FileInputStream、FileOutputStream或FileChannel的open静态方法获取)上调用getChannel( )方法获取。调用getChannel( )方法会返回一个连接到相同文件的FileChannel对象且该FileChannel对象具有与file对象相同的访问权限,然后就可以使用该通道对象来利用强大的FileChannel API了。还要注意的是,文件通道是阻塞式的,只有网络通道才可以非阻塞式。
(1)文件通道常用方法
方法 | 描述 |
---|---|
int read(ByteBuffer dst) | 从Channel中读取数据到ByteBuffer |
long read(ByteBuffer[] dsts) | 将Channel中的数据“分散”到ByteBuffer[] |
int write(ByteBuffer src) | 将ByteBuffer中的数据写入到Channel |
long write(ByteBuffer[] srcs) | 将ByteBuffer[]中的数据“聚集”到Channel |
long position() | 返回此通道的文件位置 |
FileChannel position(long p) | 设置此通道的文件位置 |
long size() | 返回此通道的文件的当前大小 |
FileChannel truncate(long s) | 将此通道的文件截取到给定大小 |
void force(boolean metaData) | 强制将所有对此通道的文件更新写入到存储设备中 |
(2)文件通道复制文件应用示例:
package com.xsl.nio;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* 一、通道(Channel):用于源节点和目标节点之间的连接。在Java NIO中负责缓冲区中数据的传输。Channel本身是不存储数据,因此需要配合缓冲区进行操作。
* 二、通道的主要实现类
* java.nio.channels.Channel接口
* |--FileChannel
* |--SocketChannel
* |--ServerSocketChannel
* |--DatagramChannel
* 三、获取通道
* ①Java支持通道的类提供了getChannel()方法。
* 本地IO:FileInputStream、FileOutputStream、RandomAccessFile
* 网络IO:Socket、ServerSocket、DatagramSocket
* ②在JDK1.7中的NIO2针对各个通道提供了静态方法open()。
* ③在JDK1.7中的NIO2的Files工具类的newByteChannel()。
* 四、通道之间的数据传输
* transferFrom()
* transferTo()
*/
public class ChannelTest {
/**
* 利用直接缓冲区完成文件的复制
* @throws IOException
*/
@Test
public void test3() throws IOException {
FileChannel inFileChannel = FileChannel.open(Paths.get("D:\\project\\nio\\1.jpg"), StandardOpenOption.READ);
FileChannel outFileChannel = FileChannel.open(Paths.get("D:\\project\\nio\\2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);
// outChannel.transferFrom(inChannel,0,inChannel.size());
outFileChannel.close();
inFileChannel.close();
}
/**
* 利用直接缓冲区完成文件的复制(内存映射文件)
*/
@Test
public void test2() throws IOException {
FileChannel inFileChannel = FileChannel.open(Paths.get("D:\\project\\nio\\1.jpg"), StandardOpenOption.READ);
FileChannel outFileChannel = FileChannel.open(Paths.get("D:\\project\\nio\\2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
//内存映射文件
MappedByteBuffer inMappedByteBuffer = inFileChannel.map(FileChannel.MapMode.READ_ONLY, 0, inFileChannel.size());
MappedByteBuffer outMappedByteBuffer = outFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, inFileChannel.size());
//直接对缓冲区进行数据的读写操作
byte[] dst = new byte[inMappedByteBuffer.limit()];
inMappedByteBuffer.get(dst);
outMappedByteBuffer.put(dst);
inFileChannel.close();
outFileChannel.close();
}
/**
* 利用通道完成文件的复制(非直接缓冲区)
*
* @throws IOException
*/
@Test
public void test() throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\project\\nio\\1.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("D:\\project\\nio\\2.jpg");
//①获取通道
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
//②分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//③将通道中的数据存入缓冲区
while (-1 != fileInputStreamChannel.read(buffer)) {
buffer.flip();
//④将缓冲区的数据写入通道
fileOutputStreamChannel.write(buffer);
//清空缓冲区
buffer.clear();
}
fileOutputStreamChannel.close();
fileInputStreamChannel.close();
fileOutputStream.close();
fileInputStream.close();
}
}
3、通道分散于聚集
分散读取(Scattering Reads)是指从Channel中读取的数据“分散”到多个Buffer中。即按照缓冲区的顺序,从Channel中读取的数据依次将Buffer填满。
聚集写入(Gathering Writes)是指将多个Buffer中的数据“聚集”到Channel。按照缓冲区的顺序,写入position和limit之间的数据到Channel。
应用示例
package com.xsl.nio;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ChannelTest {
@Test
public void test() throws IOException {
//创建文件流
FileInputStream fileInputStream = new FileInputStream("D:\\project\\nio\\1.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("D:\\project\\nio\\2.jpg");
//获取通道
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
//创建缓冲区
ByteBuffer buffer1 = ByteBuffer.allocate(512);
ByteBuffer buffer2 = ByteBuffer.allocate(512);
//分散读取缓冲区数组
ByteBuffer[] buffers = new ByteBuffer[]{buffer1, buffer2};
//读取
while (-1 != fileInputStreamChannel.read(buffers)) {
for (ByteBuffer buffer : buffers) {
buffer.flip();
}
fileOutputStreamChannel.write(buffers);
for (ByteBuffer buffer : buffers) {
buffer.clear();
}
}
fileInputStreamChannel.close();
fileOutputStreamChannel.close();
fileInputStream.close();
fileOutputStream.close();
}
}
三、NIO的非阻塞式网络通信
1、阻塞与非阻塞
- 传统的IO流都是阻塞的。也就是说,当一个线程调用read()后write()方法的时候,该线程就会被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务。因此,在完成网络通信进行IO操作时,由于线程会阻塞,所以服务器端必须为每个客户端都提供一个独立的线程进行处理,当服务器端需要处理大量客户端请求的时候,性能急剧下降。
- Java NIO是非阻塞的。当线程从某个通道进行读写数据的时候,如果没有数据可用的时候,该线程可用执行其他任务。线程通常将非阻塞IO的空间时间用于在其他通道上执行IO操作,所以单独的线程可用管理多个输入和输出通道。因此,NIO可以让服务器端使用一个或有限几个线程来同时处理连接到服务器端的所有客户端。
2、网络通道
2.1、SocketChannel
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道。
操作步骤:
- 打开SocketChannel通道
- 读取数据
- 关闭SocketChannel通道
2.2、ServerSocketChannel
Java NIO中的ServerSOcketChannel是一个可以监听新进来的TCP连接的通道,就像标准IO中的ServerSocket一样。
2.3、DatagramChannel
DatagramChannel是一个支持UDP协议的收发网络通道
操作步骤:
- 打开DatagramChannel通道
- 收发数据
3、选择器(Selecter)
选择器(Selector)是SelectableChannel对象的多路复用器,Selector可以同时监控多个SelectableChannel的IO状况,也就是说,利用Selector可以使得一个单独的线程管理多个Channel。Selector是非阻塞IO的核心。
SelectableChannel的结构如下图所示:
3.1、选择器的常用方法
方法 | 描述 |
---|---|
Set keys() | 返回所有的SelectionKey集合。代表注册在该Selector上的Channel |
Set selectedKeys() | 返回此Selector的已选择键 |
int select() | 监控所有注册的Channel,如果有需要处理的IO操作的时候,将对应的SelectionKey加入已选择的SelectionKey集合集合中,并返回这些Channel的数量。 |
int select(long timeout) | 可以设置超时时长的select()操作 |
int selectNow() | 执行一个立即返回的select()操作,该方法不会阻塞线程 |
Selector wakeup() | 使得一个还未返回的select()方法立即返回 |
void close() | 关闭该选择器 |
3.2、选择器监听事件
当调用register(Selector sel,int ops)将通道注册选择器的时候,选择器对通道的监听事件,需要通过第二个参数ops来指定。
可以监听的事件类型(可以使用SelectionKey的四个常量来表示):
- 读:SelectionKey.OP_READ。
- 写:SelectionKey.OP_WRITE。
- 连接:SelectionKey.OP_CONNECT。
- 接收:SelectionKey.OP_ACCEPT。
如果注册的时候不止监听一个事件,可以使用"|"操作符连接。
int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
3.3、SelectionKey
SelectionKey表示SelectableChannel和Selector之间的注册关系。每次向选择器注册通道的时候就会选择一个事件(选择键)。
方法 | 描述 |
---|---|
int interestOps() | 获取感兴趣的事件集合 |
int readyOps() | 获取通道已经准备就绪的操作的集合 |
SelectableChannel channel() | 获取注册通道 |
Selector selector() | 返回选择器 |
boolean isReadable() | 检测Channel中读事件是否就绪 |
boolean isWriteable() | 检测Channel中写事件是否就绪 |
boolean isConnectable() | 检测Channel中连接是否就绪 |
boolean isAcceptable() | 检测Channel中接受是否就绪 |
3.4、应用示例
NIO阻塞式:
package com.xsl.nio;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* 一、使用NIO完成网络通信的三个核心:
* ①通道(Channel):负责连接。
* java.nio.channels.Channel接口:
* |--SelectableChannel
* |--SocketChannel
* |--ServerSocketChannel
* |--DatagramChannel
*
* |--Pipe.SinkChannel
* |--Pipe.SourceChannel
* ②缓冲区(Buffer):负责数据的存取。
* ③选择器(Selector):是SelectableChannel的多路复用器。用于监控SelectableChannel的一些IO状况。
*/
public class BlockingNIOTest {
@Test
public void client() throws IOException {
//获取通道
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9898));
FileChannel fileChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
//分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//读取本地文件,并发送到服务器端
while (-1 != fileChannel.read(buffer)) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
//通知服务器端传送完毕
socketChannel.shutdownOutput();
int len = 0;
while ((len = socketChannel.read(buffer)) != -1) {
buffer.flip();
System.out.println(new String(buffer.array(),0,len));
buffer.clear();
}
fileChannel.close();
socketChannel.close();
}
@Test
public void server() throws IOException {
//获取通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//绑定端口号
serverSocketChannel.bind(new InetSocketAddress(9898));
SocketChannel socketChannel = serverSocketChannel.accept();
FileChannel fileChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
//分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (-1 != socketChannel.read(buffer)) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
buffer.put("服务器端接受成功".getBytes());
buffer.flip();
socketChannel.write(buffer);
fileChannel.close();
socketChannel.close();
}
}
NIO非阻塞式:
package com.xsl.nio;
import org.junit.Test;
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.util.Date;
import java.util.Iterator;
public class NOBlockingNIOTest {
@Test
public void client() throws IOException {
//获取通道
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9897));
//切换成非阻塞模式
socketChannel.configureBlocking(false);
//分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//发送数据给服务器端
buffer.put(new Date().toString().getBytes());
buffer.flip();
socketChannel.write(buffer);
//关闭通道
socketChannel.close();
}
@Test
public void server() throws IOException {
//获取通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//切换成非阻塞模式
serverSocketChannel.configureBlocking(false);
//绑定端口
serverSocketChannel.bind(new InetSocketAddress(9897));
//获取选择器
Selector selector = Selector.open();
//将通道注册到选择器上,并且指定监听事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
//轮询式的获取选择器上已经“准备就绪”的事件
while (selector.select() > 0) {
//获取当前选择器中所有注册的“选择键(已注册的监听事件)”
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
//遍历
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
//判断具体是什么事件准备就绪
if (selectionKey.isAcceptable()) {
//如果是“接受就绪”,获取客户端的连接
SocketChannel socketChannel = serverSocketChannel.accept();
//切换成非阻塞模式
socketChannel.configureBlocking(false);
//将该通道注册到选择器上
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
//获取当前选择器上“读就绪”状态的通道
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
//读取数据
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = 0;
while ((len = socketChannel.read(buffer)) != -1) {
buffer.flip();
System.out.println(new String(buffer.array(), 0, len));
buffer.clear();
}
}
//取消选择键
iterator.remove();
}
}
}
}