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!
相关推荐
萧瑟余晖11 小时前
Java深入解析篇九之NIO详解
java·网络·nio
唐青枫3 天前
Java Reactor Netty 实战详解:从响应式 HTTP 到 TCP 长连接
java·netty
Full Stack Developme5 天前
Tomcat NIO连接器内部如何实现零拷贝 Session管理如何实现集群同步
java·tomcat·nio
Full Stack Developme5 天前
MinIO 设计及工作原理
nio
易连EDI—EasyLink8 天前
电动汽车供应链协同新范式:蔚来(NIO)企业级EDI平台建设实践
网络·人工智能·edi·nio·as2
皮皮林55112 天前
Netty 性能好的原因是什么?
netty
我是唐青枫13 天前
Java Netty 实战指南:从 NIO 线程模型到 TCP 编解码和心跳机制
java·tcp/ip·nio
2601_9639329814 天前
人流后多久来月经?内膜恢复周期与术后修护科普指南
nio
white_ant16 天前
5-Netty WebSocket 与 HTTP
java·网络编程·netty
ywl47081208722 天前
【Spring AI 入门07】如何具体实现低延迟大模型推理网关
大模型·netty