美文网首页
NIO Selector 选择器 Test

NIO Selector 选择器 Test

作者: dinel | 来源:发表于2020-04-13 09:11 被阅读0次
package com.lcc.nio;

import org.junit.Test;

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;

/**
 * 测试Selector
 *
 * 不要用debug运行,服务端会无限打印很多次,原因未知
 *
 * @author liangchuanchuan
 */
public class TestSelector {

    /**
     * 服务端
     */
    @Test
    public void server() throws IOException {
        // 1.serverSocketChannel注册OP_ACCEPT事件到selector
        Selector selector = Selector.open();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open().bind(new InetSocketAddress("127.0.0.1", 1001));
        // 配置非阻塞? 我也不知道为啥,不配就报错,后面在研究
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            // 2.selector 阻塞获取事件
            if (selector.select(2000) == 0) {
                System.out.println("服务端等待2秒,无客户端链接");
                continue;
            }
            // 3.获取不同类型事件分别处理
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
            while (keyIterator.hasNext()) {
                SelectionKey selectionKey = keyIterator.next();
                // 3.1 处理OP_ACCEPT 事件
                if (selectionKey.isAcceptable()) {
                    SocketChannel channel = serverSocketChannel.accept();
                    // 配置非阻塞? 我也不知道为啥,不配就报错,后面在研究
                    channel.configureBlocking(false);
                    channel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                    System.out.println("当前OP_ACCEPT 链接SocketChannel:" + channel.hashCode());
                }
                // 3.2 处理OP_READ 事件
                else if (selectionKey.isReadable()) {
                    SocketChannel channel = (SocketChannel) selectionKey.channel();
                    ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
                    channel.read(buffer);
                    System.out.println(new String(buffer.array(), "utf-8"));
                    System.out.println("当前OP_REA 链接SocketChannel:" + channel.hashCode());
                }
                // 4.处理完事件,需要删除当前事件
                keyIterator.remove();
            }
        }

    }


    /**
     * 客户端
     */
    @Test
    public void client() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();

        if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 1001))) {
            while (!socketChannel.finishConnect()) {
                System.out.println("客户端如果未链接可以去做其他事情");
            }
        }
        socketChannel.write(ByteBuffer.wrap("你好,netty".getBytes("utf-8")));
        System.in.read();
    }
}

相关文章

  • Java NIO Selector

    Java NIO Selector Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能...

  • Java NIO

    java Nio Selector 选择器Buffer 缓冲器Channel 通道 Selector是NIO的核心...

  • Java NIO学习笔记-Selector

    Selector选择器是什么 Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓...

  • NIO Selector 选择器 Test

  • Java Nio选择器Selector

    Selector Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸...

  • Java NIO(四):Selector

    Selector 作用 Selector (选择器)用于检测一到多个 NIO Channel(通道),监听通道的事...

  • NIO

    NIO NIO主要有三大核心部分: Channel(通道),Buffer(缓冲区), Selector(选择器,多...

  • Netty源码系列1--初识Netty

    Java NIO 三件套在 NIO 中有几个核心对象需要掌握:缓冲区(Buffer)、选择器(Selector)、...

  • Java NIO--Selector

    Selector简介 Selector(选择器)是javaNIO中能检测一到多个NIO通道,并能够知道通道是否为诸...

  • Java NIO

    Java 1.4 开始引入 NIO 框架,提供了 Channel(通道)、Selector(IO复用器/选择器)、...

网友评论

      本文标题:NIO Selector 选择器 Test

      本文链接:https://www.haomeiwen.com/subject/mbbwmhtx.html