本篇内容主要讲解“怎么使用Java NIO实现多人聊天室”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Java Nio实现多人聊天室”吧!NIO服务端public&n
本篇内容主要讲解“怎么使用Java NIO实现多人聊天室”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Java Nio实现多人聊天室”吧!
public class NiOServer { public void start() throws IOException { Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(8000)); serverSocketChannel.configureBlocking(false); serverSocketChannel.reGISter(selector, SelectionKey.OP_ACCEPT); System.out.println("服务器启动成功!"); for (;;) { // while(true) c for;; int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = (SelectionKey) iterator.next(); iterator.remove(); if (selectionKey.isAcceptable()) { acceptHandler(serverSocketChannel, selector); } if (selectionKey.isReadable()) { readHandler(selectionKey, selector); } } } } private void acceptHandler(ServerSocketChannel serverSocketChannel, Selector selector) throws IOException { SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); socketChannel.write(Charset.forName("UTF-8") .encode("你与聊天室里其他人都不是朋友关系,请注意隐私安全")); } private void readHandler(SelectionKey selectionKey, Selector selector) throws IOException { SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); String request = ""; while (socketChannel.read(byteBuffer) > 0) { byteBuffer.flip(); request += Charset.forName("UTF-8").decode(byteBuffer); } socketChannel.register(selector, SelectionKey.OP_READ); if (request.length() > 0) { // 广播给其他客户端 broadCast(selector, socketChannel, request); } } private void broadCast(Selector selector, SocketChannel sourceChannel, String request) { Set<SelectionKey> selectionKeySet = selector.keys(); selectionKeySet.forEach(selectionKey -> { Channel targetChannel = selectionKey.channel(); // 剔除发消息的客户端 if (targetChannel instanceof SocketChannel && targetChannel != sourceChannel) { try { // 将信息发送到targetChannel客户端 ((SocketChannel) targetChannel).write( Charset.forName("UTF-8").encode(request)); } catch (IOException e) { e.printStackTrace(); } } }); } public static void main(String[] args) throws IOException { new NioServer().start(); }}
public class Nioclient { public void start(String nickname) throws IOException { SocketChannel socketChannel = SocketChannel.open( new InetSocketAddress("127.0.0.1", 8000)); // 新开线程,专门负责来接收服务器端的响应数据 // selector , socketChannel , 注册 Selector selector = Selector.open(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); new Thread(new NioClientHandler(selector)).start(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String request = scanner.nextLine(); if (request != null && request.length() > 0) { socketChannel.write( Charset.forName("UTF-8") .encode(nickname + " : " + request)); } } } public static void main(String[] args) throws IOException {// new NioClient().start(); }}
客户端线程,处理服务器端响应的的消息
public class NioClientHandler implements Runnable { private Selector selector; public NioClientHandler(Selector selector) { this.selector = selector; } @Override public void run() { try { for (;;) { int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = (SelectionKey) iterator.next(); iterator.remove(); if (selectionKey.isReadable()) { readHandler(selectionKey, selector); } } } } catch (IOException e) { e.printStackTrace(); } } private void readHandler(SelectionKey selectionKey, Selector selector) throws IOException { SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); String response = ""; while (socketChannel.read(byteBuffer) > 0) { byteBuffer.flip(); response += Charset.forName("UTF-8").decode(byteBuffer); } socketChannel.register(selector, SelectionKey.OP_READ); if (response.length() > 0) { System.out.println(response); } }}
我们定义三个客户端,模拟三个用户在聊天室发送消息
public class AClient { public static void main(String[] args) throws IOException { new NioClient().start("AClient"); }}public class BClient { public static void main(String[] args) throws IOException { new NioClient().start("BClient"); }}public class CClient { public static void main(String[] args) throws IOException { new NioClient().start("CClient"); }}
到此,相信大家对“怎么使用Java NIO实现多人聊天室”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: 怎么使用Java NIO实现多人聊天室
本文链接: https://www.lsjlt.com/news/300421.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0