Java Socket實現聊天室功能
本文實例為大傢分享瞭Java Socket實現聊天室的具體代碼,供大傢參考,具體內容如下
1 創建登錄判斷類UserLogin
import java.util.HashSet; import java.util.Set; public class UserLogin { public static boolean login(String username) { Set<String> set = initUser(); // set中含有該username則登錄成功 return set.contains(username); } public static Set<String> initUser() { Set<String> set = new HashSet<>(); set.add("111"); set.add("222"); set.add("333"); set.add("444"); set.add("555"); return set; } }
2 創建登錄服務器LoginServer
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class LoginServer { public static void main(String[] args) { int port = 6666; try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("登錄服務器已經啟動"); while (true) { Socket socket = serverSocket.accept(); // 接收Client輸出的信息(username) InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String username = bufferedReader.readLine(); // 調用登錄方法,判斷是否成功登錄 OutputStream outputStream = socket.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); if (UserLogin.login(username)) { System.out.println(username + "登錄成功"); // 登錄成功輸出"true"到Client printStream.println("true"); } else { System.out.println(username + "登錄失敗"); // 登錄失敗則輸出"false"到Client printStream.println("false"); } printStream.flush(); } } catch (IOException e) { e.printStackTrace(); } } }
3 創建聊天服務器ChatServer
import com.socket.socketChatroom.bio.thread.ChatThread; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; public class ChatServer { // 保存當前聊天中的所有Map<username,Socket> public static Map<String, Socket> socketMap = new HashMap<>(); public static void main(String[] args) { int port = 9999; try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("聊天服務器已經啟動"); while (true) { Socket socket = serverSocket.accept(); // 接收Client輸出的username InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String username = bufferedReader.readLine(); System.out.println(username + "連接到聊天服務器"); // 連接聊天服務器成功則將它的<username,Socket>放入socketMap中 socketMap.put(username, socket); System.out.println(socketMap); // 因為可能有多個Client與ChatServer進行交互,所以一旦有Client連接成功就新創建一個線程與之交互 new ChatThread(username,socket).start(); } } catch (IOException e) { throw new RuntimeException(e); } } }
4 創建客戶端Client
import com.socket.socketChatroom.bio.thread.ReadThread; import com.socket.socketChatroom.bio.thread.WriteThread; import java.io.*; import java.net.Socket; import java.util.Scanner; public class Client { public static void main(String[] args) { // 服務端地址,端口號 String ip = "127.0.0.1";// 本機IP地址 int port = 6666; // 必須和LoginServer的端口一樣 try { Socket client = new Socket(ip, port); Scanner scanner = new Scanner(System.in); System.out.println("請輸入用戶名"); String username = scanner.nextLine(); // 將用戶名輸出到LoginServer OutputStream outputStream = client.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); printStream.println(username); printStream.flush(); // 接收LoginServer輸出的是否登錄成功標志("true"或"false") InputStream inputStream = client.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String state = bufferedReader.readLine(); // 如果為"true",則為登錄成功,可以開始聊天 if (Boolean.parseBoolean(state)) { chat(username); } } catch (IOException e) { e.printStackTrace(); } } public static void chat(String username) { try { String ip = "127.0.0.1";// 本機IP地址 int port = 9999;// 必須和ChatServer的端口一樣 // 連接聊天服務器ChatServer Socket socket = new Socket(ip, port); // 將username輸出到ChatServer OutputStream outputStream = socket.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); printStream.println(username); printStream.flush(); // 因為聊天的收發消息不一定是與同一個人,所以創建兩個線程分別用於收和發 new ReadThread(socket).start(); new WriteThread(socket).start(); } catch (IOException e) { throw new RuntimeException(e); } } }
5 創建服務器用於處理聊天的線程類ChatThread
import com.socket.socketChatroom.bio.server.ChatServer; import java.io.*; import java.net.Socket; import java.util.HashSet; import java.util.Map; import java.util.Set; public class ChatThread extends Thread { private final String username; private final Socket socket; public ChatThread(String username, Socket socket) { this.username = username; this.socket = socket; } @Override public void run() { while (true) { try { // 接收Client的WriteThread線程輸出的消息 InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String message = bufferedReader.readLine(); System.out.println(username + "說: " + message); // 將當前所有在線的Client的username存入usernameSet中 Map<String, Socket> socketMap = ChatServer.socketMap; Set<String> usernameSet = new HashSet<>(); for (Map.Entry<String, Socket> map : socketMap.entrySet()) { usernameSet.add(map.getKey()); } // 以"-"作為標識符,判斷是群聊還是單聊 String[] values = message.split("-"); if (values.length == 2 && usernameSet.contains(values[0])) { //單聊 // values[0]為要指定發送的Client的username OutputStream outputStream = socketMap.get(values[0]).getOutputStream(); PrintStream printStream = new PrintStream(outputStream); // values[1]為消息內容 printStream.println(username + "說: " + values[1]); printStream.flush(); } else {//群聊 // 將消息發送到除自己之外的所有Client for (String username : usernameSet) { if (socket != socketMap.get(username)) { OutputStream outputStream = socketMap.get(username).getOutputStream(); PrintStream printStream = new PrintStream(outputStream); printStream.println(this.username + "說: " + message); printStream.flush(); } } } } catch (IOException e) { throw new RuntimeException(e); } } } }
6 創建客戶端Client用於發送消息的線程類WriteThread
import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner; public class WriteThread extends Thread { private final Socket socket; public WriteThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { // 獲取輸出流 OutputStream outputStream = socket.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); // 鍵盤輸入要發送的消息,若為單聊,則以要發送的Client的username+"-",開頭,比如"111-hello",表示向111這個Client發送"hello" Scanner scanner = new Scanner(System.in); System.out.println("請輸入聊天內容: "); String message = scanner.nextLine(); // 輸出消息到ChatServer printStream.println(message); printStream.flush(); } catch (IOException e) { throw new RuntimeException(e); } } } }
7 創建客戶端Client用於接收消息的線程類ReadThread
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class ReadThread extends Thread { private final Socket socket; public ReadThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { // 從ChatServer獲取消息 InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String message = bufferedReader.readLine(); System.out.println(message); } catch (IOException e) { throw new RuntimeException(e); } } } }
項目結構如下圖
8 測試
先運行LoginServer和ChatServer,再運行多個Client
設置IDEA允許運行多個實例
8.1 登錄
8.2 群聊
8.3 單聊
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。