您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。