java编程实现多人聊天室功能
程序员文章站
2024-02-16 22:47:52
本文实例为大家分享了java实现多人聊天室的具体代码,供大家参考,具体内容如下
程序源代码及运行截图:
server.java
//server.java...
本文实例为大家分享了java实现多人聊天室的具体代码,供大家参考,具体内容如下
程序源代码及运行截图:
server.java
//server.java package socket; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; import java.util.arraylist; import java.util.list; import java.util.scanner; public class server implements runnable {// 服务端 static list<socket> socketlist=new arraylist<socket>(); // 读取 in static socket socket = null; static serversocket serversocket = null; public server() {// 构造方法 try { serversocket = new serversocket(9999); } catch (ioexception e) { e.printstacktrace(); } } public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("************服务端*************"); server t = new server(); int count = 0; while (true) { try { // system.out.println("端口9999等待被连接......"); socket = serversocket.accept(); count++; system.out.println("第" + count + "个客户已连接"); socketlist.add(socket); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } print p = new print(socket); thread read = new thread(t); thread print = new thread(p); read.start(); print.start(); } } @override public void run() { // 重写run方法 try { thread.sleep(1000); bufferedreader in = new bufferedreader(new inputstreamreader(socket .getinputstream())); while (true) { string jieshou = in.readline(); system.out.println( jieshou); for (int i = 0; i < socketlist.size(); i++) { socket socket=socketlist.get(i); printwriter out = new printwriter(socket.getoutputstream()); if (socket!=this.socket) { out.println(jieshou); }else{ out.println("(你)"+jieshou); } out.flush(); } } } catch (exception e) { e.printstacktrace(); } } } class print implements runnable { static list<socket> socketlist=new arraylist<socket>(); scanner input = new scanner(system.in); public print(socket s) {// 构造方法 try { socketlist.add(s); } catch (exception e) { e.printstacktrace(); } } @override public void run() { try { thread.sleep(1000); while (true) { string msg = input.next(); for (int i = 0; i < socketlist.size(); i++) { socket socket=socketlist.get(i); printwriter out = new printwriter(socket.getoutputstream()); // system.out.println("对客户端说:"); out.println("服务端说:"+msg); out.flush(); } } } catch (exception e) { // todo: handle exception e.printstacktrace(); } } }
client.java
//client.java package socket; import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import java.util.scanner; public class client implements runnable {// 客户端 static socket socket = null; scanner input = new scanner(system.in); static string name=null; public static void main(string[] args) { int x=(int)(math.random()*100); client.name="client"+x; system.out.println("************客户端"+x+"*************"); try { socket = new socket("127.0.0.1", 9999); system.out.println("已经连上服务器了"); } catch (exception e) { e.printstacktrace(); } client t = new client(); read r = new read(socket); thread print = new thread(t); thread read = new thread(r); print.start(); read.start(); } @override public void run() { try { thread.sleep(1000); printwriter out = new printwriter(socket.getoutputstream()); while (true) { string msg = input.next(); out.println(name+"说:"+msg); out.flush(); } } catch (exception e) { e.printstacktrace(); } } } class read implements runnable { static socket socket = null; public read(socket socket) { this.socket = socket; } @override public void run() { try { thread.sleep(1000); bufferedreader in = new bufferedreader(new inputstreamreader(socket .getinputstream())); while (true) { system.out.println( in.readline()); } } catch (exception e) { e.printstacktrace(); } } }
测试数据一:
服务端程序运行截图:
客户端1程序运行截图:
客户端2程序运行截图:
由测试数据一可以看出:程序可以实现多人聊天,并且效果和性能还算可以。而且每个客户端连接进服务器时,都会随机产生一个随机数作为自身的标志,避免通话过程中,分辨不清彼此,而导致交流效果不好。
测试数据二:
服务端运行截图:
客户端1程序运行截图:
客户端2程序运行截图:
客户端3程序运行截图:
客户端4程序运行截图:
客户端5程序运行截图:
客户端6程序运行截图:
客户端7程序运行截图:
客户端8程序运行截图:
客户端9程序运行截图:
客户端10程序运行截图:
测试二一共连入了10个客户端进行程序稳定性的测试,程序运行结果稳定,符合实验预期结果,充分说明了程序的鲁棒性较强。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。