Java入门系列-25-NIO(实现非阻塞网络通信)

简介:

还记得之前介绍NIO时对比传统IO的一大特点吗?就是NIO是非阻塞式的,这篇文章带大家来看一下非阻塞的网络操作。

补充:以数组的形式使用缓冲区

package testnio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestBufferArray {

    public static void main(String[] args) throws IOException {
        RandomAccessFile raf1=new RandomAccessFile("D:/1.txt","rw");
        
        //1.获取通道
        FileChannel channel1=raf1.getChannel();
        
        //2.创建缓冲区数组
        ByteBuffer buf1=ByteBuffer.allocate(512);
        ByteBuffer buf2=ByteBuffer.allocate(512);
        ByteBuffer[] bufs= {buf1,buf2};
        //3.将数据读入缓冲区数组
        channel1.read(bufs);
        
        for (ByteBuffer byteBuffer : bufs) {
            byteBuffer.flip();
        }
        System.out.println(new String(bufs[0].array(),0,bufs[0].limit()));
        System.out.println("-----------");
        System.out.println(new String(bufs[1].array(),0,bufs[1].limit()));
        
        //写入缓冲区数组到通道中
        RandomAccessFile raf2=new RandomAccessFile("D:/2.txt","rw");
        FileChannel channel2=raf2.getChannel();
        channel2.write(bufs);
        
    }
}

使用NIO实现阻塞式网络通信

TCP协议的网络通信传统实现方式是通过套接字编程(Socket和ServerSocket),NIO实现TCP网络通信需要用到 Channel 接口的两个实现类:SocketChannel和ServerSocketChannel

使用NIO实现阻塞式网络通信

客户端

