My Java project (I'm the client side) consists of sending requests and reading responses through TCP socket connection. Over the socket I create an Output and Input Stream for sending and receiving data, respectively. All works well, except the end of the message, i.e., I send correctly my request through my DataOutputStream, the final response (through DataInputStream) is ok, but the reading always hangs 60 seconds on DataInputStream.read method. After that hang of 60 seconds all works great. There's my method:
private static int BUFFER_SIZE = 1024;
public void read(DataInputStream inputStream) throws IOException {
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[BUFFER_SIZE];
while ((len = inputStream.read(buf, 0, BUFFER_SIZE)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
// parse buf
}
I thinks it has something to do with the while loope control
((len = inputStream.read(buf, 0, BUFFER_SIZE)) != -1)
but I cannot figure out what! Since the response has no fixed size I don't know when to stop reading. I already try some configurations changing de BUFFER_SIZE, controling the loop over inputStream.available(), but sometimes the available value is 0 and the inputStream.read keep reading data. I really think that's a little detail I'm missing, but struggling to find it out! thanks