10.NIO 网络编程应用实例-群聊系统

这篇具有很好参考价值的文章主要介绍了10.NIO 网络编程应用实例-群聊系统。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


highlight: arduino-light

NIO 网络编程应用实例-群聊系统

目标

需求:进一步理解 NIO 非阻塞网络编程机制,实现多人群聊

  • 编写一个 NIO 群聊系统,实现客户端与客户端的通信需求(非阻塞)
  • 服务器端:可以监测用户上线,离线,并实现消息转发功能
  • 客户端:通过 channel 可以无阻塞发送消息给其它所有客户端用户,同时可以接受其它客户端用户通过服务端转发来的消息

服务端代码实现

​ ​ /**** 监听接收和读就绪事件 1.SelectionKey是接收事件: 直接获取当前接入的客户端通道 切换成非阻塞模式 将本客户端通道注册到选择器 监听读就绪事件 移除当前接收事件对应的SelectionKey ​ 2.SelectionKey是读就绪事件: 获取消息 转发消息到其他所有的socket,排除自己 ​ ****/ public class Server {    //定义属性    private Selector selector;    private ServerSocketChannel ssChannel;    private static final int PORT = 9999;    //构造器    //初始化工作    public Server() {        try {            // 1、获取通道            ssChannel = ServerSocketChannel.open();            // 2、切换为非阻塞模式            ssChannel.configureBlocking(false);            // 3、绑定连接的端口            ssChannel.bind(new InetSocketAddress(PORT));            // 4、获取选择器Selector            selector = Selector.open();            // 5、将通道都注册到选择器上去,并且开始指定监听接收事件            ssChannel.register(selector , SelectionKey.OP_ACCEPT);       }catch (IOException e) {            e.printStackTrace();       }   }    //监听    public void listen() {        System.out.println("监听线程: " + Thread.currentThread().getName());        try {            while (selector.select() > 0){                System.out.println("开始一轮事件处理~~~");                // 7、获取选择器中的所有注册的通道中已经就绪好的事件                Iterator<SelectionKey> it = selector.selectedKeys().iterator();                // 8、开始遍历这些准备好的事件                while (it.hasNext()){                    // 提取当前这个事件                    SelectionKey sk = it.next();                    // 9、判断这个事件具体是什么                    if(sk.isAcceptable()){                        // 10、直接获取当前接入的客户端通道                        SocketChannel schannel = ssChannel.accept();                        // 11 、切换成非阻塞模式                        schannel.configureBlocking(false);                        // 12、将本客户端通道注册到选择器                        System.out.println(schannel.getRemoteAddress() + " 上线 ");                        schannel.register(selector , SelectionKey.OP_READ);                        //提示                   }else if(sk.isReadable()){                        //处理读 (专门写方法..)                        readData(sk);                   } ​                    it.remove(); // 处理完毕之后需要移除当前事件               }           }       }catch (Exception e) {            e.printStackTrace();       }finally {            //发生异常处理.... ​       }   } ​    //读取客户端消息    private void readData(SelectionKey key) {        //取到关联的channle        SocketChannel channel = null;        try {           //得到channel            channel = (SocketChannel) key.channel();            //创建buffer            ByteBuffer buffer = ByteBuffer.allocate(1024);            int count = channel.read(buffer);            //根据count的值做处理            if(count > 0) {                //把缓存区的数据转成字符串                String msg = new String(buffer.array());                //输出该消息                System.out.println("form 客户端: " + msg);                //向其它的客户端转发消息(去掉自己), 专门写一个方法来处理                sendInfoToOtherClients(msg, channel);           }       }catch (IOException e) {            try {                System.out.println(channel.getRemoteAddress() + " 离线了..");                e.printStackTrace();                //取消注册                key.cancel();                //关闭通道                channel.close();           }catch (IOException e2) {                e2.printStackTrace();;           }       }   } ​    //转发消息给其它客户(通道)    private void sendInfoToOtherClients(String msg, SocketChannel self ) throws  IOException{        System.out.println("服务器转发消息中...");        System.out.println("服务器转发数据给客户端线程: " + Thread.currentThread().getName());        //遍历 所有注册到selector 上的 SocketChannel,并排除 self        for(SelectionKey key: selector.keys()) {            //通过 key 取出对应的 SocketChannel            Channel targetChannel = key.channel();            //排除自己            if(targetChannel instanceof  SocketChannel && targetChannel != self) {                //转型                SocketChannel dest = (SocketChannel)targetChannel;                //将msg 存储到buffer                ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());                //将buffer 的数据写入 通道                dest.write(buffer);           }       }   } ​    public static void main(String[] args) {        //创建服务器对象        Server groupChatServer = new Server();        groupChatServer.listen();   } }

客户端代码实现

package com.itheima.chat; ​ 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.SocketChannel; import java.util.Iterator; import java.util.Scanner; ​ public class Client {    //定义相关的属性    private final String HOST = "127.0.0.1"; // 服务器的ip    private final int PORT = 9999; //服务器端口    private Selector selector;    private SocketChannel socketChannel;    private String username; ​    //构造器, 完成初始化工作    public Client() throws IOException { ​        selector = Selector.open();        //连接服务器        socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1", PORT));        //设置非阻塞        socketChannel.configureBlocking(false);        //将channel 注册到selector        socketChannel.register(selector, SelectionKey.OP_READ);        //得到username        username = socketChannel.getLocalAddress().toString().substring(1);        System.out.println(username + " is ok...");   } ​    //向服务器发送消息    public void sendInfo(String info) {        info = username + " 说:" + info;        try {            socketChannel.write(ByteBuffer.wrap(info.getBytes()));       }catch (IOException e) {            e.printStackTrace();       }   } ​    //读取从服务器端回复的消息    public void readInfo() {        try { ​            int readChannels = selector.select();            if(readChannels > 0) {//有可以用的通道 ​                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();                while (iterator.hasNext()) { ​                    SelectionKey key = iterator.next();                    if(key.isReadable()) {                        //得到相关的通道                       SocketChannel sc = (SocketChannel) key.channel();                       //得到一个Buffer                        ByteBuffer buffer = ByteBuffer.allocate(1024);                        //读取                        sc.read(buffer);                        //把读到的缓冲区的数据转成字符串                        String msg = new String(buffer.array());                        System.out.println(msg.trim());                   }               }                iterator.remove(); //删除当前的selectionKey, 防止重复操作           } else {                //System.out.println("没有可以用的通道..."); ​           } ​       }catch (Exception e) {            e.printStackTrace();       }   } ​    public static void main(String[] args) throws Exception {        //启动我们客户端        Client chatClient = new Client();        //启动一个线程, 每个3秒,读取从服务器发送数据        new Thread() {            public void run() { ​                while (true) {                    chatClient.readInfo();                    try {                        Thread.currentThread().sleep(3000);                   }catch (InterruptedException e) {                        e.printStackTrace();                   }               }           }       }.start(); ​        //发送数据给服务器端        Scanner scanner = new Scanner(System.in); ​        while (scanner.hasNextLine()) {            String s = scanner.nextLine();            chatClient.sendInfo(s);       }   } }

尚硅谷代码实现

java package com.atguigu.nio.groupchat; ​ import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; ​ public class GroupChatServer {    //定义属性    private Selector selector;    private ServerSocketChannel listenChannel;    private static final int PORT = 6667; ​    //构造器    //初始化工作    public GroupChatServer() { ​        try { ​            //得到选择器            selector = Selector.open();            //ServerSocketChannel            listenChannel =  ServerSocketChannel.open();            //绑定端口            listenChannel.socket().bind(new InetSocketAddress(PORT));            //设置非阻塞模式            listenChannel.configureBlocking(false);            //将该listenChannel 注册到selector            listenChannel.register(selector, SelectionKey.OP_ACCEPT);       }catch (IOException e) {            e.printStackTrace();       }   } ​    //监听    public void listen() { ​        System.out.println("监听线程: " + Thread.currentThread().getName());        try { ​            //循环处理            while (true) {                int count = selector.select();//这2步可以放到while判断中                if(count > 0) {//如果有事件处理                                        //遍历得到selectionKey 集合                    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();                    while (iterator.hasNext()) {                        //取出selectionkey                        SelectionKey key = iterator.next(); ​                        //监听到accept                        if(key.isAcceptable()) {                            SocketChannel sc = listenChannel.accept();                            sc.configureBlocking(false);                            //将该 sc 注册到seletor,监听事件为读事件                            sc.register(selector, SelectionKey.OP_READ);                            //提示                            System.out.println(sc.getRemoteAddress() + " 上线 ");                       }                        if(key.isReadable()) { //通道发送read事件,即通道是可读的状态                            //处理读 (专门写方法..)                            readData(key);                       }                        //当前的key 删除,防止重复处理                        iterator.remove();                   } ​               } else {                    System.out.println("等待....");               }           } ​       }catch (Exception e) {            e.printStackTrace(); ​       }finally {            //发生异常处理.... ​       }   } ​    //读取客户端消息    private void readData(SelectionKey key) { ​        //取到关联的channle        SocketChannel channel = null; ​        try {           //得到channel            channel = (SocketChannel) key.channel();            //创建buffer            ByteBuffer buffer = ByteBuffer.allocate(1024); ​            int count = channel.read(buffer);            //根据count的值做处理            if(count > 0) {                //把缓存区的数据转成字符串                String msg = new String(buffer.array());                //输出该消息                System.out.println("form 客户端: " + msg); ​                //向其它的客户端转发消息(去掉自己), 专门写一个方法来处理                sendInfoToOtherClients(msg, channel);           } ​       }catch (IOException e) {            try {                System.out.println(channel.getRemoteAddress() + " 离线了..");                //取消注册                key.cancel();                //关闭通道                channel.close();           }catch (IOException e2) {                e2.printStackTrace();;           }       }   } ​    //转发消息给其它客户(通道)    private void sendInfoToOtherClients(String msg, SocketChannel self ) throws  IOException{ ​        System.out.println("服务器转发消息中...");        System.out.println("服务器转发数据给客户端线程: " + Thread.currentThread().getName());        //遍历 所有注册到selector 上的 SocketChannel,并排除 self        for(SelectionKey key: selector.keys()) { ​            //通过 key 取出对应的 SocketChannel            Channel targetChannel = key.channel(); ​            //排除自己            if(targetChannel instanceof  SocketChannel && targetChannel != self) { ​                //转型                SocketChannel dest = (SocketChannel)targetChannel;                //将msg 存储到buffer                ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());                //将buffer 的数据写入 通道                dest.write(buffer);           }       } ​   } ​    public static void main(String[] args) { ​        //创建服务器对象        GroupChatServer groupChatServer = new GroupChatServer();        groupChatServer.listen();   } }

java ​ package com.atguigu.nio.groupchat; ​ 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.SocketChannel; import java.util.Iterator; import java.util.Scanner; import java.util.Set; ​ public class GroupChatClient { ​    //定义相关的属性    private final String HOST = "127.0.0.1"; // 服务器的ip    private final int PORT = 6667; //服务器端口    private Selector selector;    private SocketChannel socketChannel;    private String username; ​    //构造器, 完成初始化工作    public GroupChatClient() throws IOException { ​        selector = Selector.open();        //连接服务器        socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1", PORT));        //设置非阻塞        socketChannel.configureBlocking(false);        //将channel 注册到selector        socketChannel.register(selector, SelectionKey.OP_READ);        //得到username        username = socketChannel.getLocalAddress().toString().substring(1);        System.out.println(username + " is ok..."); ​   } ​    //向服务器发送消息    public void sendInfo(String info) { ​        info = username + " 说:" + info; ​        try {            socketChannel.write(ByteBuffer.wrap(info.getBytes()));       }catch (IOException e) {            e.printStackTrace();       }   } ​    //读取从服务器端回复的消息    public void readInfo() { ​        try { ​            int readChannels = selector.select();            if(readChannels > 0) {//有可以用的通道 ​                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();                while (iterator.hasNext()) { ​                    SelectionKey key = iterator.next();                    if(key.isReadable()) {                        //得到相关的通道                       SocketChannel sc = (SocketChannel) key.channel();                       //得到一个Buffer                        ByteBuffer buffer = ByteBuffer.allocate(1024);                        //读取                        sc.read(buffer);                        //把读到的缓冲区的数据转成字符串                        String msg = new String(buffer.array());                        System.out.println(msg.trim());                   }               }                iterator.remove(); //删除当前的selectionKey, 防止重复操作           } else {                //System.out.println("没有可以用的通道..."); ​           } ​       }catch (Exception e) {            e.printStackTrace();       }   } ​    public static void main(String[] args) throws Exception { ​        //启动我们客户端        GroupChatClient chatClient = new GroupChatClient(); ​        //启动一个线程, 每个3秒,读取从服务器发送数据        new Thread() {            public void run() { ​                while (true) {                    chatClient.readInfo();                    try {                        Thread.currentThread().sleep(3000);                   }catch (InterruptedException e) {                        e.printStackTrace();                   }               }           }       }.start(); ​        //发送数据给服务器端        Scanner scanner = new Scanner(System.in); ​        while (scanner.hasNextLine()) {            String s = scanner.nextLine();            chatClient.sendInfo(s);       }   } } ​ ​ 文章来源地址https://www.toymoban.com/news/detail-615434.html

到了这里,关于10.NIO 网络编程应用实例-群聊系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 快速入门java网络编程基础------Nio

    哔哩哔哩黑马程序员 netty实战视频 NIO(New I/O)是Java中提供的一种基于通道和缓冲区的I/O(Input/Output)模型。它是相对于传统的IO(InputStream和OutputStream)模型而言的新型I/O模型。NIO的主要特点包括: 1.通道与缓冲区: 2.NIO引入了通道(Channel)和缓冲区(Buffer)的概念。通道

    2024年01月20日
    浏览(37)
  • Java 网络编程之NIO(selector)

                        本编文章意在循环渐进,可看最后一个就可以了                Selector selector = Selector.open();                首先channel必须是非阻塞的情况下                 channel.register(选择器,操作的类型,绑定的组件);返回的是选择键              1)Ch

    2023年04月11日
    浏览(28)
  • Java网络编程-深入理解BIO、NIO

    BIO BIO 为 Blocked-IO(阻塞 IO),在 JDK1.4 之前建立网络连接时,只能使用 BIO 使用 BIO 时,服务端会对客户端的每个请求都建立一个线程进行处理,客户端向服务端发送请求后,先咨询服务端是否有线程响应,如果没有就会等待或者被拒绝 BIO 基本使用代码: 服务端: 客户端:

    2024年02月04日
    浏览(27)
  • BIO、NIO、IO多路复用模型详细介绍&Java NIO 网络编程

    上文介绍了网络编程的基础知识,并基于 Java 编写了 BIO 的网络编程。我们知道 BIO 模型是存在巨大问题的,比如 C10K 问题,其本质就是因其阻塞原因,导致如果想要承受更多的请求就必须有足够多的线程,但是足够多的线程会带来内存占用问题、CPU上下文切换带来的性能问题

    2024年02月14日
    浏览(32)
  • 由浅入深Netty基础知识NIO网络编程

    阻塞模式下,相关方法都会导致线程暂停 ServerSocketChannel.accept 会在没有连接建立时让线程暂停 SocketChannel.read 会在没有数据可读时让线程暂停 阻塞的表现其实就是线程暂停了,暂停期间不会占用 cpu,但线程相当于闲置 单线程下,阻塞方法之间相互影响,几乎不能正常工作,

    2024年02月05日
    浏览(43)
  • Java网络编程----通过实现简易聊天工具来聊聊NIO

    前文我们说过了BIO,今天我们聊聊NIO。 NIO 是什么?NIO官方解释它为 New lO ,由于其特性我们也称之为,Non-Blocking IO。这是jdk1.4之后新增的一套IO标准。 为什么要用NIO呢? 我们再简单回顾下BIO: 阻塞式IO,原理很简单,其实就是多个端点与服务端进行通信时,每个客户端有一个

    2024年02月05日
    浏览(48)
  • Linux系统应用编程(五)Linux网络编程(上篇)

    1.两个网络模型和常见协议 (1)OSI七层模型(物数网传会表应) 物理层、数据链路层、网络层、传输层、会话层、表示层、应用层(自下到上) (2)TCP/IP四层模型(网网传应) 网络接口层(链路层)、网络层、传输层、应用层 (3)常见网络协议所属层 2.字节序 (1)两种

    2023年04月25日
    浏览(24)
  • 【Netty专题】【网络编程】从OSI、TCP/IP网络模型开始到BIO、NIO(Netty前置知识)

    我是有点怕网络编程的,总有点【谈网色变】的感觉。为了让自己不再【谈网色变】,所以我想过系统学习一下,然后再做个笔记这样,加深一下理解。但是真要系统学习,其实还是要花费不少时间的,所以这里也只是简单的,尽可能地覆盖一下,梳理一些我认为比较迫切需

    2024年02月06日
    浏览(48)
  • 网络编程代码实例:IO复用版

    网络编程代码实例:IO复用版。 yezhening/Environment-and-network-programming-examples: 环境和网络编程实例 (github.com) Environment-and-network-programming-examples: 环境和网络编程实例 (gitee.com) 使用传输控制协议(TCP) 服务端多进程,一个服务端可连接多个客户端 用户在客户端终端输入,可多次

    2024年02月02日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包