博客
关于我
JAVA-NIO实现聊天室详细代码说明
阅读量:301 次
发布时间:2019-03-01

本文共 5880 字,大约阅读时间需要 19 分钟。

Java群聊服务器与客户端实现

服务器端实现

以下是群聊服务器的核心实现代码,基于Java NIO的非阻塞IO模型。

public class GroupChatServer {    private Selector selector;    private ServerSocketChannel listenChannel;    private static final int PORT = 6666;    public GroupChatServer() {        try {            selector = Selector.open();            listenChannel = ServerSocketChannel.open();            listenChannel.socket().bind(new InetSocketAddress(PORT));            listenChannel.configureBlocking(false);            listenChannel.register(selector, SelectionKey.OP_ACCEPT);        } catch (IOException e) {            e.printStackTrace();        }    }    public void listen() {        try {            while (true) {                int count = selector.select(2000);                if (count > 0) {                    Iterator
iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); if (key.isAcceptable()) { SocketChannel socketChannel = listenChannel.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); System.out.println(socketChannel.getRemoteAddress() + " 客户端 上线"); } if (key.isReadable()) { readData(key); } iterator.remove(); } } } } catch (Exception e) { e.printStackTrace(); } finally { } } private void readData(SelectionKey key) { SocketChannel channel = null; try { channel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int count = channel.read(buffer); if (count > 0) { String msg = new String(buffer.array(), "GBK"); System.out.println("from 客户端:" + msg); sendInfoToOtherClients(msg, channel); } } catch (IOException e) { try { System.out.println(channel.getRemoteAddress() + " 离线了.."); key.cancel(); channel.close(); } catch (IOException ioException) { ioException.printStackTrace(); } } } private void sendInfoToOtherClients(String msg, SocketChannel self) throws IOException { System.out.println("服务器转发消息中..."); for (SelectionKey key : selector.keys()) { Channel targetChannel = key.channel(); if (targetChannel instanceof SocketChannel && targetChannel != self) { SocketChannel dest = (SocketChannel) targetChannel; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes("GBK")); dest.write(buffer); } } } public static void main(String[] args) { GroupChatServer chatServer = new GroupChatServer(); chatServer.listen(); }}

客户端实现

以下是客户端的实现代码,同样基于Java NIO的非阻塞IO模型。

public class GroupChatClient {    private final String HOST = "127.0.0.1";    private final int PORT = 6666;    private Selector selector;    private SocketChannel socketChannel;    private String username;    public GroupChatClient() {        try {            selector = Selector.open();            socketChannel = SocketChannel.open(new InetSocketAddress(HOST, PORT));            socketChannel.configureBlocking(false);            socketChannel.register(selector, SelectionKey.OP_READ);            username = socketChannel.getLocalAddress().toString().substring(1);            System.out.println(username + " is ok ...");        } catch (Exception e) {            e.printStackTrace();        }    }    public void sendInfo(String info) {        info = username + " 说:" + info;        try {            socketChannel.write(ByteBuffer.wrap(info.getBytes("GBK")));        } catch (IOException e) {            e.printStackTrace();        }    }    public void readInfo() {        try {            int readChannels = selector.select(2000);            if (readChannels > 0) {                Iterator
iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); if (key.isReadable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); socketChannel.read(buffer); String msg = new String(buffer.array()); System.out.println(msg.trim()); } iterator.remove(); } } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { GroupChatClient chatClient = new GroupChatClient(); new Thread(new Runnable() { @Override 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 -cp src/main/java com.JolyouLu.nio.GroupChatServer
    • 服务器将监听本地的6666端口。
  • 客户端连接

    • 创建客户端实例:java -cp src/main/java com.JolyouLu.nio.GroupChatClient
    • 客户端将自动连接到服务器,并显示用户名。
  • 使用说明

    • 客户端支持输入消息并发送,支持多线程读取服务器消息。
    • 消息格式为:用户名 说:消息内容
  • 注意事项

    • 服务器端使用非阻塞IO,性能较好,但需注意线程管理。
    • 客户端需处理网络连接异常情况,确保稳定性。
  • 转载地址:http://cyxo.baihongyu.com/

    你可能感兴趣的文章
    openlayers 入门教程(三):view 篇
    查看>>
    openlayers 入门教程(九):overlay 篇
    查看>>
    openlayers 入门教程(二):map 篇
    查看>>
    openlayers 入门教程(五):sources 篇
    查看>>
    openlayers 入门教程(八):Geoms 篇
    查看>>
    openlayers 入门教程(十三):动画
    查看>>
    openlayers 入门教程(十二):定位与轨迹
    查看>>
    openlayers 入门教程(十五):与 canvas、echart,turf 等交互
    查看>>
    openlayers 入门教程(十四):第三方插件
    查看>>
    openlayers 入门教程(四):layers 篇
    查看>>
    OpenLayers 项目分析(三)-OpenLayers中定制JavaScript内置类
    查看>>
    Openlayers中使用Cluster+Overlay实现点击单个要素和聚合要素时显示不同弹窗
    查看>>
    Openlayers中使用Cluster实现点位元素重合时动态聚合与取消聚合
    查看>>
    Openlayers中使用Cluster实现缩放地图时图层聚合与取消聚合
    查看>>
    Openlayers中使用Image的rotation实现车辆定位导航带转角(判断车辆图片旋转角度)
    查看>>
    Openlayers中加载Geoserver切割的EPSG:900913离线瓦片图层组
    查看>>
    Openlayers中多图层遮挡时调整图层上下顺序
    查看>>
    Openlayers中将某个feature置于最上层
    查看>>
    Openlayers中点击地图获取坐标并输出
    查看>>
    Openlayers中设置定时绘制和清理直线图层
    查看>>