java代码 个人聊天室 要求: 1. 可使用文字聊天 2. 可以发送文件 3. 可以私聊 代码
时间: 2024-02-10 09:08:31 浏览: 35
好的,下面是一个简单的Java聊天室的代码示例,包含了文字聊天、文件传输和私聊功能。请注意,这只是一个示例,可能还需要完善和改进。另外,为了保证代码的简洁性,我省略了一些异常处理和其他细节,实际应用中需要注意。
Server.java
```
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private static final int PORT = 12345;
private static Map<String, Socket> clients = new HashMap<>();
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server started at port " + PORT);
while (true) {
Socket client = server.accept();
new Thread(new ClientHandler(client)).start();
}
}
private static class ClientHandler implements Runnable {
private Socket client;
private DataInputStream in;
private DataOutputStream out;
private String name;
public ClientHandler(Socket client) {
this.client = client;
}
public void run() {
try {
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
name = in.readUTF();
clients.put(name, client);
broadcast(name + " joined the chat");
while (true) {
String message = in.readUTF();
if (message.startsWith("/private")) {
String[] tokens = message.split("\\s+", 3);
String target = tokens[1];
String text = tokens[2];
if (clients.containsKey(target)) {
Socket targetClient = clients.get(target);
DataOutputStream targetOut = new DataOutputStream(targetClient.getOutputStream());
targetOut.writeUTF(name + " (private): " + text);
} else {
out.writeUTF("Error: " + target + " not found");
}
} else if (message.startsWith("/file")) {
String[] tokens = message.split("\\s+", 2);
String filename = tokens[1];
File file = new File(filename);
if (!file.exists()) {
out.writeUTF("Error: " + filename + " not found");
} else {
out.writeUTF("Sending file " + filename + " to all clients...");
broadcastFile(name, file);
}
} else {
broadcast(name + ": " + message);
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
try {
client.close();
clients.remove(name);
broadcast(name + " left the chat");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
private void broadcast(String message) throws IOException {
for (Socket client : clients.values()) {
DataOutputStream out = new DataOutputStream(client.getOutputStream());
out.writeUTF(message);
}
}
private void broadcastFile(String sender, File file) throws IOException {
byte[] buffer = new byte[4096];
FileInputStream fis = new FileInputStream(file);
for (Socket client : clients.values()) {
DataOutputStream out = new DataOutputStream(client.getOutputStream());
out.writeUTF(sender + " sent a file: " + file.getName());
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
}
fis.close();
}
}
}
```
Client.java
```
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
private static final String HOST = "localhost";
private static final int PORT = 12345;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
Socket socket = new Socket(HOST, PORT);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(name);
new Thread(() -> {
try {
while (true) {
String message = in.readUTF();
System.out.println(message);
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}).start();
while (true) {
String message = scanner.nextLine();
if (message.startsWith("/private")) {
String[] tokens = message.split("\\s+", 3);
String target = tokens[1];
String text = tokens[2];
out.writeUTF("/private " + target + " " + text);
} else if (message.startsWith("/file")) {
String[] tokens = message.split("\\s+", 2);
String filename = tokens[1];
File file = new File(filename);
if (!file.exists()) {
System.out.println("Error: " + filename + " not found");
} else {
out.writeUTF("/file " + filename);
byte[] buffer = new byte[4096];
FileInputStream fis = new FileInputStream(file);
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
fis.close();
}
} else {
out.writeUTF(message);
}
}
}
}
```
在运行程序之前,需要先在命令行中进入到Server.java和Client.java所在的目录,然后依次编译和运行Server.java和多个Client.java。在运行Client.java时,需要输入用户名并按回车键进入聊天室。可以使用命令“/private target message”发送私聊消息,使用命令“/file filename”发送文件。