用java写一个多人聊天室项目 要求包含网络编程、IO流、读线程、GUI
时间: 2023-07-10 21:32:15 浏览: 203
好的,下面是一个简单的多人聊天室项目的代码示例,它包含了网络编程、I/O流、读线程和GUI。这个项目使用Java的Swing库来创建GUI的用户界面,使用Socket和ServerSocket类来实现网络编程。这个项目的主要思路是,客户端连接到服务器后,可以向服务器发送消息并接收其他客户端发送的消息。以下是代码示例:
### 服务端代码
```
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private List<ClientThread> clients = new ArrayList<>();
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
Socket socket = serverSocket.accept();
ClientThread clientThread = new ClientThread(socket);
clients.add(clientThread);
clientThread.start();
}
}
private void broadcast(String message, ClientThread sender) throws IOException {
for (ClientThread client : clients) {
if (client != sender) {
client.sendMessage(message);
}
}
}
private class ClientThread extends Thread {
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public ClientThread(Socket socket) throws IOException {
this.socket = socket;
this.inputStream = socket.getInputStream();
this.outputStream = socket.getOutputStream();
}
public void sendMessage(String message) throws IOException {
outputStream.write(message.getBytes());
}
@Override
public void run() {
try {
while (true) {
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
if (length == -1) {
clients.remove(this);
break;
}
String message = new String(buffer, 0, length);
broadcast(message, this);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start();
}
}
```
### 客户端代码
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client extends JFrame {
private JTextField textField;
private JTextArea textArea;
private JButton sendButton;
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public Client() {
setTitle("多人聊天室");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
textArea = new JTextArea();
textArea.setEditable(false);
add(new JScrollPane(textArea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
add(inputPanel, BorderLayout.SOUTH);
textField = new JTextField();
inputPanel.add(textField, BorderLayout.CENTER);
sendButton = new JButton("发送");
inputPanel.add(sendButton, BorderLayout.EAST);
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = textField.getText();
try {
sendMessage(message);
textField.setText("");
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
private void connect() throws IOException {
socket = new Socket("localhost", 8888);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
if (length == -1) {
break;
}
String message = new String(buffer, 0, length);
textArea.append(message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
readThread.start();
}
private void sendMessage(String message) throws IOException {
outputStream.write(message.getBytes());
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.setVisible(true);
client.connect();
}
}
```
这个代码示例是一个简单的多人聊天室的实现,它使用了Java中的Socket和ServerSocket类进行网络编程,使用了I/O流来进行数据传输,使用了读线程来实现异步读取数据,使用了Swing库来创建GUI的用户界面。当客户端连接到服务器后,可以向服务器发送消息并接收其他客户端发送的消息。
阅读全文