Netty NIO 非阻塞模式

1.概要

1.1 说明

使用非阻塞的模式,就可以用一个现场,处理多个客户端的请求了

1.2 要点

  • ssc.configureBlocking(false);
  • if(sc!=null){ sc.configureBlocking(false); channels.add(sc); }
  • if(len>0){ byteBuffer.flip();

2.代码

2.1 服务端代码

复制代码
package com.xjc.springcloundtest;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        ssc.bind(new InetSocketAddress(8080));
        List<SocketChannel> channels = new ArrayList<>();
        while (true){
            System.out.println("accept 前");
            SocketChannel sc = ssc.accept();
            System.out.println("accept 后");
            if(sc!=null){
                sc.configureBlocking(false);
                channels.add(sc);
            }
            for (SocketChannel channel: channels){
                System.out.println("read 后");
                int len = channel.read(byteBuffer);
                if(len>0){
                    byteBuffer.flip();
                    while (byteBuffer.hasRemaining()){
                        byte b = byteBuffer.get();
                        System.out.println((char)b);
                    }
                    byteBuffer.clear();
                    System.out.println("read 后");
                }
            }
        }
        //System.out.println("Hello world!");
    }
}

2.2 客户端代码

复制代码
package com.xjc.springcloundtest;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class Client {
    public static void main(String[] args) throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost", 8080));
        System.out.println("Hello world!");
    }
}

2.2 客户端发数据

复制代码
sc.write(Charset.defaultCharset().encode("hello")

3.运行结果

3.1 服务端结果

3.2 客户端结果

复制代码
2023.3.2\lib\idea_rt.jar" com.xjc.springcloundtest.Client
Connected to the target VM, address: '127.0.0.1:53167', transport: 'socket'
Hello world!
相关推荐
猫吻鱼2 天前
【系列文章合集】【全部系列文章合集】
spring boot·dubbo·netty·langchain4j
ps酷教程2 天前
HttpPostRequestDecoder源码浅析
java·http·netty
J_liaty2 天前
Netty高频面试题及答案整理
java·面试·netty
独自破碎E3 天前
解释一下NIO、BIO、AIO
java·开发语言·nio
学习是生活的调味剂4 天前
Java IO模型之BIO和NIO分析
java·nio
不知疲倦的仄仄5 天前
第二天:深入理解 Selector:单线程高效管理多个 Channel
java·nio
daidaidaiyu6 天前
一文学习和实践 当下互联网安全的基石 - TLS 和 SSL
java·netty
不知疲倦的仄仄6 天前
第一天:从 ByteBuffer 内存模型到网络粘包处理实战
java·网络·nio
enjoy编程6 天前
Spring boot 4 探究netty的关键知识点
spring boot·设计模式·reactor·netty·多线程
Ronin3057 天前
【Linux网络】基于Reactor反应堆模式的高并发服务器
linux·网络·reactor·epoll·非阻塞·et模式·高并发服务器