通过调用Selector.open()方法创建一个Selector,如下:
Selector selector = Selector.open();
向Selector注册通道
channel.configureBlocking(false);
SelectionKey key = channel.register(selector, Selectionkey.OP_READ);
与Selector一起使用时,Channel必须处于非阻塞模式下。这意味着不能将FileChannel与Selector一起使用,因为FileChannel不能切换到非阻塞模式。而套接字通道都可以。
Connect
Accept
Read
Write
通道触发了一个事件意思是该事件已经就绪。所以,某个channel成功连接到另一个服务器称为“连接就绪”。一个server socket channel准备好接收新进入的连接称为“接收就绪”。一个有数据可读的通道可以说是“读就绪”。等待写数据的通道可以说是“写就绪”。
SelectionKey.OP_CONNECT
SelectionKey.OP_ACCEPT
SelectionKey.OP_READ
SelectionKey.OP_WRITE
如果你对不止一种事件感兴趣,那么可以用“位或”操作符将常量连接起来,如下:
int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
当向Selector注册Channel时,register()方法会返回一个SelectionKey对象,通过Selector选择通道
int select()
int select(long timeout)
int selectNow()
elect()阻塞到至少有一个通道在你注册的事件上就绪了。
electionKey.attach(theObject);
Object attachedObj = selectionKey.attachment();
还可以在用register()方法向Selector注册Channel的时候附加对象,如:
SelectionKey key = channel.register(selector, SelectionKey.OP_READ, theObject);
下面是服务端以及客户端的NIO通信的实现代码,其中注释不保证全部安全,若有出错,欢迎指出:
服务端:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
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 ServerTest {
public static void main(String[] args) {
server();
}
private static void server() {
//ServerSocketChannel通道引用
ServerSocketChannel channel=null;
try {
//创建selector
Selector selector=Selector.open();
//open方法来打开一个未绑定的ServerSocketChannel实例
channel=ServerSocketChannel.open();
//把channel设置成非阻塞
channel.configureBlocking(false);
//timewait状态时,就可复用其端口,对于大量连接且经常有timewait时适用
channel.socket().setReuseAddress(true);
//与端口绑定
channel.bind(new InetSocketAddress(8082));
//在SelectionKey对象的有效期间,Selector会一直监控与SelectionKey对象相关的事件,如果事件发生,就会把SelectionKey对象加入到selected-keys集合中
//将selector对象绑定到监听信道上,并在注册过程中指出该信道进行OP_ACCEPT操作
channel.register(selector, SelectionKey.OP_ACCEPT,new Integer(1));
/*SelectionKey.OP_ACCEPT —— 接收连接继续事件,表示服务器监听到了客户连接,服务器可以接收这个连接了
SelectionKey.OP_CONNECT —— 连接就绪事件,表示客户与服务器的连接已经建立成功
SelectionKey.OP_READ —— 读就绪事件,表示通道中已经有了可读的数据,可以执行读操作了(通道目前有数据,可以进行读操作了)
SelectionKey.OP_WRITE —— 写就绪事件,表示已经可以向通道写数据了(通道目前可以用于写操作)*/
//反复循环,等待IO
while(true){
//select方法会阻塞等待,直到至少有一个注册信道中有感兴趣的操作准备就绪,返回值大于0,表明有一个或更多个通道就绪了
if(selector.select()>0){
//将就绪的对象放到set集合中
Set sets=selector.selectedKeys();
//通过迭代器遍历,依次处理selector上的每个已选择的SelectionKey
Iterator keys=sets.iterator();
while(keys.hasNext()){
//获取具体的selectionkey
SelectionKey key=keys.next();
//Selector不会自己从已选择键集中移除SelectionKey实例,必须在处理完通道时自己移除。
//下次该通道变成就绪时,Selector会再次将其放入已选择键集中。
keys.remove();
//selectionKey对应的通道是包含客户端的连接请求
if(key.isAcceptable()){
key.attach(new Integer(1));
//调用accept方法接受连接,产生服务器端对应的SocketChannel
SocketChannel socketChannel=((ServerSocketChannel) key.channel()).accept();
//同样设置为非阻塞
socketChannel.configureBlocking(false);
//同样将socketChannel注册到selector上,socketChannel设置成准备接受其他请求
socketChannel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
}
//selectionKey对应的通道有数据需要读取
if(key.isReadable()){
//获取该SelectionKey对应的Channel,该Channel中有可读的数据
SocketChannel socketChannel=(SocketChannel) key.channel();
//定义准备执行读取数据的ByteBuffer,NIO通信是基于Buffer块的
ByteBuffer buf=ByteBuffer.allocate(1024);
ByteArrayOutputStream output=new ByteArrayOutputStream();
int len=0;
while((len=socketChannel.read(buf))!=0){
buf.flip();
//remaining()返回剩余的可用长度,此长度为实际读取的数据长度
byte[] b=new byte[buf.remaining()];
buf.get(b);
output.write(b);
buf.clear();
}
String str=new String(output.toByteArray());
key.attach(str);
}
//selectionKey对应的通道是否可以写入数据
if(key.isWritable()){
Object object=key.attachment();
String attach=object!=null?"server replay: "+object.toString():"server replay: ";
//获取该SelectionKey对应的Channel,在该Channel中写入数据
SocketChannel socketChannel=(SocketChannel) key.channel();
socketChannel.write(ByteBuffer.wrap(attach.getBytes()));
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (ClosedChannelException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(channel!=null){
try {
//关闭通道
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.ByteArrayOutputStream;
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;
public class ClientTest {
public static void main(String[] args) throws Exception {
client();
}
public static void client() {
//socket这里创建的是SocketChannel
SocketChannel channel=null;
try {
//打开selector
Selector selector=Selector.open();
//打开SocketChannel通道
channel = SocketChannel.open();
//非阻塞
channel.configureBlocking(false);
//连接相应的端口号
channel.connect(new InetSocketAddress(8020));
//channel注册到selector上,设置成接受请求
channel.register(selector, SelectionKey.OP_CONNECT);
while(true){
if(selector.select()>0){
Iterator set=selector.selectedKeys().iterator();
while(set.hasNext()){
SelectionKey key=set.next();
set.remove();
SocketChannel ch=(SocketChannel) key.channel();
if(key.isConnectable()){
ch.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE,new Integer(1));
ch.finishConnect();
}
if(key.isReadable()){
key.attach(new Integer(1));
ByteArrayOutputStream output=new ByteArrayOutputStream();
ByteBuffer buffer=ByteBuffer.allocate(1024);
int len=0;
while((len=ch.read(buffer))!=0){
buffer.flip();
byte by[]=new byte[buffer.remaining()];
buffer.get(by);
output.write(by);
buffer.clear();
}
System.out.println(new String(output.toByteArray()));
output.close();
}
if(key.isWritable()){
key.attach(new Integer(1));
ch.write(ByteBuffer.wrap((("client say: hi")).getBytes()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class ClientRunnable implements Runnable{
private SocketChannel ch;
private ClientRunnable(SocketChannel ch){
this.ch=ch;
}
@Override
public void run() {
try {
while(true){
ch.write(ByteBuffer.wrap((("client say:hi")).getBytes()));
Thread.sleep(5000);
}
} catch (Exception e) {
e.printStackTrace();
try {
ch.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}