编辑代码

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws IOException {
        int port = 8888; // 服务器端口号
        ServerSocket serverSocket = new ServerSocket(port); // 创建服务器端套接字
        System.out.println("服务器已启动,等待客户端连接...");

        while (true) {
            Socket socket = serverSocket.accept(); // 等待客户端连接
            System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());

            // 处理客户端请求
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            String msg = in.readLine();
            System.out.println("收到客户端消息:" + msg);
            out.println("服务器已收到消息:" + msg);

            socket.close(); // 关闭客户端套接字
        }
    }
}