我正在阅读有关如何编程网络套接字并运行这段代码的内容:
try {
while (true) { // This is the line in question
int i = in.read( );
if (i == -1) break;
System.out.write(i);
}
}
catch (SocketException e) {
// output thread closed the socket
}
catch (IOException e) {
System.err.println(e);
}
Run Code Online (Sandbox Code Playgroud)
第二行如何知道何时失败?换句话说,while(true)循环如何工作?我想我不明白'虽然这是真的吗?'
这里的重要一点是:
if (i == -1) break;
Run Code Online (Sandbox Code Playgroud)
当时,break将退出当前循环i == -1.没有这条线,它将是一个无限循环.
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |