您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java Socket怎樣實現(xiàn)簡易聊天室,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Java-Socket編程實現(xiàn)簡易聊天室(TCP),具體內(nèi)容如下
實現(xiàn)一個服務(wù)器接收多個客戶端
測試:
首先啟動服務(wù)器,然后啟動三個客戶端,輸入三個不同的用戶名,分別在聊天室發(fā)消息
看其他客戶端是否能接收到消息
效果如下圖:
一號發(fā)消息
可以在二號和三號客戶端接收到消息
服務(wù)器設(shè)計
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.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Server1 { static public List<Client> list = new ArrayList<Client>(); static ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); public static void main(String[] args) { //創(chuàng)建一個ServerSocket監(jiān)聽端口 try (ServerSocket server = new ServerSocket(5678)) { while (true) { // 利用死循環(huán)不停的監(jiān)聽端口 Socket s = server.accept();// 利用Socket服務(wù)器的accept()方法獲取客戶端Socket對象。 addClient(s); } } catch (IOException e) { e.printStackTrace(); } } // 添加客戶端 private static void addClient(Socket s) { String name; try { BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); name = in.readLine(); Client c = new Client(name, s);// 創(chuàng)建客戶端處理線程對象 System.out.println(name); list.add(c); cachedThreadPool.execute(c); } catch (IOException e) { e.printStackTrace(); } } static class Client implements Runnable { String name; // 客戶端名字 Socket s = null; // 保存客戶端Socket對象 BufferedReader in; PrintWriter out; Client(String name, Socket s) { this.s = s; this.name = name; try { in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(s.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { try { while (true) { String str = in.readLine(); for (int j = 0; j < list.size(); j++) { Server1.Client c = list.get(j); if (c != this) { System.out.println(str); c.send(str+"-|1|2|-"+name); } } if (str.equals("end")) break; } try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public void send(String str) throws IOException { out.println(str); out.flush(); } } }
客戶端設(shè)計
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class Client1 { public static String name = null; static Socket socket = null; static BufferedReader in; static PrintWriter out; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("輸入用戶名:>"); name = sc.nextLine(); try { // socket = new Socket("116.62.173.212", 5678); socket = new Socket("127.0.0.1", 5678); System.out.println(socket); System.out.println("已連接服務(wù)器>"); out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); Thread receive = new Thread(new receiveThread()); receive.start(); out.println(name); out.flush(); String msg; while (true) { // System.out.print("你說:>"); msg = sc.nextLine(); out.println(msg); out.flush(); if (msg.equalsIgnoreCase("end")) break; } } catch (IOException e) { e.printStackTrace(); } } static class receiveThread implements Runnable{ @Override public void run() { try { while (socket!=null) { String str = in.readLine(); String message = str.split("-\\|1\\|2\\|-")[0]; String name = str.split("-\\|1\\|2\\|-")[1]; System.out.println(name + ":>" + message); } }catch(Exception e) { e.printStackTrace(); } } } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java Socket怎樣實現(xiàn)簡易聊天室”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。