Netty学习之BIO(阻塞IO)

传统的IO传输,服务端会一直等待客户端传来数据,不能做其他事,便处于阻塞状态,即阻塞IO。

通过把一个文件从客户端传输到服务端的案例来理解一下BIO:

首先是客户端的代码:

 @Test
    public void client(){
        try {
            //1.获取通道
            SocketChannel clientChannel = SocketChannel.open(
                    new InetSocketAddress("127.0.0.1", 7979));
            System.out.println("client is start connecting...");
            FileChannel fileChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
            //2.分配指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //3.读取本地文件,并向服务端发送文件
            while(fileChannel.read(buffer)!=-1){
                buffer.flip();
                clientChannel.write(buffer);
                buffer.clear();
            }
            clientChannel.shutdownOutput();//告诉服务端数据已经发送完毕,否则客户端和服务端都会处于阻塞状态
            //4.接收服务端的反馈
            int len = 0;
            while((len = clientChannel.read(buffer))!=-1){
                buffer.flip();
                System.out.println(new String(buffer.array(),0,len));
                buffer.clear();
            }
            //5.关闭通道
            clientChannel.close();
            fileChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
服务端代码如下:

@Test
    public void server(){
        try {
            //1.获取通道
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            FileChannel outChannel = FileChannel.open(
                    Paths.get("bio.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);
            //2.绑定端口
            serverChannel.bind(new InetSocketAddress(7979));
            System.out.println("Server start in port:7979...");
            //3.获取客户端连接
            SocketChannel socketChannel = serverChannel.accept();
            //4分配指定到小的缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            //5.读取数据
            while (socketChannel.read(byteBuffer)!=-1){
                byteBuffer.flip();
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
            //6.发送反馈给客户端
            byteBuffer.put("服务端接收数据成功!".getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            //7.关闭通道
            socketChannel.close();
            outChannel.close();
            serverChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
总结一下使用NIO完成网络通信的三个核心:

1.通道(Channel):负责连接;

2.缓冲区(Buffer):负责数据的存取;

3.选择器(Selector):是SelectableChannel的多路复用器,用于监控SelectableChannel的IO状况

本文还未用到选择器,在nio编程中会使用到。



你可能感兴趣的:(Netty,高性能网络通信框架—Netty)