You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Socket聊天程序发送消息卡顿,求助排查流处理问题

问题分析与解决方案

核心问题

  1. UI线程阻塞:Swing按钮点击事件运行在事件调度线程(EDT)中,直接调用in.readLine()这种阻塞方法会卡住整个UI线程,导致程序无响应。
  2. 输出流未刷新:客户端仅调用out.write()发送消息,未执行flush(),BufferedWriter的缓冲区未清空,服务器无法接收消息,客户端会一直等待服务器回复,加剧卡顿。

修复步骤

1. 客户端代码修复

  • 发送消息后立即刷新输出流,确保消息传递到服务器
  • 将消息接收逻辑放到独立线程中,避免阻塞UI线程

修改后的客户端核心代码:

out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

// 独立线程处理消息接收,避免阻塞UI
new Thread(() -> {
    String response;
    try {
        while ((response = in.readLine()) != null) {
            // 更新UI必须在EDT中执行
            SwingUtilities.invokeLater(() -> {
                textArea.append(response + "\n");
                textArea.setCaretPosition(textArea.getText().length());
            });
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}).start();

button.addActionListener(e -> {
    try {
        String msg = textField.getText() + "\n";
        out.write(msg);
        out.flush(); // 关键操作:刷新缓冲区,确保消息发送
        textField.setText("");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
});
setVisible(true);

2. 服务器端代码优化

服务器Receiver类中,readLine()已读取换行符,无需额外拼接\n,同时增加客户端断开连接的处理逻辑:

@Override
public void run() {
    String message = null;
    while (true){
        try {
            message = in.readLine(); 
            if (message == null) break; // 处理客户端断开连接的情况
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
        for(Receiver socket : Server.sockets()) {
            try {
                socket.out.write("Your message: " + message + "\n");
                socket.out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 移除已断开的连接
    Server.sockets().remove(this);
}

为何telnet测试正常?

telnet是命令行工具,自身为阻塞式交互,发送消息时会自动处理换行与刷新,且输入输出逻辑不在UI线程中,因此不会出现卡顿问题。

内容的提问来源于stack exchange,提问作者star boom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:51:22