当前位置 : 主页 > 编程语言 > java >

Java基于NIO实现聊天室功能

来源:互联网 收集:自由互联 发布时间:2021-12-01
本文实例为大家分享了Java基于NIO实现聊天室功能的具体代码,供大家参考,具体内容如下 Sever端 package com.qst.one;import java.io.IOException;import java.net.InetSocketAddress;import java.net.SocketAddress;i

本文实例为大家分享了Java基于NIO实现聊天室功能的具体代码,供大家参考,具体内容如下

Sever端

package com.qst.one;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.Channel;
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.util.Iterator;
import java.util.Set;
import java.nio.ByteBuffer;



public class Server {
 
 private static SocketChannel accept;
 public static void main(String[] args) {
  
  
  System.out.println("----服务端启动---");
  try {
   
   // 获取通道
   ServerSocketChannel channel = ServerSocketChannel.open();
   
   
   // 配置非阻塞模式
   channel.configureBlocking(false);
   // 绑定连接的端口
   channel.bind(new InetSocketAddress(9999));
   
   // 获取选择器
   Selector selector = Selector.open();
   
   // 注册通道到选择器上,开始监听事件
   channel.register(selector, SelectionKey.OP_ACCEPT);
   // 使用选择器进行轮询
   while (selector.select() > 0) {
    
    // 获取到选择器上所有注册的通道中已经就绪好的事件
    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();

    while (iterator.hasNext()) {
     // 获取事件
     SelectionKey next = iterator.next();
     // 判断事件类型
     if (next.isAcceptable()) {
      // 获取通道
      accept = channel.accept();
      // 获取当前连接分配地址
      SocketAddress address = accept.getLocalAddress();
      System.out.println(address + "上线了");
      // 切换模式
      accept.configureBlocking(false);
      // 将通道注册到选择器上
      accept.register(selector, SelectionKey.OP_READ);
     }

     // 如果为读模式
     else if (next.isReadable()) {
      SocketChannel accept = (SocketChannel) next.channel();

      // 读取事件
      ByteBuffer buffer = ByteBuffer.allocate(1024);

      int len;
      while ((len = accept.read(buffer)) > 0) {
       // 开启读模式
       buffer.flip();
       //      System.out.println((char)len);
       System.out.println(new String(buffer.array(), 0, len));
       // 归位
       buffer.clear();
      }
     }
     iterator.remove();
    }

   } 
  } catch (Exception e) {
   try {
    SocketAddress address = accept.getRemoteAddress();
    System.out.println(address+"离线了");
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   
  }
  
 }

}

Client端

package com.qst.one;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import java.nio.ByteBuffer;


public class Client {
 
 
 public static void main(String[] args) throws IOException {
  SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", 9999));
  
  
  channel.configureBlocking(false);
  
  ByteBuffer buffer = ByteBuffer.allocate(1024);
  Scanner sc = new Scanner(System.in);
  SocketAddress address = channel.getLocalAddress();
  System.out.println(address+"ready~~~");
  
  while(true) {
   System.out.print("tim:");
   String name = sc.nextLine();
   
   buffer.put(("tim :"+name).getBytes());
   buffer.flip();
   channel.write(buffer);
   buffer.clear();
   
  }
  
  
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论