您好,登錄后才能下訂單哦!
本文實例為大家分享了Java SE實現多人聊天室功能的具體代碼,供大家參考,具體內容如下
實現功能:
1.實現用戶注冊上線,下線
2.實現群聊和私聊功能
3.實現統計當前在線人數
實現思路:
1.首先,要實現服務端與客戶端之間的連接
這里是使用套接字建立TCP連接:
(1)服務器端先實例化一個描述服務器端口號的ServerSocket對象
(2)客戶端要創建Socket對象來連接指定的服務器端
(3)服務器端調用ServerSocket類的accept()方法來監聽連接到服務器端的客戶端信息
(4)若服務器端與客戶端連接成功,雙方將返回一個Socket對象,此時雙方可以進行通信
(5)服務器端與客戶端使用I/O流進行連接,服務端的輸出流連接客戶端的輸入流,客戶端的輸出流連接服務端的輸入流
(6)使用close()方法關閉套接字(一定要記得關閉)
2.因為是擁有一個服務端來實現多個客戶端的連接,此處還要解決的是多線程的問題。
每個客戶端需要兩個線程,來分別處理向服務端發送消息和向服務端接收消息
而服務端,當每增加一個客戶端與服務端連接,服務端都要多創建一個線程來處理與客戶端的連接
具體代碼:
單線程實現:
客戶端
import java.io.IOException; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner; /** * @author LXY * @email 403824215@qq.com * @date 2018/7/20 15:46 */ //客戶端 public class SingleClient { public static void main(String[] args) throws IOException { //客戶端連接服務器,返回套接字Socket對象 Socket socket = new Socket("127.0.0.1",6666); //獲取服務端的輸出流,向服務器端輸出內容 PrintStream printStream = new PrintStream(socket.getOutputStream()); printStream.println("我是客戶端" + socket.getLocalPort()); //獲取服務器端的輸入流,讀取服務器端的內容 Scanner scanner = new Scanner(socket.getInputStream()); scanner.useDelimiter("\n"); if(scanner.hasNext()) { System.out.println(scanner.next()); } //關閉流 socket.close(); } }
服務端
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * @author LXY * @email 403824215@qq.com * @date 2018/7/20 15:34 */ //服務端 public class SingleServer { public static void main(String[] args) throws IOException { //創建服務器端的ServerSocket對象,等待客戶端進行連接 ServerSocket serverSocket = new ServerSocket(6666); System.out.println("服務器的端口號為6666,等待客戶端連接。。。"); //偵聽并接收服務器端的連接,返回套接字Socket對象 Socket socket = serverSocket.accept(); //獲取客戶端的輸入流,讀取客戶端的輸入內容 Scanner scanner = new Scanner(socket.getInputStream()); scanner.useDelimiter("\n"); if(scanner.hasNext()) { System.out.println("客戶端發來消息:" + scanner.next()); } //獲取客戶端的輸出流,向客戶端輸出內容 PrintStream printStream = new PrintStream(socket.getOutputStream()); printStream.println("客戶端你好,我是服務器端:" + serverSocket.getLocalPort()); //關閉流 serverSocket.close(); } }
多線程實現
客戶端
import java.io.IOException; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner; /** * @author LXY * @email 403824215@qq.com * @date 2018/7/20 15:55 */ //客戶端 //客戶端讀取服務器端信息的線程 class ClientReadServer implements Runnable { private Socket socket; public ClientReadServer(Socket socket) { this.socket = socket; } public void run() { try { Scanner scanner = new Scanner(socket.getInputStream()); while (scanner.hasNext()) { System.out.println(scanner.next()); } socket.close(); } catch (IOException e) { e.printStackTrace(); } } } //客戶端向服務端發送信息的線程 class ClientSendServer implements Runnable { private Socket socket; public ClientSendServer(Socket socket) { this.socket = socket; } public void run() { try { PrintStream printStream = new PrintStream(socket.getOutputStream()); Scanner scanner = new Scanner(System.in); while (true) { String msg = null; if(scanner.hasNext()) { msg = scanner.next(); printStream.println(msg); } if(msg.equals("bye")) { scanner.close(); printStream.close(); break; } } } catch (IOException e) { e.printStackTrace(); } } } public class MultiClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1",6666); Thread read = new Thread(new ClientReadServer(socket)); Thread send = new Thread(new ClientSendServer(socket)); read.start(); send.start(); } }
服務端
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author LXY * @email 403824215@qq.com * @date 2018/7/20 16:12 */ class Server implements Runnable { private static Map<String,Socket> map = new ConcurrentHashMap<String, Socket>(); private Socket socket; public Server(Socket socket) { this.socket = socket; } public void run() { try { Scanner scanner = new Scanner(socket.getInputStream()); String msg = null; while (true) { if(scanner.hasNextLine()) { msg = scanner.nextLine(); Pattern pattern = Pattern.compile("\r"); Matcher matcher = pattern.matcher(msg); msg = matcher.replaceAll(""); //用戶注冊——格式:userName:用戶名 if(msg.startsWith("userName:")) { String userName = msg.split("\\:")[1]; userRegist(userName,socket); continue; } //群聊——格式:G:群聊信息 else if(msg.startsWith("G:")) { firstStep(socket); String str = msg.split("\\:")[1]; groupChat(socket,str); continue; } else if(msg.startsWith("P:") && msg.contains("-")) { firstStep(socket); String userName = msg.split("\\:")[1].split("-")[0]; String str = msg.split("\\:")[1].split("-")[1]; privateChat(socket,userName,str); continue; } else if(msg.contains("bye")) { firstStep(socket); userExit(socket); continue; } else { PrintStream printStream = new PrintStream(socket.getOutputStream()); printStream.println("格式輸入錯誤"); continue; } } } } catch (IOException e) { e.printStackTrace(); } } private void firstStep(Socket socket) throws IOException { Set<Map.Entry<String,Socket>> set = map.entrySet(); for(Map.Entry<String,Socket> entry:set) { if(entry.getValue().equals(socket)) { if(entry.getValue() == null) { PrintStream printStream = new PrintStream(socket.getOutputStream()); printStream.println("請先進行注冊操作!格式為:[userName:用戶名]"); } } } } private void userRegist(String userName, Socket socket) { map.put(userName,socket); System.out.println("用戶名:" + userName + "客戶端" + socket +"上線了!!"); System.out.println("當前在線人數為" + map.size() + "人"); } private void groupChat(Socket socket, String msg) throws IOException { Set<Map.Entry<String,Socket>> set = map.entrySet(); String userName = null; for(Map.Entry<String,Socket> entry:set) { if(entry.getValue().equals(socket)) { userName = entry.getKey(); break; } } for(Map.Entry<String,Socket> entry:set) { Socket client = entry.getValue(); PrintStream printStream = new PrintStream(client.getOutputStream()); printStream.println(userName + "說" + msg); } } private void privateChat(Socket socket, String userName, String msg) throws IOException { String curUser = null; Set<Map.Entry<String,Socket>> set = map.entrySet(); for(Map.Entry<String,Socket> entry:set) { if(entry.getValue().equals(socket)) { curUser = entry.getKey(); break; } } Socket client = map.get(userName); PrintStream printStream = new PrintStream(client.getOutputStream()); printStream.println(curUser + "私聊說" + msg); } private void userExit(Socket socket) { String userName = null; for(String key:map.keySet()) { if(map.get(key).equals(socket)) { userName = key; break; } } map.remove(userName,socket); System.out.println("用戶" + userName + "已下線"); } } public class MultiServer { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(6666); //使用線程池 ExecutorService executorService = Executors.newFixedThreadPool(20); for(int i = 0;i < 20;i++) { System.out.println("歡迎來到聊天室。。。"); Socket socket = serverSocket.accept(); System.out.println("新人加入。。。"); executorService.execute(new Server(socket)); } executorService.shutdown(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。