用Java写一个基于socket的聊天聊天室代码
时间: 2023-06-05 16:04:36 浏览: 132
以下是使用Java编写基于Socket的聊天室的参考代码:
服务器端代码:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ChatServer {
private final int serverPort;
private List<ChatServerThread> clients = new ArrayList<>();
public ChatServer(int serverPort) {
this.serverPort = serverPort;
}
public void startServer() {
try {
ServerSocket serverSocket = new ServerSocket(serverPort);
System.out.println("Chat server started on port " + serverPort);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Accepted connection from " + clientSocket);
ChatServerThread client = new ChatServerThread(this, clientSocket);
clients.add(client);
client.start();
}
} catch (IOException e) {
System.out.println("Error starting the chat server on port " + serverPort);
e.printStackTrace();
}
}
public void broadcastMessage(String message, ChatServerThread sourceClient) {
for (ChatServerThread client : clients) {
if (client != sourceClient) {
client.sendMessage(message);
}
}
}
public void removeClient(ChatServerThread client) {
clients.remove(client);
}
public static void main(String[] args) {
int serverPort = 12345;
ChatServer server = new ChatServer(serverPort);
server.startServer();
}
}
聊天室线程类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChatServerThread extends Thread {
private final ChatServer server;
private final Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChatServerThread(ChatServer server, Socket socket) {
this.server = server;
this.socket = socket;
}
public void sendMessage(String message) {
out.println(message);
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
server.broadcastMessage(message, this);
}
} catch (IOException e) {
System.out.println("Error handling input/output for " + socket);
} finally {
try {
socket.close();
server.removeClient(this);
} catch (IOException e) {
System.out.println("Error closing client socket for " + socket);
}
}
}
}
客户端代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChatClient {
private final String serverHost;
private final int serverPort;
private BufferedReader in;
private PrintWriter out;
public ChatClient(String serverHost, int serverPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
}
public void startClient() {
try {
Socket socket = new Socket(serverHost, serverPort);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Connected to chat server at " + serverHost + ":" + serverPort);
new Thread(new InputReader()).start();
new Thread(new OutputWriter()).start();
} catch (IOException e) {
System.out.println("Error connecting to chat server at " + serverHost + ":" + serverPort);
e.printStackTrace();
}
}
private class InputReader implements Runnable {
@Override
public void run() {
try {
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
System.out.println(message);
}
} catch (IOException e) {
System.out.println("Error handling input from chat server");
e.printStackTrace();
}
}
}
private class OutputWriter implements Runnable {
@Override
public void run() {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
try {
while (true) {
String message = consoleReader.readLine();
out.println(message);
}
} catch (IOException e) {
System.out.println("Error handling output to chat server");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String serverHost = "localhost";
int serverPort = 12345;
ChatClient client = new ChatClient(serverHost, serverPort);
client.startClient();
}
}
阅读全文