Java多线程机制的聊天室C/S的代码
时间: 2024-01-13 18:03:11 浏览: 83
以下是一个简单的Java多线程机制的聊天室C/S模式的代码实现,包括服务端和客户端:
服务端代码:
```
import java.io.IOException;
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 ChatServer {
private static final int PORT = 8888;
private List<ClientHandler> clients = new ArrayList<>();
private ExecutorService pool = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started, listening on port " + PORT + "...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected: " + socket);
ClientHandler clientHandler = new ClientHandler(socket, this);
clients.add(clientHandler);
pool.execute(clientHandler);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void broadcast(String message, ClientHandler excludeClient) {
for (ClientHandler client : clients) {
if (client != excludeClient) {
client.sendMessage(message);
}
}
}
public void removeClient(ClientHandler clientHandler) {
clients.remove(clientHandler);
System.out.println("Client disconnected: " + clientHandler.getSocket());
}
}
class ClientHandler implements Runnable {
private Socket socket;
private ChatServer chatServer;
private String name;
private boolean connected;
private InputReader inputReader;
private OutputWriter outputWriter;
public ClientHandler(Socket socket, ChatServer chatServer) {
this.socket = socket;
this.chatServer = chatServer;
this.connected = true;
this.inputReader = new InputReader(socket);
this.outputWriter = new OutputWriter(socket);
}
@Override
public void run() {
try {
outputWriter.writeMessage("Enter your name: ");
name = inputReader.readMessage();
outputWriter.writeMessage("Welcome, " + name + "!");
chatServer.broadcast(name + " joined the chat", this);
while (connected) {
String message = inputReader.readMessage();
if (message == null) {
break;
}
chatServer.broadcast(name + ": " + message, this);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
public void sendMessage(String message) {
outputWriter.writeMessage(message);
}
public Socket getSocket() {
return socket;
}
public void close() {
connected = false;
inputReader.close();
outputWriter.close();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
chatServer.removeClient(this);
chatServer.broadcast(name + " left the chat", null);
}
}
class InputReader {
private BufferedReader reader;
public InputReader(Socket socket) throws IOException {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public String readMessage() throws IOException {
return reader.readLine();
}
public void close() {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(Socket socket) throws IOException {
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
}
public void writeMessage(String message) {
writer.println(message);
}
public void close() {
writer.close();
}
}
```
客户端代码:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class ChatClient {
private static final String HOST = "localhost";
private static final int PORT = 8888;
private String name;
private Socket socket;
private InputReader inputReader;
private OutputWriter outputWriter;
public static void main(String[] args) {
new ChatClient().start();
}
public void start() {
try {
socket = new Socket(HOST, PORT);
System.out.println("Connected to server: " + HOST + ":" + PORT);
inputReader = new InputReader(socket);
outputWriter = new OutputWriter(socket);
outputWriter.writeMessage("Connected to server: " + HOST + ":" + PORT);
name = inputReader.readMessage();
System.out.println(name);
new Thread(new MessageReader()).start();
new Thread(new MessageSender()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
class MessageReader implements Runnable {
@Override
public void run() {
try {
while (true) {
String message = inputReader.readMessage();
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MessageSender implements Runnable {
@Override
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String message = reader.readLine();
outputWriter.writeMessage(message);
if (message.equals("quit")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
}
public void close() {
try {
inputReader.close();
outputWriter.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class InputReader {
private BufferedReader reader;
public InputReader(Socket socket) throws IOException {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public String readMessage() throws IOException {
return reader.readLine();
}
public void close() {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(Socket socket) throws IOException {
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
}
public void writeMessage(String message) {
writer.println(message);
}
public void close() {
writer.close();
}
}
```
以上代码仅作为示例,实际应用中需要根据需求进行适当的修改和优化。
阅读全文