1. 服务端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;public class ChatServer {private static final int PORT = 8070;private static List<ClientHandler> clients = new ArrayList<>();public static void main(String[] args) {try {ServerSocket serverSocket = new ServerSocket(PORT);System.out.println("Chat Server started on port " + PORT);while (true) {Socket clientSocket = serverSocket.accept();System.out.println("New client connected: " + clientSocket);ClientHandler clientHandler = new ClientHandler(clientSocket);clients.add(clientHandler);clientHandler.start();}} catch (IOException e) {e.printStackTrace();}}public static void broadcastMessage(String message) {for (ClientHandler client : clients) {client.sendMessage(message);}}}
2. 客户端消息处理器器
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;public class ClientHandler extends Thread {private Socket clientSocket;private BufferedReader reader;private PrintWriter writer;public ClientHandler(Socket socket) {this.clientSocket = socket;try {reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));writer = new PrintWriter(clientSocket.getOutputStream(), true);} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {try {String message;while ((message = reader.readLine()) != null) {System.out.println("Received message from client: " + message);ChatServer.broadcastMessage(message);}} catch (IOException e) {e.printStackTrace();}}public void sendMessage(String message) {String pod_name = System.getenv().get("POD_NAME");if (pod_name == null || "".equals(pod_name)) {pod_name = "fail to obtain pod name";}writer.println(pod_name + ":" + message);}
}
3. 客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;public class ChatClient {private static String SERVER_ADDRESS = "localhost";private static int SERVER_PORT = 8070;public static void main(String[] args) {try {if (args.length != 2) {SERVER_ADDRESS = "localhost";SERVER_PORT = 8070;} else {SERVER_ADDRESS = args[0];SERVER_PORT = Integer.parseInt(args[1]);}Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);System.out.println("Connected to chat server:" + args[0] + ":" + args[1]);BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);// 启动一个线程来接收消息Thread messageReceiverThread = new Thread(() -> {try {String message;while ((message = reader.readLine()) != null) {System.out.println(message);}} catch (IOException e) {e.printStackTrace();}});messageReceiverThread.start();// 从控制台输入消息,并发送到服务器BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));String userInput;while ((userInput = consoleReader.readLine()) != null) {writer.println(userInput);}// 关闭资源writer.close();reader.close();socket.close();} catch (IOException e) {e.printStackTrace();}}
}