Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要双重输入。为什么?_Java_Sockets - Fatal编程技术网

Java 需要双重输入。为什么?

Java 需要双重输入。为什么?,java,sockets,Java,Sockets,我正面临一个需要双重“进入”的问题,以便程序继续进行,有人能告诉我吗 public void run() { try { out.write("Enter message to encrypt: \n"); out.flush(); while (true) { entry = in.readLine(); result = caesarCipher(entry);

我正面临一个需要双重“进入”的问题,以便程序继续进行,有人能告诉我吗

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();
    }
}

当我的客户机类连接到服务器类时,我需要输入一条消息进行加密,但需要双输入来显示结果。有人能给我点化一下吗?

ReadLine将停止对流量的控制

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);
    }
}
在您的代码中,它们是两个readLine
.readLine()//(行字符串被重写两次)重复。移除它。你会没事的