要實現聊天功能,可以使用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 List<ClientHandler> clients = new ArrayList<>();
public static void main(String[] args) {
ChatServer server = new ChatServer();
server.start(9999);
}
public void start(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("服務器已啟動,監聽端口:" + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客戶端已連接:" + socket.getInetAddress());
ClientHandler clientHandler = new ClientHandler(socket);
clients.add(clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void broadcast(String message) {
for (ClientHandler handler : clients) {
handler.sendMessage(message);
}
}
private class ClientHandler implements Runnable {
private Socket socket;
private PrintWriter writer;
public ClientHandler(Socket socket) {
this.socket = socket;
try {
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message;
while ((message = reader.readLine()) != null) {
System.out.println("接收到消息:" + message);
broadcast(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sendMessage(String message) {
writer.println(message);
}
}
}
客戶端的代碼示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChatClient {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public static void main(String[] args) {
ChatClient client = new ChatClient();
client.connect("localhost", 9999);
}
public void connect(String host, int port) {
try {
socket = new Socket(host, port);
System.out.println("已連接到服務器:" + socket.getInetAddress());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
new Thread(new ServerHandler()).start();
new Thread(new ClientHandler()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private class ServerHandler implements Runnable {
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println("收到消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class ClientHandler implements Runnable {
public void run() {
try {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String message;
while ((message = consoleReader.readLine()) != null) {
writer.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
writer.close();
}
}
}
}
以上代碼演示了一個基本的聊天功能,服務器端接收到客戶端發送的消息后,再將消息廣播給所有客戶端。客戶端可以通過控制臺輸入消息,并接收服務器端廣播的消息。
注意:為了簡化代碼,沒有進行異常處理和線程安全的處理。在實際開發中,需要根據具體需求進行相應的優化和改進。