使用ServerSocket类和Socket类以及InetAddress类编写一个GUI实现一个简单的聊天程序
时间: 2024-05-18 10:18:38 浏览: 163
好的,以下是一个简单的聊天程序的实现步骤:
1. 创建一个GUI窗口,包含一个文本框用于显示聊天记录、一个文本框用于输入要发送的消息以及一个“发送”按钮。
2. 在后台创建一个ServerSocket对象,用于监听来自其他客户端的连接请求。当有新的连接请求时,创建一个新的Socket对象来处理该连接。
3. 在客户端使用Socket对象连接到服务器。
4. 当客户端发送消息时,将消息发送到服务器,服务器将消息转发给所有其他客户端。
5. 当有消息到达时,更新GUI上的聊天记录文本框。
以下是Java代码实现:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatServer extends JFrame implements ActionListener {
private JTextArea chatArea;
private JTextField inputField;
private JButton sendButton;
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedReader in;
private PrintWriter out;
public ChatServer() {
initGUI();
startServer();
}
private void initGUI() {
chatArea = new JTextArea();
chatArea.setEditable(false);
add(chatArea, BorderLayout.CENTER);
inputField = new JTextField();
add(inputField, BorderLayout.SOUTH);
sendButton = new JButton("发送");
sendButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(sendButton);
add(buttonPanel, BorderLayout.EAST);
setTitle("聊天室服务器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
setVisible(true);
}
private void startServer() {
try {
serverSocket = new ServerSocket(8888);
InetAddress address = InetAddress.getLocalHost();
chatArea.append("服务器地址:" + address + "\n");
chatArea.append("等待客户端连接...\n");
clientSocket = serverSocket.accept();
chatArea.append("客户端连接成功,IP地址:" + clientSocket.getInetAddress().getHostAddress() + "\n");
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
Thread messageThread = new Thread(new MessageThread());
messageThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage() {
String message = inputField.getText();
out.println(message);
chatArea.append("我:" + message + "\n");
inputField.setText("");
}
private class MessageThread implements Runnable {
@Override
public void run() {
try {
while (true) {
String message = in.readLine();
chatArea.append("客户端:" + message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
sendMessage();
}
}
public static void main(String[] args) {
new ChatServer();
}
}
```
客户端代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatClient extends JFrame implements ActionListener {
private JTextArea chatArea;
private JTextField inputField;
private JButton sendButton;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChatClient() {
initGUI();
connectToServer();
}
private void initGUI() {
chatArea = new JTextArea();
chatArea.setEditable(false);
add(chatArea, BorderLayout.CENTER);
inputField = new JTextField();
add(inputField, BorderLayout.SOUTH);
sendButton = new JButton("发送");
sendButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(sendButton);
add(buttonPanel, BorderLayout.EAST);
setTitle("聊天室客户端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
setVisible(true);
}
private void connectToServer() {
try {
socket = new Socket("localhost", 8888);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
Thread messageThread = new Thread(new MessageThread());
messageThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage() {
String message = inputField.getText();
out.println(message);
chatArea.append("我:" + message + "\n");
inputField.setText("");
}
private class MessageThread implements Runnable {
@Override
public void run() {
try {
while (true) {
String message = in.readLine();
chatArea.append("服务器:" + message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
sendMessage();
}
}
public static void main(String[] args) {
new ChatClient();
}
}
```
注意:在使用时需要先启动服务器,再启动客户端。
阅读全文