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!
相关推荐
滕州市燕猫虎计算机科技工作室个体工商户5 天前
Java,Java NIO,Java NIO2,Java AIO,AsynchronousServerSocketChannel
java·开发语言·nio·aio·nio2
Yeswater14 天前
从 NIO 到 Netty:五个坑,和一张把它填平的基础设施网
java·后端·netty
那个松鼠很眼熟w14 天前
【NIO】8. Selector多线程
nio
TAKETODAY19 天前
Netty 是怎么把 HTTP 请求交给 MVC 的?一个请求的完整链路
java·netty
蚰蜒螟19 天前
从内核启动到 Java NIO:一次 socket accept 的源码串联之旅
java·python·nio
骇客野人21 天前
Java BIO / NIO / AIO 完整详解 + 编程技巧
java·开发语言·nio
那个松鼠很眼熟w1 个月前
【NIO】4. ByteBuffer的几个重要方法
nio
那个松鼠很眼熟w1 个月前
【NIO】3.1 ByteBuffer的原理
nio
那个松鼠很眼熟w1 个月前
【NIO】3.2 ByteBuffer的原理-AI参考
nio
那个松鼠很眼熟w1 个月前
【NIO】5. FileChannel的基本用法
nio