java代码 个人聊天室 要求: 1. 可使用文字聊天 2. 可以发送文件 3. 可以私聊 代码
时间: 2024-03-03 17:51:53 浏览: 65
很抱歉,由于代码比较长,无法在这里直接贴出。不过,以下是具体的实现步骤和代码结构,供你参考:
1. 服务器端
(1)创建一个 ServerSocket 对象,用于监听客户端的连接请求。
```java
ServerSocket serverSocket = new ServerSocket(port);
```
(2)创建一个 HashMap 用于保存所有已连接的客户端。
```java
Map<String, Socket> clientMap = new HashMap<>();
```
(3)使用 while 循环来等待客户端的连接请求,并在接收到请求后开启一个新的线程来处理客户端的请求。
```java
while (true) {
Socket socket = serverSocket.accept();
new ServerThread(socket, clientMap).start();
}
```
(4)在线程中使用输入输出流来实现客户端之间的通信,并实现文字聊天、发送文件和私聊等功能。
2. 客户端
(1)创建一个 Socket 对象,用于连接服务器。
```java
Socket socket = new Socket(host, port);
```
(2)使用输入输出流来发送和接收数据,并实现文字聊天、发送文件和私聊等功能。
具体的代码实现可以参考下面的示例代码:
ServerThread.java
```java
import java.io.*;
import java.net.Socket;
import java.util.Map;
public class ServerThread extends Thread {
private Socket socket;
private Map<String, Socket> clientMap;
public ServerThread(Socket socket, Map<String, Socket> clientMap) {
this.socket = socket;
this.clientMap = clientMap;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String clientName = reader.readLine();
System.out.println(clientName + " connected");
// 将新连接的客户端添加到客户端列表中
clientMap.put(clientName, socket);
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("quit")) {
break;
} else if (line.startsWith("@")) {
// 私聊功能
String[] tokens = line.split(" ");
String targetClientName = tokens[0].substring(1);
String message = line.substring(tokens[0].length() + 1);
Socket targetSocket = clientMap.get(targetClientName);
if (targetSocket != null) {
BufferedWriter targetWriter = new BufferedWriter(new OutputStreamWriter(targetSocket.getOutputStream()));
targetWriter.write(clientName + " : " + message + "\n");
targetWriter.flush();
} else {
writer.write("Client " + targetClientName + " not found\n");
writer.flush();
}
} else if (line.startsWith("#")) {
// 发送文件功能
String[] tokens = line.split(" ");
String targetClientName = tokens[0].substring(1);
String fileName = tokens[1];
Socket targetSocket = clientMap.get(targetClientName);
if (targetSocket != null) {
FileInputStream fileInputStream = new FileInputStream(fileName);
OutputStream outputStream = targetSocket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
fileInputStream.close();
writer.write("File " + fileName + " sent to " + targetClientName + "\n");
writer.flush();
} else {
writer.write("Client " + targetClientName + " not found\n");
writer.flush();
}
} else {
// 文字聊天功能
for (Socket clientSocket : clientMap.values()) {
if (clientSocket != socket) {
BufferedWriter clientWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
clientWriter.write(clientName + " : " + line + "\n");
clientWriter.flush();
}
}
}
}
// 将断开连接的客户端从客户端列表中移除
clientMap.remove(clientName);
reader.close();
writer.close();
socket.close();
System.out.println(clientName + " disconnected");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
Server.java
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class Server {
private int port;
private Map<String, Socket> clientMap;
public Server(int port) {
this.port = port;
this.clientMap = new HashMap<>();
}
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server started at port " + port);
while (true) {
Socket socket = serverSocket.accept();
new ServerThread(socket, clientMap).start();
}
}
public static void main(String[] args) throws IOException {
Server server = new Server(8888);
server.start();
}
}
```
Client.java
```java
import java.io.*;
import java.net.Socket;
public class Client {
private String host;
private int port;
private String name;
public Client(String host, int port, String name) {
this.host = host;
this.port = port;
this.name = name;
}
public void start() throws IOException {
Socket socket = new Socket(host, port);
System.out.println("Connected to server");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
// 发送客户端名称给服务器
writer.write(name + "\n");
writer.flush();
while ((line = reader.readLine()) != null) {
if (line.equals("quit")) {
break;
} else if (line.startsWith("@")) {
// 私聊功能
String[] tokens = line.split(" ");
String targetClientName = tokens[0].substring(1);
String message = line.substring(tokens[0].length() + 1);
writer.write(line + "\n");
writer.flush();
String response = socketReader.readLine();
System.out.println(response);
} else if (line.startsWith("#")) {
// 发送文件功能
String[] tokens = line.split(" ");
String targetClientName = tokens[0].substring(1);
String fileName = tokens[1];
File file = new File(fileName);
if (file.exists()) {
writer.write(line + "\n");
writer.flush();
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
fileInputStream.close();
String response = socketReader.readLine();
System.out.println(response);
} else {
System.out.println("File " + fileName + " not found");
}
} else {
// 文字聊天功能
writer.write(line + "\n");
writer.flush();
}
}
reader.close();
socketReader.close();
writer.close();
socket.close();
System.out.println("Disconnected");
}
public static void main(String[] args) throws IOException {
Client client = new Client("localhost", 8888, "client1");
client.start();
}
}
```
上面的代码实现了一个简单的个人聊天室,可以实现文字聊天、发送文件和私聊等功能。你可以根据自己的需求进行修改和扩展。
阅读全文