本文实例为大家分享了java nio实现简单聊天程序的具体代码,供大家参考,具体内容如下
功能:
1、接受客户端连接
2、发送消息
3、读取客户端消息
rver.java
public class rver { private lector lector; private bytebuffer writebuffer = bytebuffer.allocate(1024); private bytebuffer readbuffer = bytebuffer.allocate(1024); msg msg = new msg(); msgnder msgnder = new msgnder(msg); public static void main(string[] args) { rver rver = new rver(); new thread(rver.msgnder).start(); rver.start(); } //初始化服务端 public rver() { try { this.lector = lector.open(); rversocketchannel ssc = rversocketchannel.open(); ssc.configureblocking(fal); ssc.bind(new inetsocketaddress(8899)); ssc.register(this.lector, lectionkey.op_accept); system.out.println("服务端初始化完毕。。。。"); } catch (ioexception e) { e.printstacktrace(); } } //启动服务端等待lector事件 public void start() { while (true) { try { int num = this.lector.lect(); if (num > 0) { iterator<lectionkey> it = this.lector.lectedkeys().iterator(); while (it.hasnext()) { lectionkey 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(lectionkey key) { socketchannel sc = (socketchannel) key.channel(); try { sc.configureblocking(fal); } catch (ioexception e) { e.printstacktrace(); } //判断是否有消息需要发送 if(msg!=null && msg.getflag()){ system.out.println(msg); try { msg.tflag(fal); writebuffer.clear(); writebuffer.put(msg.getcontent().getbytes()); writebuffer.flip(); sc.write(writebuffer); } catch (ioexception e) { e.printstacktrace(); } } } //读取客户端消息 private void read(lectionkey key) { socketchannel sc = (socketchannel) key.channel(); try { sc.configureblocking(fal); readbuffer.clear(); int read = sc.read(readbuffer); if (read == -1) { sc.clo(); key.cancel(); } readbuffer.flip(); system.out.println(new string(readbuffer.array())); } catch (ioexception e) { e.printstacktrace(); } } //接受客户端连接 private void accept(lectionkey key) { rversocketchannel ssc = (rversocketchannel) 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(fal); sc.register(this.lector, lectionkey.op_read | lectionkey.op_write); } catch (ioexception e) { e.printstacktrace(); } }}
msg.java
class msg { private boolean flag=fal; private string content; public boolean getflag() { return flag; } public void tflag(boolean flag) { this.flag = flag; } public string getcontent() { return content; } public void tcontent(string content) { this.content = content; } @override public string tostring() { return "msg{" + "flag=" + flag + ", content='" + content + '\'' + '}'; }}
msgnder.java
class msgnder implements runnable{ private msg msg; public msgnder(msg msg) { this.msg = msg; } @overr相对独立性ide public void run() { while (true) { system.out.println("input:\n"); scanner scanner = new scanner(system.in); this.msg.tcontent(scanner.next()); this.msg.tflag(true); } }}
采用的时bio,简单的使用线程实现消息的读写
功能:
1、连接服务端
2、读取消息
3、发送消息
public cla阙怎么读姓氏ss client { private socketchannel sc; bytebuffer writebuffer = bytebuffer.allocate(1024); bytebuffer readbuffer = bytebuf励志警句fer.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(); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-03 22:35:34,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/075762bc0781790988e1e2001c9b798a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java NIO实现简单聊天程序.doc
本文 PDF 下载地址:java NIO实现简单聊天程序.pdf
留言与评论(共有 0 条评论) |