这个聊天室系统JAVA的代码请编写
时间: 2024-03-22 10:38:16 浏览: 113
好的,请先确保你已经安装了JDK和Eclipse等必要的开发工具。下面是一个简单的聊天室系统的Java代码示例,包括客户端和服务器端:
服务器端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ChatServer {
private static final int PORT = 8888;
private List<ClientHandler> clients = new ArrayList<ClientHandler>();
public ChatServer() {
ServerSocket server = null;
try {
server = new ServerSocket(PORT);
System.out.println("Server started on port " + PORT);
while (true) {
Socket client = server.accept();
ClientHandler handler = new ClientHandler(client);
clients.add(handler);
new Thread(handler).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public synchronized void broadcast(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
public synchronized void removeClient(ClientHandler client) {
clients.remove(client);
}
public static void main(String[] args) {
new ChatServer();
}
class ClientHandler implements Runnable {
private Socket client;
private String username;
private ChatServer server;
public ClientHandler(Socket client) {
this.client = client;
this.server = ChatServer.this;
}
public void run() {
try {
username = Utils.receiveMessage(client);
server.broadcast(username + " has joined the chat.");
while (true) {
String message = Utils.receiveMessage(client);
server.broadcast(username + ": " + message);
}
} catch (IOException e) {
System.out.println(username + " has left the chat.");
} finally {
server.removeClient(this);
Utils.close(client);
}
}
public void sendMessage(String message) {
Utils.sendMessage(client, message);
}
}
}
```
客户端代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatClient {
private static final String SERVER_HOST = "localhost";
private static final int SERVER_PORT = 8888;
private Socket socket;
private String username;
private JTextArea messageArea;
private JTextField inputField;
public ChatClient() {
try {
socket = new Socket(SERVER_HOST, SERVER_PORT);
username = showLoginDialog();
if (username == null) {
Utils.close(socket);
return;
}
System.out.println("Connected to server " + SERVER_HOST + ":" + SERVER_PORT + " as " + username);
showChatFrame();
new Thread(new ServerHandler()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private String showLoginDialog() {
// TODO: show login dialog and return username
return "test";
}
private void showChatFrame() {
JFrame frame = new JFrame("Chat Room");
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageArea = new JTextArea();
messageArea.setLineWrap(true);
messageArea.setEditable(false);
messageArea.setFont(new Font("Arial", Font.PLAIN, 14));
JScrollPane scrollPane = new JScrollPane(messageArea);
scrollPane.setBorder(BorderFactory.createTitledBorder("Message"));
inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.PLAIN, 14));
JButton sendButton = new JButton("Send");
sendButton.setFont(new Font("Arial", Font.PLAIN, 14));
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = inputField.getText().trim();
if (!message.equals("")) {
Utils.sendMessage(socket, message);
inputField.setText("");
}
}
});
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(inputPanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] args) {
new ChatClient();
}
class ServerHandler implements Runnable {
public void run() {
try {
while (true) {
String message = Utils.receiveMessage(socket);
messageArea.append(message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
Utils.close(socket);
}
}
}
}
```
以上代码仅供参考,需要根据实际情况进行修改和完善。实现一个完整的聊天室系统需要考虑更多的细节和功能,比如用户登录、退出、私聊、文件传输等,建议你在实践中逐步完善。
阅读全文