Java 使用 NIO 实现 ServerSocket 服务端

NIO 三大核心

  • Channel (通道)
  • Buffer (缓冲区)
  • Selector (选择器)

模型图

在这里插入图片描述

代码演示

package com.xxl.job.admin.mytest;

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

public class NioDemo {

    public static void main(String[] args) {
        try {
            // 开启服务端通道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

            // selector 注册器
            Selector selector = Selector.open();

            // 绑定端口
            serverSocketChannel.socket().bind(new InetSocketAddress(6666));

            // 设置为非阻塞
            serverSocketChannel.configureBlocking(false);

            // 把 channel 注册到 selector 上面
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

            // 循环等待客户端连接
            while (true) {
                // 查询如果没有事件发生就可以起做其他的操作了,无需阻塞在这里
                if (selector.select(1000) == 0) {
                    System.out.println("服务器等待1s,无连接");
                    continue;
                }

                // 返回关注的事件集合
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator<SelectionKey> iterator = selectionKeys.iterator();

                while (iterator.hasNext()) {

                    SelectionKey selectionKey = iterator.next();
                    // 针对不同的 selectionKey 去做响应的操作
                    // 发生了客户端连接事件
                    if (selectionKey.isAcceptable()) {
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        socketChannel.configureBlocking(false);
                        System.out.println("有一个客户端连接成功,hashCode = "+socketChannel.hashCode());
                        socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                    }
                    // 发生了服务器读取事件
                    if (selectionKey.isReadable()) {
                        SocketChannel socketChannel = (SocketChannel)selectionKey.channel();
                        ByteBuffer buffer = (ByteBuffer)selectionKey.attachment();
                        socketChannel.read(buffer);
                        System.out.println("form 客户端 "+ new String(buffer.array()));
                    }
                }
                iterator.remove();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}

使用 telnet 命令进行模拟客户端,链接服务器,命令如下:

telnet 127.0.0.1 8080
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔道不误砍柴功

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值