一、客户端
package com.hao.demo.netty.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* @author haoxiansheng
* @date 2020-05-15
*/
public class NIOClient {
public static void main(String[] args) throws IOException {
// 得到一个通道
SocketChannel socketChannel = SocketChannel.open();
// 设置非阻塞模式
socketChannel.configureBlocking(false);
// 提供服务器的ip 和端口
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
// 连接服务器
if (socketChannel.connect(inetSocketAddress)) {
while (!socketChannel.finishConnect()) {
System.out.println("因为连接需要时间,客户端不会阻塞可以做其他事情 ");
}
}
// 如果连接成功 发送数据
String msg = "2020/5/15 ni hao";
// wrap a byte array into a buffer
ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
// 发送数据,将buffer 中的数据写入到channel
socketChannel.write(byteBuffer);
// 让客户端停在这
System.in.read();
}
}
二、服务端
package com.hao.demo.netty.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;
/**
* @author haoxiansheng
* @date 2020-05-15
*/
// 服务端
public class NIOServer {
public static void main(String[] args) throws IOException {
// 创建ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
// 得到selector 对象
Selector selector = Selector.open();
// 绑定一个端口9999 造服务器端监听
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
// 设置非阻塞
serverSocketChannel.configureBlocking(false);
// 把serverSocketChannel 注册到selector 关心事件OP_ACCEPT
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
// 循环等待客户端连接
while (true) {
// 等待1s 如果没有事件发生 就处理其他事情
if (selector.select(1000) == 0) { // 没有事件发生
System.out.println("服务器无连接,等待了1s");
continue;
}
// 如果返回的 > 0 获取相关的集合
/**
* 1、如果返回的 > 0 表示已经获取到关注的事件
* 2、selector.selectedKeys 拿到集合
* 3、通过selectionKeys 可以拿到反向通道
*
*/
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
while (keyIterator.hasNext()) {
// 获取到selectionKey
SelectionKey key = keyIterator.next();
// 根据key 对应的通道发生的事件做相应的处理
if (key.isAcceptable()) { // 如果是OP_ACCEPT 有新的客户端连接
// 给该客户端生成一个SocketChannel
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false); // 设置非阻塞的
System.out.println("客户端连接成功 生成了一个socketChannel" + socketChannel.hashCode());
// 将当前的channel 注册到selector 关注的事件OP_READ 同时给socketChannel 关联一个buffer
socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
}
if (key.isReadable()) { // 发生了读取事件 OP_READ
// 通过key 可以拿到反向通道
SocketChannel channel = (SocketChannel)key.channel();
// 获取到该channel 关联的 buffer
ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
channel.read(byteBuffer);
System.out.println("from客户端发送数据=》 "+ new String(byteBuffer.array()));
}
// 手动从集合中移除当前的selectionKey 防止重复操作
keyIterator.remove();
}
}
}
}