Reputation: 626
my android smartphone is tcpclient and chipkit wf32 wifi module is my tcp server.
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
The above code reads the data from stream and copies to buffer. If no data is coming it will block. But sometimes i am getting -1. Can anyone explains the reason for getting -1? In document it is mentioned "end of stream is reached". But can you explain the meaning of that? thank you.
Upvotes: 1
Views: 3794
Reputation: 310884
In the case of a socket, it means that the peer closed its end of the connection, or at least shut it down for output.
NB
response += byteArrayOutputStream.toString("UTF-8");
should be outside the loop. I've told you that before, in another of your numerous threads on this topic.
Upvotes: 4