用Java基于socket实现聊天功能
时间: 2024-05-09 13:20:19 浏览: 20
以下是一个简单的Java基于socket实现聊天功能的示例代码:
服务端:
```java
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started.");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress());
// 创建一个新的线程来处理客户端的请求
Thread thread = new Thread(new ChatHandler(socket));
thread.start();
}
}
}
class ChatHandler implements Runnable {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChatHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("Welcome to the chat room.");
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
System.out.println("Received message: " + message);
// 广播消息给其他客户端
broadcast(message);
}
} catch (IOException e) {
System.out.println("Error handling client: " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
System.out.println("Error closing client connection: " + e);
}
}
}
private void broadcast(String message) {
for (ChatHandler handler : ChatServer.handlers) {
if (handler != this) {
handler.out.println(message);
}
}
}
}
```
客户端:
```java
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to server.");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 接收服务端的欢迎消息
String message = in.readLine();
System.out.println(message);
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String input = console.readLine();
out.println(input);
}
}
}
```
以上代码只实现了一对一的聊天,如果想要实现多人聊天,可以在服务端维护一个聊天室成员列表,然后在广播消息时,只将消息发送给聊天室成员。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)