How to read data from socket connection - android
I've got TCP client app which successfully negotiates connection to server and receives buffered output, but what I need to know is how to read s开发者_StackOverflow中文版erver responses without waiting to buffer to fill out or server to end connection.
In this loop:BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
...some code here...
}
mine app just freezes.
How to read just one line or empty string if buffer is empty and continuing to execute a program? Is it possible to timeout this reading, to give server some time to respond?The underlying protocol should provide some kind of synchronisation, like HTTP's Content-Length
or \n\r
.
Otherwise, you'll get blocked even if your program reads byte by byte.
Or you could use a non-blocking socket.
精彩评论