使用ServerSocket类和Socket类以及InetAddress类编写一个GUI实现一个简单的聊天程序
时间: 2024-03-04 14:49:36 浏览: 32
好的,我可以为您提供一些帮助。这是一个简单的聊天程序的Java代码。该程序使用ServerSocket类和Socket类以及InetAddress类来建立连接并发送消息。GUI使用Java Swing库来创建。
```java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleChat extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public SimpleChat() {
super("Simple Chat");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(this);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
setSize(300, 150);
setVisible(true);
}
public void startRunning() {
try {
server = new ServerSocket(6789, 100);
while (true) {
try {
waitForConnection();
setupStreams();
whileChatting();
} catch (EOFException eofException) {
showMessage("\n Connection terminated. ");
} finally {
closeChat();
}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException {
showMessage(" Waiting for someone to connect... \n");
connection = server.accept();
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
private void whileChatting() throws IOException {
String message = " You are now connected! ";
sendMessage(message);
ableToType(true);
do {
try {
message = (String) input.readObject();
showMessage("\n" + message);
} catch (ClassNotFoundException classNotFoundException) {
showMessage("\n Unknown data received! ");
}
} while (!message.equals("CLIENT - END"));
}
private void closeChat() {
showMessage("\n Closing connection... \n");
ableToType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void sendMessage(String message) {
try {
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\nSERVER - " + message);
} catch (IOException ioException) {
chatWindow.append("\n ERROR: Message cannot be sent! \n");
}
}
private void showMessage(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
chatWindow.append(text);
}
});
}
private void ableToType(final boolean tof) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
userText.setEditable(tof);
}
});
}
public void actionPerformed(ActionEvent event) {
sendMessage(userText.getText());
userText.setText("");
}
public static void main(String[] args) {
SimpleChat server = new SimpleChat();
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.startRunning();
}
}
```
这个程序可以作为服务器运行。如果您需要作为客户端运行,只需将“SERVER -”替换为“CLIENT -”即可。请注意,此程序仅供学习和实验目的使用,不应用于生产环境中。