欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基于NIO實(shí)現(xiàn)聊天室功能

 更新時(shí)間:2021年11月23日 16:32:14   作者:林夕$相心  
這篇文章主要為大家詳細(xì)介紹了Java基于NIO實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java基于NIO實(shí)現(xiàn)聊天室功能的具體代碼,供大家參考,具體內(nèi)容如下

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("----服務(wù)端啟動---");
  try {
   
   // 獲取通道
   ServerSocketChannel channel = ServerSocketChannel.open();
   
   
   // 配置非阻塞模式
   channel.configureBlocking(false);
   // 綁定連接的端口
   channel.bind(new InetSocketAddress(9999));
   
   // 獲取選擇器
   Selector selector = Selector.open();
   
   // 注冊通道到選擇器上,開始監(jiān)聽事件
   channel.register(selector, SelectionKey.OP_ACCEPT);
   // 使用選擇器進(jìn)行輪詢
   while (selector.select() > 0) {
    
    // 獲取到選擇器上所有注冊的通道中已經(jīng)就緒好的事件
    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();

    while (iterator.hasNext()) {
     // 獲取事件
     SelectionKey next = iterator.next();
     // 判斷事件類型
     if (next.isAcceptable()) {
      // 獲取通道
      accept = channel.accept();
      // 獲取當(dāng)前連接分配地址
      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();
   
  }
  
  
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論