我在我的Android应用程序中创建一个套接字,当我连接到服务器时,我为此套接字创建一个InputStream
以从服务器读取传入数据:
socket = new Socket(address, port);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
// the execution is blocked at this line and does not get out
//from the while
}
}
获取输入流后,我尝试读取字节并填充字符串(响应),但在从服务器读取所有数据时,它会在while
循环内阻塞。这意味着不满足while条件。我发这样的短语:“你好,你好吗”
答案 0 :(得分:0)
也许你应该在\ n到达时断开while循环或从服务器那样的东西。循环仍在等待新数据读取。