java多人聊天室源码
时间: 2023-07-30 12:07:11 浏览: 137
Java多人聊天室源代码
4星 · 用户满意度95%
好的,以下是一个简单的Java多人聊天室的源码示例:
Server.java:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private Vector<ClientHandler> clients;
private int port;
public Server(int port) {
this.port = port;
clients = new Vector<>();
}
public void start() {
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server started on port " + port);
while(true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected.");
ClientHandler client = new ClientHandler(socket, this);
clients.add(client);
client.start();
}
} catch(IOException e) {
e.printStackTrace();
}
}
public void broadcast(String message, ClientHandler sender) {
for(ClientHandler client : clients) {
if(client != sender) {
client.sendMessage(message);
}
}
}
public void removeClient(ClientHandler client) {
clients.remove(client);
}
public static void main(String[] args) {
Server server = new Server(5000);
server.start();
}
}
class ClientHandler extends Thread {
private Socket socket;
private Server server;
private PrintWriter writer;
public ClientHandler(Socket socket, Server server) {
this.socket = socket;
this.server = server;
}
public void sendMessage(String message) {
writer.println(message);
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
String username = reader.readLine();
System.out.println("New user connected: " + username);
server.broadcast(username + " joined the chat.", this);
String input;
while((input = reader.readLine()) != null) {
server.broadcast(username + ": " + input, this);
}
server.removeClient(this);
writer.close();
reader.close();
socket.close();
System.out.println(username + " left the chat.");
server.broadcast(username + " left the chat.", this);
} catch(IOException e) {
e.printStackTrace();
}
}
}
```
Client.java:
```java
import java.io.*;
import java.net.*;
public class Client {
private String host;
private int port;
private String username;
public Client(String host, int port, String username) {
this.host = host;
this.port = port;
this.username = username;
}
public void start() {
try {
Socket socket = new Socket(host, port);
System.out.println("Connected to server on port " + port);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println(username);
String input;
while((input = reader.readLine()) != null) {
System.out.println(input);
}
writer.close();
reader.close();
socket.close();
} catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client("localhost", 5000, "username");
client.start();
}
}
```
这个示例实现了一个简单的多人聊天室,用户可以通过客户端连接到服务器并进行聊天。服务器接受连接并将每个新客户端添加到客户端列表中,然后向其他客户端广播新客户端的加入消息。当客户端发送消息时,服务器将其广播给其他客户端。当客户端关闭连接时,服务器将其从客户端列表中移除,并向其他客户端广播离开消息。
阅读全文