java NIO怎么实现简单聊天程序
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要为大家展示了"java NIO怎么实现简单聊天程序",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"java NIO怎么实现简单聊天程序"这篇文章
千家信息网最后更新 2025年11月07日java NIO怎么实现简单聊天程序
这篇文章主要为大家展示了"java NIO怎么实现简单聊天程序",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"java NIO怎么实现简单聊天程序"这篇文章吧。
具体内容如下
服务端
功能:
1、接受客户端连接
2、发送消息
3、读取客户端消息
Server.java
public class Server { private Selector selector; private ByteBuffer writeBuffer = ByteBuffer.allocate(1024); private ByteBuffer readBuffer = ByteBuffer.allocate(1024); Msg msg = new Msg(); MsgSender msgSender = new MsgSender(msg); public static void main(String[] args) { Server server = new Server(); new Thread(server.msgSender).start(); server.start(); } //初始化服务端 public Server() { try { this.selector = Selector.open(); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ssc.bind(new InetSocketAddress(8899)); ssc.register(this.selector, SelectionKey.OP_ACCEPT); System.out.println("服务端初始化完毕。。。。"); } catch (IOException e) { e.printStackTrace(); } } //启动服务端等待selector事件 public void start() { while (true) { try { int num = this.selector.select(); if (num > 0) { Iterator it = this.selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = it.next(); it.remove(); if (key.isValid()) { if (key.isAcceptable()) { this.accept(key); } if (key.isReadable()) { this.read(key); } if (key.isWritable()) { this.write(key); } } } } } catch (IOException e) { e.printStackTrace(); } } } //发送消息 private void write(SelectionKey key) { SocketChannel sc = (SocketChannel) key.channel(); try { sc.configureBlocking(false); } catch (IOException e) { e.printStackTrace(); } //判断是否有消息需要发送 if(msg!=null && msg.getFlag()){ System.out.println(msg); try { msg.setFlag(false); writeBuffer.clear(); writeBuffer.put(msg.getContent().getBytes()); writeBuffer.flip(); sc.write(writeBuffer); } catch (IOException e) { e.printStackTrace(); } } } //读取客户端消息 private void read(SelectionKey key) { SocketChannel sc = (SocketChannel) key.channel(); try { sc.configureBlocking(false); readBuffer.clear(); int read = sc.read(readBuffer); if (read == -1) { sc.close(); key.cancel(); } readBuffer.flip(); System.out.println(new String(readBuffer.array())); } catch (IOException e) { e.printStackTrace(); } } //接受客户端连接 private void accept(SelectionKey key) { ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); try { SocketChannel sc = ssc.accept(); System.out.println(String.format("a new client join!!!host:%s;port:%d", sc.socket().getLocalAddress(), sc.socket().getPort())); sc.configureBlocking(false); sc.register(this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); } catch (IOException e) { e.printStackTrace(); } }} Msg.java
class Msg { private Boolean flag=false; private String content; public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Msg{" + "flag=" + flag + ", content='" + content + '\'' + '}'; }}MsgSender.java
class MsgSender implements Runnable{ private Msg msg; public MsgSender(Msg msg) { this.msg = msg; } @Override public void run() { while (true) { System.out.println("input:\n"); Scanner scanner = new Scanner(System.in); this.msg.setContent(scanner.next()); this.msg.setFlag(true); } }}客户端
采用的时BIO,简单的使用线程实现消息的读写
功能:
1、连接服务端
2、读取消息
3、发送消息
public class Client { private SocketChannel sc; ByteBuffer writeBuffer = ByteBuffer.allocate(1024); ByteBuffer readBuffer = ByteBuffer.allocate(1024); public static void main(String[] args) { new Client(); } public Client() { try { sc = SocketChannel.open(); //连接服务端 sc.connect(new InetSocketAddress(8899)); //发送消息 this.write(sc); //读取消息 this.read(sc); } catch (IOException e) { e.printStackTrace(); } } private void read(SocketChannel sc) { new Thread(new Runnable() { @Override public void run() { while (true) { try { readBuffer.clear(); int read = sc.read(readBuffer); readBuffer.flip(); System.out.println(new String(readBuffer.array())); } catch (IOException e) { e.printStackTrace(); } } } }).start(); } private void write(SocketChannel sc) { new Thread(new Runnable() { @Override public void run() { while (true) { Scanner scanner = new Scanner(System.in); String next = scanner.next(); writeBuffer.clear(); writeBuffer.put(next.getBytes()); writeBuffer.flip(); try { sc.write(writeBuffer); } catch (IOException e) { e.printStackTrace(); } } } }).start(); }}以上是"java NIO怎么实现简单聊天程序"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
消息
服务
客户
客户端
内容
程序
篇文章
功能
学习
帮助
事件
易懂
更多
条理
知识
线程
编带
行业
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广东数据链软件开发优化价格
dell服务器r730
数据库跨机房
股票助手软件开发
中华人民共和国网络安全法介绍
工商银行软件开发中心 成都
计算机网络技术中的net
网络安全违规行为举报邮箱是什么
tribon数据库备份
数据库货币类型currence
无线wifi网络安全
威海卓驰软件开发
普联无线路由器首选DNS服务器
党委网络安全工作责任何时实行
T640服务器怎样做raid
思迅软件数据库置疑
王牌战争如何分享服务器
空间数据库技术学院
问道手游版怎么改服务器
太原网络安全高峰论坛
seed数据库
kompass企业数据库
通信网络安全应急实训基地
vertica数据库哪家的
网络安全审查办法的执行日期
戴尔服务器性能计数器驱动程序
网络安全五个特点
T640服务器怎样做raid
大学生计算机网络技术投标书
太原网络安全高峰论坛