package com.jikedaquan.blockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Client {

    public static void main(String[] args) {

        SocketChannel sChannel=null;

        FileChannel inChannel=null;
        try {
            //1、获取通道
            sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 1666));
            //用于读取文件            
            inChannel = FileChannel.open(Paths.get("F:/a.jpg"), StandardOpenOption.READ);

            //2、分配指定大小的缓冲区
            ByteBuffer buf=ByteBuffer.allocate(1024);

            //3、读取本地文件,发送到服务器端

            while(inChannel.read(buf)!=-1) {
                buf.flip();
                sChannel.write(buf);
                buf.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭通道
            if (inChannel!=null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(sChannel!=null) {
                try {
                    sChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

new InetSocketAddress("127.0.0.1", 1666) 用于向客户端套接字通道(SocketChannel)绑定要连接地址和端口

服务端

package com.jikedaquan.blockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Server {

    public static void main(String[] args) {

        ServerSocketChannel ssChannel=null;

        FileChannel outChannel=null;

        SocketChannel sChannel=null;
        try {
            //1、获取通道
            ssChannel = ServerSocketChannel.open();
            //用于保存文件的通道
            outChannel = FileChannel.open(Paths.get("F:/b.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);

            //2、绑定要监听的端口号
            ssChannel.bind(new InetSocketAddress(1666));
            //3、获取客户端连接的通道
            sChannel = ssChannel.accept();

            //4、分配指定大小的缓冲区
            ByteBuffer buf=ByteBuffer.allocate(1024);

            //5、接收客户端的数据,并保存到本地
            while(sChannel.read(buf)!=-1) {
                buf.flip();
                outChannel.write(buf);
                buf.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //6、关闭通道
            if(sChannel!=null) {
                try {
                    sChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outChannel!=null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ssChannel!=null) {
                try {
                    ssChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }    
            }        
        }
    }    
}

服务端套接字仅绑定要监听的端口即可 ssChannel.bind(new InetSocketAddress(1666));

上面的代码使用NIO实现的网络通信,可能有同学会问,没有看到阻塞效果啊,确实是阻塞式的看不到效果,因为客户端发送一次数据就结束了,服务端也是接收一次数据就结束了。那如果服务端接收完成数据后,再向客户端反馈呢?

能够看到阻塞效果的网络通信

客户端

package com.jikedaquan.blockingnio2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Client {

    public static void main(String[] args) {
        SocketChannel sChannel=null;
        FileChannel inChannel=null;
        try {
            sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 1666));
            inChannel = FileChannel.open(Paths.get("F:/a.jpg"), StandardOpenOption.READ);

            ByteBuffer buf=ByteBuffer.allocate(1024);

            while(inChannel.read(buf)!=-1) {
                buf.flip();
                sChannel.write(buf);
                buf.clear();
            }
            
            //sChannel.shutdownOutput();//去掉注释掉将不会阻塞

            //接收服务器端的反馈
            int len=0;
            while((len=sChannel.read(buf))!=-1) {
                buf.flip();
                System.out.println(new String(buf.array(),0,len));
                buf.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inChannel!=null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sChannel!=null) {
                try {
                    sChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端

package com.jikedaquan.blockingnio2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Server {

    public static void main(String[] args) {

        ServerSocketChannel ssChannel=null;
        FileChannel outChannel=null;
        SocketChannel sChannel=null;
        try {
            ssChannel = ServerSocketChannel.open();
            outChannel = FileChannel.open(Paths.get("F:/a.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);

            ssChannel.bind(new InetSocketAddress(1666));
            sChannel = ssChannel.accept();
            ByteBuffer buf=ByteBuffer.allocate(1024);

            while(sChannel.read(buf)!=-1) {
                buf.flip();
                outChannel.write(buf);
                buf.clear();
            }

            //发送反馈给客户端
            buf.put("服务端接收数据成功".getBytes());
            buf.flip();
            sChannel.write(buf);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sChannel!=null) {
                try {
                    sChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outChannel!=null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ssChannel!=null) {
                try {
                    ssChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端将向客户端发送两次数据

选择器(Selector)

想要实现非阻塞的IO,必须要先弄懂选择器。Selector 抽象类,可通过调用此类的 open 方法创建选择器,该方法将使用系统的默认选择器提供者创建新的选择器。

将通道设置为非阻塞之后,需要将通道注册到选择器中,注册的同时需要指定一个选择键的类型 (SelectionKey)。

选择键(SelectionKey)可以认为是一种标记,标记通道的类型和状态。

SelectionKey的静态字段:
OP_ACCEPT:用于套接字接受操作的操作集位
OP_CONNECT:用于套接字连接操作的操作集位
OP_READ:用于读取操作的操作集位
OP_WRITE:用于写入操作的操作集位

用于检测通道状态的方法:

方法名称 说明
isAcceptable() 测试此键的通道是否已准备好接受新的套接字连接
isConnectable() 测试此键的通道是否已完成其套接字连接操作
isReadable() 测试此键的通道是否已准备好进行读取
isWritable() 测试此键的通道是否已准备好进行写入

将通道注册到选择器:

ssChannel.register(selector, SelectionKey.OP_ACCEPT);

IO操作准备就绪的通道大于0,轮询选择器

while(selector.select()>0) {
    //获取选择键,根据不同的状态做不同的操作
}

实现非阻塞式TCP协议网络通信

非阻塞模式:channel.configureBlocking(false);

客户端

package com.jikedaquan.nonblockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {
        SocketChannel sChannel=null;
        try {
            //1、获取通道
            sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",1666));
            
            //2、切换非阻塞模式
            sChannel.configureBlocking(false);
            
            //3、分配指定大小的缓冲区
            ByteBuffer buf=ByteBuffer.allocate(1024);
            //4、发送数据给服务端
            Scanner scanner=new Scanner(System.in);
            //循环从控制台录入数据发送给服务端
            while(scanner.hasNext()) {
                
                String str=scanner.next();
                buf.put((new Date().toString()+"\n"+str).getBytes());
                buf.flip();
                sChannel.write(buf);
                buf.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //5、关闭通道
            if(sChannel!=null) {
                try {
                    sChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端

package com.jikedaquan.nonblockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class Server {

    public static void main(String[] args) throws IOException {
        
        //1、获取通道
        ServerSocketChannel ssChannel=ServerSocketChannel.open();
        //2、切换非阻塞模式
        ssChannel.configureBlocking(false);
        //3、绑定监听的端口号
        ssChannel.bind(new InetSocketAddress(1666));
        //4、获取选择器
        Selector selector=Selector.open();
        //5、将通道注册到选择器上,并指定“监听接收事件”
        ssChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        //6、轮询式的获取选择器上已经 “准备就绪”的事件
        while(selector.select()>0) {
            //7、获取当前选择器中所有注册的“选择键(已就绪的监听事件)”
            Iterator<SelectionKey> it=selector.selectedKeys().iterator();
            while(it.hasNext()) {
                //8、获取准备就绪的事件
                SelectionKey sk=it.next();
                //9、判断具体是什么事件准备就绪
                if(sk.isAcceptable()) {
                    //10、若“接收就绪”,获取客户端连接
                    SocketChannel sChannel=ssChannel.accept();
                    //11、切换非阻塞模式
                    sChannel.configureBlocking(false);
                    //12、将该通道注册到选择器上
                    sChannel.register(selector, SelectionKey.OP_READ);
                }else if(sk.isReadable()) {
                    //13、获取当前选择器上“读就绪”状态的通道
                    SocketChannel sChannel=(SocketChannel)sk.channel();
                    //14、读取数据
                    ByteBuffer buf=ByteBuffer.allocate(1024);
                    int len=0;
                    while((len=sChannel.read(buf))>0) {
                        buf.flip();
                        System.out.println(new String(buf.array(),0,len));
                        buf.clear();
                    }
                }
                //15、取消选择键 SelectionKey
                it.remove();
            }
            
        }
    }
}

服务端接收客户端的操作需要在判断 isAcceptable() 方法内将就绪的套接字通道以读操作注册到 选择器中

在判断 isReadable() 内从通道中获取数据

实现非阻塞式UDP协议网络通信

发送端

package com.jikedaquan.nonblockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Scanner;

public class TestDatagramSend {

    public static void main(String[] args) throws IOException {
        //获取通道
        DatagramChannel dChannel=DatagramChannel.open();
        //非阻塞
        dChannel.configureBlocking(false);
        ByteBuffer buf=ByteBuffer.allocate(1024);
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()) {
            String str=scanner.next();
            buf.put(str.getBytes());
            buf.flip();
            //发送数据到目标地址和端口
            dChannel.send(buf,new InetSocketAddress("127.0.0.1", 1666));
            buf.clear();
        }
        dChannel.close();
    }
}

接收端

package com.jikedaquan.nonblockingnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;

public class TestDatagramReceive {
    public static void main(String[] args) throws IOException {
        //获取通道
        DatagramChannel dChannel=DatagramChannel.open();
        dChannel.configureBlocking(false);
        //绑定监听端口
        dChannel.bind(new InetSocketAddress(1666));
        //获取选择器
        Selector selector=Selector.open();
        //读操作注册通道
        dChannel.register(selector, SelectionKey.OP_READ);
        while(selector.select()>0) {
            Iterator<SelectionKey> it=selector.selectedKeys().iterator();
            //迭代选择键
            while(it.hasNext()) {
                SelectionKey sk=it.next();
                //通道可读
                if(sk.isReadable()) {
                    ByteBuffer buf=ByteBuffer.allocate(1024);
                    //接收数据存入缓冲区
                    dChannel.receive(buf);
                    buf.flip();
                    System.out.println(new String(buf.array(),0,buf.limit()));
                    buf.clear();
                }
            }
            
            it.remove();
        }
    }
}
相关文章
|
13天前
|
机器学习/深度学习 人工智能 算法
深度学习入门:理解神经网络与反向传播算法
【9月更文挑战第20天】本文将深入浅出地介绍深度学习中的基石—神经网络,以及背后的魔法—反向传播算法。我们将通过直观的例子和简单的数学公式,带你领略这一技术的魅力。无论你是编程新手,还是有一定基础的开发者,这篇文章都将为你打开深度学习的大门,让你对神经网络的工作原理有一个清晰的认识。
|
1月前
|
监控 安全 数据安全/隐私保护
智能家居安全入门:保护你的网络家园
本文旨在为初学者提供一份简明扼要的指南,介绍如何保护自己的智能家居设备免受网络攻击。通过分析智能家居系统常见的安全漏洞,并提供实用的防御策略,帮助读者建立起一道坚固的数字防线。
|
7天前
|
域名解析 网络协议 应用服务中间件
网络编程入门如此简单(四):一文搞懂localhost和127.0.0.1
本文将以网络编程入门者视角,言简意赅地为你请清楚localhost和127.0.0.1的关系及区别等。
16 2
网络编程入门如此简单(四):一文搞懂localhost和127.0.0.1
|
7天前
|
存储 网络协议 Java
Java NIO 开发
本文介绍了Java NIO(New IO)及其主要组件,包括Channel、Buffer和Selector,并对比了NIO与传统IO的优势。文章详细讲解了FileChannel、SocketChannel、ServerSocketChannel、DatagramChannel及Pipe.SinkChannel和Pipe.SourceChannel等Channel实现类,并提供了示例代码。通过这些示例,读者可以了解如何使用不同类型的通道进行数据读写操作。
Java NIO 开发
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
深度学习中的卷积神经网络(CNN)入门与实践
【8月更文挑战第62天】本文以浅显易懂的方式介绍了深度学习领域中的核心技术之一——卷积神经网络(CNN)。文章通过生动的比喻和直观的图示,逐步揭示了CNN的工作原理和应用场景。同时,结合具体的代码示例,引导读者从零开始构建一个简单的CNN模型,实现对图像数据的分类任务。无论你是深度学习的初学者还是希望巩固理解的开发者,这篇文章都将为你打开一扇通往深度学习世界的大门。
|
10天前
|
Java 程序员 UED
Java中的异常处理:从入门到精通
【9月更文挑战第23天】在Java编程的世界中,异常是程序执行过程中不可避免的事件,它们可能会中断正常的流程并导致程序崩溃。本文将通过浅显易懂的方式,引导你理解Java异常处理的基本概念和高级技巧,帮助你编写更健壮、更可靠的代码。我们将一起探索如何捕获和处理异常,以及如何使用自定义异常来增强程序的逻辑和用户体验。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供有价值的见解和实用的技巧。
28 4
|
14天前
|
机器学习/深度学习 人工智能 算法
深度学习中的卷积神经网络(CNN)入门与实践
【9月更文挑战第19天】在这篇文章中,我们将探索深度学习的一个重要分支——卷积神经网络(CNN)。从基础概念出发,逐步深入到CNN的工作原理和实际应用。文章旨在为初学者提供一个清晰的学习路径,并分享一些实用的编程技巧,帮助读者快速上手实践CNN项目。
|
19天前
|
机器学习/深度学习 人工智能 TensorFlow
深度学习入门:理解卷积神经网络(CNN)
【9月更文挑战第14天】本文旨在为初学者提供一个关于卷积神经网络(CNN)的直观理解,通过简单的语言和比喻来揭示这一深度学习模型如何识别图像。我们将一起探索CNN的基本组成,包括卷积层、激活函数、池化层和全连接层,并了解它们如何协同工作以实现图像分类任务。文章末尾将给出一个简单的代码示例,帮助读者更好地理解CNN的工作原理。
38 7
|
2月前
|
图形学 C#
超实用!深度解析Unity引擎,手把手教你从零开始构建精美的2D平面冒险游戏,涵盖资源导入、角色控制与动画、碰撞检测等核心技巧,打造沉浸式游戏体验完全指南
【8月更文挑战第31天】本文是 Unity 2D 游戏开发的全面指南,手把手教你从零开始构建精美的平面冒险游戏。首先,通过 Unity Hub 创建 2D 项目并导入游戏资源。接着,编写 `PlayerController` 脚本来实现角色移动,并添加动画以增强视觉效果。最后,通过 Collider 2D 组件实现碰撞检测等游戏机制。每一步均展示 Unity 在 2D 游戏开发中的强大功能。
82 6
|
22天前
|
Java 程序员
Java中的异常处理:从入门到精通
在Java编程的世界中,异常处理是保持程序稳定性和可靠性的关键。本文将通过一个独特的视角—把异常处理比作一场“捉迷藏”游戏—来探讨如何在Java中有效管理异常。我们将一起学习如何识别、捕捉以及处理可能出现的异常,确保你的程序即使在面对不可预见的错误时也能优雅地运行。准备好了吗?让我们开始这场寻找并解决Java异常的冒险吧!
下一篇
无影云桌面