I am developing an application to communicate with a third party application through socket. Basically, my application needs to send some requests to the server, and the server returns some data. When the server sends me back small amount of data, everything works well. But, when I request large amount data from server, my application sometimes doesn't receive complete data and it happens intermittently.
I've done some research on internet and followed the socket programming examples that I found but still, I couldn't solve the issue. Below is my current implementation.
BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
//I know the size of data that I am expecting from the server
byte[] buffer = new byte[length];
int count = 0;
int current = 0;
while(count < length) {
current = is.read(buffer, count, length - count);
if(current == -1) {
break;
} else {
count += current;
}
}
I know the size of data that I am expecting from the server. When the problem occurs, read() method returns -1 before the data is fully received from the server. I don't have any access to the server side implementation. Please advise me if I miss out anything in my code or if there is any better way to do it.