双输入需要。为什么?

时间:2014-04-14 17:41:07

标签: java sockets

我面临一个问题,需要加倍“输入”以便程序继续进行,有人可以启发我吗?

public void run() {
    try {
        out.write("Enter message to encrypt: \n");
        out.flush();

        while (true) {

            entry = in.readLine();
            result = caesarCipher(entry);

            out.write("The encrypted message is " + result);
            out.write("\tType q to end else type another message to encrypt");
            out.flush();

        }
    } catch (IOException e) {
        System.out.println(e);
    }
}

这已经在客户端结束了

public EncryptClient() throws IOException {
    Socket cSock = new Socket("LocalHost", portNumber);
    Reader iRead = new InputStreamReader(cSock.getInputStream());
    input = new BufferedReader(iRead);

    userTerminal = new BufferedReader(new InputStreamReader(System.in));
    output = new OutputStreamWriter(cSock.getOutputStream());

    while (true) {
        String line = input.readLine();
        System.out.println(line);
        line = userTerminal.readLine();

        if (line.equals("q")) {
            break;
        }
        output.write(line + "\n");
        output.flush();
    }
}

当我的客户端类连接到服务器类时,我需要输入一条加密消息,但需要输入双输入才能显示结果。有人可以启发我吗?

1 个答案:

答案 0 :(得分:1)

ReadLine将停止对流量的控制。

在您的代码中,它们是两个readLine .readLine(); //(行字符串被覆盖两次)重复。去掉它。你没事。