Java基於NIO實現聊天室功能

本文實例為大傢分享瞭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();
   
  }
  
  
 }

}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: