服务端
/**
* @author : zhangzhen
* @Description :
* @date :
* @Modified By:
*/
public class NioServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel= ServerSocketChannel.open();
//配置为非阻塞
serverSocketChannel.configureBlocking(false);
//绑定端口
serverSocketChannel.bind(new InetSocketAddress("127.0.0.1",6666));
//selector
Selector selector = Selector.open();
//serverSocketChannel 注册到selector中,并且监听连接时间
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
int i =0;
while (true){
if(selector.select(1000)==0){
System.out.println("未检测出的连接");
continue;
}
//所有发生事件的通道 对应的SelectionKey
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
if (selectionKey.isAcceptable()) {
//获取新连接的客户端Socket
SocketChannel socketChannel = serverSocketChannel.accept();
//配置为非阻塞
socketChannel.configureBlocking(false);
//把客户端Socket 注册进selector 并且监听 读 事件,以及配置服务端缓存区
socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(512));
}else if(selectionKey.isReadable()){
//获取发生的读事件的客户端Socket
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
//获取缓存区
ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
buffer.clear();
socketChannel.read(buffer);
String s = new String(buffer.array());
System.out.println("服务端读:"+new String(buffer.array()));
socketChannel.register(selector,SelectionKey.OP_WRITE);
}else if(selectionKey.isWritable()){
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
System.out.println("服务端写"+i++);
ByteBuffer wrap = ByteBuffer.wrap(("服务端写"+i++).getBytes());
socketChannel.write(wrap);
wrap.clear();
socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(512));
}
//移除通道,避免重复处理
iterator.remove();
}
}
}
}
客户端
/**
* @author : zhangzhen
* @Description :
* @date : Created in
* @Modified By:
*/
public class NioClient {
private static Charset charset = Charset.forName("UTF-8");
public static void main(String[] args) throws IOException, InterruptedException {
SocketChannel socketChannel= SocketChannel.open();
Selector selector = Selector.open();
socketChannel.configureBlocking(false);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 6666);
if (!socketChannel.connect(address)) {
while (!socketChannel.finishConnect()) {
System.out.println("连接中,客户端可以进行其他工作");
}
int i=0;
String str = "hello world!" + i++;
ByteBuffer wrap = ByteBuffer.wrap((str.getBytes()));
socketChannel.write(wrap);
wrap.clear();
socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(512));
System.out.println("客户端写");
while (true) {
if( selector.select(1) > 0){
//.....
}
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
if(selectionKey.isReadable()){
//获取发生的读事件的客户端Socket
socketChannel = (SocketChannel) selectionKey.channel();
//获取缓存区
ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
buffer.clear();
socketChannel.read(buffer);
String s = new String(buffer.array());
System.out.println("客户端读:"+s);
socketChannel.register(selector,SelectionKey.OP_WRITE);
}else if(selectionKey.isWritable()){
System.out.println("客户端写"+i++);
socketChannel = (SocketChannel) selectionKey.channel();
str = "客户端写" + i++;
wrap = ByteBuffer.wrap((str.getBytes()));
socketChannel.write(wrap);
wrap.clear();
socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(512));
}
}
}
}
}
}