本文实例为大家分享了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个客户端进行程序稳定性的测试,程序运行结果稳定,符合实验预期结果,充分说明了程序的鲁棒性较强。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程(cainiaojc.com)。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。