netty学习笔记十一、Selector小案例代码演示

一、客户端

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();
            }

        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值