91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

java怎么實現聊天功能

小億
163
2023-08-11 03:55:08
欄目: 編程語言

要實現聊天功能,可以使用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();
}
}
}
}

以上代碼演示了一個基本的聊天功能,服務器端接收到客戶端發送的消息后,再將消息廣播給所有客戶端。客戶端可以通過控制臺輸入消息,并接收服務器端廣播的消息。

注意:為了簡化代碼,沒有進行異常處理和線程安全的處理。在實際開發中,需要根據具體需求進行相應的優化和改進。

0
略阳县| 兴和县| 章丘市| 定安县| 灵宝市| 福泉市| 宁海县| 吉木萨尔县| 随州市| 万载县| 福清市| 满洲里市| 皮山县| 罗定市| 贵州省| 永靖县| 宜君县| 海阳市| 和顺县| 随州市| 会昌县| 泰宁县| 来凤县| 吉安市| 定陶县| 旺苍县| 临洮县| 德保县| 新巴尔虎右旗| 咸阳市| 如皋市| 乌海市| 望谟县| 土默特左旗| 新沂市| 东安县| 临朐县| 成安县| 平远县| 巴东县| 淮阳县|