请你生成一段Java代码,完成基于GUI的网络通信程序设计,内容为图1为Socket通信服务器端界面,点击该界面中的【Start】按钮,启动服务器监听服务(在图1界面中间的多行文本区域显示“Server starting…”字样)。图2为Socket通信客户端界面,点击该界面中的【Connect】按钮与服务器建立链接,并在图2所示界面中间的多行文本区域显示“Connect to server…”字样,当服务器端监听到客户端的连接后,在图1界面中间的多行文本区域追加一行“Client connected…”字样,并与客户端建立Socket连接。 3.当图1所示的服务器端和图2所示的客户机端建立Socket连接后,编程实现服务端、客户端之间的“单向通信”:在客户端的输入界面发送消息,在服务端接收该消息,并将接收到对方的数据追加显示在多行文本框中。
时间: 2023-06-03 17:08:09 浏览: 120
Java学习资料 Java SE 介绍 Java SE 阶段的相关代码,主要为课堂中用到的事例
以下是一段Java代码,实现基于GUI的网络通信的程序:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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 SocketGUI {
private static final int SERVER_PORT = 9999;
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedReader in;
private PrintWriter out;
private JFrame serverFrame;
private JTextArea serverTextArea;
private JButton startServerButton;
private JFrame clientFrame;
private JTextArea clientTextArea;
private JTextField clientTextField;
private JButton connectServerButton;
public SocketGUI() {
initServerGUI();
initClientGUI();
}
private void initServerGUI() {
serverFrame = new JFrame("Socket通信服务器端");
serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
serverTextArea = new JTextArea(10, 30);
serverTextArea.setEditable(false);
panel.add(serverTextArea);
startServerButton = new JButton("Start");
startServerButton.addActionListener(e -> {
startServer();
serverTextArea.append("Server starting...\n");
});
panel.add(startServerButton);
serverFrame.getContentPane().add(panel);
serverFrame.pack();
serverFrame.setVisible(true);
}
private void initClientGUI() {
clientFrame = new JFrame("Socket通信客户端");
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
clientTextArea = new JTextArea(10, 30);
clientTextArea.setEditable(false);
panel.add(clientTextArea);
clientTextField = new JTextField(20);
panel.add(clientTextField);
connectServerButton = new JButton("Connect");
connectServerButton.addActionListener(e -> {
connectServer();
clientTextArea.append("Connect to server...\n");
});
panel.add(connectServerButton);
clientTextField.addActionListener(e -> {
sendMessage(e.getActionCommand());
clientTextField.setText("");
});
panel.add(clientTextField);
clientFrame.getContentPane().add(panel);
clientFrame.pack();
clientFrame.setVisible(true);
}
private void startServer() {
new Thread(() -> {
try {
serverSocket = new ServerSocket(SERVER_PORT);
clientSocket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
receiveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
private void connectServer() {
new Thread(() -> {
try {
clientSocket = new Socket("localhost", SERVER_PORT);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
receiveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
private void sendMessage(String message) {
out.println(message);
}
private void receiveMessage() {
new Thread(() -> {
try {
String message;
while ((message = in.readLine()) != null) {
serverTextArea.append("Received message: " + message + "\n");
clientTextArea.append("Received message: " + message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
public static void main(String[] args) {
new SocketGUI();
}
}
```
这段代码实现了一个基于GUI的Socket通信程序,包括服务器端和客户端。服务器端和客户端的界面分别用JFrame和JPanel实现,其中包括多行文本区域、输入框和连接按钮等。服务器端界面中的Start按钮启动服务器监听服务,客户端界面中的Connect按钮与服务器建立连接。当服务器端监听到客户端的连接后,在服务器端界面中间的多行文本区域显示“Client connected…”字样,并与客户端建立Socket连接。服务端、客户端之间的单向通信是通过输入框和多行文本框实现的,客户端在输入框发送消息,服务器端接收该消息,并将接收到对方的数据追加显示在多行文本框中。
阅读全文