NIO即时通讯/聊天室DEMO

一、服务端

package com.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;


public class ChatServer {

    private ServerSocketChannel listenChannel;  //监听通道
    private Selector selector;  //选择器对象
    private static final int PORT = 9999;
    
    public ChatServer() {
        try {
            // 1得到监听通道
            listenChannel = ServerSocketChannel.open();
            // 2得到选择器
            selector = Selector.open();
            // 3绑定端口
            listenChannel.bind(new InetSocketAddress(PORT));
            // 4设置为非阻塞模式
            listenChannel.configureBlocking(false);
            // 5将选择器绑定到监听通道并监听accept事件
            listenChannel.register(selector, SelectionKey.OP_ACCEPT);
            printInfo("server ok ...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 6干活
    public void start() {
        try {
            while (true) {
                if (selector.select(3000) == 0) {
                    continue;
                }
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while(iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    if (key.isAcceptable()) {   // 连接请求事件
                        SocketChannel sc = listenChannel.accept();
                        sc.configureBlocking(false);
                        sc.register(selector, SelectionKey.OP_READ);
                        System.out.println(sc.getRemoteAddress().toString().substring(1) + "上线了...");
                    }
                    if (key.isReadable()) { // 读取数据事件
                        readMsg(key);
                    }
                    // 把keys删掉,防止重复处理
                    iterator.remove();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 读取到客户端发送过来的消息并广播出去
    public void readMsg(SelectionKey key) throws Exception {
        SocketChannel sc = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        if (sc.read(buffer) > 0) {
            // 打印接收到的消息
            String msg = new String(buffer.array());
            printInfo(msg.trim());
            // 发广播
            broadCast(sc, msg);
        }
    }
    
    public void broadCast(SocketChannel sc, String msg) throws Exception {
        printInfo("服务器发送了广播...");
        for (SelectionKey key : selector.keys()) {
            SelectableChannel channel;
            if ((channel = key.channel()) instanceof SocketChannel && key.channel() != sc) {
                ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
                ((SocketChannel) channel).write(buffer);
            }
            
        }
    }
    
    private void printInfo(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("["+sdf.format(new Date()) + "]->" + str);
    }
    
    public static void main(String[] args) {
        new ChatServer().start();
    }

}

二、客户端

package com.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class ChatClient {

    private final String HOST = "127.0.0.1";
    private int PORT = 9999;
    private SocketChannel socketChannel;
    private String userName;

    public ChatClient() {
        try {
            // 1得到一个网络通道
            socketChannel = SocketChannel.open();
            // 2设置非阻塞方式
            socketChannel.configureBlocking(false);
            // 3服务端IP和端口
            InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT);
            // 4连接服务器
            if (!socketChannel.connect(inetSocketAddress)) {
                while (!socketChannel.finishConnect()) {
                    System.out.println("客户端一直在连接.....");
                }
            }
            // 5得到客户端IP和端口,作为聊天用户名
            userName = socketChannel.getLocalAddress().toString().substring(1);
            System.out.println("-------------client: " + userName + " is ready-------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 向服务器发送数据
    public void sendMsg(String msg) throws Exception {
        if (msg.equalsIgnoreCase("bye")) {
            socketChannel.close();
            return;
        }
        msg = userName + "say: " + msg;
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        socketChannel.write(buffer);
    }

    // 从服务端接收数据
    public void receiveMsg() throws Exception {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        if (socketChannel.read(buffer) > 0) {
            String msg = new String(buffer.array());
            System.out.println(msg.trim());
        }
    }

}

三、测试客户端,启多个线程

package com.nio;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) throws Exception {
        ChatClient chatClient = new ChatClient();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        chatClient.receiveMsg();
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
            chatClient.sendMsg(scanner.nextLine());
        }

    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 5,249评论 2 54
  • Java知识点1、==和equals的区别基本类型比较==比较内容 equals比较地址值引用类型比较==比较地址...
    压抑的内心阅读 606评论 0 0
  • 必备的理论基础 1.操作系统作用: 隐藏丑陋复杂的硬件接口,提供良好的抽象接口。 管理调度进程,并将多个进程对硬件...
    drfung阅读 3,569评论 0 5
  • 文~慧眼 一次,两次,三次…… 向前再向前 跌倒,又爬起 或许 她只是想 走出这黑夜的困扰 穿过内心的迷茫 再次和...
    0c7746660850阅读 207评论 0 5
  • 大学放假回到家已经两周,可是家里最近发生的几件事,让我非常心寒,此时此刻的我宁愿一个人在外地的大学孤单过年,也好比...
    萌者雪儿阅读 2,470评论 1 0