实现一个基于nio的discard server

写在前面

源码

为了能够进一步的熟悉下nio相关的api操作,本文来实现一个基于nio的discard server。

discard server的意思是,server接收到来自client的一个消息之后,直接就将连接关闭,即discard。

1:正戏

1.1:server

java 复制代码
package com.dahuyou.nio.discard.server.server;

import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;

/**
 * nio discard server服务端
 */
public class NioDiscardServer {

    public static void main(String[] args) throws Exception {
        // 1:打开选择器
        Selector selector = Selector.open();
        // 2:获取通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 3:设置为非阻塞(阻塞了也就失去了nio的意义了!并且只有非阻塞才支持绑定事件)
        serverSocketChannel.configureBlocking(false);
        // 4:绑定链接
        serverSocketChannel.bind(new InetSocketAddress(18899));
        // 5:注册io事件
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("服务器启动成功");

        // 6:阻塞方式轮询感兴趣的通道事件(IO就绪)
        while (selector.select() > 0) {
            // 7:获取封装事件+通道的选择键的集合
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                // 8:获取某个选择键进行处理
                SelectionKey selectionKey = iterator.next();
                // 9:判断具体的事件,并进行处理
                if (selectionKey.isAcceptable()) {
                    System.out.println("新连接来了:" + selectionKey.channel());
                    // 10:就绪事件,获取客户端的链接(一个个来建立链接???可能不是个问题!!!不太明白这里为什么
                    // 这么写???)
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    // 11:设置为非阻塞
                    socketChannel.configureBlocking(false);
                    // 12:注册到选择器监听相关事件
//                    SelectionKey register = socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT);
                    socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT);
                }
                // 可写事件
                if (selectionKey.isWritable()) {
                    System.out.println("可写啦," + selectionKey.channel());
                }
                if (selectionKey.isConnectable()) {
                    System.out.println("连接成功啦," + selectionKey.channel());
                }
                if (selectionKey.isReadable()) {
                    System.out.println("数据可读啦," + selectionKey.channel());
                    // 13:获取通道,为读取数据做准备啦
                    SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                    // 14:读取数据啦
                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    int length = 0;
                    while ((length = socketChannel.read(byteBuffer)) > 0) {
                        // 转成读模式
                        byteBuffer.flip();
                        System.out.println("读取到数据:" + new String(byteBuffer.array(), 0, length));
                        // 恢复成写模式(这里其实不用的,但当个好习惯吧!)
                        byteBuffer.clear();
                    }
                    // 读完消息了,就discard(discard server的体现指出)
                    socketChannel.close();
                }
                // 15:移出选择键,防止重复处理
                iterator.remove();
            }
        }
        System.out.println("discard 啦!!!!!!!!!!!!");
        // 关闭链接(这里应该是执行不到的,因为selector.select啦)
        serverSocketChannel.close();
    }

}

代码socketChannel.close();在read消息后,就discard啦!另外,关键步骤的代码看带标号的。

1.2:client

java 复制代码
package com.dahuyou.nio.discard.server.client;

import io.netty.buffer.ByteBuf;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioDiscardClient {

    public static void main(String[] args) throws Exception {
        InetSocketAddress inetSocketAddress
                = new InetSocketAddress("127.0.0.1", 18899);
        // 1:获取通道
        SocketChannel socketChannel = SocketChannel.open(inetSocketAddress);
        // 2:切换成非阻塞
        socketChannel.configureBlocking(false);
        // 不断自旋,等待连接完成
        while (!socketChannel.finishConnect()) {}
        System.out.println("链接server成功");
        // 3:创建缓冲区,填充数据,并向通道写入数据
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("hello discard server".getBytes());
        // 转成读模式,开始读数据写出去
        byteBuffer.flip();
        socketChannel.write(byteBuffer);
        System.out.println("向server写数据done");

        // 关了,释放资源,好习惯
        socketChannel.shutdownOutput();
        socketChannel.close();
    }
}

1.3:运行测试

写在后面

参考文章列表

使用NIO实现Discard服务器的实践案例

使用NIO实现Discard服务器的实践案例

相关推荐
刘婉晴9 小时前
【Java】NIO 简单介绍
java·nio
a587692 天前
Java核心概念精讲:TCP与UDP的区别、Java NIO的几个核心组件与HTTP和HTTPS的区别等(46-50)
java·面试·nio
MuMuMu#2 天前
JAVA NIO学习笔记基础强化学习总结
java·学习·nio
我叫汪枫3 天前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
青鱼入云5 天前
java面试中经常会问到的IO、NIO问题有哪些(基础版)
java·面试·nio
奔跑吧邓邓子5 天前
【Java实战⑳】从IO到NIO:Java高并发编程的飞跃
java·实战·nio·高并发编程
编啊编程啊程7 天前
Netty从0到1系列之Selector
java·spring boot·spring cloud·java-ee·kafka·maven·nio
鼠鼠我捏,要死了捏16 天前
深入解析Java NIO多路复用原理与性能优化实践指南
java·性能优化·nio
码luffyliu1 个月前
Java NIO 核心原理与秋招高频面试题解析
java·nio
带只拖鞋去流浪1 个月前
Java文件读写(IO、NIO)
java·开发语言·nio