栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java NIO 聊天室案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java NIO 聊天室案例

服务端:

package javaniodemo.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

public class ChatServer {

    //服务器启动
    public void startServer() throws IOException {
        // 创建selector选择器
        Selector selector = Selector.open();
        // 创建serversocketchannel通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 为channel绑定监听端口
        serverSocketChannel.bind(new InetSocketAddress(8777));
        // 设置通道为非阻塞模式
        serverSocketChannel.configureBlocking(false);
        // 把channel注册到选择器
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("server started successfully !");
        // 循环,等待是否有新链接接入
        for (;;) {
            // 获取channel数量
            int readChannels = selector.select();
            if (readChannels == 0) {
                continue;
            }
            // 获取可用的channel
            Set selectionKeys = selector.selectedKeys();
            // 遍历集合
            Iterator iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                // 移除掉set集合当前selectionKey
                iterator.remove();
                // 根据就绪状态调用对应方法实现具体业务操作
                // 如果accept状态
                if (selectionKey.isAcceptable()) {
                    acceptOperator(serverSocketChannel,selector);
                }
                // 如果可读状态
                if (selectionKey.isReadable()) {
                    readOperator(selector,selectionKey);
                }
            }
        }
    }

    // 处理可读状态操作
    private void readOperator(Selector selector, SelectionKey selectionKey) throws IOException {
        // 从SelectionKey获取到已经就绪的channel
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
        // 创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 循环读取客户端消息
        int readLength = socketChannel.read(byteBuffer);
        String message = "";
        if (readLength > 0) {
            // 切换成读模式
            byteBuffer.flip();
            // 读取内容
            message += Charset.forName("UTF-8").decode(byteBuffer);

        }
        // 将channel再次注册到选择器上
        socketChannel.register(selector,SelectionKey.OP_READ);
        // 把客户端发送的消息,广播到其他客户端
        if (message.length() > 0) {
            System.out.println(message);
            castOtherClients(message,selector,socketChannel);
        }
    }

    // 广播给其他客户端
    private void castOtherClients(String message, Selector selector, SocketChannel socketChannel) throws IOException {
        // 获取所有接入的客户端
        Set selectionKeySet = selector.keys();
        // 循环向所有channel广播消息
        for (SelectionKey selectionKey : selectionKeySet) {
            // 获取每个channel
            Channel targetChannel = selectionKey.channel();
            // 不需要给自己发送
            if (targetChannel instanceof SocketChannel && targetChannel != socketChannel) {
                ((SocketChannel) targetChannel).write(Charset.forName("UTF-8").encode(message));
            }
        }

    }

    // 处理接入状态操作
    private void acceptOperator(ServerSocketChannel serverSocketChannel, Selector selector) throws IOException {
        // 创建socketchannel
        SocketChannel socketChannel = serverSocketChannel.accept();
        // 把socketchannel设置成非阻塞模式
        socketChannel.configureBlocking(false);
        // 把channel注册到selector选择器上,监听可读状态
        socketChannel.register(selector,SelectionKey.OP_READ);
        // 给客户端回复信息
        socketChannel.write(Charset.forName("UTF-8")
                .encode("welcome join this chat room ! Please put your attention on your privacy !"));

    }


    public static void main(String[] args) {
        try {
            new ChatServer().startServer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

客户端:

package javaniodemo.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

public class ChatClient {


    // 启动客户端方法
    public void startClient(String name) throws IOException {
        // 连接服务器端
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8777));

        // 接收服务端响应数据
        Selector selector = Selector.open();
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);

        // 创建线程
        new Thread(new ClientThread(selector)).start();

        // 向服务器端发送消息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String msg = scanner.nextLine();
            if (msg.length() > 0) {
                socketChannel.write(Charset.forName("UTF-8").encode(name + ":" + msg));
            }
        }
        // 接收服务器端响应的消息
    }

}

package javaniodemo.client;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

public class ClientThread implements Runnable {

    private Selector selector;

    public ClientThread(Selector selector) {
        this.selector = selector;
    }

    @Override
    public void run() {

        try {
            // 循环,等待是否有新链接接入
            for (;;) {
                // 获取channel数量
                int readChannels = selector.select();
                if (readChannels == 0) {
                    continue;
                }
                // 获取可用的channel
                Set selectionKeys = selector.selectedKeys();
                // 遍历集合
                Iterator iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    // 移除掉set集合当前selectionKey
                    iterator.remove();
                    // 根据就绪状态调用对应方法实现具体业务操作
                    // 如果可读状态
                    if (selectionKey.isReadable()) {
                        readOperator(selector,selectionKey);
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    // 处理可读状态操作
    private void readOperator(Selector selector, SelectionKey selectionKey) throws IOException {
        // 从SelectionKey获取到已经就绪的channel
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
        // 创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 循环读取客户端消息
        int readLength = socketChannel.read(byteBuffer);
        String message = "";
        if (readLength > 0) {
            // 切换成读模式
            byteBuffer.flip();
            // 读取内容
            message += Charset.forName("UTF-8").decode(byteBuffer);

        }
        // 将channel再次注册到选择器上
        socketChannel.register(selector,SelectionKey.OP_READ);
        // 把客户端发送的消息,广播到其他客户端
        if (message.length() > 0) {
            System.out.println(message);
        }
    }
}

package javaniodemo.client;

import java.io.IOException;

public class AClient {
    public static void main(String[] args) {
        try {
            new ChatClient().startClient("mary");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package javaniodemo.client;

import java.io.IOException;

public class BClient {

    public static void main(String[] args) {
        try {
            new ChatClient().startClient("lucy");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号