编辑代码

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

public class Client {
    public static void main(String[] args) throws IOException {
        String serverAddress = "localhost"; // 服务器地址
        int port = 8888; // 服务器端口号
        Socket socket = new Socket(serverAddress, port); // 创建客户端套接字
        System.out.println("已连接到服务器:" + serverAddress);

        // 发送消息给服务器
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println("Hello, Server!");

        // 接收服务器响应
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String response = in.readLine();
        System.out.println("收到服务器响应:" + response);

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