NIO阻塞模式

NIO阻塞模式

在这里插入图片描述

图示简单理解

在这里插入图片描述

代码

Server

package com.example;

import lombok.extern.slf4j.Slf4j;

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

import static com.example.util.ByteBufferUtil.debugRead;

@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        // 使用nio来理解 【阻塞模式】

        // 0.ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);

        // 1.Opens a server-socket channel
        ServerSocketChannel ssc = ServerSocketChannel.open();

        // 2.绑定监听端口
        ssc.bind(new InetSocketAddress(8080));

        // 3.连接集合
        List<SocketChannel> channels = new ArrayList<>();
        while(true){
            // 4.accept 建立与客户端的连接 socketChannel 用来和客户端之间通信
            log.debug("connecting...");
            SocketChannel sc = ssc.accept();
            log.debug("connected...{}",sc);
            channels.add(sc);
            for(SocketChannel channel : channels){
                // 5.接受客户端发送的数据
                log.debug("before read... {}",channel);
                channel.read(buffer);
                buffer.flip();
                debugRead(buffer);
                buffer.clear();
                log.debug("after read ...{}", channel);
            }
        }
    }
}

client

package com.example;

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

说明

  • ssc.accept() 是阻塞方法
    服务启动后,没有客户端的连接,线程在执行ssc.accept()时阻塞。
22:53:50.620 [main] DEBUG com.example.Server - connecting...
  • channel.read(buffer) 是阻塞方法

    客户端client启动后,sc.connect(new InetSocketAddress("localhost",8080))客户端的socketChannel连接到服务端的socketChannel。ssc.accept()继续执行,客户端没有向buffer中写数据,read方法阻塞。

22:53:50.620 [main] DEBUG com.example.Server - connecting...
22:59:51.538 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
22:59:51.539 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
  • 客户端发送数据

    客户端向buffer中写数据,channel.read(buffer);服务端将channel中的数据读取到buffer中。

sc.write(Charset.defaultCharset().encode("hello!"))
  • 服务端打印

    打印后继续卡在ssc.accept()方法上,等待新的连接。

22:53:50.620 [main] DEBUG com.example.Server - connecting...
22:59:51.538 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
22:59:51.539 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.680 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [6]
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 21                               |hello!          |
+--------+-------------------------------------------------+----------------+
23:04:13.693 [main] DEBUG com.example.Server - after read ...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.693 [main] DEBUG com.example.Server - connecting...
  • 启动第二个客户端

    新的客户端端口为51608。

23:04:13.693 [main] DEBUG com.example.Server - after read ...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.693 [main] DEBUG com.example.Server - connecting...
23:41:23.092 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:51608]
23:41:23.092 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值