NIO 非阻塞网络编程原理分析图
- 当客户端连接时,会通过
ServerSocketChannel
得到SocketChannel
。Selector
进行监听select
方法,返回有事件发生的通道的个数。- 将
socketChannel
注册到Selector
上,register(Selector sel, int ops)
,一个Selector
上可以注册多个SocketChannel
。- 注册后返回一个
SelectionKey
,会和该Selector
关联(集合)。- 进一步得到各个
SelectionKey
(有事件发生)。- 在通过
SelectionKey
反向获取SocketChannel
,方法channel()
。- 可以通过得到的
channel
,完成业务处理。
案例.NIO群聊系统
服务端
public class GroupChatServer {
//通用属性
private Selector selector;
private ServerSocketChannel listenChannel;
public static final int PORT = 6667;
//初始化构造器
public GroupChatServer(){
try {
//获取选择器
selector = Selector.open();
//获取SocketChannel
listenChannel = ServerSocketChannel.open();
//绑定端口
listenChannel.socket().bind(new InetSocketAddress(PORT));
//设置非阻塞模式
listenChannel.configureBlocking(false);
//将listenChannel注册到selector
listenChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
public void listen(){
try {
//循环处理请求
while (true){
int count = selector.select();
if (count > 0){//有请求需要处理
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
if (key.isAcceptable()){
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
//sc注册到selector
sc.register(selector, SelectionKey.OP_READ);
//提示
System.out.println(sc.getRemoteAddress() + "上线");
}
if (key.isReadable()) {//通道发送read事件,即通道是可读的状态
readData(key);
}
//当前的 key 删除,防止重复处理
iterator.remove();
}
}else {
System.out.println("等待....");
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
//读取客户端消息
public void readData(SelectionKey key){
SocketChannel channel = null;
try {
channel = (SocketChannel) key.channel();
//创建buffer接收数据
ByteBuffer buffer = ByteBuffer.allocate(1024);
int count = channel.read(buffer);
//根据 count 的值做处理
if (count > 0) {
String msg = new String(buffer.array());
//输出该消息
System.out.println("from客户端" + msg);
//向其他客户端转发消息
sendInfoToOtherClients(msg, channel);
}
} catch (Exception e) {
try {
System.out.println(channel.getRemoteAddress() + "离线了。。。");
//取消注册
key.cancel();
channel.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
private void sendInfoToOtherClients(String msg, SocketChannel self) throws IOException {
System.out.println("服务器转发消息中...");
//遍历所有注册到 selector 上的 SocketChannel,并排除 self
for (SelectionKey key : selector.keys()) {
//通过 key 取出对应的 SocketChannel
Channel targetChannel = key.channel();
//排除自己
if (targetChannel instanceof SocketChannel && targetChannel != self) {
//转型
SocketChannel dest = (SocketChannel) targetChannel;
//将 msg 存储到 buffer
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
//将 buffer 的数据写入通道
dest.write(buffer);
}
}
}
public static void main(String[] args) {
//创建服务器对象
GroupChatServer groupChatServer = new GroupChatServer();
groupChatServer.listen();
}
}
客户端
public class GroupChatClient {
//定义相关的属性
private final String HOST = "127.0.0.1";//服务器的ip
private final int PORT = 6667;//服务器端口
private Selector selector;
private SocketChannel socketChannel;
private String username;
//构造器,完成初始化工作
public GroupChatClient() throws IOException {
selector = Selector.open();
//连接服务器
socketChannel = SocketChannel.open(new InetSocketAddress(HOST, PORT));
//设置非阻塞
socketChannel.configureBlocking(false);
//把channel注册到selector
socketChannel.register(selector, SelectionKey.OP_READ);
//构建username
username = socketChannel.getLocalAddress().toString().substring(1);
System.out.println(username + " is ok ....");
}
//向服务器发消息
public void sendInfo(String info){
info = username + " 说:" + info;
try {
socketChannel.write(ByteBuffer.wrap(info.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
//读取服务器端传来的消息
public void readInfo(){
try {
int select = selector.select();
if (select > 0){
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
if (key.isReadable()){
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
channel.read(allocate);
//打印信息
System.out.println(new String(allocate.array()).trim());
}
iterator.remove();
}
}else{
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//启动客户端
GroupChatClient groupChatClient = new GroupChatClient();
//新建线程,每三秒从服务器读取数据
new Thread(){
@Override
public void run() {
while (true){
groupChatClient.readInfo();
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
//向服务器发送数据
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String s = scanner.nextLine();
groupChatClient.sendInfo(s);
}
}
}
内容by尚硅谷netty教程