Java开发带有私聊功能的简易聊天室代码
时间: 2023-06-17 12:03:51 浏览: 109
一个简单java聊天室代码
5星 · 资源好评率100%
以下是一个简易聊天室的 Java 代码,它带有私聊功能。它使用了 Java 的 Socket 编程和多线程技术来实现。请注意,这只是一个基本的实现,需要进一步扩展和改进。
Server.java:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private static ArrayList<Socket> clients = new ArrayList<Socket>();
private static ArrayList<String> usernames = new ArrayList<String>();
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(1234);
while (true) {
System.out.println("Waiting for clients to connect...");
Socket client = server.accept();
System.out.println("Client connected from " + client.getInetAddress().getHostAddress());
clients.add(client);
new Thread(new ClientHandler(client)).start();
}
}
public static void broadcast(String message, Socket sender) throws Exception {
for (Socket client : clients) {
if (client != sender) {
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer.println(message);
}
}
}
public static void sendPrivateMessage(String message, String recipient, Socket sender) throws Exception {
int recipientIndex = usernames.indexOf(recipient);
if (recipientIndex != -1) {
Socket recipientSocket = clients.get(recipientIndex);
PrintWriter writer = new PrintWriter(recipientSocket.getOutputStream(), true);
writer.println("Private message from " + sender.getInetAddress().getHostAddress() + ": " + message);
} else {
PrintWriter writer = new PrintWriter(sender.getOutputStream(), true);
writer.println("Recipient not found.");
}
}
private static class ClientHandler implements Runnable {
private Socket client;
public ClientHandler(Socket client) {
this.client = client;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer.println("Enter your username:");
String username = reader.readLine();
usernames.add(username);
broadcast(username + " has joined the chat.", client);
String message;
while ((message = reader.readLine()) != null) {
if (message.startsWith("@")) {
int spaceIndex = message.indexOf(" ");
if (spaceIndex != -1) {
String recipient = message.substring(1, spaceIndex);
String privateMessage = message.substring(spaceIndex + 1);
sendPrivateMessage(privateMessage, recipient, client);
}
} else {
broadcast(username + ": " + message, client);
}
}
usernames.remove(username);
clients.remove(client);
broadcast(username + " has left the chat.", client);
client.close();
} catch (Exception e) {
System.out.println("Error handling client: " + e);
}
}
}
}
```
Client.java:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter server IP address: ");
String serverAddress = scanner.nextLine();
Socket socket = new Socket(serverAddress, 1234);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
System.out.print("Enter your username: ");
String username = scanner.nextLine();
writer.println(username);
new Thread(new ServerHandler(socket)).start();
String message;
while ((message = scanner.nextLine()) != null) {
writer.println(message);
}
socket.close();
}
private static class ServerHandler implements Runnable {
private Socket socket;
public ServerHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message;
while ((message = reader.readLine()) != null) {
System.out.println(message);
}
} catch (Exception e) {
System.out.println("Error receiving message: " + e);
}
}
}
}
```
这个聊天室允许用户输入他们的用户名,然后发送消息到聊天室中的所有人或私人消息到一个特定的用户。私人消息应该以“@username message”的形式发送。例如,如果您想向名为“john”的用户发送私人消息“hello”,您可以输入“@john hello”。
阅读全文