Netty_Day2_阻塞模式和非阻塞模式

1.阻塞模式:

服务器代码:

package com.ityin.netty.c1;

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.ityin.netty.c1.ByteBufferUtil.debugRead;
@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        //使用nio来理解阻塞模式,单线程
        ByteBuffer buffer =ByteBuffer.allocate(16);
        //1.创建了服务器
        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("connecting...{}",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);
            }
        }
    }
}

客户端代码:

package com.ityin.netty.c1;

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...");
//    }
//}
public class Client {
    public static void main(String[] args) {
        try (SocketChannel socketChannel = SocketChannel.open()) {
            // 建立连接
            socketChannel.connect(new InetSocketAddress("localhost", 8080));
            System.out.println("waiting...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

通过debug后的evaluate expression建立连接:
在这里插入图片描述
得到以下的结果:
在这里插入图片描述

2.非阻塞模式

客户端代码修改为:

package com.ityin.netty.c1;

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.ityin.netty.c1.ByteBufferUtil.debugRead;
@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        //使用nio来理解阻塞模式,单线程
        ByteBuffer buffer =ByteBuffer.allocate(16);
        //1.创建了服务器
        ServerSocketChannel ssc=ServerSocketChannel.open();
        ssc.configureBlocking(false);//切换成非阻塞模式
        //2.绑定监听端口
        ssc.bind(new InetSocketAddress(8080));
        //3.连接集合
        List<SocketChannel> channels=new ArrayList<>();
        while(true){
            //4.accept建立与客户端连接,SocketChannel用来与客户端之间通信
            log.debug("connecting...");
            SocketChannel sc=ssc.accept();//非阻塞方法,线程运行
            if(sc!=null){
                log.debug("connected...{}",sc);
                sc.configureBlocking(false);
                channels.add(sc);
            }
           // log.debug("connecting...{}",sc);
            for(SocketChannel channel:channels){
                //5.接收客户端发送的数据
                log.debug("before read...{}",channel);
                int read=channel.read(buffer);//非阻塞,线程仍然会继续运行,如果没有读到数据,read返回0
                if(read>0){
                    buffer.flip();
                    debugRead(buffer);
                    buffer.clear();
                }
                log.debug("after read...{}",channel);
            }
        }
    }
}

主要修改了两个方面,分别是:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值