我正在从我从套接字的服务器端接收到的输入流中读取数据。我不知道我的回答的大小将是什么方式,我读入一个字节数组这是代码输入流中的无限循环
public static String readString(InputStream inputStream) throws IOException {
ByteArrayOutputStream into = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int n; 0 < (n = inputStream.read(buf));) {
System.out.println("Got to this point n is "+n);
into.write(buf, 0, n);
}
into.close();
System.out.println("passed this point");
return new String(into.toByteArray(), AddATudeMessage.CHARENC);
}
写信跟11和235获得打印到控制台的数字,所以我假定服务器仍然将输出写入输入流。但它在235被卡住,没有什么可以发生。我尝试暂停主执行线程长达20秒,此时我从服务器接收到241个字节,然后同一无限循环再次发生任何人知道发生了什么
@crazycasta服务器没有关闭套接字,我不得不检查是否有行尾字符,然后我停止阅读 – sola