读取输入流时无限循环

时间:2015-01-25 22:52:45

标签: java sockets

我想打印正在写入输入流的所有文本。在文本的最后,我希望循环终止,以便它可以接受更多的用户输入。由于某种原因,我的while循环永远不会终止并无限制地停止我的程序。如何编写while循环,以便在没有更多文本可以打印时终止?

public static void open() throws IOException {
    sock.connect(new InetSocketAddress(InetAddress.getByName(arg[1]), Integer.parseInt(arg[2])));
    InputStream stream = sock.getInputStream();
    BufferedReader buff = new BufferedReader(new InputStreamReader(stream));
    String output;
    while ((output = buff.readLine()) != null) {
        System.out.println(output);
    }
}    

2 个答案:

答案 0 :(得分:1)

对等体尚未关闭连接,因此循环永远不会终止。

答案 1 :(得分:-1)

问题出在哪里?如果我的代码没有错误,那么当你没有在套接字上接收数据时,while循环就会完成。

也许你的套接字总是有请求?