server代码:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class Server extends JFrame {private JTextArea messageArea;private JTextField textField;private PrintWriter writer;public Server() {setTitle("Server");setSize(400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);messageArea = new JTextArea();messageArea.setEditable(false);JScrollPane scrollPane = new JScrollPane(messageArea);textField = new JTextField(20);JButton sendButton = new JButton("Send");sendButton.addActionListener(e -> sendMessage(textField.getText()));JPanel inputPanel = new JPanel();inputPanel.setLayout(new FlowLayout());inputPanel.add(textField);inputPanel.add(sendButton);getContentPane().setLayout(new BorderLayout());getContentPane().add(scrollPane, BorderLayout.CENTER);getContentPane().add(inputPanel, BorderLayout.SOUTH);}private void sendMessage(String message) {if (writer != null) {writer.println(message);writer.flush();messageArea.append("Server: " + message + "\n");} else {messageArea.append("Error: Writer is not initialized.\n");}}public void startServer() {try {ServerSocket serverSocket = new ServerSocket(12345);messageArea.append("Server started. Waiting for clients...\n");Socket clientSocket = serverSocket.accept();messageArea.append("Client connected.\n");BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));writer = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()), true);String message;while ((message = reader.readLine()) != null) {messageArea.append("Client: " + message + "\n");}clientSocket.close();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {Server server = new Server();server.setVisible(true);server.startServer();}
}
client代码
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.Socket;public class Client extends JFrame {private JTextArea messageArea;private JTextField textField;private PrintWriter writer;public Client() {setTitle("Client");setSize(400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);messageArea = new JTextArea();messageArea.setEditable(false);JScrollPane scrollPane = new JScrollPane(messageArea);textField = new JTextField(20);JButton sendButton = new JButton("Send");sendButton.addActionListener(e -> sendMessage(textField.getText()));JPanel inputPanel = new JPanel();inputPanel.setLayout(new FlowLayout());inputPanel.add(textField);inputPanel.add(sendButton);getContentPane().setLayout(new BorderLayout());getContentPane().add(scrollPane, BorderLayout.CENTER);getContentPane().add(inputPanel, BorderLayout.SOUTH);}private void sendMessage(String message) {if (writer != null) {writer.println(message);writer.flush();messageArea.append("Client: " + message + "\n");} else {messageArea.append("Error: Writer is not initialized.\n");}}public void startClient() {try {Socket socket = new Socket("localhost", 12345);messageArea.append("Connected to server.\n");BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);String message;while ((message = reader.readLine()) != null) {messageArea.append("Server: " + message + "\n");}socket.close();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {Client client = new Client();client.setVisible(true);client.startClient();}
}