這篇文章將為大家詳細(xì)講解有關(guān)使用java怎么實現(xiàn)一個多人聊天室功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
目前成都創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、北關(guān)網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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 {// 服務(wù)端 static List<Socket> socketList=new ArrayList<Socket>(); // 讀取 In static Socket socket = null; static ServerSocket serverSocket = null; public server() {// 構(gòu)造方法 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("************服務(wù)端*************"); 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) {// 構(gòu)造方法 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("服務(wù)端說:"+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("已經(jīng)連上服務(wù)器了"); } 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(); } } }
測試數(shù)據(jù)一:
服務(wù)端程序運行截圖:
客戶端1程序運行截圖:
客戶端2程序運行截圖:
由測試數(shù)據(jù)一可以看出:程序可以實現(xiàn)多人聊天,并且效果和性能還算可以。而且每個客戶端連接進(jìn)服務(wù)器時,都會隨機產(chǎn)生一個隨機數(shù)作為自身的標(biāo)志,避免通話過程中,分辨不清彼此,而導(dǎo)致交流效果不好。
測試數(shù)據(jù)二:
服務(wù)端運行截圖:
客戶端1程序運行截圖:
客戶端2程序運行截圖:
客戶端3程序運行截圖:
客戶端4程序運行截圖:
客戶端5程序運行截圖:
客戶端6程序運行截圖:
客戶端7程序運行截圖:
客戶端8程序運行截圖:
客戶端9程序運行截圖:
客戶端10程序運行截圖:
Java中的集合主要分為四類:1、List列表:有序的,可重復(fù)的;2、Queue隊列:有序,可重復(fù)的;3、Set集合:不可重復(fù);4、Map映射:無序,鍵唯一,值不唯一。
關(guān)于使用java怎么實現(xiàn)一個多人聊天室功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站欄目:使用java怎么實現(xiàn)一個多人聊天室功能
網(wǎng)站路徑:http://aaarwkj.com/article12/gdscgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、、軟件開發(fā)、服務(wù)器托管、用戶體驗、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)