您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java NIO怎么實現聊天室功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
代碼里面已經包含了必要的注釋,這里不詳述了。實現了基本的聊天室功能。
常量類:
public class Constant { public static final int serverPort = 44444; }
服務端:
package server; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.Iterator; import java.util.Set; import constant.Constant; public class SocketServer { private Charset charset = Charset.forName("UTF-8"); private ServerSocketChannel serverSocketChannel; private Selector serverSocketSelector; private SelectionKey serverRegisterKey; private ByteBuffer buffer = ByteBuffer.allocate(1024); public static void main(String[] args) throws IOException { new SocketServer().openServer(new InetSocketAddress(Constant.serverPort)); } public void openServer(SocketAddress address) throws IOException { init(address); handle(); } private void init(SocketAddress address) throws IOException { serverSocketSelector = Selector.open(); serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverRegisterKey = serverSocketChannel.register(serverSocketSelector, SelectionKey.OP_ACCEPT); serverSocketChannel.socket().bind(address); } private void handle() throws IOException { System.out.println("服務端open"); while (serverSocketSelector.select() > 0) { Iterator<SelectionKey> iterator = serverSocketSelector.selectedKeys().iterator(); // 為什么這里要用迭代器,而不用增強for循環之類的呢?是因為這里獲得一個key之后,要對其進行移除,避免二次處理,造成影響 while (iterator.hasNext()) { dispatch(iterator.next()); iterator.remove(); } } } private void dispatch(SelectionKey key) throws IOException { if (key.isAcceptable()) { accept(key); } else if (key.isReadable()) { readMessage(key); } else if (key.isValid() && key.isWritable()) { writeMessage(key); } } private void accept(SelectionKey key) throws IOException, ClosedChannelException { // 主要的是,接收事件是發生在服務器這邊的,所以這邊的通道要強轉為ServerSocketChannel ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel client = server.accept(); client.configureBlocking(false); // 同時再給該通道注冊選擇器,監聽的內容的讀取 client.register(serverSocketSelector, SelectionKey.OP_READ); } private void readMessage(SelectionKey key) throws IOException { SocketChannel client = (SocketChannel) key.channel(); client.read(buffer); // 調整為讀取模式 buffer.flip(); String content = charset.decode(buffer).toString(); // 壓縮空間,即拋棄已經讀取的內容(實際上還在里面,只是處于等待被覆蓋狀態) buffer.compact(); // 這里可以根據業務邏輯,設置不設置都可以,但是這里想接受到消息后立馬回復一條消息,所以設置下一次感興趣的(監聽)事件為寫 key.interestOps(SelectionKey.OP_WRITE); // 設置系統回復信息 key.attach("系統已經收到你的消息\n"); // 開始廣播這個客戶端的內容到其他客戶端 broadcast(key, content); } private void broadcast(SelectionKey self, String content) throws IOException { Set<SelectionKey> selectedKeys = self.selector().keys(); for (SelectionKey key : selectedKeys) { // 不能發送給自己,也不要服務器自己本身對這個有反應 if (key != self && key != serverRegisterKey) { String oldMessage = (String) key.attach(null); // 如果有舊消息的話,在下一次發送時,連同舊消息一起發送 key.attach(oldMessage != null ? oldMessage + content : content); key.interestOps(key.interestOps() | SelectionKey.OP_WRITE); } } } private void writeMessage(SelectionKey key) throws IOException { SocketChannel client = (SocketChannel) key.channel(); // 獲取發給這個客戶端的消息,并清空消息 client.write(charset.encode((String) key.attach(null))); key.interestOps(SelectionKey.OP_READ); } }
客戶端(包含了Socket版本和SocketChannel版本):
package client; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.Scanner; import constant.Constant; public class SocketClient { public static void main(String[] args) throws IOException { nioVersion(); // ioVersion(); } private static void ioVersion() throws UnknownHostException, IOException { System.out.println("客戶端"); final Socket socket = new Socket(); socket.connect(new InetSocketAddress(Constant.serverPort)); new Thread() { @Override public void run() { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String line = scanner.nextLine(); try { socket.getOutputStream().write((line + "\n").getBytes("UTF-8")); } catch (IOException e) { e.printStackTrace(); } } scanner.close(); try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); new Thread() { @Override public void run() { try { Scanner scanner = new Scanner(socket.getInputStream(), "utf-8"); while (scanner.hasNext()) { String line = scanner.nextLine(); System.out.println("收到消息:" + line); } scanner.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } private static void nioVersion() throws IOException { Charset charset = Charset.forName("UTF-8"); System.out.println("客戶端"); SocketChannel socketChannel = SocketChannel.open(); // 設置為非阻塞模式 socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress(Constant.serverPort)); while (true) { if (socketChannel.finishConnect()) { new Thread() { @Override public void run() { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String input = scanner.nextLine(); try { socketChannel.write(charset.encode(input)); } catch (IOException e) { e.printStackTrace(); } } scanner.close(); } }.start(); new Thread() { ByteBuffer dst = ByteBuffer.allocate(1024); @Override public void run() { while (true) { try { int len = socketChannel.read(dst); if (len > 0) { dst.flip(); System.out.println("收到消息:" + charset.decode(dst)); dst.compact(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); return; } } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java NIO怎么實現聊天室功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。