NIO实践群聊Demo

1.服务端

/**
 * @author : zjx
 * @Description :
 * @date : Created in 9:18 2021/5/8
 * @Modified By:
 */
public class GroupChatServer {
    private final ServerSocketChannel serverSocketChannel;
    private final Selector selector;

    public GroupChatServer() throws IOException {
        this.serverSocketChannel = ServerSocketChannel.open();
        this.serverSocketChannel.bind(new InetSocketAddress(6666));
        this.serverSocketChannel.configureBlocking(false);
        this.selector = Selector.open();
        this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    }

    public static void main(String[] args) throws IOException {
        GroupChatServer groupChatServer = new GroupChatServer();
        groupChatServer.startServer();
    }

    @SuppressWarnings("InfiniteLoopStatement")
    public void startServer() throws IOException {
        while (true) {
            //阻塞,监听客户端事件
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            for (Iterator<SelectionKey> iterator = selectionKeys.iterator(); iterator.hasNext(); ) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    handleAcceptable();
                } else if (key.isReadable()) {
                    //把事件转发给其他客户端
                    SocketChannel self = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(512);
                    //读取数据
                    int read = getRead(key, self, buffer);
                    if(read>0){
                        //转发数据
                        forwardMessage(key,buffer);
                    }
                }
                //避免重复处理
                iterator.remove();
            }
        }
    }

    /**
     * 处理新的连接
     * @throws IOException  IOException
     */
    private void handleAcceptable() throws IOException {
        //获取新连接的客户端Socket
        SocketChannel socketChannel = serverSocketChannel.accept();
        //配置为非阻塞
        socketChannel.configureBlocking(false);
        //把客户端Socket 注册进selector 并且监听 读 事件,以及配置服务端缓存区
        socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(512));
        System.out.println(socketChannel.getRemoteAddress() + "客户端上线啦");
    }

    /**
     * 读取数据
     */
    private int getRead(SelectionKey key, SocketChannel self, ByteBuffer buffer) throws IOException {
        int read = 0;
        try {
            read = self.read(buffer);
            System.out.println("接收到客户端信息[" + new String(buffer.array()).trim() + "],转发给其他客户端");
        } catch (IOException e) {
            System.out.println(self.getRemoteAddress() + "客户端离线了");
            key.cancel();
            self.close();
        }
        return read;
    }

    /**
     * 转发信息给其他客户端
     */
    private void forwardMessage(SelectionKey key, ByteBuffer buffer) throws IOException {
        for (SelectionKey selectedKey : selector.keys()) {
            if (selectedKey != key && selectedKey.channel() instanceof SocketChannel) {
                System.out.println("开始转发");
                SocketChannel channel = (SocketChannel) selectedKey.channel();
                channel.write(ByteBuffer.wrap(buffer.array()));
            }
        }
    }

}

2.客户端

/**
 * @author : zjx
 * @Description :
 * @date : Created in 9:54 2021/5/8
 * @Modified By:
 */
public class GroupChatClient {

    private SocketChannel socketChannel;
    private Selector selector;

    public GroupChatClient() {
        try {
            socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 6666));
            socketChannel.configureBlocking(false);
            selector = Selector.open();
            socketChannel.register(selector, SelectionKey.OP_READ);
            System.out.println("客户端准备就绪");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("InfiniteLoopStatement")
    public static void main(String[] args) {
        GroupChatClient groupChatClient = new GroupChatClient();
        ExecutorService executorService = new ThreadPoolExecutor(1, 1, 3,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(5));
        executorService.execute(() -> {
            while (true) {
                try {
                    groupChatClient.readMessage();
                    Thread.sleep(1000);
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String next = scanner.next();
            groupChatClient.sendMessage(next);
        }
    }

    public void readMessage() throws IOException {
        selector.select();
        Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
        while (iterator.hasNext()) {
            SelectionKey key = iterator.next();
            if (key.isReadable()) {
                SocketChannel channel = (SocketChannel) key.channel();
                ByteBuffer buffer = ByteBuffer.allocate(512);
                channel.read(buffer);
                System.out.println(new String(buffer.array()).trim());
            }
            iterator.remove();
        }
    }

    public void sendMessage(String message) {
        try {
            socketChannel.write(ByteBuffer.wrap(message.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值