简体   繁体   English

Java,while循环中的BufferedReader问题

[英]Java, problems with BufferedReader in a while loop

I'm stuck trying to advance. 我一直在努力前进。 This while loop seems to hang when receiving lines from a socket. 从套接字接收线路时,此while循环似乎挂起。

Here's the server side of the code: 这是代码的服务器端:

out = new PrintWriter(socketcliente.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socketcliente.getInputStream()));
//receive and shows client data
            linea = 0;
            while ((buffer[linea] = in.readLine()) != null) {
                System.out.println(buffer[linea]);
                linea = linea+1;
            }
//asks for a command and sends it to client
            System.out.println("Enter remote command");
            while ((input = scanner.nextLine()) != null) {
                out.println(input);
            }

and here's the client side: 这是客户端:

out = new PrintWriter(socketcliente.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socketcliente.getInputStream()));
//sends a limited amount of lines to the server
linea = 0;
        while (buffer[linea] != null) {
            out.println(buffer[linea]);
            linea = linea+1;
        }
//receive the command from server, executes and save output for sending to the server
        while ((comandor = in.readLine()) != null) {
            proceso = Runtime.getRuntime().exec(comandor);
            pinput = new BufferedReader(new InputStreamReader(proceso.getInputStream()));
            buffer = new String[100];
            linea = 0;
            while ((psalida = pinput.readLine()) != null) {
                buffer[linea] = psalida;
                linea = linea+1;
            }
        }

After the server prints the data it receives, it does nothing, nor exit the while loop. 服务器打印收到的数据后,将不执行任何操作,也不会退出while循环。 Must send data back to the client. 必须将数据发送回客户端。 I'm using PrintWriter and BufferedReader. 我正在使用PrintWriter和BufferedReader。

Edited: More complete code to understand what I'm trying. 编辑:更完整的代码,以了解我在尝试什么。 The command execution and output's save runs fine, the problem is receiving the data in the server, it gets all the data, but stops at end and don't exit the first while loop. 命令执行和输出的保存运行良好,问题在于在服务器中接收数据,它获取了所有数据,但在最后停止并且不退出第一个while循环。 How can I allow it to exit the loop? 如何允许它退出循环? I tried sending a null byte from the client after the message, or a "quit text" that the server can understand like: 我尝试在消息后从客户端发送空字节,或服务器可以理解的“退出文本”,例如:

while ((buffer[linea] = in.readLine()) != null && (buffer[linea] = in.readLine()) != "quit")

I'm lost and didn't find how to do. 我迷路了,没找到怎么做。

All these are in try statements. 所有这些都在try语句中。 Nothing works. 没用。 I'm a beginner, thanks for helping. 我是一个初学者,感谢您的帮助。

In your server side code the incrementing line you have written for line is wrong, it must be corrected as line = line+1; server side代码中,您为line编写的递增行是错误的,必须将其更正为line = line+1; so the whole `server side code must look as below 因此,整个服务器端代码必须如下所示

line = 0;
while ((buffer[line] = in.readLine()) != null) {
    System.out.println(buffer[line]);
    line++;
} 

Read string to a StringBuilder instead - less allocations, faster and eaasier: 而是将字符串读取到StringBuilder-更少的分配,更快,更容易:

StringBuilder builder = new StringBuilder();
String aux = "";
BufferedReader pinput = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((aux = pinput.readLine()) != null) {
     builder.append(aux);
}

String text = builder.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM