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

如何修复Java Echo Server中输入输出行跳过的问题

问题根因

  • 服务端/caesarcipher处理逻辑重复读取输入流:while循环已经通过inputLine = input.readLine()获取到了客户端发送的消息,else分支中额外执行了一次input.readLine(),导致每处理一条消息需要等待客户端发送两次内容,对应服务端要等后续两条消息才执行功能的问题。
  • 服务端完成消息解密后没有将结果回写给客户端,客户端无法收到服务端返回。
  • 客户端进入凯撒加密模式后,没有先打印服务端返回的密钥提示,且发送加密消息后未读取服务端返回,直接打印了本地输入内容,导致出现要等下一条消息发送才显示回显的问题。

修复代码

服务端EchoServer /caesarcipher分支修正

case "/caesarcipher":
    int maxKeyValue = 25;
    int randomKey = (int) Math.floor(Math.random() * maxKeyValue + 1);
    System.out.println("[CLIENT HAS INITIATED CAESAR CIPHER PROGRAM ----- CLIENT CAESAR CIPHER IS "
            + randomKey + " (Type 'bye' to exit)!]");
    output.println("Your Caesar Cipher is " + randomKey + ". Enter a message to encrypt: ");

    while((inputLine = input.readLine()) != null) {
        if (inputLine.equalsIgnoreCase("/caesarcipher")) {
            output.println("Your Caesar Cipher is " + randomKey + ". Enter a message to encrypt: ");
            output.flush();
        }else if (inputLine.equalsIgnoreCase("/bye") ||
                inputLine.equalsIgnoreCase("bye")) {
            System.out.println("[CLIENT HAS REQUESTED TO END CAESAR CIPHER MODE!]");
            output.println("Quitting Caesar cipher mode...");
            break;
        }else{
            CaesarCipher caesarCipher = new CaesarCipher();
            // 去掉重复的readLine,直接使用循环已经读到的inputLine
            String decryptedMessage = caesarCipher.decrypt(inputLine, randomKey);
            System.out.println("[SERVER:] Received: " + inputLine + " ==> " + decryptedMessage);
            // 新增:将解密结果回写给客户端
            output.println("Decrypted result: " + decryptedMessage);
        }
    }
    break;

客户端EchoClient 凯撒模式逻辑修正

if(cipherActive) {
    CaesarCipher caesarCipher = new CaesarCipher();
    String serverKeyMessage = message;
    int serverKey = Integer.parseInt(serverKeyMessage.replaceAll("[^0-9]", ""));
    // 新增:先打印服务端返回的密钥提示
    System.out.println("echo: " + serverKeyMessage);
    while ((usrInput = stdIn.readLine()) != null) {
        if (usrInput.equalsIgnoreCase("/caesarcipher")){
            output.println(usrInput);
            // 读取服务端返回的提示并打印
            System.out.println("echo: " + input.readLine());
        }else if (usrInput.equalsIgnoreCase("/bye")
                || usrInput.equalsIgnoreCase("bye")) {
            output.println(usrInput);
            // 读取服务端返回的退出提示并打印
            System.out.println("echo: " + input.readLine());
            break;
        }else{
            String encryptedMessage = caesarCipher.encrypt(usrInput, serverKey);
            output.println(encryptedMessage);
            // 读取服务端返回的解密结果并打印
            String serverResp = input.readLine();
            System.out.println("echo: " + serverResp);
        }
    }
    break;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 01:45:04