这篇文章将为大家详细讲解有关使用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程序运行截图:
Java中的集合主要分为四类:1、List列表:有序的,可重复的;2、Queue队列:有序,可重复的;3、Set集合:不可重复;4、Map映射:无序,键唯一,值不唯一。
关于使用java怎么实现一个多人聊天室功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。