您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java基于NIO如何實現群聊模式,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
Client
package com.qst.chat; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Scanner; public class GroupChatClient { private final int PORT = 9999; private final String HOST = "localhost"; private SocketChannel channel; private static Selector selector; private String name; public GroupChatClient() throws IOException { selector = Selector.open(); // 連接服務器 channel = SocketChannel.open(new InetSocketAddress(HOST, PORT)); // 設置非阻塞 channel.configureBlocking(false); // 將channel 注冊到selector channel.register(selector, SelectionKey.OP_READ); name = channel.getLocalAddress().toString().substring(1); System.out.println(name + "is ok ...."); } // 向服務器發送消息 public void sendTO(String msg) { ByteBuffer buffer = ByteBuffer.wrap((name+":"+msg).getBytes()); try { channel.write(buffer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 讀取從服務器端回復的消息 public static void getInfo() { try { if(selector.select() >0) { Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while(iterator.hasNext()) { SelectionKey key = iterator.next(); if(key.isReadable()) { // 得到通道 SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len; // 把讀到的緩沖區的數據轉成字符串 while((len = sc.read(buffer)) > 0) { System.out.println(new String(buffer.array())); } } } // 刪除當前的selectionKey, 防止重復操作 iterator.remove(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { try { GroupChatClient client = new GroupChatClient(); new Thread() { public void run() { while(true) { try { Thread.sleep(3000); GroupChatClient.getInfo(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); Scanner sc = new Scanner(System.in); // while(true) { // String name = sc.nextLine(); // client.sendTO(name); // } while(sc.hasNextLine()) { String s = sc.nextLine(); client.sendTO(s); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Server端
package com.qst.chat; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; 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.time.chrono.IsoChronology; import java.util.Iterator; import com.sun.accessibility.internal.resources.accessibility; import sun.print.resources.serviceui; public class GroupChatServer { private static ServerSocketChannel socketChannel; private static Socket socket; private static Selector selector; private static SocketChannel accept; public GroupChatServer() throws IOException { socketChannel = ServerSocketChannel.open(); selector = Selector.open(); // 綁定端口 socketChannel.socket().bind(new InetSocketAddress(9999)); // 設置非阻塞模式 socketChannel.configureBlocking(false); // 將該通道 注冊到selector socketChannel.register(selector, SelectionKey.OP_ACCEPT); } // 監聽 public static void listen() { System.out.println("監聽線程: " + Thread.currentThread().getName()); try { while (selector.select() > 0) { Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); if (iterator.hasNext()) { // 遍歷得到selectionKey 集合 SelectionKey next = iterator.next(); if (next.isAcceptable()) { next.channel(); // socketChannel = (ServerSocketChannel) next.channel(); SocketChannel accept = socketChannel.accept(); accept.configureBlocking(false); accept.register(selector, SelectionKey.OP_READ); System.out.println(accept.getRemoteAddress()+" 上線 了。。。"); } if (next.isReadable()) { readDate(next); } // 移除當前的next,防止重復處理 iterator.remove(); // System.out.println("未發現"); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 讀取客戶端消息 public static void readDate(SelectionKey key) { try { accept = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len = accept.read(buffer); if (len > 0) { buffer.flip(); String msg = new String(buffer.array()); System.out.println("user = " + msg); // 向其它的客戶端轉發消息(去掉自己) sendToAll(msg, accept); buffer.clear(); } } catch (IOException e) { // TODO Auto-generated catch block try { String msg = accept.getRemoteAddress().toString(); // 取消注冊 key.cancel(); // 關閉通道 accept.close(); System.out.println(msg + "離線了"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // e.printStackTrace(); } finally { // TODO: handle finally clause } } public static void sendToAll(String msg, SocketChannel ssc) { for (SelectionKey ss : selector.keys()) { // 通過 key 取出對應的 SocketChannel SelectableChannel channel = ss.channel(); // 排除自己 if (channel instanceof SocketChannel && channel != ssc) { // 轉型 SocketChannel sh = (SocketChannel) channel; // 轉存到buffer ByteBuffer wrap = ByteBuffer.wrap(msg.getBytes()); try { // 寫入通道 sh.write(wrap); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) throws IOException { GroupChatServer server = new GroupChatServer(); GroupChatServer.listen(); } }
key.isAcceptable()進行接入 操作的時候, 獲取通道有兩種方式
1、 通過selector獲取 (Selector key) socketChannel = (ServerSocketChannel) key.channel();
建立連接 socketChannel .accept();
2、定義一個全局變量
在進行初始化的時候,存儲(socketChannel = ServerSocketChannel.open();)
建立連接 socketChannel .accept();
key.isReadable() 當進行到讀入操作的時候( ) SelectionKey key accept = (SocketChannel) key.channel();
服務器啟動,客戶端啟動
客戶端發送消息
啟動第二個客戶端
兩個客戶端相互通信
離線信息顯示
關于“java基于NIO如何實現群聊模式”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。