基于Java的Socket多客戶端Client-Server聊天程序的實現
任務要求
編寫一個簡單的Socket多客戶端聊天程序:
客戶端程序,從控制臺輸入字符串,發送到服務器端,并將服務器返回的信息顯示出來 服務器端程序,從客戶機接收數據并打印,同時將從標準輸入獲取的信息發送給客戶機 滿足一個服務器可以服務多個客戶低配版本鏈接
實現代碼
工具類
import java.io.DataOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;public class SocketUtils {public static void writeToSocket(Socket socket, String message) throws IOException {writeToOutputStream(socket.getOutputStream(), message);}public static void writeToDataOutputStream(DataOutputStream dos, String message) throws IOException {dos.writeUTF(message);dos.flush();}public static void writeToOutputStream(OutputStream os, String message) throws IOException {writeToDataOutputStream(new DataOutputStream(os), message);}}
服務器端線程
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;public class ChatServerRunnable implements Runnable {private Socket socket;private DataOutputStream dos;private DataInputStream dis;private String currentUserNickName;public ChatServerRunnable(Socket socket) throws IOException {this.socket = socket;this.dos = new DataOutputStream(socket.getOutputStream());this.dis = new DataInputStream(socket.getInputStream());}@Overridepublic void run() {try {write('歡迎來到聊天室!');login();System.out.println(currentUserNickName + '用戶登錄成功');write(currentUserNickName + ', 您已登錄。n輸入【list users】可以查看當前登錄用戶列表n輸入【to all 消息內容】可以群發消息n輸入【to 某個用戶 消息內容】可以給指定用戶發送消息n輸入【exit】可以退出聊天');String input = dis.readUTF();while (!ChatServer.EXIT.equals(input)) {System.out.println(currentUserNickName + '輸入了' + input);if (input.startsWith('to ')) {sendMessage(input);} else if ('list users'.equals(input)) {showOnlineUsers();} else {write('您輸入的命令不合法,請重新輸入!');}input = dis.readUTF();}} catch (IOException e) {e.printStackTrace();} finally {ChatServer.nickNameSocketMap.remove(currentUserNickName);try {dis.close();} catch (IOException e) {e.printStackTrace();}try {dos.close();} catch (IOException e) {e.printStackTrace();}try {socket.close();} catch (IOException e) {e.printStackTrace();}}}private void login() throws IOException {write('請輸入你的昵稱:');while (true) {String nickName = dis.readUTF();System.out.println('用戶輸入了昵稱:' + nickName);synchronized (ChatServerRunnable.class) {if (!ChatServer.nickNameSocketMap.containsKey(nickName)) {currentUserNickName = nickName;ChatServer.nickNameSocketMap.put(nickName, socket);break;} else {write('您輸入的昵稱已存在,請重新輸入:');}}}}private void sendMessage(String input) throws IOException {int receiverEndIndex = input.indexOf(' ', 3);String receiver = input.substring(3, receiverEndIndex);String message = input.substring(receiverEndIndex + 1);if ('all'.equals(receiver)) {broadcast(message);} else {sendIndividualMessage(receiver, message);}}private void sendIndividualMessage(String receiver, String orignalMessage) throws IOException {Socket receiverSocket = ChatServer.nickNameSocketMap.get(receiver);if (receiverSocket != null) {SocketUtils.writeToSocket(receiverSocket, formatMessage('你', orignalMessage));} else {write('您要單獨聊天的用戶【' + receiver + '】不存在或者已經下線');}}private String formatMessage(String receiver, String originalMessage) {StringBuilder messageBuilder = new StringBuilder();messageBuilder.append(currentUserNickName).append(' 對 ').append(receiver).append(' 說:n').append(originalMessage).append('n發送時間:').append(new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date()));return messageBuilder.toString();}private void broadcast(String orignalMessage) throws IOException {for (Map.Entry<String, Socket> entry : ChatServer.nickNameSocketMap.entrySet()) {if (!currentUserNickName.equals(entry.getKey())) {SocketUtils.writeToSocket(entry.getValue(), formatMessage('所有人', orignalMessage));}}}private void showOnlineUsers() throws IOException {StringBuilder users = new StringBuilder();users.append('當前在線的用戶有:n');for (String nickName : ChatServer.nickNameSocketMap.keySet()) {users.append('【').append(nickName).append('】n');}write(users.toString());}private void write(String message) throws IOException {SocketUtils.writeToDataOutputStream(dos, message);}}
客戶端線程
import java.io.DataInputStream;import java.io.IOException;public class ClientMessageReceiver implements Runnable {private DataInputStream dis;private boolean timeToStop = false;public ClientMessageReceiver(DataInputStream dis) {this.dis = dis;}@Overridepublic void run() {while (!timeToStop) {try {System.out.println(dis.readUTF());} catch (IOException e) {if ('Connection reset'.equals(e.getMessage())) {System.out.println('與服務器的連接已中斷!');break;}if (!timeToStop) {e.printStackTrace();}}}}public void stop() {timeToStop = true;}}
服務器端程序
import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Map;public class ChatServer {public static final String EXIT = 'exit';public static final int PORT = 8888;static Map<String, Socket> nickNameSocketMap = new HashMap<>();public static void main(String[] args) {try (ServerSocket ss = new ServerSocket(PORT)) {System.out.println('聊天室服務器端已啟動,正在監聽' + PORT + '端口');while (true) {try {Socket socket = ss.accept();System.out.println('有新用戶連接到服務器端,信息為:' + socket);new Thread(new ChatServerRunnable(socket)).start();} catch (Exception e) {e.printStackTrace();}}} catch (IOException e) {e.printStackTrace();}}}
客戶端程序
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.util.Scanner;public class ChatClient {private static Scanner scanner = new Scanner(System.in);public static void main(String[] args) {try (Socket socket = new Socket('127.0.0.1', 8888);DataInputStream dis = new DataInputStream(socket.getInputStream());DataOutputStream dos = new DataOutputStream(socket.getOutputStream())) {ClientMessageReceiver messageReceiver = new ClientMessageReceiver(dis);new Thread(messageReceiver).start();String input = null;do {input = scanner.nextLine();write(dos, input);} while (!ChatServer.EXIT.equals(input));messageReceiver.stop();} catch (IOException e) {e.printStackTrace();}}private static void write(DataOutputStream dos, String message) throws IOException {dos.writeUTF(message);dos.flush();}}
運行說明
啟動服務器:
啟動第一個客戶端,輸入客戶昵稱:
服務器監聽到了這個事件:
獲取所有用戶列表,發送給所有用戶“hhh”的信息:
服務器端接收到了這個事件:
新的客戶端登錄,注冊用戶昵稱:
服務器接收到這個事件:
用戶1向用戶2發送私聊消息:
用戶2收到用戶1的消息:
客戶2向所有用戶發送消息:
客戶1收到客戶2的群發消息:
服務器監聽到了這些事件:
客戶2退出:
客戶1顯示的在線列表只有1人了:
客戶1也退出:
客戶端用戶退出的時候,該線程終止。
沒客戶端用戶,服務器也正常跑自己的事。
到此這篇關于基于Java的Socket多客戶端Client-Server聊天程序的實現的文章就介紹到這了,更多相關Java Socket多客戶端Client-Server聊天內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: