Java基于NIO實(shí)現(xiàn)聊天室功能
本文實(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ù)端啟動(dòng)---"); try { // 獲取通道 ServerSocketChannel channel = ServerSocketChannel.open(); // 配置非阻塞模式 channel.configureBlocking(false); // 綁定連接的端口 channel.bind(new InetSocketAddress(9999)); // 獲取選擇器 Selector selector = Selector.open(); // 注冊(cè)通道到選擇器上,開(kāi)始監(jiān)聽(tīng)事件 channel.register(selector, SelectionKey.OP_ACCEPT); // 使用選擇器進(jìn)行輪詢(xún) while (selector.select() > 0) { // 獲取到選擇器上所有注冊(cè)的通道中已經(jīng)就緒好的事件 Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { // 獲取事件 SelectionKey next = iterator.next(); // 判斷事件類(lèi)型 if (next.isAcceptable()) { // 獲取通道 accept = channel.accept(); // 獲取當(dāng)前連接分配地址 SocketAddress address = accept.getLocalAddress(); System.out.println(address + "上線了"); // 切換模式 accept.configureBlocking(false); // 將通道注冊(cè)到選擇器上 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) { // 開(kāi)啟讀模式 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(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java?NIO實(shí)現(xiàn)多人聊天室
- Java?NIO實(shí)現(xiàn)聊天室功能
- Java實(shí)現(xiàn)NIO聊天室的示例代碼(群聊+私聊)
- java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)
- Java NIO實(shí)戰(zhàn)之聊天室功能詳解
- Java NIO Selector用法詳解【含多人聊天室實(shí)例】
- 使用Java和WebSocket實(shí)現(xiàn)網(wǎng)頁(yè)聊天室實(shí)例代碼
- java聊天室的實(shí)現(xiàn)代碼
- Java基于socket實(shí)現(xiàn)簡(jiǎn)易聊天室實(shí)例
- JAVA NIO實(shí)現(xiàn)簡(jiǎn)單聊天室功能
相關(guān)文章
springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn)
這篇文章主要介紹了springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02解決mapper.xml中resultType映射類(lèi)型的問(wèn)題
這篇文章主要介紹了解決mapper.xml中resultType映射類(lèi)型的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享
在最近的工作開(kāi)發(fā)之中,慢慢習(xí)慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個(gè)流,這篇文章主要給大家介紹了關(guān)于Java8中List轉(zhuǎn)Map(Collectors.toMap) 的相關(guān)資料,需要的朋友可以參考下2021-07-07idea打開(kāi)運(yùn)行配置java?web項(xiàng)目的全過(guò)程
這篇文章主要給大家介紹了關(guān)于idea打開(kāi)運(yùn)行配置java?web項(xiàng)目的相關(guān)資料,有些時(shí)候我們用IDEA跑之前用eclipse中運(yùn)行的項(xiàng)目的時(shí)候,總是不止所措,要不就是只展示html,要不就是不能部署成功,需要的朋友可以參考下2023-08-08解析Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能的方法詳解
本篇文章是對(duì)Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05JAVA中使用FTPClient實(shí)現(xiàn)文件上傳下載實(shí)例代碼
本文給大家介紹如何利用jakarta commons中的FTPClient(在commons-net包中)實(shí)現(xiàn)上傳下載文件。非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06簡(jiǎn)單談?wù)凧ava中String類(lèi)型的參數(shù)傳遞問(wèn)題
這篇文章主要介紹了簡(jiǎn)單談?wù)凧ava中String類(lèi)型的參數(shù)傳遞問(wèn)題的相關(guān)資料,需要的朋友可以參考下2015-12-12Spring Boot開(kāi)發(fā)Web應(yīng)用詳解
這篇文章主要介紹了Spring Boot開(kāi)發(fā)Web應(yīng)用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04