Java NIO编程
* 引入NIO的原因:
传统的Socket阻塞通讯模式中
1. 客户端发送请求,服务器端等待接收请求时,若客户端发送较慢,而服务器端接收的比较快,那么服务器端就会阻塞
2. 服务器端向客户端输出数据时,若服务器端发送比快,而客户端接收比较慢,那么服务器端就会阻塞
3. 当线程过多时,通讯的效率极低。
* Non-Blocking I/O 的优点
- 提供非阻塞通讯等方式
- 避免同步I/O通讯效率过低
- 一个线程可以管理多个连接
- 减少线程多的压力
* Non-BlockingI/O,非阻塞I/O,(又名NewI/O)
* JDK1.4引入,1.7升级NIO2.0(包括了AIO)
* 主要在java.nio包中
* 主要类
- Buffer缓冲区
- Channel通道
- Selector多路选择器
NIO服务端-客户端通讯示意图
* Buffer缓冲区,一个可以读写的内存区域
- ByteBuffer,
- CharBuffer,
- DoubleBuffer,
- IntBuffer,
- LongBuffer,
- ShortBuffer(StringBuffer 不是缓冲区)
* Buffer的四个主要属性
- capacity,容量
- ppsition,读写位置
- limit,界限
- mark,标记,用于重复一个读/写操作
* Channel通道
- 全双工的,支持读/写(而Stream流是单向的)
- 支持异步读写
- 和Buffer配合,提高效率
* ServerSocketChannel,服务器TCP Socket接入通道,接收客户端
* SocketChannel, TCP Socket通道,可支持阻塞/非阻塞通讯
- DatagramChannel UDP通道
- FileChannel文件通道
* Selector多路选择器
- 每隔一段时间,不断轮询注册在其上的Channel。(一个Select对应多个)
- 如果有一个Channel有接入、读、写操作,就会被轮询出来
- 根据SelectionKey可以获取响应的Channel,进行后续IO操作
- 可以避免过多的线程
- SelectionKey四种类型
* OP_CONNECT,有人连接过来,CONNECT
* OP_ACCEPT,连接成功,ACCEPT
* OP_READ,读,read
* OP_WRITE,写,write
实例:启动两个NioClient,同时向一个NioServer发起请求,一个Server用一个线程,通过两个Channel和两个客户端进行通讯,这样就节省了线程数量
NioServer
package nio;
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.Iterator;
import java.util.Set;
public class NioServer {
public static void main(String[] args) throws IOException {
int port = 8001; //定义一个端口,8001端口
Selector selector = null; //定义Selector对象,多路选择器
ServerSocketChannel servChannel = null; //定义ServerSocketChannel对象,管道
try {
//进行服务器的Channel的初始化过程,之后吧等待客户端连接上来
selector = Selector.open(); //打开多路选择器
servChannel = ServerSocketChannel.open(); //建立服务器通道
servChannel.configureBlocking(false); //配置为非阻塞模式
servChannel.socket().bind(new InetSocketAddress(port), 1024); //指明服务器Channel驻守在本机的8001端口
servChannel.register(selector, SelectionKey.OP_ACCEPT); //把多路选择器和Channel进行绑定
System.out.println("服务器在8001端口守候");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while(true)
{
try {
selector.select(1000); //轮询所有的Channel。看哪一个Channel有动静
Set<SelectionKey> selectedKeys = selector.selectedKeys(); //如果获取到有Channel在数据交换的话,那么selector.selectedKeys()就得到了所有数据响应的selectedKeys
Iterator<SelectionKey> it = selectedKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next(); //如果有,就把key拿出来
it.remove();
try {
handleInput(selector,key); //把key丢给handleInput函数来处理
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null)
key.channel().close();
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
Thread.sleep(500);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void handleInput(Selector selector, SelectionKey key) throws IOException {
if (key.isValid()) {
// 处理新接入的请求消息
if (key.isAcceptable()) {
// Accept the new connection,连接刚刚建立好的
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
// Add the new connection to the selector
sc.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
// Read the data,数据可以读的
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
String request = new String(bytes, "UTF-8"); //接收到的输入
System.out.println("client said: " + request);
String response = request + " 666";
doWrite(sc, response);
} else if (readBytes < 0) {
// 对端链路关闭
key.cancel();
sc.close();
} else
; // 读到0字节,忽略
}
}
}
public static void doWrite(SocketChannel channel, String response) throws IOException {
if (response != null && response.trim().length() > 0) {
byte[] bytes = response.getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
channel.write(writeBuffer);
}
}
}
NioClient
package nio;
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.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
public class NioClient {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 8001;
Selector selector = null;
SocketChannel socketChannel = null;
try
{
selector = Selector.open();